This project is read-only.

Adjust Spacing of Tick Marks

Apr 13, 2010 at 1:03 AM

Is there a way to adjust the spacing of the tick marks? I have a date time axis on the horizontal axis of the plot and it only displays a tick mark label every 12 hours. I want to adjust it so that it displays a tick mark label every 1 hour. I couldn't find a sample of how to do this in the sample code.

Thanks

Apr 14, 2010 at 8:44 PM

Let me ask a different question. Is there a way to force the graph to recalculate where it puts the major tick marks?

It seems like the graph calculates the spacing between the tick marks correctly, but if you zoom in and out alot it can get in a mode where it is only showing one major tick mark on the graph. Then if you double click on the graph it zooms in a little bit and recalculates the amount of major tick marks show on the graph. I want to know how to force it to recalculate the major tick marks.

Thanks

 

Apr 23, 2010 at 10:36 AM

Hi,

Override ModifyTicks method and

/// <summary>
    /// This class will provide custom ticks for x axis.
    /// </summary>
    public class ChartCustomTicksProvider : DateTimeTicksProvider
    {
        /// <summary>
        /// Modifies the ticks.
        /// </summary>
        /// <param name="ticks">The ticks list.</param>
        /// <param name="info">The difference in info.</param>
        /// <returns>The calculated ticks</returns>
        protected override DateTime[] ModifyTicks(DateTime[] ticks, object info)
        {
            if (ticks[0].Ticks > 0)
            {
                if (info.Equals(DifferenceIn.Minute))
                {
                    var largerResult = SetMinutesTicks(ticks);
                    return largerResult;
                }
                else if (info.Equals(DifferenceIn.Hour))
                {
                    var largerResult = SetHoursTicks(ticks);
                    return largerResult;
                }
                else if (info.Equals(DifferenceIn.Day))
                {
                    var largerResult = SetDaysTicks(ticks);
                    return largerResult;
                }
                else if (info.Equals(DifferenceIn.Month))
                {
                    var largerResult = SetMonthTicks(ticks);
                    return largerResult;
                }
            }

            return base.ModifyTicks(ticks, info);
        }

        /// <summary>
        /// Sets the month ticks.
        /// </summary>
        /// <param name="ticks">The ticks.</param>
        /// <returns>The calculated month ticks</returns>
        private static DateTime[] SetMonthTicks(DateTime[] ticks)
        {
            TimeSpan ts = ticks[ticks.ToList().Count - 1].Subtract(ticks[0]);
            int noOfTicks = 0;
            int factor = 1;
            if (ts.TotalDays > 30 && ts.TotalDays <= 60)
            {
                factor = 5;
                noOfTicks = (int)ts.Days / factor;
            }
            else if (ts.TotalDays > 60 && ts.TotalDays <= 120)
            {
                factor = 15;
                noOfTicks = (int)ts.Days / factor;
            }
            else if (ts.TotalDays > 120 && ts.TotalDays <= 240)
            {
                factor = 30;
                noOfTicks = (int)ts.Days / factor;
            }
            else if (ts.TotalDays > 240 && ts.TotalDays <= 360)
            {
                factor = 60;
                noOfTicks = (int)ts.Days / factor;
            }
            else if (ts.TotalDays > 360)
            {
                factor = 120;
                noOfTicks = (int)ts.Days / factor;
            }

            var largerResult = new DateTime[noOfTicks + 2];
            largerResult[0] = ticks[0];

            for (int l = 1; l < largerResult.ToList().Count; l++)
            {
                largerResult[l] = largerResult[l - 1].AddDays(factor);
            }

            return largerResult;
        }

        /// <summary>
        /// Sets the days ticks.
        /// </summary>
        /// <param name="ticks">The ticks.</param>
        /// <returns>The calculated day ticks </returns>
        private static DateTime[] SetDaysTicks(DateTime[] ticks)
        {
            TimeSpan ts = ticks[ticks.ToList().Count - 1].Subtract(ticks[0]);
            int noOfTicks = 0;
            int factor = 1;
            bool minFactor = false;
            bool daysFactor = false;
            if (ts.TotalMinutes > 1440 && ts.TotalMinutes <= 2880)
            {
                factor = 240;
                noOfTicks = (int)ts.TotalMinutes / factor;
                minFactor = true;
                daysFactor = false;
            }
            else if (ts.TotalMinutes > 2880 && ts.TotalMinutes <= 7200)
            {
                factor = 720;
                noOfTicks = (int)ts.TotalMinutes / factor;
                minFactor = true;
                daysFactor = false;
            }
            else if (ts.TotalDays > 5 && ts.TotalDays <= 14)
            {
                factor = 1;
                noOfTicks = (int)ts.TotalDays / factor;
                minFactor = false;
                daysFactor = true;
            }
            else if (ts.TotalDays > 14 && ts.TotalDays <= 30)
            {
                factor = 2;
                noOfTicks = (int)ts.TotalDays / factor;
                minFactor = false;
                daysFactor = true;
            }
            else if (ts.TotalDays > 30)
            {
                factor = 2;
                noOfTicks = (int)ts.TotalDays / factor;
                minFactor = false;
                daysFactor = true;
            }

            var largerResult = new DateTime[noOfTicks + 2];
            largerResult[0] = ticks[0];
            if (minFactor && !daysFactor)
            {
                for (int l = 1; l < largerResult.ToList().Count; l++)
                {
                    largerResult[l] = largerResult[l - 1].AddMinutes(factor);
                }
            }
            else if (!minFactor && daysFactor)
            {
                for (int l = 1; l < largerResult.ToList().Count; l++)
                {
                    largerResult[l] = largerResult[l - 1].AddDays(factor);
                }
            }

            return largerResult;
        }

        /// <summary>
        /// Sets the hours ticks.
        /// </summary>
        /// <param name="ticks">The ticks.</param>
        /// <returns>The calculated hour ticks</returns>
        private static DateTime[] SetHoursTicks(DateTime[] ticks)
        {
            TimeSpan ts = ticks[ticks.ToList().Count - 1].Subtract(ticks[0]);
            int noOfTicks = 0;
            int factor = 1;
            if (ts.TotalMinutes > 60 && ts.TotalMinutes <= 90)
            {
                factor = 10;
                noOfTicks = (int)ts.TotalMinutes / factor;
            }
            else if (ts.TotalMinutes > 90 && ts.TotalMinutes <= 120)
            {
                factor = 15;
                noOfTicks = (int)ts.TotalMinutes / factor;
            }
            else if (ts.TotalMinutes > 120 && ts.TotalMinutes <= 200)
            {
                factor = 20;
                noOfTicks = (int)ts.TotalMinutes / factor;
            }
            else if (ts.TotalMinutes > 200 && ts.TotalMinutes <= 300)
            {
                factor = 30;
                noOfTicks = (int)ts.TotalMinutes / factor;
            }
            else if (ts.TotalMinutes > 300 && ts.TotalMinutes <= 540)
            {
                factor = 60;
                noOfTicks = (int)ts.TotalMinutes / factor;
            }
            else if (ts.TotalMinutes > 540 && ts.TotalMinutes <= 720)
            {
                factor = 90;
                noOfTicks = (int)ts.TotalMinutes / factor;
            }
            else if (ts.TotalMinutes > 720 && ts.TotalMinutes <= 960)
            {
                factor = 120;
                noOfTicks = (int)ts.TotalMinutes / factor;
            }
            else if (ts.TotalMinutes > 960 && ts.TotalMinutes <= 1440)
            {
                factor = 180;
                noOfTicks = (int)ts.TotalMinutes / factor;
            }
            else if (ts.TotalMinutes > 1440)
            {
                factor = 240;
                noOfTicks = (int)ts.TotalMinutes / factor;
            }

            var largerResult = new DateTime[noOfTicks + 2];
            largerResult[0] = ticks[0];

            for (int l = 1; l < largerResult.ToList().Count; l++)
            {
                largerResult[l] = largerResult[l - 1].AddMinutes(factor);
            }

            return largerResult;
        }

        /// <summary>
        /// Sets the minutes ticks.
        /// </summary>
        /// <param name="ticks">The ticks.</param>
        /// <returns>The calculated minutes ticks</returns>
        private static DateTime[] SetMinutesTicks(DateTime[] ticks)
        {
            TimeSpan ts = ticks[ticks.ToList().Count - 1].Subtract(ticks[0]);
            int noOfTicks = 0;
            int factor = 1;
            bool secondsFactor = false;
            if (ts.TotalMinutes <= 3)
            {
                secondsFactor = true;
                factor = 30;
                noOfTicks = (int)ts.TotalSeconds / factor;
            }
            else if (ts.TotalMinutes > 3 && ts.TotalMinutes <= 6)
            {
                factor = 1;
                noOfTicks = (int)ts.TotalMinutes / factor;
                secondsFactor = false;
            }
            else if (ts.TotalMinutes > 6 && ts.TotalMinutes <= 12)
            {
                factor = 2;
                noOfTicks = (int)ts.TotalMinutes / factor;
                secondsFactor = false;
            }
            else if (ts.TotalMinutes > 12 && ts.TotalMinutes <= 24)
            {
                factor = 3;
                noOfTicks = (int)ts.TotalMinutes / factor;
                secondsFactor = false;
            }
            else if (ts.TotalMinutes > 24 && ts.TotalMinutes <= 45)
            {
                factor = 5;
                noOfTicks = (int)ts.TotalMinutes / factor;
                secondsFactor = false;
            }
            else if (ts.TotalMinutes > 45 && ts.TotalMinutes < 60)
            {
                factor = 6;
                noOfTicks = (int)ts.TotalMinutes / factor;
                secondsFactor = false;
            }
            else if (ts.TotalMinutes >= 60)
            {
                factor = 10;
                noOfTicks = (int)ts.TotalMinutes / factor;
                secondsFactor = false;
            }

            var largerResult = new DateTime[noOfTicks + 2];
            largerResult[0] = ticks[0];

            if (secondsFactor)
            {
                for (int l = 1; l < largerResult.ToList().Count; l++)
                {
                    largerResult[l] = largerResult[l - 1].AddSeconds(factor);
                }
            }
            else
            {
                for (int l = 1; l < largerResult.ToList().Count; l++)
                {
                    largerResult[l] = largerResult[l - 1].AddMinutes(factor);
                }
            }

            return largerResult;
        }
    }
And in XAML 
<c:ChartPlotter>
   <c:ChartPlotter.MainHorizontalAxis>
        <c:HorizontalDateTimeAxis x:Name="dateAxis">
              <c:HorizontalDateTimeAxis.TicksProvider>
                     <chart:ChartCustomTicksProvider/>
              </c:HorizontalDateTimeAxis.TicksProvider>
        </c:HorizontalDateTimeAxis>
   </c:ChartPlotter.MainHorizontalAxis>
</c:ChartPlotter>
This will help
refer DevSamples\AxesApp
Regards
Ravi.Battula
Feb 15, 2011 at 10:09 PM

Ravi,

 

In this ModifyTicks function, base.ModifyTicks(ticks, info); is not found in base class.

Feb 16, 2011 at 2:22 AM

Hi,

Which version are you using.

I used some old version. Check  "DateTimeTicksProvider" class name properly. They might have changes the name.

Regards

Ravi.

Jul 9, 2012 at 5:30 PM

Hi Ravi,

It looks like ModifyTicks() no longer exists in DateTimeTicksProvider and I cannot find a similar method with a different name. Any update on this thread?

Thanks,

Evan