Dynamic max Y, min Y and tickcount

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 wrox on 8th September 2015
Hi Richard,

we want to decide value of Y-Axis max and Y-Axis min values dynamically depends on data that comes from database.

Also we want to decide how many lines will be plotted. I mean numhlines value for chart.

Please suggest best way to plot values.

Thanks

Posted by Richard on 8th September 2015
Hi,

Not knowing anything about your data or you desired range I can't honestly say. Though you'll probably need to use the RGraph API function:

RGraph.arrayMax()

There's no arrayMin function but its easy enough to make.

Using these functions you can then either use those exact values or round them to the next 1/10/100/1000/whatever. The string length of the biggest number (how many characters it contains) will give you an idea of the size of number you need.

Eg For a 4 character number you could use 10,000 or 5,000 as the maximum - whichever is more appropriate.

For the minimum you could do some similar, possibly (if your minimum value is less than one) counting how many zeros there are as you count from the left side of the number. Eg If 0.0008 is your minimum value then it has 4 leading zeros so:

Math.pow(10, -1 * 4)

Or in that case you could use zero perhaps.


For the grid you can set the number of horizontal grid lines with backgroundGridAutofitNumhlines but again - how many you set will depend on your data.

Richard
If RGraph has helped you - please share the website and give your feedback - thanks! You can give your feedback or leave a testimonial by adding a forum post
Posted by wrox on 8th September 2015
Hi Richard,

I think I was not able to explain you the problem correctly.

I have observer that Ymin in any graph is always 0 and Ymax is calculated from my data i.e. if I have data [4.68,4.66,4.64,4.65] then there will be lots of white space below my line graph. Also min and max value of data has very small difference hence will not able to see variations in graph.

Is there way Rgraph can automatically calculate Ymin and Ymax based on my data given in above example like Ymin~4.62 and Ymax~4.69?

Thanks.
Posted by Richard Heyes on 8th September 2015
Hi there,

There's no built in way - but it's easy enough to do. At the most basic level you could use the arrayMax() function an the arrayMin function below to set the array max and min yourself:


$(document).ready(function ()
{
     var data = [4.68,4.66,4.64,4.65];
     var max = RGraph.arrayMax(data);
     var min = arrayMin(data); // This function is defined below

     var bar = new RGraph.Bar({
         id: 'cvs',
         data: data,
         options: {
             ymax: max,
             ymin: min
         }
     }).draw();
});


/**
* Returns the minimum numeric value which is in an array. This will be a
* part of the RGraph common library as of the next release
*
* @param array arr The array (can also be a number, in which case it's returned as-is)
* @param int Whether to ignore signs (ie negative/positive)
* @return int The minimum value in the array
*/
function arrayMin (arr)
{
     var max = null,
         ma = Math;
      
     if (typeof arr === 'number') {
         return arr;
     }
        
     if (RGraph.isNull(arr)) {
         return 0;
     }

     for (var i=0,len=arr.length; i<len; ++i) {
         if (typeof arr[i] === 'number') {

             var val = arguments[1] ? ma.abs(arr[i]) : arr[i];
                
             if (typeof min === 'number') {
                 min = ma.min(min, val);
             } else {
                 min = val;
             }
         }
     }

     return min;
};


Or perhaps you could try this (this also uses the arrayMin function from above):

var data = [4.68,4.66,4.64,4.65];
var max = Math.ceil(RGraph.arrayMax(data) * 10) / 10;
var min = Math.floor(arrayMin(data) * 10) / 10;

alert(max);
alert(min);



Richard
If RGraph has helped you - please share the website and give your feedback - thanks!
Posted by wrox on 9th September 2015
Hi Richards,

Thanks for the prompt response. We are almost there to the solution that I am looking for. With the example you gave I am now able to see variations in line chart. If you see the chart all y-axis labels shows values as 5, how I can control and provide appropriate values their. Also can I control not of segments in on Y-axis currently it is five by default.

Thanks.
Posted by Richard on 9th September 2015
Hi,

Try setting the scaleDecimals option - the numbers may be being rounded to five.

As for the number of labels you could try the ylabelsCount option.

Richard
If RGraph has helped you - please share the website and give your feedback - thanks! You can give your feedback or leave a testimonial by adding a forum post
Posted by wrox on 10th September 2015
Thanks Richard.

You made my day!!!

Add a reply




« Back to message list