Maximum bar width in Highcharts column charts

后端 未结 8 1648
梦毁少年i
梦毁少年i 2021-02-18 14:20

I\'m using Highcharts to create some vertical bars (a.k.a. \"column charts\") a lot like here: highcharts.com/demo/column-basic

Thing is, sometimes there are 30 bars in

8条回答
  •  别跟我提以往
    2021-02-18 14:40

    Update: As of Highcharts version 4.1.8 , see Adam Goodwin's answer, below.



    For many years, the only way to set the maximum width of the columns, was dynamically via JavaScript.

    Basically:

    1. Check the current bar width.
    2. If it is greater than desired, reset the series' pointWidth.
    3. Force a redraw to make the change visible.

    Something like:

    var chart = new Highcharts.Chart ( { ... ...
    
    //--- Enforce a Maximum bar width.
    var MaximumBarWidth = 40; //-- Pixels.
    var series          = chart.series[0];
    
    if (series.data.length) {
    
        if (series.data[0].barW  >  MaximumBarWidth) {
            series.options.pointWidth = MaximumBarWidth;
            /*--- Force a redraw.  Note that redraw() will not 
                fire in this case!
                Hence we use something like setSize().
            */
            chart.setSize (400, 300);   
        }
    }
    


    See the demo at jsFiddle.

提交回复
热议问题