Page 1 of 1

Y-Axis labels in logarithmic scale

Posted: Mon Jan 27, 2020 10:08 am
by Algernon
Hi,

I am making a chart with a logarithmic Y-axis, and I have an issue where numerical labels only appear on multiples of 10. If I zoom in between labels, the labels will eventually disappear entirely. Is there any way I can have new numerical labels automatically displayed as I zoom in while using ScaleType.Logarithmic?
Yaxis.png
Yaxis.png (5.58 KiB) Viewed 3729 times
Thank you,
Algernon

Re: Y-Axis labels in logarithmic scale

Posted: Tue Jan 28, 2020 7:59 am
by Arction_LasseP
Hello,

When using a logarithmic axis, the labels are automatically drawn in the positions of multiples of log base (controlled by yAxis.LogBase property), for example 1, 10, 100, 1000 etc with log base 10 or 2, 4, 8, 16, 32 etc with log base 2. The labels between these values are not drawn, and unfortunately this behaviour cannot currently be switched off while having logarithmic axis ScaleType.

It is still possible to show the labels when zooming close to the chart, if some kind of custom labels are used. For example, Annotations or SeriesEventMarkes (with transparent marker and visible label) could be positioned to the locations of the actual labels. The easiest way however is probably to use CustomTicks. When enabled, CustomTicks replace all the standard ticks of the axis. The downside is that you have to set each tick manually. For instance:

Code: Select all

CustomAxisTick[] cts = new CustomAxisTick[100];
for (int i = 0; i < 100; i++)
{
    CustomAxisTick cat = new CustomAxisTick(yAxis, i * 10, (i * 10).ToString(), 10, true, Colors.White, CustomTickStyle.TickAndGrid);
    cts[i] = cat;
}
yAxis.AutoFormatLabels = false;
yAxis.CustomTicks.AddRange(cts);
yAxis.CustomTicksEnabled = true;
yAxis.InvalidateCustomTicks();
The above always uses the CustomTicks for the Y-axis. However, if you want to show them only if you zoom close, you can use RangeChanged -event for the axis:

Code: Select all

yAxis.RangeChanged += YAxis_RangeChanged;

private void YAxis_RangeChanged(object sender, RangeChangedEventArgs e)
{
    if (e.NewMax - e.NewMin < 100)
        _chart.ViewXY.YAxes[0].CustomTicksEnabled = true;
    else
        _chart.ViewXY.YAxes[0].CustomTicksEnabled = false;
}
The above switches to CustomTicks only if the axis range is less than 100.

Hope this is helpful.
Best regards,
Lasse