【问题标题】:Highcharts xAxis labels formatter callback data emptyHighcharts xAxis 标签格式化程序回调数据为空
【发布时间】:2016-05-05 00:26:17
【问题描述】:

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

【问题讨论】:

  • 您能否将您的示例复制为 jsfiddle.net 上的现场演示?

标签: javascript angularjs highcharts highcharts-ng


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-16
    相关资源
    最近更新 更多