A switch animation effect
There are numerous ways of switching between charts - some of which have examples in the download archive. Here's a nice example of a CSS transition.
It's a nice transition effect where one chart blends into another with a "folding-away" type motion. The other chart is shown in the reverse direction. The effect is not animated by using javascript animation but by using javascript to apply css properties. The css transitions that are also specified on the canvas then take care of doing the animating.
The charts themselves are quite straightforward - with the Line chart having a larger linewidth and bigger tickmarks than normal.
This goes in the documents header:
<script src="RGraph.common.core.js"></script>
<script src="RGraph.line.js"></script>
<script src="RGraph.bar.js"></script>
<style>
div#canvas-container {
position: relative;
width: 600px;
height: 250px;
}
div#canvas-container canvas {
position: absolute;
top: 0;
left: 0;
width: 600px;
height: 250px;
background-color: white;
transition: all 1s;
opacity: 1;
}
div#canvas-container canvas#cvs1 {
top: 125px;
left: 300px;
width: 0;
height: 0;
opacity: 0;
transform: rotate(90deg);
}
</style>
Put this where you want the chart to show up:
<div id="canvas-container" style="display: inline-block; float: right">
<canvas id="cvs1" width="600" height="250">[No canvas support]</canvas>
<canvas id="cvs2" width="600" height="250">[No canvas support]</canvas>
</div>
This is the code that generates the chart - it should be placed AFTER the canvas tag(s):
<script>
// First create the Bar chart but don't call the draw() function.
bar = new RGraph.Bar({
id: 'cvs1',
data: [4,8,12],
options: {
colors: ['#5690C9'],
marginInner: 25,
textSize: 16,
titleSize: 12,
title: 'A Bar chart (click to switch to the Line chart)',
xaxisLabels: ['John','Fred','Lucy']
}
}).draw();
bar.canvas.style.width = 0;
bar.canvas.style.height = 0;
bar.canvas.style.left = '300px';
bar.canvas.style.top = '125px';
// Create the Line chart with no X-axis
line = new RGraph.Line({
id: 'cvs2',
data: [
[1,6,4],
[5,3,8]
],
options: {
colors: ['#B71A1A','#54A4CF'],
tickmarksStyle: 'filledcircle',
tickmarksSize: 10,
linewidth: 10,
xaxisLabels: ['John','Fred','Lucy'],
title: 'A Line chart (click to switch)',
marginInner: 25,
marginTop: 50,
textSize: 16,
textColor: 'black',
titleSize: 24
}
}).draw();
// This is the click event handler that swaps the canvas
// tag width/height/opacity CSS properties.
document.getElementById('cvs1').onclick =
document.getElementById('cvs2').onclick = function (e)
{
var id = e.target.id;
var el1 = document.getElementById('cvs1');
var el2 = document.getElementById('cvs2');
// If the canvas that was clicked on was cvs1 then do this
// (hide cvs1 and show cvs2)
if (id === 'cvs1') {
el1.style.width = 0;
el1.style.height = 0;
el1.style.top = '125px';
el1.style.left = '300px';
el1.style.opacity = 0;
el1.style.transform = 'rotate(180deg)';
el2.style.width = '600px';
el2.style.height = '250px';
el2.style.top = 0;
el2.style.left = 0;
el2.style.opacity = 1;
el2.style.transform = 'rotate(0)';
// If the canvas that was clicked on was cvs2 then do
// this (hide cvs2 and show cvs1)
} else {
el2.style.width = 0;
el2.style.height = 0;
el2.style.top = '125px';
el2.style.left = '300px';
el2.style.opacity = 0;
el2.style.transform = 'rotate(180deg)';
el1.style.width = '600px';
el1.style.height = '250px';
el1.style.top = 0;
el1.style.left = 0;
el1.style.opacity = 1;
el1.style.transform = 'rotate(0)';
}
};
</script>