简体   繁体   English

C#图表类-x轴的自定义标签?

[英]C# chart class — custom labels for x axis?

I use C# to communicate with an MSSQL database using EntityFramework. 我使用C#通过EntityFramework与MSSQL数据库进行通信。 The two columns I would like to graph from a table in a chart are in the following format: the first column is a string (that's how it was supplied to me, I know it probably isn't the best design option) in format "MM/dd/yyyy HH:mm:ss.fff" (where fff stands for milliseconds), and the second table is a simple float value. 我想从图表中的表格中绘制出的两列的格式如下:第一列是一个字符串 (这是它提供给我的方式,我知道它可能不是最佳的设计选择),格式为“ MM / dd / yyyy HH:mm:ss.fff“(其中fff代表毫秒),第二个表是简单的float值。

I want the x axis to represent time, and I want y axis to represent the float values. 我希望x轴代表时间,我希望y轴代表浮点值。 If I just feed the strings as they are to the chart class, the points are equally spaced horizontally. 如果我只是按原样将字符串输入图表类,则这些点在水平方向上等距分布。 The data I am trying to visualize does not come in regular time intervals, so this is not the desired behavior. 我尝试可视化的数据不是按固定的时间间隔出现的,因此这不是理想的行为。

What I have tried doing is converting the date strings to ints, and each int represents how many milliseconds have passed since the earliest point I am graphing. 我尝试做的是将日期字符串转换为int,每个int表示自从我绘制图表以来的最早点以来经过了多少毫秒。 This shows up well in the chart, but I do not want the points on the x axis to be labelled with the newly calculated ints. 这在图表中很好地显示,但是我不希望x轴上的点用新计算的整数标记。 I want the x axis to show the date as it was originally, but I want to keep the relative horizontal order as it should be, when the values are converted to ints. 我希望x轴显示原始日期,但是当值转换为整数时,我想保持相对水平顺序不变。 I hope I am clear in my question. 我希望我的问题很清楚。 So for an example, if I want to visualize the following three points (01/01/2014 00:00:00.000, y1), (01/01/2014 00:00:00.500, y2) and (01/01/2014 00:00:02.000, y3), how can I make sure the horizontal distance between the second and the third is three time larger compared to the horizontal difference between the first and the second point, and yet keep the original date labels on the x axis? 因此,例如,如果我想可视化以下三个点(01/01/2014 00:00:00.000,y1),(01/01/2014 00:00:00.500,y2)和(01/01/2014 00:00:02.000,y3),如何确保第二个点和第三个点之间的水平距离是第一个点和第二个点之间的水平差的三倍,并且仍将原始日期标签保留在x上轴?

Any ideas how I can do this? 有什么想法可以做到吗? Is there a way to specify a function that would for each int return a label string for the x axis? 有没有一种方法可以指定一个函数,该函数将为每个int返回x轴的标签字符串?

Thanks. 谢谢。

If you're adding points one by one - eg, 如果您要一一加分-例如,

chart1.Series[0].Points.AddXY(dateString, yValue);

you can convert the string objects to DateTime objects, which the chart control can easily display as you would like. 您可以将string对象转换为DateTime对象,图表控件可以根据需要轻松显示这些对象。 Just convert dateString before you call AddXY . 只需在调用AddXY之前转换dateString AddXY

If you're data binding the resulting table from the database directly to the chart, this may not work. 如果您要将数据表从数据库直接绑定到图表,则可能无法正常工作。 You can try setting the Format property of the axis: 您可以尝试设置轴的Format属性:

chart1.ChartAreas[0].AxisX.LabelStyle.Format = "MM/dd/yyyy hh:mm:ss.fff";

or something similar. 或类似的东西。 Not sure if this will work properly though. 不知道这是否可以正常工作。

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

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