javascript - Angular-Nvd3 日期和数字与数据不匹配

标签 javascript angularjs nvd3.js angularjs-nvd3-directives

我正在使用 Angular-Nvd3 来表示我的数据集,目前我从数据中得到了一些奇怪的结果。我的数据中的日期和数字与图表上显示的日期和数字不对应。

我的 $scope.data 包含以下 JSON 数组: http://www.jsoneditoronline.org/?id=654aba4a1a8f3fd7d31f75dd295da363

我的$scope.options看起来像这样:

            $scope.options = {
                chart: {
                    type: 'cumulativeLineChart',
                    height: 450,
                    margin : {
                        top: 20,
                        right: 20,
                        bottom: 60,
                        left: 65
                    },
                    x: function(d){ return d[0]; },
                    y: function(d){ return d[1]; },
                    average: function(d) { return d.mean/100; },

                    color: d3.scale.category10().range(),
                    transitionDuration: 300,
                    useInteractiveGuideline: true,
                    clipVoronoi: false,

                    xAxis: {
                        axisLabel: 'X Axis',
                        tickFormat: function(d) {
                            return d3.time.format('%m/%d/%Y')(new Date(d))
                        },
                        showMaxMin: false,
                        staggerLabels: true
                    },

                    yAxis: {
                        axisLabel: 'Y Axis',
                        tickFormat: function(d){
                            return d3.format('')(d);
                        },
                        axisLabelDistance: 20
                    }
                }
            };

问题是我的图表只显示 1970 年以来的日期,而且数字完全不正确:

enter image description here

我不知道为什么图表显示错误的日期和数字,请帮我解决这个问题。

最佳答案

您的值实际上是在 1970 中。

你可以自己看看:

var json = [
  {
    "key": "IBM",
    "values": [
      [
        1112313600,
        90.44
      ],
      [
        1124755200,
        82.03
      ],
      [
        1137456000,
        83
      ],
      [
        1149811200,
        77.63
      ],
      [
        1162252800,
        92.33
      ],
      [
        1175040000,
        94.26
      ],
      [
        1187568000,
        109.22
      ],
      [
        1200009600,
        97.67
      ],
      [
        1212624000,
        128.47
      ],
      [
        1225065600,
        79.66
      ],
      [
        1237766400,
        98.71
      ],
      [
        1250121600,
        119.58
      ],
      [
        1262736000,
        130
      ],
      [
        1275350400,
        124.34
      ],
      [
        1287619200,
        139.83
      ],
      [
        1300233600,
        153
      ],
      [
        1312761600,
        166.22
      ],
      [
        1325116800,
        186.18
      ],
      [
        1337731200,
        196.12
      ],
      [
        1350259200,
        208.93
      ],
      [
        1363132800,
        212.06
      ],
      [
        1375660800,
        195.5
      ],
      [
        1388016000,
        185.35
      ],
      [
        1400630400,
        186.39
      ],
      [
        1413158400,
        183.52
      ],
      [
        1425859200,
        160.77
      ],
      [
        1438214400,
        160.96
      ]
    ]
  }
];

[].forEach.call(json[0].values, function(value) {
  document.body.innerHTML += '<div>' + new Date(value[0]) + '</div>';
});

关于javascript - Angular-Nvd3 日期和数字与数据不匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31992223/

相关文章:

javascript - Array.fill 中的奇怪行为

NVD3.js: ReferenceError: nv 未定义

javascript - 为动态创建的 Google 表单设置驱动器文件夹位置

javascript - 将表值导出为 csv

javascript - 使用 php 后端和 redux 在 React js 中实现动态元标记

javascript - 使用 jQuery 日期选择器编辑单元格时 AngularJS ngGrid 问题

javascript - Angular JS - 重新加载整个页面

javascript - 类型错误 : "this..." is not a function

javascript - D3 和​​ JavaScript : Uncaught TypeError: Cannot read property 'apply' of undefined

date - NVD3 线加条形图 X 轴刻度错误对齐