« Previous       Home page demos       Next »

A combined Bar and Line chart

[No canvas support]

This demo is an example of a combined Bar and Line. In this case the Line is a spline but it doesn't have to be. In fact there's a button beneath the chart that you can use to toggle whether its a spline (curvy) or a regular angular Line. All thi button does is toggle the spline setting and then redraw the canvas.

In RGraph combining a Bar and Line is made easy by the CombinedChart class. You can see this being used in the code below. It simply takes the Bar and the Line objects and sets the correct settings on them and then draws the graph. But normally you would want only one set of labels (ie the Bar or the Line) drawn, however here the Bar labels are on one side and the Line labels are on the other. So after the combo class is drawn the Y labels are turned back on on the Line object and everything is redrawn. You can see how this is done in the code below.

The CombinedChart class is freely usable by yourself in a very similar way to the other RGraph classes. It's created in the same way and you pass it an array of objects - which usually is a combination of Bar and Line objects. You can give it more types if you want.

Apart from this the graph has "larger than the default" text and the Bar uses a gradient for the fill color: (Gradient(#A18AC5:#D1AAF5)

This goes in the documents header:
<script src="RGraph.common.core.js"></script>
<script src="RGraph.bar.js"></script>
<script src="RGraph.line.js"></script>
Put this where you want the chart to show up:
<canvas id="cvs" width="900" height="250" style="background-color: #555; border: 5px solid black; border-radius: 7px; box-shadow: 2px 2px 3px gray">
    [No canvas support]
</canvas><br />
<button style="border-radius: 5px; font-size: 20px; margin: 10px; padding: 7px" onclick="line.set('spline', !line.get('spline')); RGraph.redraw()">Toggle spline</button>
This is the code that generates the chart:
<script>
    bar = new RGraph.Bar({
        id: 'cvs',
        data: [81,90,75,85,90,78,61],
        options: {
            yaxisScaleMax: 100,
            backgroundGridVlines: false,
            backgroundGridBorder: true,
            backgroundGridColor: '#999',
            xaxisLabels: ['Mon','Tue','Wed','Thu','Fri','Sat','Sun'],
            hmargin: 20,
            marginLeft: 55,
            marginRight: 55,
            marginBottom: 45,
            colors: ['Gradient(#A18AC5:#D1AAF5)'],
            textColor: '#ccc',
            axesColor: '#999',
            xaxisTickmarksCount: 0,
            yaxisTickmarksCount: 0,
            shadowColor: 'black',
            shadowOffsetx: 0,
            shadowOffsety: 0,
            shadowBlur: 15,
            colorsStroke: 'rgba(0,0,0,0)',
            combinedEffect: 'wave',
            combinedEffectOptions: '{frames: 30}',
            textSize: 18
        }
    });



    var line = new RGraph.Line({
        id: 'cvs',
        data: [150,255,566,542,588,122,131],
        options: {
            yaxisScaleMax: 600,
            axes: false,
            backgroundGrid: false,
            linewidth: 7,
            colors: ['#d00'],
            yaxisPosition: 'right',
            axesColor: '#999',
            textColor: '#ccc',
            marginLeft: 45,
            marginRight: 45,
            tickmarksStyle: null,
            spline: true,
            combinedEffect: 'trace',
            textSize: 18
        }
    });
    
    var combo = new RGraph.CombinedChart([bar, line])
        .draw();
    
    // Because the combo class turns the line chart labels off,
    // turn them back on
    line.set({
        yaxisLabels: true
    });

    RGraph.redraw();
</script>