Ticks must be between DateTime.MinValue.Ticks and DateTime.MaxValue.Ticks.

Apr 16, 2009 at 12:00 PM

Hi,

 

I am trying to plot value – date time plot.

 

If date time axis denomination is day then its working, If change date time axis denomination to minutes, It is giving “Ticks must be between DateTime.MinValue.Ticks and DateTime.MaxValue.Ticks.Parameter name: ticks” this exception. Here by I am pasting code

 

Working code

{

            private void InitTrend()

      {

            const int N = 100;

            double[] x = new double[N];

            double[] y = new double[N];

            DateTime[] date = new DateTime[N];

            Color[] colors = ColorHelper.CreateRandomColors(5);

           

            for (int i = 0; i < N; i++)

            {

                x[i] = i * 0.1;

                y[i] = Math.Sin(x[i]);

                date[i] = DateTime.Now.AddDays(-N + i);

            }

 

 

            TrendData = LoadTrendValues(date, y);

 

EnumerableDataSource<TrendValueInfo> dataSource = CreateTrendDataSource(TrendData);

           

if (currentgraph != null)

                plot.Children.Remove(currentgraph);

            else

    currentgraph = plot.AddLineGraph(dataSource, colors[2], 1, "Trend");

 

dateAxis.ConvertToDouble = dt => TimeSpan.FromTicks(dt.Ticks).TotalDays;

 

dateAxis.ConvertFromDouble = d => new DateTime(TimeSpan.FromDays(d).Ticks);

      }

 

private static List<TrendValueInfo> LoadTrendValues(DateTime[] trendtimes, double[] trendvalues)

      {

            var res = new List<TrendValueInfo>(trendvalues.Length - 1);

            for (int i = 0; i < trendvalues.Length; i++)

            {

                double value = trendvalues[i];

                DateTime date = trendtimes[i];

res.Add(new TrendValueInfo { Date = date, Value = value });

            }

 

            return res;

        }

 

private EnumerableDataSource<TrendValueInfo>   CreateTrendDataSource(List<TrendValueInfo> trenddata)

        {

EnumerableDataSource<TrendValueInfo> ds = new EnumerableDataSource<TrendValueInfo>(trenddata);

           

ds.SetXMapping(ci => TimeSpan.FromTicks(ci.Date.Ticks).Days);

           

      ds.SetYMapping(ci => ci.Value);

            return ds;

     }

}

 

I Replaced days with minutes, Then its not working, Is there any other place/ code do I need to modify. I need graph in minutes and hours.

 

Not working code

{

private void InitTrend()

      {

            const int N = 100;

            double[] x = new double[N];

            double[] y = new double[N];

            DateTime[] date = new DateTime[N];

            Color[] colors = ColorHelper.CreateRandomColors(5);

           

            for (int i = 0; i < N; i++)

            {

                x[i] = i * 0.1;

                y[i] = Math.Sin(x[i]);

                date[i] = DateTime.Now.AddMinutes(-N + i);

            }

 

 

            TrendData = LoadTrendValues(date, y);

 

EnumerableDataSource<TrendValueInfo> dataSource = CreateTrendDataSource(TrendData);

           

      if (currentgraph != null)

                plot.Children.Remove(currentgraph);

            else

    currentgraph = plot.AddLineGraph(dataSource, colors[2], 1, "Trend");

 

dateAxis.ConvertToDouble = dt => TimeSpan.FromTicks(dt.Ticks).TotalMinutes;

 

dateAxis.ConvertFromDouble = d => new DateTime(TimeSpan.FromMinutes(d).Ticks);

      }

 

private static List<TrendValueInfo> LoadTrendValues(DateTime[] trendtimes, double[] trendvalues)

      {

            var res = new List<TrendValueInfo>(trendvalues.Length - 1);

            for (int i = 0; i < trendvalues.Length; i++)

            {

                double value = trendvalues[i];

                DateTime date = trendtimes[i];

res.Add(new TrendValueInfo { Date = date, Value = value });

            }

 

            return res;

        }

         
        
private EnumerableDataSource<TrendValueInfo>   CreateTrendDataSource(List<TrendValueInfo> trenddata)

        {

EnumerableDataSource<TrendValueInfo> ds = new EnumerableDataSource<TrendValueInfo>(trenddata);

           

ds.SetXMapping(ci => TimeSpan.FromTicks(ci.Date.Ticks).Minutes);

           

      ds.SetYMapping(ci => ci.Value);

            return ds;

        }

 

}

 

Please guide me this is urgent. Bold words modified content.

Regards

Ravi.

 

Editor
Apr 16, 2009 at 1:09 PM
Hi Ravi,

This is code of the simplest way of creationg chart with DateTime values on x axis and double on y axis:

using System;

using System.Windows;

using Microsoft.Research.DynamicDataDisplay;

using Microsoft.Research.DynamicDataDisplay.Charts;

using Microsoft.Research.DynamicDataDisplay.DataSources;

 

namespace BattulaRepro

{

    /// <summary>

    /// Interaction logic for Window1.xaml

    /// </summary>

    public partial class Window1 : Window

    {

        private ChartPlotter plot = new ChartPlotter();

        private HorizontalDateTimeAxis axis = new HorizontalDateTimeAxis();

        public Window1()

        {

            InitializeComponent();

 

            Content = plot;

 

            plot.HorizontalAxis = axis;

 

            const int N = 10;

 

            double[] x = new double[N];

            double[] y = new double[N];

            DateTime[] date = new DateTime[N];

 

            for (int i = 0; i < N; i++)

            {

                x[i] = i * 0.1;

                y[i] = Math.Sin(x[i]);

                date[i] = DateTime.Now.AddMinutes(-N + i);

            }

 

            EnumerableDataSource<double> xs = new EnumerableDataSource<double>(x);

            xs.SetYMapping(_x => _x);

            EnumerableDataSource<DateTime> ys = new EnumerableDataSource<DateTime>(date);

            ys.SetXMapping(axis.ConvertToDouble);

 

            CompositeDataSource ds = new CompositeDataSource(xs, ys);

 

            plot.AddLineGraph(ds);

        }

    }

}

// ========================

As I have discovered, it is not necessary to set different y-mapping according to date or time period your data lays in - it is possible to set as y-mapping mapping, used by corresponding axis.

Hope this will help you.

Mikhail Brinchuk,

DynamicDataDisplay Dev Team.

Apr 16, 2009 at 1:43 PM
Yes,

Thank you very much, Its working.

Regards
Ravi.