Thursday, March 12, 2009

Finding Excel Chart Value Axis Maximum Before You Render the Chart

Say you, for some crazy reason, needed to know what the maximum value of your chart's value axis (y-axis, usually) was so you could use it to calculate random things related to the chart (for example, the coordinates of data points so you could draw lines ending at those data points using Aspose.Cells).

You might do this, if you had already found out what your maximum value on the whole chart was:
int magnitude = (int)java.lang.Math.pow(10,java.lang.Math.round(java.lang.Math.log10(maximum / 8)));
int increments = (int)( java.lang.Math.round((maximum / 8)/magnitude)*magnitude);
int axismaximum = java.lang.Math.round((increments * 8)/magnitude)*magnitude + increments;
Assuming your axis has a linear scale, and your minimum is zero, it should work. 8 is the number of horizontal lines on your chart.  Adjust accordingly.

No comments: