【问题标题】:Xaml, System.Windows.Controls.DataVisualization.Charting - Display X-Axis label as seconds instead of Time?Xaml,System.Windows.Controls.DataVisualization.Charting - 将 X 轴标签显示为秒而不是时间?
【发布时间】: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"]);

提前致谢!

【问题讨论】:

  • 你能分享代码来构建数据吗?
  • 感谢您的帮助。我添加了一个人为的代码版本。

标签: c# wpf xaml charts


【解决方案1】:

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

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

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

【讨论】:

  • 啊,哈!使用 double 和 Timespan.TotalSeconds 的数据类型是解决方案! tX.Add(new Tuple&lt;double, double&gt;(ts.TotalSeconds, dx));
猜你喜欢
  • 2023-03-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-06-19
  • 1970-01-01
  • 2017-12-02
  • 1970-01-01
相关资源
最近更新 更多