MENU
.net Powerful JavaScript charts
About
RGraph is a JavaScript charts library based on HTML5 SVG and canvas. RGraph is mature (over 16 years old) and has a wealth of features making it an ideal choice to use for showing charts on your website.

More »

 

Download
Get the latest version of RGraph (version 6.20, 1st December 2024) from the download page. You can read the changelog here. There's also older versions available, minified files and links to cdnjs.com hosted libraries.

Download »

 

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

More »

Integrating with the ModalDialog

Example

Shown below is an example of the ModalDialog that can be seen be right-clicking the chart and clicking on the Login to admin area... option. This shows the ModalDialog being used to show a login form.

There's another example of using the ModalDialog to show the some help information that you can see on the forum search page (click on the help link).

[No canvas support]

Introduction

This page shows you how to make use of the ModalDialog library that's bundled with RGraph. This particular example adds a context menu to the chart, of which the only option is to show a login dialog. This could be used to allow logging into a user account or an admin area. Note that you don't have to use RGraph in order to use the ModalDialog - the library can be used standalone without making use of RGraph if desired.

The dialog doesn't need to require user input - it could just be a static "Please wait..." type dialog, which is shown while a subsequent page loads, which takes a few seconds. Another use-case is to show more information to the user - an example of which is the Help link on the forum search page.

The ModalDialog was originally an external library, however it's now bundled as part of RGraph.

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

<script>
    //
    // Make the context menu
    //
    contextmenu_submenu = [
        ['A submenu item!', function () {alert('A submenu item!');}]
    ];

    contextmenu = [
        ['A submenu', contextmenu_submenu],
        null,
        ['Login...', function ()
        {
            ModalDialog.show({
                id: 'myDialog',
                zoomFactorStart: 0,
                zoomFactorEnd: 0
                // ...
            });
        }]
    ];
    

    line = new RGraph.Line({
        id: 'cvs',
        data: [45,12,16,18,44,54,23,21,56,58,33,47],
        options: {
            backgroundGrid: false,
            marginInner: 10,
            marginTop:50,
            linewidth: 15,
            shadow: false,
            xaxisLabels: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'],
            title: 'A line chart with a context menu (right click)',
            backgroundGridVlines: false,
            backgroundGridBorder: false,
            xaxisTickmarks: false,
            yaxisTickmarks: false,
            xaxisLinewidth: 2,
            yaxisLinewidth: 2,
            textSize: 18,
            spline: true,
            shadow: true,

            // Add the contextmenu which was defined above
            contextmenu: contextmenu
        }
    }).draw();
</script>

Hiding the ModalDialog

To hide the ModalDialog (from a "Cancel" button for example), you can use the ModalDialog.hide method:

<input type="reset" value="Cancel" onclick="ModalDialog.hide()">

Updates in December 2024

In December 2024 the ModalDialog was updated so that when it's being shown the background is no longer scrollable (previously you could scroll up and down the page with your mousewheel even whilst the ModalDialog was being displayed). You can see this in action by trying the example above.

Updates in September 2024

In September 2024 a few options were added to help in controlling the appearance of the ModalDialog - largely as a result of a few effects having been added. These effects are now the default so you only need to change the two zoomFactor* properties to 1 if you don't wish to use them. The example chart above shows the dialog and how it zooms in and zooms out when being shown and hidden. The full list of options is shown below.

ModalDialog configuration properties

Name: width
Description: 
If you want to specify a width for the dialog you can use this property to do so. You may not need to specify this - but if you want the dialog to be larger than the html that you give makes the dialog then you will.
Default: none
Name: styleBackground
Description: 
Any styles that you want to apply to the background cover. Because these styles are applied after the regular css you shouldn't need to use the !important modifier.
...
styleBackground: {
    opacity: 0.75,
    color: '#fcc'
}
...
Default: {} (An empty object)
Name: styleDialog
Description: 
Any styles that you want to apply to the dialog. Because these styles are applied after the regular css you shouldn't need to use the !important modifier.
...
styleDialog: {
    borderRadius: '0',
    backgroundColor: 'red'
}
...
Default: {} (An empty object)
Name: styleTopbar
Description: 
Any styles that you want to apply to the decorative top bar. Because these styles are applied after the regular css you shouldn't need to use the !important modifier.
...
styleTopbar: {
    backgroundColor: 'black',
    border: 'none'
}
...
Default: {} (An empty object)
Name: zoomFactorStart
Description: 
When the ModalDialog is shown, by default, there's a zoom effect used. This setting controls the starting zoom factor and thus how much the dialog zooms. The default setting of zero poduces an elargement effect, a setting of 1 means that effectively there is no zoom and a setting of 2 (for example) means that the dialog starts large and then shrinks to the correct size.
Default:  0
Name: zoomFactorEnd
Description: 
When the ModalDialog is hidden, by default, there's a zoom effect used. This setting controls the ending zoom factor and thus how much the dialog zooms. The default setting of zero poduces a shrinking effect, a setting of 1 means that effectively there is no zoom and a setting of 2 (for example) means that the dialog expands whilst fading out.
Default:  0
Name: hideOnBackground
Description: 
When the semi-opaque background is clicked it hides the dialog (if you click the dialog itself though it doesn't). This option allows you to disable that if you prefer. You can still press the esc key on the keyboard though to hide the dialog.
Default: true
Name: topbar
Description: 
This controls whether the top bar of the ModalDialog is shown or not. If you set this to false then the bar is hidden and the padding-top css property is reduced accordingly (so that there isn't a large space left in the bars place).
Default: true
Name: removeFromDOMTimeout
Description: 
This is the delay before removing the ModalDialog from the dom after the ModalDialog.hide function is called. This delay is in place in order to allow the enlarge and the fadeout effects to complete before removing the dialog from the dom.
Default: 500
Name: pageScroll
Description: 
By default when the ModalDialog is shown the page scrollbars are disabled so the page cannot be scrolled using the mouse wheel. If you want to change this behaviour so that you can scroll the page whilst the ModalDialog is displayed then set this property to true.
Default: false

...
contextmenu: [
    [
        'Login to admin area...',
        function ()
        {
            ModalDialog.show({
                id: 'myDialog',
                width: 300
                zoomFactorStart: 2,
                zoomFactorEnd: 2,
                // ...
            });
        }
    ]
]
...

Customising the ModalDialog

You can customise the appearance of the ModalDialog by using three css classes, which are documented here and also shown below:

<style>
    /*
    * These are the CSS classes that you can use to customise the appearance of the ModalDialog.
    * If you're trying to override something which the scripts set, then because of the ordering
    * you may need to use the !important modifier.
    */
    
    /* This is the semi-opaque background */
    .ModalDialog_background {
    }


    /* This is the dialog */
    .ModalDialog_dialog {
        box-shadow: none !important;
    }


    /* This is gray bar at the top of the dialog */
    .ModalDialog_topbar {
    }
</style>

You can also use the styleBackground styleDialog and styleTopbar configuration properties to get a very similar effect. There are slight differences though: Firstly, the javascript properties are applied last and therefore you probably won't need to use the !important modifier and secondly, javascript properties are javascript not css - thus you'll need to use the javascript versions of the names (eg paddingTop vs padding-top)

ModalDialog integration

To integrate the ModalDialog look at the sample code above (the key section is where the context menu is defined). The method you should call is ModalDialog.show({id: 'myDialog', width: 300}). The id is the id of the layer to use. Only the innerHTML is used, not the layer itself, so it can be hidden by setting the css display property to none. The width is a number that is used as the width of the dialog.

The only library needed for the ModalDialog to work is RGraph.modaldialog.js - you do not need to use RGraph.common.core.js. This makes for far smaller download requirements.

Covering the scroll bars

Normally, a regular div lives inside the browser and cannot cover the scroll bars. There is however a way to achieve this but it does mean that restructuring your website may be necessary, and it's done by using an iframe. The following steps are involved:

  1. The index page of your website creates an iframe and sets it to cover the entire window.
  2. This iframe then loads the website.
  3. The div then covers the top-most window.

The ModalDialog doesn't use this technique because it would require restructuring of your website, but there is an example of it here

Supplying the dialog as a string

Normally, having forms in you ModalDialog isn't a problem. However, if you want to access the form elements with javascript it can be tricky because there are two form elements with the same ID - the original, hidden one, and also the one in the ModalDialog when it's shown. Because of this you can supply the dialog as a string, like this:

<script>
    str = 'string:<h1>My Dialog</h1><p align="center"><button' +
          'onclick="ModalDialog.hide();">Close</button></p>'
    
    ModalDialog.show(str);
</script>

Also, you may find this method easier to use if you are showing multiple different dialogs on your page.