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 use for showing charts on your website.
Get the latest version of RGraph (version 6.19, 28th September 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.
How do I reference an RGraph object that was loaded with jQuery?
Posted by Joachim at 16:37 on Wednesday 30th August 2023[link]
hi,
I'm creating RGRaph a charts using jQuery - e.g.
<script>jQuery(document).ready(function() {
var chartcvs = new RGraph.Scatter( {"chart_type":"...});
</script>
If I then try to access chart object e.g. with: "chartcvs.clearLassoState();"
It will fail with: "Uncaught ReferenceError: chartcvs is not defined"
Can you help with this?
Posted by Richard at 18:20 on Wednesday 30th August 2023[link]
Because the Scatter chart object is created inside a function along with the var keyword the chartcvs variable is local to that function.
To access it outside of the jQuery.ready() function you would need to omit the var keyword. That then makes the chartcvs variable global. So you could access it by name.
For example:
<script>
jQuery(document).ready(function()
{ // A global variable on purpose!
myChart = new RGraph.Scatter({ ... });
});
myChart.draw();
</script>
There are other ways, like going via the RGraph registry to get to the chart object - but they're a little convoluted.
One thing you could do to make things clearer is use a GLOBALS object like this:
<script>
GLOBALS = {};
jQuery(document).ready(function()
{
GLOBALS.myChart = new RGraph.Scatter({ // ...
});
});
// You can then access the chart object by doing this:
GLOBALS.myChart.draw();
</script>
Posted by Joachim at 09:59 on Thursday 31st August 2023[link]