Tweet about RGraph
Follow for HTML5 news
Bookmark and share:

# RGraph: HTML5 canvas graph library - Context menus

- What are context menus?
- What do they look like?
- Can I have multiple levels of menus?
- How do I define context menus?
- Can I have "separators"?
- How do I bypass them?
- What can I use them for?
- Is there an example file that uses the CSS classes?
- Context menus, Macs, Safari and Opera

### 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?

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" 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?

Yes, as of 24th 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('chart.contextmenu', [
                                  ['Zoom', RGraph.Zoom],
                                  ['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('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:

### Is there an example file that uses the CSS classes?

You can take a look at the bottom of the CSS file for this website.

### 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.