Xaml,System.Windows.Controls.DataVisualization.Charting-将X轴标签显示为秒而不是时间?

时间:2019-09-05 21:37:21

标签: c# wpf xaml charts

我们以50 Hz至800 Hz的可变采样频率从陀螺仪收集数据。 Y轴值是双精度值,X轴值是从OADate转换的DateTime值。目前,图表在X轴上显示时间戳(例如8:00:32 am,8:00:33 am)。我也尝试过在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"]);

enter image description here

谢谢!

1 个答案:

答案 0 :(得分:1)

累积TimeSpan值是正确的,但是您可以使用TimeSpan的Seconds值代替使用本身的TimeSpan / DateTime值。并将您的ItemSource更改为

List<Tuple<int, double>> tX = new List<Tuple<int, double>>();

如果要以秒而不是时间显示经过的持续时间(例如0、1、2、3 ...),则在数据构建期间,您应该记住第一个DateTime值(0点),而不是从下一个样本的DateTime中减去它,并使用Seconds的{​​{1}}或TotalSeconds(如果有数千个样本)作为经过的秒值