# A grouped and stacked SVG Bar chart

This is a grouped and stacked Bar chart. A regular grouped Bar chart is created and then the coordinates of that Bar chart are used to position vertical progress bars (which in reality are actually more Bar charts).

The bars are animated by using the Grow() effect - but each grow effect is delayed by 50 milliseconds which gives it the appearance of being the Wave() effect.

This goes in the documents header:

    <script src="RGraph.svg.common.core.js"></script>
    <script src="RGraph.svg.common.tooltips.js"></script>
    <script src="RGraph.svg.common.key.js"></script>
    <script src="RGraph.svg.bar.js"></script>

Put this where you want the chart to show up:

    <div style="padding: 15px">
        <div style="width: 950px; height: 300px" id="cc"></div>
    </div>

This is the code that generates the chart:

    <script>
        data = [
            [9,7,8],
            [14,10,11],
            [9,11,12],
            [8,6,9],
            [9,11,12]
        ];

        tooltips = [];

        for (var i=0; i<18; i++) {
            tooltips.push([
                'Fred',
                'Jemima',
                'Lucifer'
            ])
        }

        bar = new RGraph.SVG.Bar({
            id: 'cc',
            data: data,
            options: {
                gutterTop: 75,
                key: ['Fred','Jemima','Lucifer'],
                keyColors: [
                    'rgba(255,0,0,0.25)',
                    'rgba(0,255,0,0.25)',
                    'rgba(0,0,255,0.25)'
                ],
                hmargin: 20,
                hmarginGrouped: 5,
                yaxis: false,
                xaxis: false,
                xaxisLabels: [
                    'Monday',
                    'Tuesday',
                    'Wednesday',
                    'Thursday',
                    'Friday'
                ],
                colors: [
                    'rgba(0,0,0,0)',
                    'rgba(0,0,0,0)',
                    'rgba(0,0,0,0)'
                ],
                title: 'Widgets created by day, worker and time of day',
                titleSubtitle: 'Making widgets is hard work and this shows the results of last weeks output',
                titleSubtitleY: '+5',
                labelsAbove: true,
                labelsAboveSize: 8,
                labelsAboveSpecific: [
                    '8am','1pm','10pm',
                    '8am','1pm','10pm',
                    '8am','1pm','10pm',
                    '8am','1pm','10pm',
                    '8am','1pm','10pm'
                ]
            }
        }).on('draw', function (obj)
        {
            for (var i=0; i<obj.coords.length; ++i) {
                (function (index)
                {
                    var coords = obj.coords[index];

                    setTimeout(function ()
                    {
                        new RGraph.SVG.Bar({
                            id: 'cc',
                            data: [[33.333,33.333,33.333]],
                            options: {
                                backgroundGrid: false,
                                colors: [
                                    'rgba(255,0,0,0.75)',
                                    'rgba(0,255,0,0.75)',
                                    'rgba(0,0,255,0.75)'
                                ],
                                tooltips: tooltips[index],
                                yaxisMax: 100,
                                hmargin: 0,
                                hmarginGrouped: 0,
                                grouping: 'stacked',
                                yaxis: false,
                                xaxis: false,
                                yaxisScale: false,
                                gutterLeft: coords.x,
                                gutterRight: obj.width - coords.x - coords.width,
                                gutterTop: coords.y,
                                gutterBottom: obj.height - coords.y - coords.height
                            }
                        }).grow();
                    }, 50 * index);
                })(i);
            }
        }).draw();

        // Some tooltips appearance customisation
        RGraph.SVG.tooltips.style.backgroundColor = 'black';
        RGraph.SVG.tooltips.style.color           = 'white';
        RGraph.SVG.tooltips.style.fontWeight      = 'bold';
    </script>

« Back