How can I change the width of the bars in a highchart?

后端 未结 4 494
深忆病人
深忆病人 2020-12-17 02:37

With a bar chart like this one, is is possible to change the width of the bars to represent another data attribute, say the weight of the fruits. The heavier the fruit is, t

4条回答
  •  情歌与酒
    2020-12-17 02:55

    I use a set of area charts to simulate a variable-width-column/bar-chart. Say, each column/bar is represented by a rectangle area.

    See my fiddle demo (http://jsfiddle.net/calfzhou/TUt2U/).

    $(function () {
        var rawData = [
            { name: 'A', x: 5.2, y: 5.6 },
            { name: 'B', x: 3.9, y: 10.1 },
            { name: 'C', x: 11.5, y: 1.2 },
            { name: 'D', x: 2.4, y: 17.8 },
            { name: 'E', x: 8.1, y: 8.4 }
        ];
    
        function makeSeries(listOfData) {
            var sumX = 0.0;
            for (var i = 0; i < listOfData.length; i++) {
                sumX += listOfData[i].x;
            }
            var gap = sumX / rawData.length * 0.2;
            var allSeries = []
            var x = 0.0;
            for (var i = 0; i < listOfData.length; i++) {
                var data = listOfData[i];
                allSeries[i] = {
                    name: data.name,
                    data: [
                        [x, 0], [x, data.y],
                        {
                            x: x + data.x / 2.0,
                            y: data.y,
                            dataLabels: { enabled: true, format: data.x + ' x {y}' }
                        },
                        [x + data.x, data.y], [x + data.x, 0]
                    ],
                    w: data.x,
                    h: data.y
                };
                x += data.x + gap;
            }
            return allSeries;
        }
    
        $('#container').highcharts({
            chart: { type: 'area' },
            xAxis: {
                tickLength: 0,
                labels: { enabled: false}
            },
            yAxis: {
                title: { enabled: false}
            },
            plotOptions: {
                area: {
                    marker: {
                        enabled: false,
                        states: {
                            hover: { enabled: false }
                        }
                    }
                }
            },
            tooltip: {
                followPointer: true,
                useHTML: true,
                headerFormat: '{series.name}: ',
                pointFormat: '{series.options.w} x {series.options.h}'
            },
            series: makeSeries(rawData)
        });
    });
    

    my demo chart

提交回复
热议问题