由于披露 - 我在javascript上很糟糕,但是想要学习!
我有一个数组,其中包含一些类似的条目:
[1349013600] => 232
关键是unix时间戳,值是当天的销售额.我目前正在多线图上绘制这些图表,效果很好.
问题是我的x轴,目前定义如下:
chart.xAxis
.tickFormat(d3.format(',f'));
Run Code Online (Sandbox Code Playgroud)
它将unix时间戳作为直线int输出,与x轴标签和悬停工具提示相对应.我想尝试将其输出为DMY或类似的人类可读日期指示器.
我找到的最接近的是这段代码:
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))
});
Run Code Online (Sandbox Code Playgroud)
但我正在努力了解它正在做什么,并且无法在我的特定图表上实现.当前代码如下所示:
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;
});
Run Code Online (Sandbox Code Playgroud)
有人可以解释如何使它工作 - 更重要的是 - 为什么?感谢任何指导!
CQM*_*CQM 16
我一打开赏金就解决了这个问题(哦哦!)
这对我有用
.tickFormat(function(d){return d3.time.format('%d-%b')(new Date(d));})
并且诀窍是重新格式化nvd3的数据之后这是创建轴