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

More »

 

SQLite Editor for PHP
The SQLite Editor for PHP software is a tool which will help you and/or your users administer and maintain your SQLite databases. Built as a tool that you can easily provide to your users, there's no danger of them damaging your database.

More »

 

Version 7.20
Version 7.20 (released in June 2026) is the latest version of RGraph and the major change in this version is an update to the default values of properties making for better looking charts without having to set any properties. Read more about this and other changes in the changelog.

Download »

 

Download
Get the latest version of RGraph (version 7.20, 9th June 2026) 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 »

 

Latest forum posts
These are the latest support forum posts that have been posted or updated.


23rd June, Richard
The SQLite Editor for PHP admin tool is now available for you to download

16th June, Rachel
I have a question about the 3D Bar chart

12th June, Marco
Should I use SVG or canvas for the charts on my website?

9th June, Richard
New version of RGraph: version 7.20

3rd June, Patrick
Question about installing RGraph

Support forum »

 

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 »

An infographic SVG Horizontal Bar chart

An SVG Horizontal Bar chart that could be used in an infographic. It's a Horizontal Bar chart with circles drawn at the end of the bars.

Here's a small Horizontal Bar chart that has had some extra work done to it so that it appears similar to what you might see used in an infographic.

It's just a case of sorting the data so that the data pieces appear in the correct order and the bars also have a bulb at the end indicating the position in the results (assuming that there was a competition of some kind). The chart is using svg so this bulb doesn't have to be drawn in the draw event.

The raw data for the chart is sorted using the javascript array function sort so that the highest number is at the top. Since the array is two-dimensional a custom sort function has to be used so that the action of sorting can be stipulated. This is the call to the sort function:

data.sort(function (a, b)
{
    return b[0] - a[0];
});

The original data array is two-dimensional (as you can see below in the source code) so that after sorting it, the label and the color are still all correlated and the correct name and color are associated with the correct bar.

This goes in the documents header:
<script src="RGraph.svg.common.core.js"></script>
<script src="RGraph.svg.hbar.js"></script>
Put this where you want the chart to show up:
<div style="float: right">
    <div style="width: 350px; height: 300px; display: inline-block" id="chart-container"></div>
</div>
This is the code that generates the chart - it should be placed AFTER the div tag:
<script>
    // The "raw" data. This array contains the data, the label and the color.
    data = [
        [4,'John', 'red'],
        [8,'Luis','green'],
        [6,'Joan','blue'],
        [5,'Larry','brown']
    ];

    // Sort the data array defined above using the JavaScript sort function.
    // It uses a custom function that allows you to control the sort procedure.
    // This function sorts the array based on the data in the second dimension.
    data.sort(function (a, b)
    {
        return b[0] - a[0];
    });
    
    // Initialise the 'extracted' arrays that are given to RGraph
    data_extracted   = [];
    labels_extracted = [];
    colors_extracted = [];
    
    // Go through the main data and separate out the different bits of
    // data into the relevant arrays
    data.forEach(function (v, k, arr)
    {
        data_extracted.push(v[0])
        labels_extracted.push(v[1] + ' ({1}%)'.format(v[0]))
        colors_extracted.push(v[2]);
    });

    // Create the Horizontal Bar chart using the data that was extracted above.
    // At this point the chart is a standard Horizontal Bar chart.
    hbar = new RGraph.SVG.HBar({
        id: 'chart-container',
        data: data_extracted,
        options: {
            yaxisLabels: labels_extracted,
            colors: ['red','green','blue','brown'],
            colorsSequential: true,
            marginInner: 10,
            responsive: [
                {maxWidth:null,parentCss:{'float':'right',textAlign:'none'}},
                {maxWidth:700,parentCss:{'float':'none',textAlign:'center'}}
            ]
        }
    }).draw();
    
    // Loop through the coordinates that are stored in the Horizontal Bar
    // object.
    for (var i=0; i<hbar.coords.length; ++i) {
    
        // Get the credentials of the bars on the Horizontal Bar chart from the
        // attributes of the SVG elements.
        var x = parseFloat(hbar.coords[i].element.getAttribute('x')),
            y = parseFloat(hbar.coords[i].element.getAttribute('y')),
            w = parseFloat(hbar.coords[i].element.getAttribute('width')),
            h = parseFloat(hbar.coords[i].element.getAttribute('height')),
            c = hbar.coords[i].element.getAttribute('fill');

        // Add the circles at the end of each bar. Remember that this doesn't need to
        // be in a draw event like with the canvas charts.
        RGraph.SVG.create({
            svg: hbar.svg,
            type: 'circle',
            attr: {
                cx: x + w,
                cy: y + (h / 2),
                r: 30,
                fill: c,
                stroke: 'white',
                'stroke-width': 7
            }
        });

        // This adds the text that sits inside the circles and indicates
        // the position (ie 1-4).
        RGraph.SVG.text({
            object: hbar,
            x: x + w,
            y: y + (h / 2),
            text: i+1,
            halign: 'center',
            valign: 'center',
            color: 'white',
            size: 30,
            bold: true
        });
    }
</script>