How to apply borderRadius only for top side of Column or Bar in Highchart
Asked Answered
P

3

7

According to API document of Highchart: https://api.highcharts.com/highcharts/plotOptions.column.borderRadius

But I can't find solution to apply borderRadius only for Top Side of column.

plotOptions: {
    column: {
        borderRadius: 10  // How to make it only for top side. Don't apply it to bottom side ? 
    }
},

Sample Image

Sample Image

Purebred answered 15/11, 2017 at 20:9 Comment(1)
I guess that's not currently possible. If you look at the source, it is rendered with <rect> tag using rx and ry attributes to specify radius. There is a way to do it with <path> #34924388. But have not found something similar via highcharts.Recognizance
Z
8

Use this highcharts-border-radius plugin.

$(function() {
  'use strict';
  (function(factory) {
    if (typeof module === 'object' && module.exports) {
      module.exports = factory;
    } else {
      factory(Highcharts);
    }
  }(function(Highcharts) {
    (function(H) {
      H.wrap(H.seriesTypes.column.prototype, 'translate', function(proceed) {
        const options = this.options;
        const topMargin = options.topMargin || 0;
        const bottomMargin = options.bottomMargin || 0;

        proceed.call(this);

        H.each(this.points, function(point) {
          if (options.borderRadiusTopLeft || options.borderRadiusTopRight || options.borderRadiusBottomRight || options.borderRadiusBottomLeft) {
            const w = point.shapeArgs.width;
            const h = point.shapeArgs.height;
            const x = point.shapeArgs.x;
            const y = point.shapeArgs.y;

            let radiusTopLeft = H.relativeLength(options.borderRadiusTopLeft || 0, w);
            let radiusTopRight = H.relativeLength(options.borderRadiusTopRight || 0, w);
            let radiusBottomRight = H.relativeLength(options.borderRadiusBottomRight || 0, w);
            let radiusBottomLeft = H.relativeLength(options.borderRadiusBottomLeft || 0, w);

            const maxR = Math.min(w, h) / 2

            radiusTopLeft = radiusTopLeft > maxR ? maxR : radiusTopLeft;
            radiusTopRight = radiusTopRight > maxR ? maxR : radiusTopRight;
            radiusBottomRight = radiusBottomRight > maxR ? maxR : radiusBottomRight;
            radiusBottomLeft = radiusBottomLeft > maxR ? maxR : radiusBottomLeft;

            point.dlBox = point.shapeArgs;

            point.shapeType = 'path';
            point.shapeArgs = {
              d: [
                'M', x + radiusTopLeft, y + topMargin,
                'L', x + w - radiusTopRight, y + topMargin,
                'C', x + w - radiusTopRight / 2, y, x + w, y + radiusTopRight / 2, x + w, y + radiusTopRight,
                'L', x + w, y + h - radiusBottomRight,
                'C', x + w, y + h - radiusBottomRight / 2, x + w - radiusBottomRight / 2, y + h, x + w - radiusBottomRight, y + h + bottomMargin,
                'L', x + radiusBottomLeft, y + h + bottomMargin,
                'C', x + radiusBottomLeft / 2, y + h, x, y + h - radiusBottomLeft / 2, x, y + h - radiusBottomLeft,
                'L', x, y + radiusTopLeft,
                'C', x, y + radiusTopLeft / 2, x + radiusTopLeft / 2, y, x + radiusTopLeft, y,
                'Z'
              ]
            };
          }

        });
      });
    }(Highcharts));
  }));


  Highcharts.chart('container', {
    chart: {
      type: 'column'
    },
    xAxis: {
      categories: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
    },

    plotOptions: {
      column: {
        grouping: false,
        borderRadiusTopLeft: 10,
        borderRadiusTopRight: 10
      }
    },

    series: [{
      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]
    }]
  });
});
<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>

<div id="container" style="height: 400px"></div>

Fiddle demo

Top Rounded Column Highcharts

Zigzag answered 16/11, 2017 at 5:46 Comment(1)
How to implement this in react native? any idea?Lees
D
4

Just in case I ever wonder again:

clip-path: inset(0% 0% -2px 0% round 2px);

This adds 2px border radius to the top of that column. (apply to these rects)

Desperation answered 16/11, 2021 at 19:36 Comment(1)
Do you have a solution for stacking bars too?Whipcord
M
0

As @user1497119 mentioned you can apply the css to override the styles like this in column chart type

.highcharts-series-group > g.highcharts-series.highcharts-column-series.highcharts-series-0 > rect {
  clip-path: inset(0% 0% -4px 0% round 4px);
}


Misrepresent answered 25/3 at 17:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.