RGraph: HTML5 canvas graph library - Context menus

What are context menus?

Context menus are what you see when you click your right mouse button. With RGraph, you can specify custom menus for your graphs if you wish. This way you can define custom actions to be associated with menu items. Context menus are a very easy way to extend the functionality of your graphs, allowing you to associate presentation style functionality with them.

Important: Opera does not allow you to customise the context menu, so with this browser you should use the left mouse button on the graph, instead of the right.

What do they look like?

An RGraph context menu

Context menus look like the image on the right. You can of course customise their look by using stylesheets. The CSS class you need to use is RGraph_contextmenu. Eg:


<style type="text/css">
    .RGraph_contextmenu {
        color: red ! important;
    }
</style>

The "! important" is not necessary if you're not overriding a style that is set by the graph script. If you're having trouble, it's a good idea to use it though.

Can I have multiple levels of menus?

No. Context menus are a single level only.

How do I define context menus?

Defining a context menu is quite a simple affair. Eg:

myBar.Set('chart.contextmenu', [
                                ['Menu item 1', Callback1],
                                ['Menu item 2', Callback2]
                               ]);

As you can see, the value is a two dimension array. The second being an array consisting of a string which is used as the name of the menu item, and a function object (NOT the function name as a string). The function object is the function called when the menu item is selected.

Can I have "separators"?

Yes. Simply pass null instead of an array as your menu item. Eg:

myBar.Set('chart.contextmenu', [
                                ['Menu item 1', Callback1],
                                null,
                                ['Menu item 2', Callback2]
                               ]);

How do I bypass them?

If for any reason you wish to access the browsers own context menu, you can hold down your CTRL key on your keyboard when you click, and the canvas context menu will be bypassed. Try it on the graph below by holding your CTRL key whilst right clicking.

What can I use them for?

Since the context menu items run Javascript functions when selected, you can use them for pretty much anything you want. For example you could make a presentation system, with the context menu controlling which graph is shown on the canvas. For example:

[No canvas support]

Context menus and Macs/Safari

Mac Safari and Firefox and Windows Safari can have trouble displaying the context menu. So for this reason, for these browsers, the context menu is attached to a double click of the left mouse button.