Highcharts xAxis标记格式化程序回调数据为空

时间:2016-05-05 00:26:17

标签: javascript angularjs highcharts highcharts-ng

我是Highcharts的新手,需要使用xAxis.labels.formatter函数来生成标签。问题是当格式化程序函数运行时,数据为空。如果单击图例,则会加载数据并正确创建标签。我尝试使用charts.events.load来调用labelFormatter函数,但仍然没有运气。在加载系列数据并渲染图表之后,我基本上需要生成标签。

    chart: {
        type: 'line',
        backgroundColor: '#eee',
        events: {
            load: function () {
                console.log(this);
                this.xAxis[0].labelFormatter();
            }
        }
    },
    xAxis: {
        categories: [],
        labels: {
          formatter: formatVehicleConversionLabels,
          useHTML: true
        }
    },
    ...

  function formatVehicleConversionLabels() {
    //console.log(this);
    var month = this.value;
    var totalConnectedVehiclesValue = null;
    var notificationsValue = null;
    var downloadsValue = null;
    var installationsValue = null;
    for(var i = 0; i < this.chart.series.length; i++) {
      var currentSeries = this.chart.series[i];
      if(currentSeries.name === "Total Connected Vehicles") {
        if(currentSeries.data.length > 0) {
          for(var j = 0; j < currentSeries.data.length; j++) {
            var currentData = currentSeries.data[j];
            if(currentData.category === month) {
              totalConnectedVehiclesValue = currentData.y;
            }
          }
        }
      }
      if(currentSeries.name === "Notifications") {
        if(currentSeries.data.length > 0) {
          for(var j = 0; j < currentSeries.data.length; j++) {
            var currentData = currentSeries.data[j];
            if(currentData.category === month) {
              notificationsValue = currentData.y;
            }
          }
        }
      }
      if(currentSeries.name === "Downloads") {
        if(currentSeries.data.length > 0) {
          for(var j = 0; j < currentSeries.data.length; j++) {
            var currentData = currentSeries.data[j];
            if(currentData.category === month) {
              downloadsValue = currentData.y;
            }
          }
        }
      }
      if(currentSeries.name === "Installations") {
        if(currentSeries.data.length > 0) {
          for(var j = 0; j < currentSeries.data.length; j++) {
            var currentData = currentSeries.data[j];
            if(currentData.category === month) {
              installationsValue = currentData.y;
            }
          }
        }
      }
    }
    return '<table class="x-axis"><tr><th>' + this.value + '</th></tr>' +
    '<tr><td>' + totalConnectedVehiclesValue + '</td></tr>' +
    '<tr><td>' + notificationsValue + '</td></tr>' +
    '<tr><td>' + downloadsValue + '</td></tr>' +
    '<tr><td>' + installationsValue + '</td></tr></table';
  }

// FIX

通过向options属性添加一个数组并将其用作标签格式化函数的数据来解决这个问题。

options:{
  labelData: [],
  ...
  angular.forEach(vm.downloadAndInstallationRateChartConfig.options.xAxis.categories, function(d, i) {
        vm.downloadAndInstallationRateChartConfig.options.labelData.push({
          col: d,
          successfulDownloads: successfulDownloadsData[i],
          successfulInstallations: successfulInstallationsData[i]
        });
      });

      function formatDownloadAndInstallationRateLabels() {
        var labelData = null;
        for(var i = 0; i < this.chart.options.labelData.length; i++) {
          if(this.chart.options.labelData[i].col === this.value) {
            labelData = this.chart.options.labelData[i];
          }
        }
        return '<span style="margin-bottom: 20px; display: inline-block; font-size: 12px;">'+this.value+'</span><br />'+
        '<span style="margin-top: 1px; display: inline-block; font-size: 12px;">'+labelData.successfulDownloads+'</span><br />'+
        '<span style="margin-top: 1px; display: inline-block; font-size: 12px;">'+labelData.successfulInstallations+'</span>';
      }

1 个答案:

答案 0 :(得分:1)

修复是将标签数据的数组添加到options属性,见上文。