An SVG Bar chart showing the UK inflation rate

If you like RGraph please share it:

This goes in the documents header:
<script src="RGraph.svg.common.core.js"></script>
<script src="RGraph.svg.bar.js"></script>
Put this where you want the chart to show up:
<div style="width: 750px; height: 300px" id="chart-container"></div>
This is the code that generates the chart:
<script>
    // This is the source data
    data = {            
        source: [
            {rate: 2.9, label: 'May 2017'},
            {rate: 2.7, label: 'Apr 2017'},
            {rate: 2.3, label: 'Mar 2017'},
            {rate: 2.3, label: 'Feb 2017'},
            {rate: 1.8, label: 'Jan 2017'},
            {rate: 1.6, label: 'Dec 2016'},
            {rate: 0.9, label: 'Oct 2016'},
            {rate: 1.2, label: 'Nov 2016'},
            {rate: 1.0, label: 'Sep 2016'},
            {rate: 0.6, label: 'Aug 2016'},
            {rate: 0.6, label: 'Jul 2016'},
            {rate: 0.5, label: 'Jun 2016'}
        ],
        
        // These are for once the data has been extracted and split up from
        // the source
        data:   [],
        labels: []
    };

    // Reverse the data so that the latest figure is on the right
    data.source = RGraph.SVG.arrayReverse(data.source);
    
    
    // Loop through the source data extracting the required parts
    for (var i=0; i<data.source.length; ++i) {
        if (data.source[i]) {
            
            // Extract the data piece from the source data
            data.data[i] = data.source[i].rate;

            // Extract the label from the source data
            data.labels[i] = ((i+1) % 3 === 0 ? data.source[i].label : '');
        }
    }




    new RGraph.SVG.Bar({
        id: 'chart-container',
        data: data['data'],
        options: {
            xaxisLabels: data['labels'],
            colors: ['Gradient(#4B86B6:#4B86B6:white)'],
            strokestyle: 'black',
            textFont: 'Monospace',
            textSize: 10,
            hmargin: 8,
            backgroundGridColor: '#eee',
            backgroundGridVlines: false,
            backgroundGridBorder: false,
            yaxis: false,
            xaxis: false,
            yaxisDecimals: 1,
            labelsAbove: true,
            labelsAboveDecimals: 1,
            title: 'UK inflation rate',
            titleSize: 10,
            titleHalign: 'left',
            titleX: 50,
            yaxisMax: 3.5,
            linewidth: 0.75
        }
    }).draw();
</script>

« Back