Hello,
I'm working on implementing the ability to view logarithmic X axes in my app. It works as expected mostly, except for one scenario:
When you dont have a ton of data, the axis doesn't show much info. for instance, given the x values 177-8000, with 18 points, the log axis looks like this:
Is there any way for me to force than one label? I understand that it only wants to label 1,10,100,1000, but i'd sometimes like to add some more in the case where there'd only be 1 label.
Thanks!
Grandmaster Greg
Log axes - more tick marks?
Moderator: Queue Moderators
-
- Posts: 141
- Joined: Wed Mar 27, 2019 1:05 pm
Re: Log axes - more tick marks?
Hello
When ScaleType is set to Logarithmic, the axis labels are automatically calculated based on LogBase setting of the axis. LogBase = 10 results in 1, 10, 100, 1000 etc. Currently this behaviour cannot be disabled.
If you want to show more ticks, you can use CustomTicks. These allow you to manually set any number of ticks at any positions. Enabling CustomTicks hides the original ticks.
Adding CustomTicks doesn't automatically show them. You have to set CustomTicksEnabled to true for the axis. Disabling AutoFormatLabels is also a good idea. In your case, you could check inside RangeChanged event how many original labels are visible. If only one or none, then enable CustomTicks.
GetMajorTicks only returns the number of original ticks while ignoring CustomTicks.
Note that we use AfterRendering event above, because RangeChanged is actually triggered when the axis is created. Since the axis or the chart itself might not be rendered yet at this point, a crash could occur.
Hope this is helpful.
Best regards,
Lasse
When ScaleType is set to Logarithmic, the axis labels are automatically calculated based on LogBase setting of the axis. LogBase = 10 results in 1, 10, 100, 1000 etc. Currently this behaviour cannot be disabled.
If you want to show more ticks, you can use CustomTicks. These allow you to manually set any number of ticks at any positions. Enabling CustomTicks hides the original ticks.
Code: Select all
// Adding CustomTicks
axisX.CustomTicks.Add(new CustomAxisTick(axisX, 800, "800", 10, true, Color.White, CustomTickStyle.TickAndGrid));
axisX.CustomTicks.Add(new CustomAxisTick(axisX, 900, "900", 10, true, Color.White, CustomTickStyle.TickAndGrid));
axisX.CustomTicks.Add(new CustomAxisTick(axisX, 1000, "1000", 10, true, Color.White, CustomTickStyle.TickAndGrid));
axisX.CustomTicks.Add(new CustomAxisTick(axisX, 1100, "1100", 10, true, Color.White, CustomTickStyle.TickAndGrid));
axisX.CustomTicks.Add(new CustomAxisTick(axisX, 1200, "1200", 10, true, Color.White, CustomTickStyle.TickAndGrid));
Code: Select all
_chart.AfterRendering += _chart_AfterRendering;
private void _chart_AfterRendering(object sender, AfterRenderingEventArgs e)
{
_chart.AfterRendering -= _chart_AfterRendering;
_chart.ViewXY.XAxes[0].RangeChanged += AxisX_RangeChanged;
}
private void AxisX_RangeChanged(object sender, RangeChangedEventArgs e)
{
double ticks = _chart.ViewXY.XAxes[0].GetMajorTicks().Length;
_chart.BeginUpdate();
if (ticks < 2)
{
_chart.ViewXY.XAxes[0].AutoFormatLabels = false;
_chart.ViewXY.XAxes[0].CustomTicksEnabled = true;
}
else if (_chart.ViewXY.XAxes[0].CustomTicksEnabled)
{
_chart.ViewXY.XAxes[0].AutoFormatLabels = true;
_chart.ViewXY.XAxes[0].CustomTicksEnabled = false;
}
_chart.EndUpdate();
}
Note that we use AfterRendering event above, because RangeChanged is actually triggered when the axis is created. Since the axis or the chart itself might not be rendered yet at this point, a crash could occur.
Hope this is helpful.
Best regards,
Lasse
-
- Posts: 183
- Joined: Tue Mar 18, 2014 2:55 pm
Re: Log axes - more tick marks?
Thanks. I'll use this method. Because the X axis could be any valueset, it might be tricky to figure out the logical places to put custom tick marks, but that's my issue
Appreciate the help
Appreciate the help