Highcharts-ng如何从json响应加载数据

时间:2017-09-25 07:20:44

标签: angular highcharts-ng

Highcharts-ng似乎适用于静态数据,但不适用于动态数据。我有两个相似的数据集但图表在使用动态创建的数据集时拒绝渲染。

这是我的控制器代码:

$scope.sent_array = [];
$scope.success_array = [];
$scope.failed = [];
$scope.table_data = [];
$scope.date_array = [];

$scope.dateRangeTransactions = function() {
  ApiService.dateRange(payload).then(function(response) {
    data = response.data;
    console.log(data);

    for (var i = 0; i < data.length; i++) {
      date = data[i].date.replace(/\//g, ",").split(",");
      unix_date = Date.UTC(date[0], date[1] - 1, date[2]);
      console.log(unix_date);
      $scope.sent_array.push([unix_date, data[i].total_sent]);
      $scope.success_array.push([unix_date, data[i].total_success]);
      $scope.failed.push([unix_date, data[i].total_pending_failed]);
      $scope.table_data.push([data[i].date, data[i].total_sent, data[i].total_success, data[i].total_pending_failed]);
      $scope.date_array.push(data[i].date);
    }

    $scope.minDate = $scope.date_array[0];
    $scope.maxDate = $scope.date_array[$scope.date_array.length - 1];

    console.log($scope.sent_array);
    console.log($scope.success_array);
    console.log($scope.failed);

    $scope.chartConfig.series[0].data = $scope.sent_array;
    $scope.chartConfig.series[1].data = $scope.success_array;
    $scope.chartConfig.series[2].data = $scope.failed;




  }, function(error) {
    console.log(error);
  });
}

$scope.dateRangeTransactions(payload);

$scope.chartConfig = {
  global: {
    useUTC: false,
  },
  chart: {
    type: 'column',
    height: 500,
    width: 900,
    backgroundColor: 'transparent',
    zoomType: 'x',

  },
  navigator: {
    enabled: false,
    series: {
      data: []
    }
  },
  rangeSelector: {
    enabled: true,
  },
  plotOptions: {
    series: {
      lineWidth: 1,
      fillOpacity: 0.5,
      showInNavigator: true

    }


  },
  exporting: false,
  xAxis: [{
    type: 'datetime',
    title: {
      text: 'timeline',
      style: {
        color: '#80a3ca'
      }
    },

  }],
  yAxis: [

    {

      min: 0,
      allowDecimals: false,
      title: {
        text: 'rate of usage',
        style: {
          color: '#80a3ca'
        }
      },
      labels: {
        format: '{value}',
        style: {
          color: '#80a3ca'
        }
      }


    }
  ],

  legend: {
    enabled: true
  },
  title: {
    text: ' '
  },
  credits: {
    enabled: false
  },

  loading: true,
  tooltip: {

    headerFormat: '<div class="header">{point.key}</div>',
    pointFormat: '<span style="color:{point.color}">\u25CF</span> {series.name}: <b>{point.y}</b><br/>',
    borderWidth: 1,
    borderRadius: 5,
    borderColor: '#a4a4a4',
    shadow: true,
    useHTML: true,
    percentageDecimals: 2,
    backgroundColor: "rgba(255,255,255,.7)",
    style: {
      padding: 0
    },
    shared: true,

  },

  series: [{
      id: 'Sent',
      name: 'Sent',
      data: [],
      tooltip: {
        valueSuffix: ' times'
      },
      color: 'blue'
    },
    {
      id: 'Delivered',
      name: 'Delivered',
      data: [],
      tooltip: {
        valueSuffix: ' times'
      },
      color: 'green'
    },
    {
      id: 'Failed',
      name: 'Failed',
      data: [],
      tooltip: {
        valueSuffix: ' times'
      },
      color: 'red'
    }
  ],
  useHighStocks: true,




};

,样本回复是:

[{
    "date": "2017\/09\/18",
    "total_sent": 0,
    "total_success": 0,
    "total_pending_failed": 0
  },
  {
    "date": "2017\/09\/19",
    "total_sent": 2,
    "total_success": 0,
    "total_pending_failed": 3
  },
  {
    "date": "2017\/09\/20",
    "total_sent": 5,
    "total_success": 5,
    "total_pending_failed": 0
  },
  {
    "date": "2017\/09\/21",
    "total_sent": 0,
    "total_success": 0,
    "total_pending_failed": 0
  },
  {
    "date": "2017\/09\/22",
    "total_sent": 0,
    "total_success": 0,
    "total_pending_failed": 0
  },
  {
    "date": "2017\/09\/23",
    "total_sent": 0,
    "total_success": 0,
    "total_pending_failed": 0
  },
  {
    "date": "2017\/09\/24",
    "total_sent": 0,
    "total_success": 0,
    "total_pending_failed": 0
  },
  {
    "date": "2017\/09\/25",
    "total_sent": 3,
    "total_success": 3,
    "total_pending_failed": 0
  }
]

当我console.log($scope.chartConfig);时,似乎插入了数据,但图表仍然是空的。

1 个答案:

答案 0 :(得分:0)

最后我能够弄明白所以我把它留在这里以备将来参考我修改了我的代码并且这个有效:D

$scope.dateRangeTransactions = function() {
  ApiService.dateRange(payload).then(function(response) {
    data = response.data;
    console.log(data);

    for (var i = 0; i < data.length; i++) {
      date = data[i].date.replace(/\//g, ",").split(",");
      unix_date = Date.UTC(date[0], date[1] - 1, date[2]);
      console.log(unix_date);
      $scope.sent_array.push([unix_date, data[i].total_sent]);
      $scope.success_array.push([unix_date, data[i].total_success]);
      $scope.failed.push([unix_date, data[i].total_pending_failed]);
      $scope.date_array.push(data[i].date);
    }

    $scope.minDate = $scope.date_array[0];
    $scope.maxDate = $scope.date_array[$scope.date_array.length - 1];

    console.log($scope.sent_array);
    console.log($scope.success_array);
    console.log($scope.failed);

    $scope.chartConfig.getChartObj().series[0].setData($scope.sent_array);
    $scope.chartConfig.getChartObj().series[1].setData($scope.success_array);
    $scope.chartConfig.getChartObj().series[2].setData($scope.failed);


  }, function(error) {
    console.log(error);
  });
}

$scope.dateRangeTransactions(payload);