(D3折线图)错误:属性d:预期编号,“MNaN,NaNLNaN,NaN”

问题描述:

我对D3.js很新。我正在尝试使用D3.jsangularjs来构建折线图。我收到上述错误。(D3折线图)错误:<path>属性d:预期编号,“MNaN,NaNLNaN,NaN”

在控制台我穿过休息服务该数据看起来像

[{"id":1,"time":"Jun 19, 2017 13:17:21","ltp":9620.5,"time1":20170619}, 
    {"id":2,"time":"Jun 20, 2017 13:27:21","ltp":9617.7,"time1":20170620}, 
    {"id":3,"time":"Jun 21, 2017 13:36:51","ltp":9615.3,"time1":20170621}] 

下面是我的控制器类的代码中,我正在追加图表。

var niftyController = function($scope,$http, $state, $stateParams, $location){ 
    var niftyController = this; 
    $scope.loadNiftyData = function(){ 
     $http.get('/nifty') 
     .then(function successCallback(response){ 
      var responseNifty = JSON.parse(response.data); 

      $scope.chartConfig={ 
       data:[{}] 
      }; 

      $scope.chartConfig.data.push(responseNifty); 
      console.log(response.data); 

      var chartConfig = $scope.chartConfig; 
      var svgConfig = { id:"mySvg", width:600, height:300, margin : { top: 20, right: 20, bottom: 20, left: 50 } }; 
      var bodySelection = d3.select("body"); var svgSelection = bodySelection.append("svg") .attr("id", svgConfig.id) .attr("width",svgConfig.width) .attr("height",svgConfig.height); 

      xScale = d3.scale.linear() .range([ svgConfig.margin.left, svgConfig.width - svgConfig.margin.right ]) .domain([ d3.min(chartConfig.data, function(d) {return +d.time1;}), d3.max(chartConfig.data, function(d) {return +d.time1;}) ]); 
      yScale = d3.scale.linear() .range([ svgConfig.height - svgConfig.margin.top, svgConfig.margin.bottom ]) .domain([ d3.min(chartConfig.data, function(d) {return +d.ltp;}), d3.max(chartConfig.data, function(d) {return +d.ltp;}) ]); 

      xAxis = d3.svg.axis() .scale(xScale); 
      yAxis = d3.svg.axis() .orient("left") .scale(yScale); 

      svgSelection.append("svg:g") .attr("id","xAxis") .call(xAxis); 
      d3.select("#xAxis") .attr("transform", "translate(0," + (svgConfig.height - svgConfig.margin.bottom) + ")"); 
      svgSelection.append("svg:g") .attr("id","yAxis") .call(yAxis); 
      d3.select("#yAxis") .attr("transform", "translate(" + (svgConfig.margin.left) + ",0)"); 
      var lineSelection = d3.svg.line() .x(function(d){ return xScale(d.time1); }) .y(function(d){ return yScale(d.ltp) }); 
      svgSelection.append("svg:path") .attr('d', lineSelection(chartConfig.data)) .attr('stroke', 'green') .attr('stroke-width', 2) .attr('fill', 'none'); 

     }, function errorCallback(response) { 

     }); 
    }; 
    $scope.loadNiftyData(); 
}; 

请指导我构建图表。

在此先感谢。

+0

你必须解析日期和使用时间尺度。你不能简单地做'+ d.time',那不是一个包含数字的字符串。 –

+0

我对x和y轴分别使用“time1”和“ltp”。 “time1”和“ltp”包含数字。 –

+0

现在我明白了。虽然不是最好的名字选择。 –

我找到了解决方案。

在上面的代码中,执行$ scope.chartConfig是错误的。要实现正确的方法是

$scope.chartConfig = {data:responseNifty}; 

代替

$scope.chartConfig={ 
    data:[{}] 
}; 
$scope.chartConfig.data.push(responseNifty);