Page 1 of 1

Logarithmic axis labelling (any base) and req. for E base

Posted: Wed Jun 06, 2007 12:26 pm
by 9793833
Hi,

I've got a working solution for putting labels on a logarithmic axis which always displays the next logarithmic decade. For instance, you plot values ranging from y=8 to 78 and you want the 1, 10 and 100 label to be visible - and I don't think that "myAxis.Automatic = true;" gets it right in most cases. In the y=8 to 78 example for instance, only 1 and 10 are visible :?

Add this below method to your .cs class. It generates the next logarithmic number based on the max value to plot and the logarithmic base you use on the axis:

private double GetLogarithmicMaximumLabel(double maxSeriesValue, double logBase)
{
int pow = 0;
while (maxSeriesValue >= 1)
{
maxSeriesValue /= logBase;
pow++;
}
return Math.Pow(logBase, pow);
}

Then in your code, apply the following property values to your axis:

double maxY = mySeries.MaxYValue();

myAxis.Automatic = false;
myAxis.Logarithmic = true;
myAxis.LogarithmicBase = 10; // for instance... it could be anything
if (myAxis.Minimum > myAxis.LogarithmicBase)
myAxis.Minimum = myAxis.LogarithmicBase;
myAxis.Maximum = GetLogarithmicMaximumLabel(Math.Ceiling(maxY), myAxis.LogarithmicBase);
myAxis.MinorGrid.Visible = true;
myAxis.Labels.Exponent = true;
myAxis.TickOnLabelsOnly = false;
myAxis.MinorTickCount = 9;
myAxis.MinorTicks.Length = 2;

If you got a solution for displaying the strings "E", "E^2", "E^3" etc. for base Math.E on the axis labels I would be glad to hear about it! The optimal solution would be properly formattted powers, eg. html equvivalent of "E<sup>2</sup>" and not "E^2".

Best regards
Michael Schøler
External Consultant
www.vestas.com

Posted: Wed Jun 06, 2007 1:30 pm
by narcis
Hi Michael,
I've got a working solution for putting labels on a logarithmic axis which always displays the next logarithmic decade. For instance, you plot values ranging from y=8 to 78 and you want the 1, 10 and 100 label to be visible - and I don't think that "myAxis.Automatic = true;" gets it right in most cases. In the y=8 to 78 example for instance, only 1 and 10 are visible
This is because when axes are automatic their minimum and maximum values are the minimum and maximum values of the series. In that case, since 78 is the max. value 100 label is not displayed. To achieve what you request the easiest way is manually setting axes min. and max. values using SetMinMax method.
If you got a solution for displaying the strings "E", "E^2", "E^3" etc. for base Math.E on the axis labels I would be glad to hear about it! The optimal solution would be properly formattted powers, eg. html equvivalent of "E<sup>2</sup>" and not "E^2".
Yes, you can achieve that using something like this:

Code: Select all

			tChart1.Axes.Left.Labels.Exponent = true;
			tChart1.Axes.Left.Labels.ValueFormat = "00-e0";
Here you can find more information about .NET Framework's custom numeric format strings.

Posted: Wed Jun 06, 2007 1:57 pm
by 9793833
Hi,

Thanks for a quick reply =)

Manually setting the min/max as you suggest is clearly an option - however, my GetLogarithmicMaximumLabel method makes it possible to insert the correct next logarithmic decade for any base and any max serie value without the need of hardcoded min/max values. The method could just as well be used with SetMinMax instead of my .Maximum and .Minimum assignments from my initial post.

Regarding the e-exponent, when I'm using

Code: Select all

tChart1.Axes.Left.Automatic = true;
tChart1.Axes.Left.Logarithmic = true;
tChart1.Axes.Left.LogarithmicBase = Math.E;
tChart1.Axes.Left.Labels.Exponent = true; 
tChart1.Axes.Left.Labels.ValueFormat = "00-e0";
on a dataset containg values ranging from yMin = 8.0 to yMax = 15.0, I get three y-axis labels (from top to bottom): 20^0, 74^-1 and 27^-1, which I find odd. Can you make any suggestions on how to correct the code in order to get the desired e^0, e^1 and e^2 labels?

Best regards
Michael Schøler
External Consultant
www.vestas.com

Posted: Wed Jun 06, 2007 2:34 pm
by narcis
Hello Michael,

Could you please send us a simple example project we can run "as-is" to reproduce the problem here?

You can either post your files at news://www.steema.net/steema.public.attachments newsgroup or at our upload page.

Thanks in advance.