【发布时间】:2016-11-16 12:50:36
【问题描述】:
我正在尝试创建一个折线图来显示一年中的数据,并且我使用 Nvd3 制作了一个简单的图表,但是我在 x 轴上显示月份时遇到了困难,我查看了其他帖子,但是出于某种原因,它只显示“Jan”12 次。
这是我的代码
var chart;
var data;
nv.addGraph(function () {
chart = nv.models.lineChart()
.options({
duration: 300,
useInteractiveGuideline: true
});
// chart sub-models (ie. xAxis, yAxis, etc) when accessed directly, return themselves, not the parent chart, so need to chain separately
chart.xAxis
.axisLabel("Month")
.tickFormat(function(d) { return d3.time.format('%b')(new Date(d)); });
chart.yAxis
.axisLabel('Tenants')
.tickFormat(function (d) {
if (d == null) {
return 'N/A';
}
return d3.format(',.2f')(d);
});
//DATA
data = getData();
d3.select('#chart1').append('svg')
.datum(data)
.call(chart);
nv.utils.windowResize(chart.update);
return chart;
});
//GETTING DATA
function getData() {
var finance = [];
for (var i = 1; i <= 12; i++) {
finance.push({
x: i,
y: i
})
}
return [
{
values: finance,
key: "Finance",
color: "#2ca02c"
}
];
}
【问题讨论】:
标签: javascript d3.js nvd3.js