在使用 MetricsGraphics.js 时获取 TypeError

Getting TypeError while useing MetricsGraphics.js

本文关键字:获取 TypeError js MetricsGraphics      更新时间:2023-09-26

我正在尝试使用 MetricsGraphics 绘制一些时间序列数据.js,但似乎无法让它工作,我在网上任何地方都找不到答案。 指标构建在 D3 之上.js

错误是:

 TypeError: Cannot read property '0' of undefined
    at process_line (http://localhost:9000/bower_components/metrics-graphics/dist/metricsgraphics.js:4211:50)
    at Object.charts.line.init (http://localhost:9000/bower_components/metrics-graphics/dist/metricsgraphics.js:2333:13)
    at Object.charts.line (http://localhost:9000/bower_components/metrics-graphics/dist/metricsgraphics.js:2973:14)
    at Object.MG.data_graphic (http://localhost:9000/bower_components/metrics-graphics/dist/metricsgraphics.js:216:20)
    at init (http://localhost:9000/pages/home/home-controller.js:46:12)
    at new <anonymous> (http://localhost:9000/pages/home/home-controller.js:59:5)
    at invoke (http://localhost:9000/bower_components/angular/angular.js:4182:17)
    at Object.instantiate (http://localhost:9000/bower_components/angular/angular.js:4190:27)
    at http://localhost:9000/bower_components/angular/angular.js:8453:28
    at link (http://localhost:9000/bower_components/angular-route/angular-route.js:981:26) <div class="container ng-scope" ng-view="">

以下是使图表对象的代码:

MG.data_graphic({
          title: "Projector Usage",
          description: "Projector Usage in Folsom.",
          data: chartData,
          width: 800,
          height: 450,
          target: "#chart",
          x_accessor: "date",
          y_accessor: "value"
        });

这段代码正在生成 JSON 对象数组:

for(var x=0; x < data.length; x++){
            chartData.push({
                            date  : new Date(data[x]['timestamp']),
                            value : data[x]['result']['projectorsOn']
                          })
          }

所以.....这是一个愚蠢的错误。 在我调用填充对象的异步数据调用之前,MG.data_graphics正在尝试访问 chartData。

为了解决这个问题,我只是将该函数添加到 $http.post().success() 回调中。