Bookmark and share:
Bookmark with delicious tweet this site Post to Google Buzz! Post to Facebook Share on FriendFeed Stumble! this site

RGraph: HTML5 canvas graph library - Tooltips

[No canvas support]
Tooltip effect:


Tooltips are a very effective and straight forward way to extend your graphs and add more information to them, without overloading the user.

What can they hold?

At the base level, tooltips are DIVs, so they can hold a multitude of HTML - images, videos etc. See below for information on showing graphs in tooltips.

How can I specify them?

You can specify them by including the tooltips code and then using the chart.tooltips property. For example:

  1. Include the RGraph libraries.
    <script src="RGraph.common.core.js"></script>
    <script src="RGraph.common.tooltips.js"></script>
    <script src="RGraph.line.js"></script>
  2. Define your graph and set the tooltips property.
        window.onload = function ()
            var line = new RGraph.Line('cvs', [64,34,26,35,51,24]);
            line.Set('chart.tooltips', ['<b>Winner!</b><br />John', 'Fred', 'Jane', 'Lou', 'Pete', 'Kev']);
            line.Set('chart.tooltips.effect', 'expand');
            line.Set('chart.gutter', 30);
            line.Set('chart.hmargin', 10);
            line.Set('chart.linewidth', 2);
            line.Set('chart.shadow', true);
            line.Set('chart.shadow.offsetx', 0);
            line.Set('chart.shadow.offsety', 0);
            line.Set('chart.shadow.color', 'green');
            line.Set('chart.shadow.blur', 25);
            line.Set('chart.colors', ['green']);
            line.Set('chart.tickmarks', 'circle');
            line.Set('chart.labels', ['John', 'Fred', 'Jane', 'Lou', 'Pete', 'Kev']);

What can I specify?

The tooltips that you specify are usually strings (which can contain HTML). They can however also be functions which are called when they're about to be displayed. The function should return the text that is used as the tooltip. You have the option to either specify one function per data point, or just one function for all of the tooltips. You can mix functions and strings if you wish. These functions are passed the numerical, zero-indexed tooltip index and the return value is used as the tooltip text. So to summarise:

Can I show graphs in tooltips?

[No canvas support]

You can, and with the custom event support that RGraph has (as of 10th July 2010), it's reasonably easy. Simply attach your function that creates the graph to the ontooltip event. This allows the tooltip HTML to be created and added to the page so that the code that creates the graph can run. The sequence is:

  1. Specify the HTML for the tooltip as normal (including the <canvas> tag).
  2. Use the ontooltip RGraph event so that a function is called when a tooltip is shown.
  3. This function should subsequently create the graph.

The tooltip DIV is to be found in the RGraph registry - RGraph.Registry.Get('chart.tooltip'). And if you want it the numerical zero indexed count of the tooltip is to be found in the __index__ property: RGraph.Registry.Get('chart.tooltip').__index__

<script src="RGraph.common.core.js" ></script>
<script src="RGraph.common.tooltips.js" ></script>
<script src="RGraph.line.js" ></script>

    .RGraph_tooltip {
        background-color: white ! important;

<canvas id="cvs" width="600" height="250">[No canvas support]</canvas>

    window.onload = function ()
        labels = ['Gary','Pete','Lou','Ned','Kev','Fred'];

        line = new RGraph.Line("cvs", [4.5,28,13,26,35,36]);
        line.Set('chart.tooltips', function (idx) {return labels[idx] + 's stats<br/><canvas id="__tooltip_canvas__" width="400" height="150">[No canvas support]</canvas>';});
        line.Set('chart.hmargin', 10);
        line.Set('chart.tickmarks', 'endcircle');
        line.Set('chart.colors', ['blue']);
        line.Set('chart.ymax', 100);
        line.Set('chart.gutter', 30);
        line.Set('chart.labels', labels);
        RGraph.AddCustomEventListener(line, 'ontooltip', CreateTooltipGraph);

    * This is the function that is called (by the ontooltip event) to create the tooltip charts
    * @param obj object   The graph object
    function CreateTooltipGraph(obj)
        // This data could be dynamic
        var line  = new RGraph.Line('__tooltip_canvas__', [5,8,7,6,9,5,4,6,3,5,4,4]);
        line.Set('chart.labels', ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']);
        line.Set('chart.gutter', 20);
        line.Set('chart.hmargin', 5);
        line.Set('chart.tickmarks', 'endcircle');
        line.Set('chart.background.grid.autofit', true);

If you want to see more source code, simply view the source of this page and look at the code that creates the bar chart.

Can I customise the appearance of tooltips?

Yes. You can either use the default CSS class RGraph_tooltip, or you can specify a specific CSS class that a graphs tooltips should use with the property chart.tooltips.css.class. The two graphs on this page have different looking tooltips by using this method - the line graph uses the default look, whilst the bar chart overrides the CSS class name and sets it to bar_chart_tooltips_css. For example:

bar.Set('chart.tooltips.css.class', 'bar_chart_tooltips_css');
    .bar_chart_tooltips_css {
        background-color: white ! important;
        border: 2px solid black ! important;
        padding: 3px;

You can read more about RGraph CSS classes here.

What tooltip effects are available?

These effects are available to you:

All of them are as their names imply. fade is a straight forward fade in effect, expand is another effect which expands outward from the center of the tooltip, contract is like the expand effect but in reverse, snap is an effect which can be used in a limited set of circumstances and "snaps" to the current mouse position and none is simply no effect at all. The default effect used by all graph types is fade. Note: If you're showing canvases in your tooltips then the expand, contract and snap effects will not work - you must use fade or none.

Note: The snap effect is only available to graph types where the tooltip is triggered using the onmousemove event. Currently this means the Line chart, Rscatter graph, Scatter graph and Tradar chart. It can also be negatively effected when using multiple graphs on one page.

Tooltips, the clipboard and Firefox

If you're using Firefox, there's a note about tooltips and the clipboard (ie copying text) on the issues page.

Can I override the tooltip function?

You can by stipulating chart.tooltips.override. This should be a function object that handles everything with regard to showing the tooltip. Highlighting the graph is still done for you - the override function is only concerned with showing the tooltip. The override function is passed these arguments:

Note: Although "id:xxx" strings are not expanded for you, you can easily do this yourself by using the RGraph.getTooltipText('id:xxx') function.

myObj.Set('chart.tooltips.override', function (canvas, text, x, y, idx) {alert('In tooltip override function...');});