MENU
.net Powerful JavaScript charts
About
RGraph is a JavaScript charts library based on HTML5 SVG and canvas. RGraph is mature (over 16 years old) and has a wealth of features making it an ideal choice to use for showing charts on your website.

More »

 

Download
Get the latest version of RGraph (version 6.20, 1st December 2024) from the download page. You can read the changelog here. There's also older versions available, minified files and links to cdnjs.com hosted libraries.

Download »

 

License
RGraph can be used for free under the GPL or if that doesn't suit your situation there's an inexpensive (£129) commercial license available.

More »

A canvas-based black Line chart

[No canvas support]

This is quite an old demo of a Line chart but it has stood up through the years very well and still looks very presentable.

It's a black Line chart with two datasets that both use custom tickmarks. These custom tickmarks are drawn by utilising the RGraph.path api function. This is a function that makes drawing with the canvas api far less verbose than it usually is.

Before drawing commences, using the beforedraw event, the canvas is cleared to a plain black color. You might find this as a performance-enhancing tip on various canvas performance articles though by the second frame you'll not be drawing on a transparent canvas - so it might be a redundant action.

The trace Line chart effect is used here - but an options object is passed to the effects function call. This is just a regular javascript object which contains a single property: frames As the name suggests this stipulates the number of frames in the animation. Lower numbers make for quicker animations that finish sooner.

The responsive function changes the size of the text and the margins, changes the css float to none and a callback function updates a global variable that controls the size of the custom tickmarks. The callback function runs after the chart has been redrawn so if you change anything here (as we're doing) then you need to redraw the chart. That can be done by calling the RGraph.redraw function.

This goes in the documents header:
<script src="RGraph.common.core.js"></script>
<script src="RGraph.line.js"></script>
Put this where you want the chart to show up:
<div style="float: right">
    <canvas id="cvs" width="600" height="250" style="border-radius: 5px; box-shadow: 3px 3px 3px #ccc">[No canvas support]</canvas>
</div>
This is the code that generates the chart - it should be placed AFTER the canvas tag(s):
<script>
    tickmarkRadius = 6;

    line = new RGraph.Line({
        id: 'cvs',
        data: [
            [7.0,6.9,9.5,14.5,18.2,21.5,25.2,26.5,23.3,18.3,13.9,9.6],
            [0.2,0.8,5.7,11.3,17.0,22.0,24.8,24.1,20.1,14.1,8.6, 2.5]
        ],
        options: {
            xaxisLabels: ['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'],
            shadow: false,
            backgroundGridBorder: true,
            xaxis: false,
            yaxis: false,
            colors: ['#0f0','#76D0FF'],
            tickmarksStyle: function (obj, data, value, index, x, y, color, prevX, prevY)
            {
                obj.path(
                    'b lw 2 a % % % 0 % false f null s black',
                    x, y, tickmarkRadius, RGraph.TWOPI
                );
            },
            linewidth: 3,
            textColor: '#eee',
            responsive: [
                {maxWidth: null, width:600,height:250,options:{textSize: 12,marginLeft: 40,marginBottom: 40,marginTop: 40,marginRight: 40},parentCss:{'float':'right',textAlign:'none'},callback:function (){tickmarkRadius = 6; RGraph.redraw();}},
                {maxWidth: 900, width:400,height:150,options:{textSize: 10,marginTop:15,marginRight:15,marginBottom:25,marginLeft:30},parentCss:{'float':'none',textAlign:'center'},callback:function (){tickmarkRadius = 5; RGraph.redraw();}}
            ]
        }
    }).on('beforedraw', function (obj)
    {
        RGraph.clear(obj.canvas, 'black');

    }).trace({frames: 15});
</script>