gpt4 book ai didi

javascript - 格式化 nvd3 折线图轴

转载 作者:行者123 更新时间:2023-11-29 21:31:37 25 4
gpt4 key购买 nike

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

这是 fiddle :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;
});

Looks like this

我无法正确绘制图表。我对 d3 没有足够的经验。如果有人可以帮我解决这个问题。我将不胜感激。

最佳答案

使用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/

结果:

enter image description here

关于javascript - 格式化 nvd3 折线图轴,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36174806/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com