【问题标题】:How to create datime chart如何创建日期时间图表
【发布时间】:2018-11-11 20:15:07
【问题描述】:

我有一个来自 SQL Server 的数据表,其中包含日期时间字段 ScannedDateTime 我正在尝试在 VB NET 中使用 2 线型系列制作图表:

X - 应该是来自字段 ScannedDateTime 的日期

Y - 应该是来自字段 ScannedDateTime 的时间

我不知道该怎么做?将时间转换为十进制数字并将其显示为 y 轴或什么?

我想将 y 保留为“hh:mm”的时间格式,并将最小值限制为工作时间示例:从 07:00 到 15:00

开发快递?

我试过了,为什么时间重复???

谢谢你的想法

【问题讨论】:

    标签: datetime charts .net-core


    【解决方案1】:

    您似乎需要使用 TimeSpan 比例显示每个数据源记录的时间值。虽然 DevExpress ChartControl 不支持 TimeSpan 刻度类型,但您可以使用以下方法生成所需的图表布局。

    首先,定义一个额外的数字字段,该字段将用作 ValueDataMember 并包含与 TimeSpan 值对应的刻度数。

    要显示 TimeSpan 标签而不是数值,请填充 自定义轴标签 元素的集合。

    最后,通过处理 CustomDrawCrosshair 事件自定义十字准线光标标签文本。

    请参阅说明此方法的示例源代码:

     public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            List<MyData> inputData = new List<MyData>();
            inputData.Add(new MyData() { ID = 1, ScannedDateTime = new DateTime(2019, 4, 1, 11, 22, 33) });
            inputData.Add(new MyData() { ID = 2, ScannedDateTime = new DateTime(2019, 4, 2, 15, 00, 12) });
            inputData.Add(new MyData() { ID = 3, ScannedDateTime = new DateTime(2019, 4, 3, 09, 11, 03) });
            Series lineSeries = new Series("Scanned Date", ViewType.Line);
            lineSeries.ArgumentDataMember = "ScannedDateTime";
            lineSeries.ValueScaleType = ScaleType.Numerical;
            lineSeries.ValueDataMembers.AddRange(new string[] { "TimeTicks" });
            lineSeries.DataSource = inputData;
            chartControl1.Series.Add(lineSeries);
            XYDiagram diagram = (XYDiagram)chartControl1.Diagram;
            diagram.AxisX.DateTimeScaleOptions.MeasureUnit = DateTimeMeasureUnit.Day;
            for (int i = 0; i < 24; i++)
            {
                TimeSpan ts = new TimeSpan(i, 0, 0); //display tickmarks for every hour
                diagram.AxisY.CustomLabels.Add(new CustomAxisLabel() { AxisValue = ts.Ticks, Name = ts.ToString() });
            }
            chartControl1.CustomDrawCrosshair += ChartControl1_CustomDrawCrosshair; // optional - if you need to show TimeSpan values in the Crosshair Cursor
        }
    
        private void ChartControl1_CustomDrawCrosshair(object sender, CustomDrawCrosshairEventArgs e)
        {
           foreach(CrosshairElementGroup g in e.CrosshairElementGroups)
            {
                foreach(CrosshairElement el in g.CrosshairElements)
                {
                    TimeSpan ts = TimeSpan.FromTicks((long)el.SeriesPoint.Values[0]);
                    el.LabelElement.Text = string.Format("{0} - {1}", el.SeriesPoint.Argument, ts);
                }
            }
        }
    }
    
    public class MyData
    {
        public int ID { get; set; }
        public DateTime ScannedDateTime { get; set; }
    
        public double TimeTicks
        {
            get
            {
                return ScannedDateTime.TimeOfDay.Ticks;
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2017-11-16
      • 1970-01-01
      • 2011-11-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多