MS Chart 控制轴格式
我正在编写的 Winforms 应用程序中使用 MS Chart Control。我显示的散点图的 X 轴部分是 Int64 数据,它最终表示 UTC 时间。我想获取 Int64 数据,并在其上执行 DataTime.FromFileTimeUTC(theTime).ToString() 以显示有意义的最终用户 X 轴标签。
目前,我正在内存中的 DataTable 中创建另一列来保存与 Int64 等效的 DateTime,如下所示:
dataTable.Columns.Add("mytimestamp");
foreach (DataRow dr in dataTable.Rows)
{
dr["mytimestamp"] = DateTime.FromFileTimeUTC(Convert.ToInt64(dr["theint64val"].ToString()));
}
然后使用“mytimestamp”列作为 x 轴值。这工作正常,我可以将 x 轴标签显示为日期时间值。
但是,我不想经历创建列和实质上复制其他列数据的麻烦,但没有看到任何格式化 x 轴标签的方法。我想可能错过了这一点。我在文档中看到了 AxisViewChanged 事件,并了解了如何使用该数据设置图表标题,而不是 x 轴标签本身。
有什么想法吗?
I'm using the MS Chart Control in a Winforms app I'm writing. The X-axis component of the scatter plot I'm displaying is Int64 data, which ultimately represents a UTC time. I'd like to take that Int64 data and essentially do a DataTime.FromFileTimeUTC(theTime).ToString() on it to show the end-user X-axis labels that are meaningful.
Currently, I'm creating another column in the in-memory DataTable to hold the DateTime equivalent of that Int64 like so:
dataTable.Columns.Add("mytimestamp");
foreach (DataRow dr in dataTable.Rows)
{
dr["mytimestamp"] = DateTime.FromFileTimeUTC(Convert.ToInt64(dr["theint64val"].ToString()));
}
And then using the "mytimestamp" column as the x-axis value. This works fine and I can show the x-axis labels as datetime values.
But, I'd rather not go through the trouble of creating the column and essentially duplicating the other column's data but didn't see any way to format the x-axis labels. Might have missed this, I supposed. I saw the AxisViewChanged event in the documentation and saw how I might set the chart title with that data but not the x-axis labels themselves.
Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我很晚了,但我希望这对其他人有用...
一种可能的方法是订阅
chart.FormatNumber
事件,例如:由于在多个数据转换期间调用此事件处理程序图表元素,为了确保仅设置所需轴的格式,您可以将自定义格式传递给轴标签:
然后在事件处理程序中添加检查:
I'm very late, but I hope this can be useful for other people...
A possible way to do this is subscribing the
chart.FormatNumber
event, e.g. :Since this event handler is called during the conversion of several elements of the chart, to be sure to format only the desired axis, you can pass a custom format to the axis labels:
then add a check in the event handler:
你有没有尝试过
Did you tried to