How to get rounded corners on a Bar chart

This quick little HOWTO document demonstrates how you can redefine the standard rect() function in order to achieve rounded tops to your bars on a Bar chart.

[No canvas support]

This might be a desirable effect for you. By default the canvas API doesn't have the facility to add rounded corners to a rectangle that it draws.

This, though, is easily added by redefining the rect() function to a customised version that provides the rounded corners.

This is what's being done here - the standard rect() API function is redefined to a function of our own creating that gives the rectangle a rounded corner effect.

Include the RGraph library files

Here the library files are being included. Nothing special here - the bare minimum that you would need is the Bar chart library and the core library. The tooltips and dynamic libraries are only necessary if you want tooltips.

<script src="/libraries/RGraph.common.core.js" ></script>
<script src="/libraries/RGraph.common.dynamic.js" ></script> <!-- Just needed for the tooltips -->
<script src="/libraries/RGraph.common.tooltips.js" ></script> <!-- Just needed for the tooltips -->
<script src="/libraries/" ></script>

Redefine the canvas 2D API rect() function

This is where the standard rect() function is redefined to a function that draws the rectangle how we want it to (with rounded corners at the top) instead of it being a bog-standard 90deg cornered rectangle.

The severity of the rounding effect is held in the global variable roundedCorners so it's easy for you to change this if you need to.

You could just as easily make this an extra argument to the function if you're going to be drawing lots of rectangles with different sized corners.

    // A global variable - this controls the extent of the rounding effect
    window.roundedCorners = 5;

    // This code redefines the standard canvas rect() function on the 2D drawing
    // context. The radius controls how severly the corners are rounded and it's
    // hard coded to be set to the roundedCorners global variable (ie the
    // variable is on the window object). Feel free to change that though and
    // make it an optional argument.
    // @param number x      The X coordinate
    // @param number y      The Y coordinate
    // @param number width  The width of the rectangle
    // @param number height The height of the rectangle
    CanvasRenderingContext2D.prototype.rect = function (x, y, width, height)
        // Get the extent of the rounding effect from the global variable
        var radius = window.roundedCorners;

        // Because the function is added to the 2D context prototype
        // the 'this' variable is actually the context.
        // Save the existing state of the canvas so that it can be restored later;
            // Translate to the X/Y coordinates at which we are to draw
            // the rectangle
            this.translate(x, y);

            // Move to the center of the top horizontal line of the rectangle
            this.moveTo(width / 2,0);
            // Draw the rounded corners. The connecting lines in between them are drawn automatically
            this.arcTo(width,0,width,height, Math.min(height / 2, radius));
            this.arcTo(width, height, 0, height, Math.min(width / 2, 0)); // The zero radius here means the rectangle has a flat bottom
            this.arcTo(0, height, 0, 0, Math.min(height / 2, 0)); // The zero radius here means the rectangle has a flat bottom
            this.arcTo(0, 0, radius, 0, Math.min(width / 2, radius));

            // Draw a line back to the start coordinates which effectively closes the rectangle
            this.lineTo(width / 2,0);

        // Restore the state of the canvas to as it was before the save()

The RGraph code to make the Bar chart

And here's the code that makes the Bar chart. It's just standard RGraph code doing nothing special. Because the rect() function was redefined at the canvas API level the RGraph Bar chart is effectively unaware of the change - and uses the rect() function as it would normally.

    // Here's a regular RGraph Bar chart that will now make use of the above
    // rect() function. Nothing has to be altered here because the native rect()
    // function was redefined above - so the Bar chart effectively doesn't think
    // anything is different.
    new RGraph.Bar({
        id: 'cvs',
        data: [12,13,16,15,16,19,19,12,23,16,13,24],
        options: {
            marginTop: 35,
            marginLeft: 35,
            shadow: true,
            shadowOffsetx: 2,
            shadowOffsety: 2,
            shadowBlur: 2,
            title: 'A Bar chart with rounded corners',
            xaxisLabels: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'],
            tooltips: ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'],
            backgroundGridVlines: false,
            backgroundGridBorder: false,
            xaxis: false,
            yaxis: false,
            colors: ['Gradient(#fcc:red)']

A downloadable demo

There's an example available in the download archive that demonstrates this technique and all of the necessary code is easily accessible. The example is bar-rounded-corners.html in the demos folder.

Using the grow() animation effect

[No canvas support]

The rounded corners effect shown here works perfectly well with the wave() effect however the grow() effect does not like it.

Here's a demonstration of what happens when you try to use rounded corners and the grow() effect together (click the Animate button to see the effect). I've increased the number of frames on the animation - effectively slowing it down - so that you can see what's happening.

Is there anything that can be done about the grow() effect issue?

[No canvas support]

Yes! Fortunately canvas has provisions that enable us to restrict the drawing area to the area confined by the margins - ie the chart area.

Normally this would mean problems for drawing the labels - ie they wouldn't then be visible.

However since RGraph uses the textAccessible option by default then this is not a problem. This is because the text that RGraph canvas charts use is DOM based text and therefore not constrained by the canvas based clipping region.

This is a version of the chart when it's using the clipping region. Again, like the chart above, it has been slowed down so that you can see the difference.

    bar_grow2  = new RGraph.Bar({
        id: 'cvs3',
        data: [12,13,16,15,16,19,19,12,23,16,13,24],
        options: {
            marginTop: 35,
            marginLeft: 35,
            title: 'A demonstration of the grow() effect when using clipping',
            xaxisLabels: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'],
            tooltips: ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'],
            backgroundGridVlines: false,
            backgroundGridBorder: false,
            xaxis: false,
            yaxis: false,
            colors: ['red']
    // Create the clipping region. This can be done after the
    // initial draw() because the problem is only evident when
    // the grow() effect is used.
    context = bar_grow2.context;

        bar_grow2.canvas.width - bar_grow2.marginLeft - bar_grow2.marginRight,
        bar_grow2.canvas.height - bar_grow2.marginTop - bar_grow2.marginBottom