Highcharts Drilldown设置XAxis标题的方式不同

时间:2018-08-13 09:25:30

标签: jquery highcharts

在Highcharts向下钻取图表中,我需要在每个向下钻取级别中分别设置x轴标题。但是我找不到任何这样的属性来不同地设置x轴标题。

1 个答案:

答案 0 :(得分:1)

您可以使用drilldown / up事件和每个系列的自定义名称来实现此目标,如下所示:

钻探事件:

chart: {
  events: {
    drilldown: function(e) { 
      this.xAxis[0].setTitle({text: e.seriesOptions.customName})
    }, drillup: function(e) { 
      this.xAxis[0].setTitle({text: e.seriesOptions.customName})
    }, drillupall: function() {
      this.xAxis[0].setTitle({text: 'TextTopLevel'})
    }
  }
},

系列:

{
  id: 'fruits',
  customName: 'Custom title goes here',
  data: [
    ['Apples', 4],
    ['Oranges', 2]
  ]
}

// Create the chart
Highcharts.chart('container', {
    chart: {
        type: 'column',
        events: {
        drilldown: function(e) { 
          this.xAxis[0].setTitle({text: e.seriesOptions.customName})
        }, drillup: function(e) { 
          this.xAxis[0].setTitle({text: e.seriesOptions.customName})
        }, drillupall: function() {
          this.xAxis[0].setTitle({text: 'TextTopLevel'})
        }
      }
    },
    title: {
        text: 'Basic drilldown'
    },
    xAxis: {
        type: 'category',
        title: {
        	text: 'Categories'
        }
    },

    legend: {
        enabled: false
    },

    plotOptions: {
        series: {
            borderWidth: 0,
            dataLabels: {
                enabled: true
            }
        }
    },

    series: [{
        name: 'Things',
        colorByPoint: true,
        data: [{
            name: 'Animals',
            y: 5,
            drilldown: 'animals'
        }, {
            name: 'Fruits',
            y: 2,
            drilldown: 'fruits'
        }, {
            name: 'Cars',
            y: 4,
            drilldown: 'cars'
        }]
    }],
    drilldown: {
        series: [{
            id: 'animals',
            customName: 'test1',
            data: [
                ['Cats', 4],
                ['Dogs', 2],
                ['Cows', 1],
                ['Sheep', 2],
                ['Pigs', 1]
            ]
        }, {
            id: 'fruits',
            customName: 'test2',
            data: [
                ['Apples', 4],
                ['Oranges', 2]
            ]
        }, {
            id: 'cars',
            customName: 'test3',
            data: [
                ['Toyota', 4],
                ['Opel', 2],
                ['Volkswagen', 2]
            ]
        }]
    }
});
<script src="https://code.highcharts.com/highcharts.js"></script>
<script src="https://code.highcharts.com/modules/drilldown.js"></script>

<div id="container" style="min-width: 310px; height: 200px; margin: 0 auto"></div>

JSFiddle工作示例: http://jsfiddle.net/ewolden/p8vfy27h/