# How do I change the scaleDecimals value depending on the maximum value?

Share RGraph:

« Back to message list

Posted by Claus on 7th September 2017
I'm using a scatter chart with automatic calculation of the y scale size (ymax unspecified).

I would like the number of decimals on the y axis to depend on the current maximum value. For example, if RGraph decides that the maximum y value should be 5, I want the labels to be 0, 1, 2, 3, 4 and 5 with no decimals. Buf if the maximum y value becomes 0.05, I want the labels to be 0.00, 0.01, 0.02, 0.03, 0.04 and 0.05 with two decimals.

What's the best way for me to do this?

Posted by Richard on 7th September 2017
Hi there,

You could use the draw event and then in it check the maximum value - so off the top of my head you could do something like this:

var scatter = new RGraph.Scatter({
id: 'cvs',
data: [[6,3],[8, 12],[4,4.8]],
options: {
xmax: 50
}
}).on('draw', function (e, obj)
{
// Calculate the new max value
if (obj.scale2.max < 1) { obj.set('scaleDecimals', 2);
} else if (obj.scale2.max < 5) { obj.set('scaleDecimals', 1);
} else { obj.set('scaleDecimals', 0);
}

RGraph.redraw();
}).draw();

Richard
Posted by on 7th September 2017
Hi Richard,

Thanks for your answer. I did a very brief test of your proposal, and I ran into some problems. It'll have to wait until tomorrow before I can investigate it further, but initially I encountered two problems:

1) The obj parameter in the event handler is not set. If I change "function (e,obj)" to simply "function (obj)", the obj parameter appears to be correct.
2) The RGraph.redraw() apparently causes the draw event to fire again and again and again ...

But I'll look more into it tomorrow.
Posted by Richard on 7th September 2017
Hi there,

1. I've probably got the order of the parameters mixed up.
2. Change the event to firstdraw instead of draw.

You could changing it to this:

var scatter = new RGraph.Scatter({
id: 'cvs',
data: [[6,3],[8, 12],[4,4.8]],
options: {
xmax: 50
}
}).on('firstdraw', function (obj, e)
{
// Calculate the new max value
if (obj.scale2.max < 1) { obj.set('scaleDecimals', 2);
} else if (obj.scale2.max < 5) { obj.set('scaleDecimals', 1);
} else { obj.set('scaleDecimals', 0);
}

RGraph.redraw();
}).draw();

Richard
Posted by on 8th September 2017
Hi Richard,

Changing the event to 'firstdraw' did not solve the infinite recursion problem. The RGraph.scatter.js file contains this code:

/**
* Fire the onfirstdraw event
*/
if (this.firstDraw) {
RG.fireCustomEvent(this, 'onfirstdraw');
this.firstDraw = false;
this.firstDrawFunc();
}

This means that if RG.fireCustomEvent() causes the graph to be redrawn, the 'firstdraw' event will fire again. I think, perhaps, 'this.firstDraw = false' should be executed before the call to RG.fireCustomEvent().

Anyway, if I change the event handler to this:

.on('firstdraw', function (obj, e)
{
// Calculate the new max value
if (obj.scale2.max < 1) { obj.set('scaleDecimals', 2);
} else if (obj.scale2.max < 5) { obj.set('scaleDecimals', 1);
} else { obj.set('scaleDecimals', 0);
}

this.firstDraw = false; // Prevent event from firing again
RGraph.redraw();
})

the mechanism works.

Cheers,
Claus
Posted by Richard on 8th September 2017
Hi there,

Good catch - thank you.

Richard

« Back to message list