[英]Xaml, System.Windows.Controls.DataVisualization.Charting - Display X-Axis label as seconds instead of Time?
We collect data from a gyroscope at a variable sampling frequency from 50 Hz to 800 Hz. 我们以50 Hz至800 Hz的可变采样频率从陀螺仪收集数据。 The Y-Axis values are doubles, the X-Axis values are
DateTime
values converted from OADate. Y轴值是双精度值,X轴值是从OADate转换的
DateTime
值。 Currently the chart displays a timestamp (eg 8:00:32 am, 8:00:33 am) on the X-Axis. 当前,图表在X轴上显示时间戳(例如8:00:32 am,8:00:33 am)。 I've also tried accumulating the periods in a
TimeSpan
, but the chart creates a label for each sample (of which there are thousands) which causes the X-Axis labels to be unreadable. 我还尝试在
TimeSpan
累积时间段,但是图表为每个样本(其中有数千个)创建了一个标签,这导致X轴标签不可读。 I would like the X-Axis to display the elapsed duration in seconds instead of a time (eg 0,1,2,3...). 我希望X轴以秒为单位显示经过的持续时间,而不是时间(例如0、1、2、3 ...)。 How do I do that programmatically?
我该如何以编程方式做到这一点?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.DataVisualization.Charting;
using System.Windows.Media;
using System.Windows.Navigation;
using MathNet.Numerics.Data.Text;
using MathNet.Numerics.LinearAlgebra;
public class BlahBlah
{
private const int REC_NUM = 0;
private const int DATE = 1;
private const int X_FIELD = 2;
private void CreateData(Dictionary<string, Series> dictionary, IEnumerable<Vector<double>> record)
{
dictionary.Clear();
List<Tuple<DateTime, double>> tX = new List<Tuple<DateTime, double>>();
//Take the first record and perfrom some initialization...
var firstRecord = record.First();
long count = 0;
DateTime last = DateTime.FromOADate(firstRecord[DATE]);
DateTime current;
TimeSpan ts = new TimeSpan(0);
//Now loop through all the records
foreach (var v in record)
{
double dx = 0;
dx = v[X_FIELD]);
current = DateTime.FromOADate(v[DATE]);
ts = ts + current.Subtract(last);
//My attempt at using timespan
//tX.Add(new Tuple<TimeSpan, double>(ts, lx));
tX.Add(new Tuple<DateTime, double>(DateTime.FromOADate(v[DATE]), dx));
last = current;
count++;
}
Color cs = (Color)ColorConverter.ConvertFromString("#4FD7DA");
Style style = new Style();
style = new Style(typeof(Control));
style.Setters.Add(new Setter(Control.BackgroundProperty, new SolidColorBrush(cs)));
//Turn off datapoints by setting the style to null
style.Setters.Add(new Setter(LineDataPoint.TemplateProperty, null));
dictionary.Add("x", new LineSeries() {
Name = "x",
ItemsSource = tX,
IndependentValueBinding = new System.Windows.Data.Binding("Item1"),
DependentValueBinding = new System.Windows.Data.Binding("Item2"),
Title = "X Axis",
DataPointStyle = style
});
}
}
...
chart1.Series.Add(dictionary["x"]);
Thanks in advance! 提前致谢!
You are right with accumulating the TimeSpan
values, but instead of using TimeSpan/DateTime values itself you can use Seconds value of TimeSpan. 累积
TimeSpan
值是正确的,但是您可以使用TimeSpan的Seconds值来代替本身使用TimeSpan / DateTime值。 And change your ItemSource
to 并将您的
ItemSource
更改为
List<Tuple<int, double>> tX = new List<Tuple<int, double>>();
If you want to display the elapsed duration in seconds instead of a time (eg 0,1,2,3...), during the build of data you should remember the first DateTime
value (0 point), than subtract it from the DateTime
of next sample and use the Seconds
or TotalSeconds
(if there are a thousands samples) of resulting TimeSpan
as elapsed seconds value 如果要以秒为单位显示经过的持续时间而不是时间(例如0、1、2、3 ...),则在数据构建期间,您应该记住第一个
DateTime
值(0点),而不是从下一个样本的DateTime
并将生成的TimeSpan
的Seconds
或TotalSeconds
(如果有数千个样本)用作经过的秒数值
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.