【发布时间】:2020-01-08 19:34:50
【问题描述】:
我们以 50 Hz 到 800 Hz 的可变采样频率从陀螺仪收集数据。 Y 轴值是双精度值,X 轴值是从 OADate 转换而来的 DateTime 值。目前,图表在 X 轴上显示时间戳(例如上午 8:00:32、上午 8:00:33)。我还尝试在TimeSpan 中累积句点,但图表会为每个样本(其中有数千个)创建一个标签,这会导致 X 轴标签不可读。我希望 X 轴以秒为单位显示经过的持续时间而不是时间(例如 0、1、2、3 ...)。我如何以编程方式做到这一点?
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"]);
提前致谢!
【问题讨论】:
-
你能分享代码来构建数据吗?
-
感谢您的帮助。我添加了一个人为的代码版本。