简体   繁体   English

如何使xAxis标签与条形图的位置匹配

[英]How to make xAxis labels match the positions of bars Highchart

I made a drill down bar chart with highchart js library where the columns have different intervals.Because I want to make different size of bars, I used load and render function to change the size of the bars. 我使用highchart js库制作了一个向下钻取的条形图,其中的列具有不同的间隔。由于我想制作不同大小的条形,因此我使用了load和render函数来更改条形的大小。

Now I wonder how to make the xAxis labels match the current position of each bar.Also my tooltip and data labels have the same problems as xAxis labels,I wonder how to solve this problem? 现在我想知道如何使xAxis标签与每个条的当前位置匹配。我的工具提示和数据标签也有与xAxis标签相同的问题,我想知道如何解决此问题?

 // Create the chart Highcharts.chart('container', { chart: { type: 'column', events: { load: function() { this.series[0].data[0].graphic.attr({ x: 10, width: 20 }); this.series[0].data[1].graphic.attr({ x: 80, width: 40 }); this.series[0].data[2].graphic.attr({ x: 150, width: 80 }); }, render: function() { console.log(this.series[0].options._levelNumber) if (this.series[0].options._levelNumber == 0) { this.series[0].data[0].graphic.attr({ x: 10, width: 20 }); this.series[0].data[1].graphic.attr({ x: 80, width: 40 }); this.series[0].data[2].graphic.attr({ x: 150, width: 80 }); } } } }, title: { text: 'Basic drilldown' }, xAxis: { type: 'category', title: { enabled: true, text: "Percentage Range" } }, yAix: { title: { enabled: true, text: "Number of Schools" } }, legend: { enabled: false }, plotOptions: { series: { borderWidth: 0, dataLabels: { enabled: true } } }, series: [{ name: 'Things', colorByPoint: true, data: [{ name: '100-70%', y: 5, drilldown: '100-70%' }, { name: '70-30%', y: 2, drilldown: '70-30%' }, { name: '30-0%', y: 4, drilldown: '30-0%' }] }], drilldown: { series: [{ id: '100-70%', data: [ ['Cats', 4], ['Dogs', 2], ['Cows', 1], ['Sheep', 2], ['Pigs', 1] ] }, { id: '70-30%', data: [ ['Apples', 4], ['Oranges', 2] ] }, { id: '30-0%', data: [ ['Toyota', 4], ['Opel', 2], ['Volkswagen', 2] ] }] } }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> <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: 400px; margin: 0 auto"></div> 

What you do in load and render functions? 您在loadrender功能中做什么? If delete it, looks like everything is ok: 如果删除它,看起来一切正常:

Highcharts.chart('container', {
    chart: {
        type: 'column',
    },
    title: {
        text: 'Basic drilldown'
    },
    xAxis: {
        type: 'category',
        title:{
        enabled:true,
        text: "Percentage Range"
        }
    },
        yAix:{
    title:{
        enabled:true,
        text: "Number of Schools"
        }
    },
    legend: {
        enabled: false
    },

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

    series: [{
        name: 'Things',
        colorByPoint: true,
        data: [{
            name: '100-70%',
            y: 5,
            drilldown: '100-70%'
        }, {
            name: '70-30%',
            y: 2,
            drilldown: '70-30%'
        }, {
            name: '30-0%',
            y: 4,
            drilldown: '30-0%'
        }]
    }],
    drilldown: {
        series: [{
            id: '100-70%',
            data: [
                ['Cats', 4],
                ['Dogs', 2],
                ['Cows', 1],
                ['Sheep', 2],
                ['Pigs', 1]
            ]
        }, {
            id: '70-30%',
            data: [
                ['Apples', 4],
                ['Oranges', 2]
            ]
        }, {
            id: '30-0%',
            data: [
                ['Toyota', 4],
                ['Opel', 2],
                ['Volkswagen', 2]
            ]
        }]
    }
});

https://jsfiddle.net/w13u9hk9/1/ https://jsfiddle.net/w13u9hk9/1/

Instead of using event to change the width of the columns,you can define with of each data series either using pointPadding or pointWidth . 您可以使用pointPaddingpointWidth定义每个数据系列的with,而不是使用event来更改列的宽度。 This display the chart properly 这样可以正确显示图表

Series will be 系列将

  series: [{
    name: 'Things',
    colorByPoint: true,
    pointPadding: 0,
   // pointWidth:80, you can use this as well
    data: [{
      name: '100-70%',
      y: 5,
      drilldown: '100-70%',
    }]
  }, {
    name: 'Things',
    colorByPoint: true,
    pointPadding: 0.2,
    // pointWidth:100, you can use this as well
    data: [{
      name: '70-30%',
      y: 2,
      drilldown: '70-30%'
    }]
  }, {
    name: 'Things',
    colorByPoint: true,
    pointPadding: 0.3,
    // pointWidth:120, you can use this as well
    data: [{
      name: '30-0%',
      y: 4,
      drilldown: '30-0%'
    }]
  }],

Fiddle demonstration 小提琴示范

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

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