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 »

HOWTO: Create sparklines and inline charts

[No canvas support] [No canvas support] [No canvas support] [No canvas support]

Introduction

A previous request has been for inline plus/minus Bar charts - much like sparklines. Both of these are possible by using the configuration options available to make the chart appear as desired. The main thing is to use a smaller canvas size to make the chart fit inline with your text.

Inline Bar charts

Here the colors are defined by looping through the data beforehand to determine whether a value is positive or negative. When this is ascertained the color can be set appropriately and also the colorsSequential option is set to true. This is an example of an inline Bar chart:

<canvas id="cvs" width="50" height="20" style="background-color: white">[No canvas support]</canvas>

<script>
    data = [10,9,8,-8,-9,-10];
    colors = [];

    // Create the colors
    for (var i=0; i<data.length; ++i) {
        colors.push(data[i] > 0 ? '#0f0' : 'red');
    }
        
    new RGraph.Bar({
        id: 'cvs',
        data: data,
        options: {
            xaxisPosition: 'center',
            yaxisScale: false,
            yaxis: false,
            backgroundGrid: false,
            colors: colors,
            colorsSequential: true,
            marginLeft: 2,
            marginRight: 2,
            marginTop: 2,
            marginBottom: 2,
            colorsStroke: 'rgba(0,0,0,0)',
            marginInner: 1,
            xaxisTickmarksLength: 0
        }
    }).draw();
</script>

Inline Line charts (sparklines)

With sparklines the process is much the same except without the loop for the colors.

<canvas id="cvs" width="50" height="20">[No canvas support]</canvas>

<script>
    new RGraph.Line({
        id: 'cvs',
        data: [4,5,3,4,2,3,5,4,5,4,3,5,3],
        options: {
            marginLeft: 1,
            marginRight: 1,
            marginTop: 1,
            marginBottom: 1,
            backgroundGrid: false,
            xaxis: false,
            yaxis: false,
            linewidth: 1,
            tickmarksStyle: null,
            yaxisScaleMax: 10,
            yaxisScale: false,
            shadow: false
        }
    }).draw();
</script>

Inline Pie charts

<canvas id="cvs" width="30" height="20">[No canvas support]</canvas>

<script>
    new RGraph.Pie({
        id: 'cvs',
        data: [1,1,1,1],
        options: {
            colors: ['black','#ccc','red','#77f'],
            marginTop: 1,
            marginBottom: 1,
            marginLeft: 1,
            marginRight: 1,
            linewidth: 0,
            colorsStroke: '#0000',
            shadow: false
        }
    }).draw();
</script>

Inline Waterfall charts

<canvas id="cvs" width="50" height="20">[No canvas support]</canvas>

<script>
    new RGraph.Waterfall({
        id: 'cvs',
        data: [1,3,6,-5,4,8,-7],
        options: {
            colors: ['black','red','blue'],
            marginTop: 1,
            marginBottom: 1,
            marginLeft: 1,
            marginRight: 1,
            linewidth: 1,
            xaxis: false,
            yaxis: false,
            yaxisScale: false,
            shadow: false,
            backgroundGrid: false,
            colorsStroke: '#0000',
            colorsConnectors: '#0000'
        }
    }).draw();
</script>

Taking it further

One option if you're finding the charts too small, or you wish to enlarge them when clicked is to add an anchor (link) around the canvas so that when clicked a ModalDialog is shown with a larger version of the chart - a detail view if you like. There's a HOWTO document about using the ModalDialog here.