Adding round corners to Highcharts Bar Chart with stacked bars when value is 0

M H

I'm using highcharts and I love them, but my issue is that when I add use the round corner plugin, if I have a stacked chart with a 0 in it, then the radius wont be applied.

Issue: Here Take a look at Jun and July.

Goal: In this case I need a way to assign the radius to the left of june and the right of july.

var chart = new Highcharts.Chart({
    chart: {
        renderTo: 'container',
        type: 'bar'
    },
    xAxis: {
        categories: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
    },
    plotOptions: {
        bar: {
            stacking: 'normal',
            animation: false
        }
    },
    series: [
        {
            data: [29.9, 71.5, 106.4, 129.2, 144.0, 176.0, 0, 148.5, 216.4, 194.1, 95.6, 54.4],
            // usage:
            borderRadiusTopLeft: 5,
            borderRadiusTopRight: 5
        }, {
            data: [29.9, 71.5, 106.4, 129.2, 144.0, 176.0, 135.6, 148.5, 216.4, 194.1, 95.6, 54.4],
            // usage:
            borderRadiusBottomRight: 10,
            borderRadiusBottomLeft: 10
        }
    ]
});
davcs86

You can adjust rounded-corners.js to your needs to something like this

(function (H) {
    var curPercentage=[];
    H.wrap(H.seriesTypes.column.prototype, 'translate', function (proceed) {

        var options = this.options,
            rTopLeft = options.borderRadiusTopLeft || 0,
            rTopRight = options.borderRadiusTopRight || 0,
            rBottomRight = options.borderRadiusBottomRight || 0,
            rBottomLeft = options.borderRadiusBottomLeft || 0,
            topMargin = options.topMargin || 0,
            bottomMargin = options.bottomMargin || 0;

        proceed.call(this);

        if (rTopLeft || rTopRight || rBottomRight || rBottomLeft) {

            H.each(this.points, function (point) {
                var iBottomRight = rBottomRight,
                    iBottomLeft = rBottomLeft,
                    iTopRight = rTopRight,
                    iTopLeft = rTopLeft;

                //console.log(point);
                if (typeof(curPercentage[point.index])=='undefined'){
                    curPercentage[point.index]=0;
                }
                var prevPercentage = curPercentage[point.index];
                curPercentage[point.index]+=1.0*parseFloat(point.percentage).toFixed(6);
                //console.log(prevPercentage);
                //console.log(curPercentage);

                if (prevPercentage==0 & curPercentage[point.index] == 100) {
                    // special case, only one value > 0, preserve all border radius
                    // reset for the next call
                    curPercentage[point.index]=0;

                } else if (prevPercentage==0) {
                    //right side
                    iBottomRight = 0;
                    iBottomLeft = 0;
                } else if (curPercentage[point.index] == 100) {
                    //left side
                    iTopRight = 0;
                    iTopLeft = 0;
                    // reset for the next call
                    curPercentage[point.index]=0;
                } else {
                    // no radius
                    iBottomRight = 0;
                    iBottomLeft = 0;
                    iTopRight = 0;
                    iTopLeft = 0;
                }

                var shapeArgs = point.shapeArgs,
                    w = shapeArgs.width,
                    h = shapeArgs.height,
                    x = shapeArgs.x,
                    y = shapeArgs.y;

                // Preserve the box for data labels
                point.dlBox = point.shapeArgs;

                point.shapeType = 'path';
                point.shapeArgs = {
                    d: [
                        'M', x + iTopLeft, y + topMargin,
                    // top side
                    'L', x + w - iTopRight, y + topMargin,
                    // top right corner
                    'C', x + w - iTopRight / 2, y, x + w, y + iTopRight / 2, x + w, y + iTopRight,
                    // right side
                    'L', x + w, y + h - iBottomRight,
                    // bottom right corner
                    'C', x + w, y + h - iBottomRight / 2, x + w - iBottomRight / 2, y + h, x + w - iBottomRight, y + h + bottomMargin,
                    // bottom side
                    'L', x + iBottomLeft, y + h + bottomMargin,
                    // bottom left corner
                    'C', x + iBottomLeft / 2, y + h, x, y + h - iBottomLeft / 2, x, y + h - iBottomLeft,
                    // left side
                    'L', x, y + iTopLeft,
                    // top left corner
                    'C', x, y + iTopLeft / 2, x + iTopLeft / 2, y, x + iTopLeft, y,
                        'Z']
                };

            });
        }

    });
}(Highcharts));

Working JSFiddle demo

with this change, you will need to set the border radius in all series

