格式化nvd3折线图轴

海豹突击队

我正在尝试修复使用nvd3(D3)库完成的简单折线图,但似乎无法修复该代码。

这是小提琴:http : //jsfiddle.net/sourabhtewari/o24ffe99/

数据看起来像这样

  var reportData = [{
    "key": "ActualElapsedTime",
    "color": "#d62728",
    "values": [{
      "x": "2016-03-21T00:00:00",
      "y": 100.00
    }, {
      "x": "2016-03-22T00:00:00",
      "y": 99.00
    }]
  }];

整个代码就像

nv.addGraph(function() {
  var chart = nv.models.lineChart()
    .margin({
      left: 100
    }) //Adjust chart margins to give the x-axis some breathing room.
    .useInteractiveGuideline(true) //We want nice looking tooltips and a guideline!
    .transitionDuration(350) //how fast do you want the lines to transition?
    .showLegend(true) //Show the legend, allowing users to turn on/off line series.
    .showYAxis(true) //Show the y-axis
    .showXAxis(true) //Show the x-axis
  ;

  chart.xAxis //Chart x-axis settings
    .axisLabel('Date')
    .tickFormat(function(d) {
      return d3.time.format('%x')(new Date(d))
    });

  chart.yAxis //Chart y-axis settings
    .axisLabel('Consistancy')
    .tickFormat(d3.format('.02f'));

  var reportData = [{
    "key": "ActualElapsedTime",
    "color": "#d62728",
    "values": [{
      "x": "2016-03-21T00:00:00",
      "y": 100.00
    }, {
      "x": "2016-03-22T00:00:00",
      "y": 99.00
    }]
  }];
  /* Done setting the chart up? Time to render it!*/

  d3.select('#chart svg') //Select the <svg> element you want to render the chart in.   
    .datum(reportData) //Populate the <svg> element with chart data...
    .call(chart); //Finally, render the chart!

  //Update the chart when window resizes.
  nv.utils.windowResize(function() {
    chart.update()
  });
  return chart;
});

看起来像这样

我无法正确绘制图表。我没有d3的足够经验。如果有人可以帮助我解决此问题。我会很感激。

卢卡斯·维克多(Lukasz Wiktor)

在您的中使用Date对象而不是字符串reportData

  var reportData = [{
    "key": "ActualElapsedTime",
    "color": "#d62728",
    "values": [{
      "x": new Date("2016-03-21T00:00:00"),
      "y": 100.00
    }, {
      "x": new Date("2016-03-22T00:00:00"),
      "y": 99.00
    }]
  }];

此外,您可以tickValues根据自己的数据进行设置

var tickValues = reportData[0].values.map(function(p) { return p.x});
chart.xAxis.tickValues(tickValues);

工作示例:http : //jsfiddle.net/LukaszWiktor/rcL0uot9/

结果:

在此处输入图片说明

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章