Log axes - more tick marks?

A forum dedicated to WinForms version of LightningChart Ultimate.

Moderator: Arction_LasseP

greggorob64
Posts: 183
Joined: Tue Mar 18, 2014 2:55 pm

Log axes - more tick marks?

Post by greggorob64 » Wed Jul 15, 2020 5:56 pm

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:

Image

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

Arction_LasseP
Posts: 94
Joined: Wed Mar 27, 2019 1:05 pm

Re: Log axes - more tick marks?

Post by Arction_LasseP » Thu Jul 16, 2020 12:22 pm

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.

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));
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.

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();
        }
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

greggorob64
Posts: 183
Joined: Tue Mar 18, 2014 2:55 pm

Re: Log axes - more tick marks?

Post by greggorob64 » Fri Jul 24, 2020 1:40 pm

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