【发布时间】:2012-12-20 05:32:15
【问题描述】:
适当的披露 - 我不擅长 javascript,但正在努力学习!
我有一个数组,里面有一些类似的条目:
[1349013600] => 232
键是一个unix时间戳,值是那天的销售额。我目前正在多条形图上绘制这些图表,效果很好。
问题是我的 x 轴,目前是这样定义的:
chart.xAxis
.tickFormat(d3.format(',f'));
它将 unix 时间戳输出为针对 x 轴标签和悬停工具提示的直接 int。我想尝试让它输出为 D-M-Y 或类似的人类可读日期指示器。
我找到的最接近的是这段代码:
chart.xAxis.tickFormat(function(d) {
var dx = testdata[0].values[d] && testdata[0].values[d].x || 0;
return d3.time.format('%x')(new Date(dx))
});
但我很难理解它在做什么并且无法在我的特定图表上实施。当前代码如下所示:
nv.addGraph(function() {
var chart = nv.models.multiBarChart()
.stacked(true)
chart.xAxis
.tickFormat(d3.format(',f'));
chart.yAxis
.tickFormat(function(d) { return '$' + d3.format(',f')(d) });
d3.select('#chart1 svg')
.datum(test_data2)
.transition().duration(500).call(chart);
nv.utils.windowResize(chart.update);
return chart;
});
有人能解释一下如何让它发挥作用吗?更重要的是——为什么?感谢任何指导!
【问题讨论】: