简体   繁体   English

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

[英]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并将生成的TimeSpanSecondsTotalSeconds (如果有数千个样本)用作经过的秒数值

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 缺少 System.Windows.Controls.DataVisualization.Charting 程序集参考 - Missing System.Windows.Controls.DataVisualization.Charting assembly reference 获取图表上 LinearAxis 的自动间隔值 (System.Windows.Controls.DataVisualization.Charting) - Get the value of automatic interval for LinearAxis on Chart (System.Windows.Controls.DataVisualization.Charting) 用工作日标记 System.Windows.Forms.DataVisualization.Charting 图表中的 X 轴 - label the X axis in a System.Windows.Forms.DataVisualization.Charting chart with weekdays 无法获取标签和标题以显示在保存的图表中(C#.NET,System.Windows.Controls.DataVisualization.Charting) - Can't get labels and title to show up in saved chart (C#.NET, System.Windows.Controls.DataVisualization.Charting) 是否可以以十六进制显示DataVisualization.Charting.Chart的x轴值? - Is it possible to display the x-axis values of the DataVisualization.Charting.Chart in hexadecimal? 如何在X轴上为System.Windows.Forms.DataVisualization.Charting设置DateTime范围? - How do you set DateTime range on X axis for System.Windows.Forms.DataVisualization.Charting? 具有System.Windows.Forms.DataVisualization.Charting的互斥和从属轴 - Reciprocal & dependent axis with System.Windows.Forms.DataVisualization.Charting 有没有一种方法可以使用System.Web.UI.DataVisualization.Charting旋转烛台图表以水平显示,而不是垂直显示? - Is there a way using System.Web.UI.DataVisualization.Charting to rotate a Candlestick chart to display horizontally instead of vertically? 如何在System.Windows.Forms.DataVisualization.Charting中设置与主要Axis(AxisX)不同的次要Axis(AxisX2)的最小值和最大值 - How to Set Secondary Axis(AxisX2) Minimum and Maximum different from Primary Axis(AxisX) in System.Windows.Forms.DataVisualization.Charting 如何在c# windows forms中的图表中显示起点和终点x轴label? - How to display start and end points x-axis label in a chart in c# windows forms?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM