Loading a page where the RGraph libraries code are on the parent page

Share RGraph:   To help my Google visibility (it can't get much worse!), if you like and use RGraph I'd appreciate it if you could link to me

« Back to message list

Enter your email address to get email updates on this topic. You can stop receiving updates by clicking the link in the update email messages.

Posted by David Cook on 26th June 2017
Hi Richard:

Quick question. In my usage, I have many iFrames on a single page with a widgit per iFrame (some RGraph, some not).

Currently, for my RGraph widgits, each iFrame loads your common js code, and whatever specific js RGraph libraries that are needed (eg,. such as Gauge, etc).

I attempted to load the required js files in the parent page - hoping to reduce resources by only needing once copy of each library (rather than duplicating them for each iFrame).

When I attempt to create an RGraph using parent.RGraph.Gauge(...), it does not find the canvas context.

(If I include all the js in the iFrame, and call it via the iFrame, everything works well.)

Is it possible to have all the RGraph code on the parent page, and reference it from the iFrame - to avoid duplication of the JS code?

Posted by Richard on 26th June 2017
Hi there,

It can be done - though you'll need to modify the Gauge library to allow you to pass in the canvas object instead of it getting it via the document.getElementById() function - currently it uses the parent frames' function and cannot find the canvas. So change this:

var canvas = document.getElementById(id);

To this:

var canvas = conf.canvas || document.getElementById(id);

(its on line 30 in my copy).

Then you can do this in your frames chart configuration:

new window.parent.RGraph.Gauge({
     canvas: document.getElementById('cvs'),
     id: 'cvs',
     min: 0,
     max: 100,
     value: 56,
     options: {

WARNING: I doubt this is very sturdy and will probably break at the drop of a hat.

Posted by David Cook on 26th June 2017
Thanks Richard...

Though I am a bit hesitant to modify the RGraph code itself, as that becomes a maintenance nightmare for me when you release new versions.

I also find myself a bit apprehensive at your "will probably break at the drop of a hat" comment ;)

Perhaps this could be a 'feature request' for a future upgrade? It would help in situations where many many widgits are on the same page in separate iFrames.

In our situation, the user can create their own Dashboards, with any number of Widgits tied to real devices (sensors, etc) so we have no idea as to how complex a users Dashboard can possible become.

Just a suggestion!


Add a reply

« Back to message list