Page 1 of 1

DateTime Axis Scroll

Posted: Wed Mar 15, 2017 2:22 am
by 17279216
hi,
i have a chart with a datetime x axis. I want to prevent scrolling beyond my start and end dates.
How can I do this?
I have tried this
tChart.Chart.Axes.Bottom.SetMinMax(startDate, endDate);

with no change.


Also, how I can scroll to a specific date ?
Tried this..
tChart.Axes.Bottom.Scroll(DateTime.Now.ToOADate(), false);

Re: DateTime Axis Scroll

Posted: Mon Mar 20, 2017 9:40 am
by Christopher
Hello,

Apologies for the delay in response to these questions.

You can modify default scroll behaviour using the ScrollMod event, e.g.

Code: Select all

    private void InitializeChart()
    {
      Line series = new Line(tChart1.Chart);

      DateTime today = DateTime.Today;
      Random rnd = new Random();

      for (int i = 0; i < 20; i++) 
      {
        series.Add(today, rnd.Next(500));

        today = today.AddDays(2);
      }

      tChart1.Axes.Bottom.Labels.Angle = 90;

      tChart1.ScrollMod += TChart1_ScrollMod;
    }

    private void TChart1_ScrollMod(Axis a, ScrollModEventArgs e)
    {
      double max = new DateTime(2017, 5, 1).ToOADate();

      if (a.Equals(tChart1.Axes.Bottom))
      {
        e.Max = max;    
      }
    }
To scroll to a specific date using a button, you could do:

Code: Select all

    private void button2_Click(object sender, EventArgs e)
    {
      tChart1.Axes.Bottom.SetMinMax(new DateTime(2017, 4, 22), new DateTime(2017, 5, 1));
    }

Re: DateTime Axis Scroll

Posted: Mon Jun 26, 2017 4:29 am
by 17279216
That is the only way to prevent scrolling past the enddate and startdate?
It doesn't work if using series.HorizAxis = HorizontalAxis.Both;

I have the date displaying on top axis with topAxis.Labels.DateTimeFormat = "d MMM";
and bottomAxis.Labels.DateTimeFormat = "hh:mm tt";
Any other suggestions?

Re: DateTime Axis Scroll

Posted: Mon Jun 26, 2017 10:21 am
by Christopher
Hello,

You can make a small modification to the previous code by checking for both axes in the event, e.g.

Code: Select all

    private void InitializeChart()
    {
      Line series = new Line(tChart1.Chart);

      DateTime today = DateTime.Today;
      Random rnd = new Random();

      for (int i = 0; i < 20; i++)
      {
        series.Add(today, rnd.Next(500));

        today = today.AddDays(2);
      }

      tChart1.Axes.Bottom.Labels.Angle = 90;

      series.HorizAxis = HorizontalAxis.Both;
      tChart1.Axes.Top.Labels.DateTimeFormat = "d MMM";
      tChart1.Axes.Bottom.Labels.DateTimeFormat = "hh:mm tt";

      tChart1.ScrollMod += TChart1_ScrollMod;
    }

    private void TChart1_ScrollMod(Axis a, ScrollModEventArgs e)
    {
      double max = new DateTime(2017, 8, 3).ToOADate();

      if (a.Equals(tChart1.Axes.Bottom) || a.Equals(tChart1.Axes.Top))
      {
        e.Max = max;
      }
    }