简体   繁体   English

asp.net图表X轴上的力标签均匀交错

[英]asp.net Chart Force Labels On X-Axis Staggered Evenly

I'm creating a C# WinForms app that displays nearly real-time, time-based (~5s refresh rate) data from a SQL server using the WinForms Line Chart control. 我正在创建一个C#WinForms应用程序,该应用程序使用WinForms折线图控件显示来自SQL Server的几乎实时的,基于时间的(〜5s刷新率)数据。 The fetching and inserting of data on the chart works just fine, it is just the formatting I'm having issues with. 图表上数据的获取和插入工作正常,这只是我遇到问题的格式。 The X-Axis contains the DateTime of each point and is set to display the date on the X-Axis. X轴包含每个点的DateTime,并设置为在X轴上显示日期。

The problem I'm encountering is the X-Axis labels not being displayed evenly across the chart. 我遇到的问题是X轴标签未在图表中均匀显示。 I'm able to get the number of axis labels that I want, but they are not consistently in the same position on the axis. 我可以获取所需的轴标签数量,但是它们在轴上的位置不一致。

Ideally I'd like to be able to set the number of x-axis labels, have a label at each ends of the chart, then the remaining labels evenly split across the rest of the chart. 理想情况下,我希望能够设置x轴标签的数量,在图表的两端都有一个标签,然后将其余标签平均分配到图表的其余部分。 Then every time new data is added to the chart the labels stay in the exact same spot, just the label text is updated. 然后,每当将新数据添加到图表时,标签就会停留在完全相同的位置,只是标签文本会更新。

Here are two pictures that should make it clear what I'm after. 这是两张图片,应该清楚说明我的追求。

The first picture is data from the range 16:10:00 to 16:11:00. 第一张图片是从16:10:00到16:11:00范围内的数据。

This is what I would like for any start/end date: 这是我想要的任何开始/结束日期:

The second picture shows what happens when I add 5 seconds of new data and remove 5 seconds of old data. 第二张图片显示了当我添加5秒钟的新数据并删除5秒钟的旧数据时发生的情况。

Chart after refreshing with new data: 用新数据刷新后的图表:

Here is the snippet of code that I use for my x-axis formatting. 这是我用于x轴格式化的代码段。

            chart.ChartAreas[0].AxisX.IntervalType = DateTimeIntervalType.Seconds;              
            chart.ChartAreas[0].AxisX.Interval = timeDiff.TotalSeconds / numXTicks;
            chart.ChartAreas[0].AxisX.Minimum = timeMin.ToOADate();
            chart.ChartAreas[0].AxisX.Maximum = timeMax.ToOADate();
            chart.ChartAreas[0].AxisX.LabelStyle.Format = "yyyy-MM-dd" + Environment.NewLine + "HH:mm:ss";
            chart.ChartAreas[0].AxisX.LabelStyle.IsEndLabelVisible = true;
            chart.ChartAreas[0].AxisX.IsMarginVisible = true;

Thanks for any help. 谢谢你的帮助。 Long time lurker and first time poster. 长期潜伏和第一次海报。

For anyone that may be curious I found a reasonable workaround by setting the IntervalType to MilliSeconds. 对于任何可能感到好奇的人,我都将IntervalType设置为MilliSeconds,从而找到了一种合理的解决方法。

            chart.ChartAreas[0].AxisX.IntervalType = DateTimeIntervalType.Milliseconds;
            chart.ChartAreas[0].AxisX.Interval = unixDiff.TotalMilliseconds / numXTicks;

While this doesn't necessarily fix the x-axis labels in place, by having an Interval will multiply evenly across the chart and not result in a fraction of an interval the labels will be neatly labeled across the entire chart. 尽管这不一定会将x轴标签固定在适当的位置,但通过使Interval可以在图表上均匀地增加,并且不会导致间隔的一小部分,标签将在整个图表上被整齐地标记。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM