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 show charts on your website.

More »

 

License
RGraph can be used for free under the GPL or if that doesn't suit your situation there's an inexpensive (£99) commercial license available.

More »

 

Download
Get the latest version of RGraph (version 6.17) from the download page. There's also older versions available, minified files and links to cdnjs.com hosted libraries.

More »

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 charts 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 charts, 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 chart, 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 classes you need to use are RGraph_contextmenu_background, RGraph_contextmenu and RGraph_contextmenu_item. Eg:


<style type="text/css">
    .RGraph_contextmenu {
    }

    .RGraph_contextmenu_item {
    }

    .RGraph_contextmenu_background {
    }
</style>

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

Can I have multiple levels of menus?

Yes, as of April 2010 you can have a dual-level context menu, which can reduce "option overload" in your user interface. This example defines a simple context menu:

bar.set({
    contextmenu: [
        ['Application', [['Login...', function () {ModalDialog.show('modaldialog_login', 300);}]] ],
        null,
        ['Cancel', function () {}]
    ]
});

As you can see there could easily get to be a lot of arrays, so it may help you during development to structure your code by using indentation.

How do I define context menus?

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

myBar.set({
    contextmenu: [
        ['Menu item 1', Callback1],
        ['Menu item 2', Callback2]
    ]
});

As you can see, the value is a two-dimensional 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({
    contextmenu: [
        ['Menu item 1', Callback1],
        null,
        ['Menu item 2', Callback2]
    ]
});

How do I bypass them?

If for any reason you wish to access the browser's own context menu, you can hold down your ctrl key on your keyboard when you click, and the canvas context menu will be bypassed.

How do I get the underlying shape that was clicked on?

In some circumstances, you may want to know which bar/point/segment was right-clicked on when showing the context menu (if any). In these cases, you will find the pertinent information (the same as what you get from the various .get*() methods) on the context menu object - which is held in the registry: RGraph.Registry.get('contextmenu').__shape__.

Note: Check your console (CTRL+SHIFT+J in Chrome) for the notifications.


<script>
    function myListener (obj)
    {
        console.log(RGraph.Registry.get('contextmenu').__shape__)
    }
    RGraph.addCustomEventListener(myBar, 'oncontextmenu', myListener);
</script>

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 chart is shown on the canvas.

Context menus, Macs, Safari and Opera

Mac Safari, Mac 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. Opera doesn't support customising the context menu so this browser also uses a left mouse button double click to trigger the context menu. Note: More recent versions of these browsers may well support the traditional right-click context menus.

Related events

There are two context menu related events that you can utilise:

As their names suggest, one fires before the context menu is shown, and one after. Important: Because of the fading effect, it may seem that both events fire before the context menu is shown, however, this is just due to the nature of javascript timers and the fact that alert's will block them (pause them in effect).