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

More »

 

Download
Get the latest version of RGraph (version 6.19, 28th September 2024) 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 »

 

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 »

HOWTO: Use CSS3 transitions

By using css transitions you can make smooth css animations and effects for your canvas tag. The effect shown below switches between two charts using a css transition and also changing the css width and height (the html width and height attributes which set the width and height of the canvas remain unchanged). The code changes the css width and height settings at the correct times and the css transition does the rest. The css transition is defined on the canvas tag (in css like this:

<style>
    canvas {
        transition: width .5s, height .5s, top .5s, left .5s;
    }
</style>
<script>
    window.onload = (function ()
    {
        //
        // Used to track which chart is currently being displayed
        //
        var type = true;

        //
        // This function is called to draw the bar chart
        //
        function drawBar ()
        {
            var bar = new RGraph.Bar({
                id: 'cvs',
                data: [5,8,16,5,8,9,4,3,2,4,3,2],
                options: {
                    xaxisLabels: ['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'],
                    textSize: 14
                }
            }).draw();
        
            type = false;
        }




        //
        // This function is called to draw the line chart
        //
        function drawLine ()
        {
            var line = new RGraph.Line({
                id: 'cvs',
                data: [5,8,16,5,8,9,4,3,2,4,3,2],
                options: {
                    xaxisLabels: ['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'],
                    textSize: 14
                }
            }).draw();
                
            type = true;
        }



        //
        // This is the onclick event for the button
        //
        document.getElementById('myButton').onclick = function (e)
        {
            var canvas   = document.getElementById('cvs');

            //
            // This CSS change results in the <canvas> shrinking (due to the CSS transition)
            //
            canvas.style.width  = 0;
            canvas.style.height = 0;
            canvas.style.top    = '100px'
            canvas.style.left   = '300px';
            
            //
            // Now that the <canvas> has been shrunk - wait 0.75 seconds, draw the alternate
            // chart and unshrink it.
            //
            setTimeout(function ()
            {
                RGraph.reset(canvas);
                canvas.__rgraph_aa_translated__ = false;
                
                if (!type) {
                    drawBar();
                } else {
                    drawLine();
                }


                //
                // This CSS change results in the canvas expanding (due to the CSS transition)
                //
                canvas.style.width    = '600px';
                canvas.style.height   = '250px';
                canvas.style.position = 'relative';
                canvas.style.left     = 0;
                canvas.style.top      = 0;
                
            }, 750)
            
        }
        
        drawBar();
    });
</script>