繁体   English   中英

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

[英]Highcharts xAxis labels formatter callback data empty

我是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';
  }

//修复

通过将数组添加到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>';
      }

解决方法是将标签数据的数组添加到options属性,请参见上文。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM