How to make a transition effect

Recommend RGraph:  
    Read comments...

Summary
A guide for making a transition effect between different charts. You can use various effects for transitions but this guide uses the Conceal/Reveal effects

You can use the range of RGraph effects to easily make an effective transition effect. This page shows you how you can do just that with the Conceal and Reveal effects. If you need to show multiple charts this can be an effective way to switch between the two.

[No canvas support]

Include the libraries

This HTML code, which goes in the page <HEAD>, simply includes the relevant RGraph libraries along with jQuery.

<script src="RGraph.common.core.js" ></script>
<script src="RGraph.common.key.js" ></script>
<script src="RGraph.common.effects.js" ></script>
<script src="RGraph.bar.js" ></script>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>

Define the Javascript that draws the charts

The charts are drawn by individual functions. This makes it easy to call those functions when needed and draw the charts.

<script>
    /**
    * This function shows the chart that displays total sales by day.
    */
    function showChart1 ()
    {
        RGraph.clear(document.getElementById("cvs"));

        /**
        * Deregister the other chart so that it doesn't keep getting redrawn
        */
        if (typeof bar2 === 'object') RGraph.ObjectRegistry.remove(bar2);


        // Enable the correct button
        document.getElementById("butDayAndPerson").disabled = false;
        
        bar1 = new RGraph.Bar({
            id: 'cvs',
            data: [4,5,8,6,4,3,2],
            options: {
                labels: ['Monday','Tuesday','Wednesday','Thursday','Friday','Saturday','Sunday'],
                title: 'Total sales by day'
                textAccessible: true,
                textSize: 14,
                gutterTop: 40
            }
        }).reveal()
    }





    /**
    * This chart shows the total sales by day again, but this time broken down by person as well.
    */
    function showChart2 ()
    {
        // Enable the correct button
        document.getElementById("butDay").disabled = false;

        RGraph.Clear(document.getElementById("cvs"));
        
        /**
        * Deregister the other chart so that it doesn't keep getting redrawn
        */
        if (typeof bar1 === 'object') RGraph.ObjectRegistry.remove(bar1);

        // A global on purpose
        // Enable the correct button
        document.getElementById("butDayAndPerson").disabled = false;
        
        bar2 = new RGraph.Bar({
            id: 'cvs',
            data: [[2,2],[3,2],[5,3],[3,3],[3,1],[2,1],[1,1]],
            options: {
                labels: ['Monday','Tuesday','Wednesday','Thursday','Friday','Saturday','Sunday'],
                key: ['John','Brandon']
                title: 'Sales broken down by day and person',
                ymax: 10,
                textAccessible: true,
                textSize: 14,
                gutterTop: 40
            }
        }).reveal()
    }





    function transitionTo(func)
    {
        // Disable both buttons
        document.getElementById("butDay").disabled = true;
        document.getElementById("butDayAndPerson").disabled = true;

        document.getElementById("cvs").__object__.conceal(null, func);
    }





    /**
    * Initially the canvas is blabk so there is no need to clear anything. So it is sufficient to
    * just call the relevant function to show the first chart.
    */
    window.onload = (function ()
    {
        showChart1();
    });
</script>

The buttons that trigger the transitions

These are just some regular HTML buttons that trigger the transitioning to a new chart. Buttons are disabled when clicked so that double clicking does not cause any ill-effects. The appropriate button is then enabled when the new chart is shown.

<button onclick="transitionTo(showChart1)" disabled="disabled" id="butDay">Show sales by day only</button>
<button onclick="transitionTo(showChart2)" disabled="disabled" id="butDayAndPerson">By day and person</button>

Notes

  1. Some effects may be unusable in this type of situation because of the references that are added to the canvas. If this situation arises however it is feasible to use two separate canvas tags which are positioned at the exact same point, and use the display: CSS attribute to switch between the two. This may also be necessary if you use any interactive features such as tooltips.

  2. Since the ObjectRegistry was introduced (facilitating the combining of charts) objects have to be registered regardless of whether they use dynamic features or not. As a result of this you may be experiencing objects being redrawn when you don't want them to be. To solve this you need to remove them from the ObjectRegistry. How to do this is documented here.

Comments