数据放在xAxis -nvd3上错误的日期

问题描述 投票:0回答:1

我用nvd3创建了一个linelusBarChart。该图的数据是从REST服务接收的时间序列(对于行和条)。我面临的问题涉及在xAxis上放置数据。当我缩小图表时,数据没有放在正确的位置(结果如下图所示):enter image description here但是当我放大时,数据被放置在不同的日期而不是正确的日期(查看下图):enter image description here怎么能我解决了这个问题?

我的代码是:

chart = nv.models.linePlusBarChart()
                    .focusEnable(true) //gia na exei to focus Chart (range slider)     
                    .margin({ top: 50, right: 80, bottom: 30, left: 80 })
                    .color(["rgb(226, 144, 36)", "rgb(66, 30, 109)", "rgb(58, 112, 150)"]);
                    chart.xAxis.tickFormat(function (d) {
                    return d3.time.format('%d/%m/%Y')(new Date(d * 1000))
                }).showMaxMin(true);

                chart.y1Axis.axisLabel("Sentiment Value");

                chart.y2Axis.tickFormat(function (d) { return d3.format(',f')(d) });
                   chart.bars.forceY([0]).padData(false); 
                chart.lines.forceY([0]).padData(false);

                chart.x2Axis.tickFormat(function (d) {
                    return d3.time.format('%d/%m/%Y')(new Date(d * 1000))
                }).showMaxMin(true);

                chart.showLegend(false);

                multichart_graph = d3.select('#multichart svg');
                testdata.map(function (series) {
                series.values = series.values.map(function (d) { return { x: d[0], y: d[1] } });
                return series;
                });

                multichart_graph.datum(testdata)
                    .transition()
                    .call(chart);

使用的数据概述如下:

var testdata = [{
                 "key": "Orange line", "values": [[1279314000, -0.476492389], [1279400400, -0.4764799323],[1279486800, -0.4764816604],....]}, 
                {
                 "key": "Forecast line", "values": [[1516140000, -1.0070195523], [1516226400, 3.9973810749],...]},
                {
                "key": "Bars", "values": [[1516140000, 3], [1516226400, 2], [1516312800, 3],...],"bar": "true"}
]
javascript d3.js nvd3.js lineplusbarchart
1个回答
0
投票

根据您获得NVD3的位置,您的代码将无法运行。

我用

<script src="http://nvd3.org/assets/js/nv.d3.js"></script>

这是一个与github代码不同的版本。它没有此图表类型的chart.x2Axis

查看代码(github和nvd3.org)条形图使用historicalBar模型显示条形图。这使用d3.extent值的x作为指定为d3.scale.linear的值的xAxis(bar)的域。使用刻度尺放置条形,但条形的宽度为width/data[0].values.length。如果在组合域启动(2010-07)中添加虚拟数据点,则结果是巨大的条形图。用户和xScale使用的域控制条宽度是不可能的。

唯一的解决方案是自己编写图表。

© www.soinside.com 2019 - 2024. All rights reserved.