How to use DOM1-style RGraph custom events

Share RGraph:   

Summary
A guide for using the newer DOM1-style custom events. They can make using custom events much easier than previously.

[No canvas support]

Previously, adding custom RGraph events was rather verbose - being modelled on the DOM2 method of adding events. Now though you can use the older but more concise DOM1 method. You can only add one DOM1 event listener (which could do multiple things) but you can add both DOM1 and DOM2 style event listeners at the same time.

This example uses the ondraw custom event to add a highlight after-effect to the bars.

The code to achieve this is shown below.


<script>
    var bar = new RGraph.Bar({
        id: 'cvs',
        data: [[47,75],[32,74],[71,85],[25,19],[23,71],[81,59],[43,130],[23,20]],
        options: {
            colors: ['#494949','#35A0DA'],
            labels: ['Alf','Bob','Carry','Dara','Edgar','Fliss','Gary','Harry'],
            ylabelsCount: 3,
            numyticks: 6,
            backgroundGridVlines: false,
            backgroundGridBorder: false
            textAccessible: true,
            textSize: 14,
            scaleZerostart: true
        }
    }).ondraw = function (obj)
    {
        var len = obj.coords.length;
        
        obj.context.fillStyle = 'rgba(255,255,255,0.15)';
        for (var i=0; i<len; ++i) {
            obj.context.fillRect(
                obj.coords[i][0],
                obj.coords[i][1],
                obj.coords[i][2] / 2,
                obj.coords[i][3]
            );
        }
    }
    
    bar.draw();
</script>

The .on() function

Because DOM1 events don't work particularly well with chaining there is an .on() function which helps facilitate use of DOM1 events and chaining. For example:

<script>
    var bar = new RGraph.Bar({
        id: 'cvs',
        data: [5,8,9,6,4,5,5],
        options: {
            // ...
        }
    }).on('draw', function (obj)
    {
        // ...
    }).draw()
    
    bar.draw();
</script>

An example of the ontooltip event

[No canvas support]

This is an example of the ontooltip event that you can use to create charts in tooltips.


<script>
    var line = new RGraph.Line({
        id: 'cvs2',
        data: [14,16,13,15,5,8,9,12],
        options: {
            labels: ['Luis','Kev','Jim','Harry','Hoolio','liss','Rhonda','Fred'],
            tooltips: function () {return '<canvas id="tooltip_canvas" width="300" height="100"></canvas>';}),
            hmargin: 5,
            tickmarks: 'endcircle',
            shadow: false,
            textAccessible: true,
            textSize: 14,
            scaleZerostart: true
        }
    }).draw()
    
    line.ontooltip = function (obj)
    {
        // This is the data for the tooltip bar chart
        var tooltip_data = [[4,4,6],[4,7,5],[10,1,2],[5,5,5],[1,2,2],[4,2,2],[2,4,3],[6,4,2]];
    
        // Get the visible tooltip
        var tooltip = RGraph.Registry.get('chart.tooltip');
        var index   = tooltip.__index__;
        
        var bar = new RGraph.Bar({
            id: 'tooltip_canvas',
            data: tooltip_data[index],
            options: {
                gutterLeft: 17,
                gutterRight: 5,
                gutterTop: 15,
                labels: ['Monday','Tuesday','Wednesday'],
                backgroundGrid: false,
                noyaxis: true,
                ylabels: false,
                labelsAbovebar: true,
                shadow: false
            }
        }).draw();
    }
</script>

Comments

Add a new comment...