How do I pass a variable number of charts into the CombinedChart?

« 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 James Parsons on 15th January 2013
I am trying to use RGraph to plot share prices. I would like the end user to be able to compare the share performance of different companies over time. In order to plot the performance of multiple companies in the same graph, I am trying to pass multiple RGraph.Scatter objects into RGraph.CombinedChart's constructor following the example at ::

The problem I'm encountering is that RGraph.CombinedChart seems to assume that the number of graphs added to RGraph.CombinedChart is determined by the programmer during coding, rather than by the end user at runtime. I would like the end user to be able to compare any number of shares in the same graph. I am trying to use JavaScript's .apply(this, args) function to pass an array of graphs into RGraph.CombinedChart's constructor. This is tricky because it appears you cannot call .apply(this, args) on a constructor. I am trying the workaround I found at, but I'm having problems.

It would be nice if the RGraph.CombinedChart constructor took an array of graphs as a single parameter, rather than each graph as a separate argument. To maintain backwards compatibility, perhaps you could do something like this:

     RGraph.CombinedChart = function() {
         if (arguments.length === 1 && arguments[0].isArray()) {
             // Assume an array of charts was passed in
         else {
             // Current functionality

The .isArray() function may not be backwards compatible with older web browsers.

An alternative approach (and the approach used by JpGraph IIRC) is to have the RGraph.CombinedChart constructor take no parameters, and then add charts to it using an add(chart) function. Like this:

     var combinedChart = new RGraph.CombinedChart();

I can add this functionality to my instance of RGraph myself, but upgrading to newer versions of RGraph would then become a hassle.

Is there a simpler solution to my problem? Am I making things more difficult on myself than they need to be?
Posted by James Parsons on 16th January 2013
Whoops! I guess it would be Array.isArray(arguments[0])

I'm trying to do the primary stock as a boxplot (RGraph.Scatter), and the others appearing as line graphs (which may be implemented with RGraph.Line or RGraph.Scatter with connecting lines). It appears that RGraph.Scatter is preferred for financial data, although I don't fully understand why.
Posted by RGraph support on 16th January 2013
Hi James,

If you're combining Scatter charts then you don't need to use the CombinedChart class - this is just to allow Line and Bar charts to be more easily combined - it sets up the correct margins so that the data-points correspond. There's an example of a Scatter chart combined with a HProgress bar in the test area - and this shows you that you don't have to use the CombinedChart class to combine charts.

Obviously you'd need to ensure the gutters are set correctly - ie each chart the same - and that the scales correspond (chart.ymax and chart.xmax)

Allowing the arguments of CombinedChart to be specified as an array, or specify an .add() function should be OK - I'll look at adding this.

You can do stock plots if you prefer with the Line - but keep in mind that the Line charts X axis is not scaled - it's simply all the items spaced equally across the canvas - there's no X scale. Plus it doesn't do box plots.


Richard, RGraph Support
Posted by RGraph support on 16th January 2013

Another example of combining charts here:

This time the Bar chart which has the bars "replaced" with VProgress bars (using the coords array). It doesn't use the Scatter chart - but shows you that you can combine charts without the CombinedChart class.

Richard, RGraph Support

Add a reply

« Back to message list