HippoDuck
HippoDuck

Reputation: 2194

Set a chart control's X axis to a 10 minute timeline

I am using a line chart to display motion detection levels on a video.

I need to get the X Axis to display a timeline. For example, if the media player control returns 600 seconds as the length of video, then the X Axis should display from 00:00 to 10:00 along the buttom, with perhaps 10 intervals in total.

So far, I have got this:

chart1.Series[0].XValueType = ChartValueType.Time;

Which isn't much. I have searched online but the examples that I have found seem to be aimed at a real life date range,and not a simple, 0 to 10 minute range.

How can I get a simple 10 minute time line for the x axis?

Upvotes: 1

Views: 3635

Answers (1)

TaW
TaW

Reputation: 54433

If you want to use actual DateTime values in your Chart, here is how to set it up:

Series s = chart1.Series[0];
Axis ax = chart1.ChartAreas[0].AxisX;

s.ChartType = SeriesChartType.Line;
s.XValueType = ChartValueType.Time;

DateTime dt0 = DateTime.MinValue;  // any date will do, just know which you use!
ax.Minimum = dt0.AddSeconds(0).ToOADate();
ax.Maximum = dt0.AddSeconds(600).ToOADate();
ax.Interval = 10;
ax.IntervalType = DateTimeIntervalType.Seconds;
ax.MajorGrid.Interval = 15;
ax.MajorGrid.IntervalType = DateTimeIntervalType.Seconds;
ax.MinorGrid.Interval = 5;
ax.MinorGrid.IntervalType = DateTimeIntervalType.Seconds;
ax.LabelStyle.Interval = 60;
ax.LabelStyle.IntervalType = DateTimeIntervalType.Seconds;
ax.LabelStyle.Format = "mm:ss";

for (int i = 0; i < 10; i++)
{
    s.Points.AddXY(dt0.AddSeconds(rnd.Next(30) + 40 * i).ToOADate(), i);
}

here is the result:

enter image description here

You could instead keep everything in the number domain; but then you can't format the x-axis labels as times nor use the x-values directly in calculating time values, which may or may not be a problem:

s.XValueType = ChartValueType.Int32;

ax.Minimum = 0;
ax.Maximum = 600;
ax.Interval = 10;
ax.IntervalType = DateTimeIntervalType.Number;
ax.MajorGrid.Interval = 15;
ax.MajorGrid.IntervalType = DateTimeIntervalType.Number;
ax.MinorGrid.Interval = 5;
ax.MinorGrid.IntervalType = DateTimeIntervalType.Number;
ax.LabelStyle.Interval = 60;
ax.LabelStyle.IntervalType = DateTimeIntervalType.Number;

enter image description here

Upvotes: 3

Related Questions