series: [{
        data: [50, 71.5, 106.4, 129.2, 144.0, 176.0, 0, 148.5, 216.4, 194.1, 95.6, 54.4],
        // usage:
        borderRadiusTopLeft: 5,
        borderRadiusTopRight: 5,
        borderRadiusBottomRight: 5,
        borderRadiusBottomLeft: 5,        
    }, {
        data: [29.9, 71.5, 106.4, 129.2, 144.0, 176.0, 135.6, 148.5, 216.4, 194.1, 95.6, 54.4],
        borderRadiusBottomRight: 10,
        borderRadiusBottomLeft: 10,
        borderRadiusTopLeft: 10,
        borderRadiusTopRight: 10
    }]

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

stacked bar chart shows hanging zeros when all values are 0 highcharts

From Dev

Grouping a stacked bar chart in Highcharts

From Dev

Stacked Bar Chart: incorrect height of bars and labels

From Dev

MPAndroidChart: Hide 0 value labels in a stacked bar chart

From Dev

Highcharts 4.0.3 stacked bar chart bug adds â-

From Dev

HighCharts Stacked Column Adding Onclick Event to the chart

From Dev

Hide empty bars in Grouped Stacked Bar Chart - chart.js

From Dev

Google chart create stacked bar besides 2 "normal" bars

From Dev

How to display series name inside bars of stacked bar chart?

From Dev

D3: Stacked bar chart exit().remove() not removing bars

From Dev

D3 - Stacked bar chart, position bars

From Dev

Highcharts bar chart with bar colored based on value

From Dev

Recharts Event not working when bar chart bar value is 0

From Dev

SSRS - Sorting Series by value In a Stacked Bar chart

From Dev

How to display sum of value in stacked bar chart

From Dev

Control spacing and height between bars in Highcharts.js bar chart

From Dev

How to remove bars for those bars with zero value in Chartjs bar chart?

From Dev

Stacked geom_bar() with 2 bars per x value

From Dev

How can I create a horizontal stacked bar chart with labels on the bars themselves as well as labels above the bars?

From Dev

Adding Error Bars to Grouped Bar Chart with D3.js

From Dev

MS Excel - adding line to some series in stacked bar chart

From Dev

Stacked bar chart

From Dev

100% stacked bar chart

From Dev

Stacked bar chart

From Dev

Stacked bar chart reversed

From Dev

Pandas stacked bar chart

From Dev

likert stacked bar chart

From Dev

Highcharts sizing bars in a Grouped and stacked barcharts

From Dev

Highcharts drilldown in stacked area chart

Related Related

  1. 1

    stacked bar chart shows hanging zeros when all values are 0 highcharts

  2. 2

    Grouping a stacked bar chart in Highcharts

  3. 3

    Stacked Bar Chart: incorrect height of bars and labels

  4. 4

    MPAndroidChart: Hide 0 value labels in a stacked bar chart

  5. 5

    Highcharts 4.0.3 stacked bar chart bug adds â-

  6. 6

    HighCharts Stacked Column Adding Onclick Event to the chart

  7. 7

    Hide empty bars in Grouped Stacked Bar Chart - chart.js

  8. 8

    Google chart create stacked bar besides 2 "normal" bars

  9. 9

    How to display series name inside bars of stacked bar chart?

  10. 10

    D3: Stacked bar chart exit().remove() not removing bars

  11. 11

    D3 - Stacked bar chart, position bars

  12. 12

    Highcharts bar chart with bar colored based on value

  13. 13

    Recharts Event not working when bar chart bar value is 0

  14. 14

    SSRS - Sorting Series by value In a Stacked Bar chart

  15. 15

    How to display sum of value in stacked bar chart

  16. 16

    Control spacing and height between bars in Highcharts.js bar chart

  17. 17

    How to remove bars for those bars with zero value in Chartjs bar chart?

  18. 18

    Stacked geom_bar() with 2 bars per x value

  19. 19

    How can I create a horizontal stacked bar chart with labels on the bars themselves as well as labels above the bars?

  20. 20

    Adding Error Bars to Grouped Bar Chart with D3.js

  21. 21

    MS Excel - adding line to some series in stacked bar chart

  22. 22

    Stacked bar chart

  23. 23

    100% stacked bar chart

  24. 24

    Stacked bar chart

  25. 25

    Stacked bar chart reversed

  26. 26

    Pandas stacked bar chart

  27. 27

    likert stacked bar chart

  28. 28

    Highcharts sizing bars in a Grouped and stacked barcharts

  29. 29

    Highcharts drilldown in stacked area chart

HotTag

Archive