【问题标题】:Angular-Nvd3 dates and numbers do not match dataAngular-Nvd3 日期和数字与数据不匹配
【发布时间】:2015-08-13 15:14:17
【问题描述】:

我正在使用 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 年的日期,而且数字完全不正确:

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

【问题讨论】:

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


    【解决方案1】:

    您的值实际上在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>';
    });

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-04-17
      • 2022-06-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-07-08
      • 1970-01-01
      相关资源
      最近更新 更多