2016-10-02 79 views
1

我正在使用折線圖來顯示視頻上的運動檢測級別。將圖表控件的X軸設置爲10分鐘時間線

我需要讓X軸顯示時間軸。例如,如果媒體播放器控件返回600秒作爲視頻長度,則X軸應沿着按鈕顯示從00:00到10:00,總共可能有10個間隔。

到目前爲止,我有這樣的:

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

這並不多。我在網上搜索,但我發現的例子似乎是針對真實生活日期範圍,而不是一個簡單的0到10分鐘的範圍。

如何獲得x軸的簡單10分鐘時間線?

回答

2

如果你想在你的圖使用實際的日期時間值,這裏是如何設置它:

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

這裏是結果:

enter image description here

你可以代替保留一切在數字領域;但你不能格式化x軸標籤的時間,也不直接在計算時間值使用x值,這可能會或可能不會是一個問題:

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

+0

感謝您的時間寫這個。雖然我得到溢出錯誤。我應該說,圖中的X軸是逐幀生成的。因此,10分鐘視頻中大約有4000幀。所以我想這是我要去錯的地方。 – user2924019

+0

是的,可能。請注意,我如何通過將數據點添加到開始時間來創建數據點的時間值。如何添加數據點 – TaW

+0

我的代碼循環遍歷視頻的每個幀,併爲每個幀生成運動值。我簡單地用下面的代碼將它添加到圖表中:'chart1.Series [「Series1」] .Points.AddY(valueToAdd);' – user2924019

相關問題