MS Chart 控制轴格式

发布于 2024-08-08 12:45:58 字数 638 浏览 4 评论 0原文

我正在编写的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

中性美 2024-08-15 12:45:58

我很晚了,但我希望这对其他人有用...

一种可能的方法是订阅 chart.FormatNumber 事件,例如:

void chart1_FormatNumber(object sender, FormatNumberEventArgs e)
{
    if (e.ElementType == ChartElementType.AxisLabels &&
        e.ValueType == ChartValueType.Int64)
    {
        e.LocalizedValue = DateTime.FromFileTimeUtc((long)e.Value).ToShortDateString();
    }
}

由于在多个数据转换期间调用此事件处理程序图表元素,为了确保设置所需轴的格式,您可以将自定义格式传递给轴标签:

this.chart1.ChartAreas[0].AxisX.LabelStyle.Format = "MyAxisXCustomFormat";

然后在事件处理程序中添加检查:

void chart1_FormatNumber(object sender, FormatNumberEventArgs e)
{
    if (e.ElementType == ChartElementType.AxisLabels &&
        e.ValueType == ChartValueType.Int64 && 
        e.Format == "MyAxisXCustomFormat")
    {
        e.LocalizedValue = DateTime.FromFileTimeUtc((long)e.Value).ToShortDateString();
    }
}

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. :

void chart1_FormatNumber(object sender, FormatNumberEventArgs e)
{
    if (e.ElementType == ChartElementType.AxisLabels &&
        e.ValueType == ChartValueType.Int64)
    {
        e.LocalizedValue = DateTime.FromFileTimeUtc((long)e.Value).ToShortDateString();
    }
}

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:

this.chart1.ChartAreas[0].AxisX.LabelStyle.Format = "MyAxisXCustomFormat";

then add a check in the event handler:

void chart1_FormatNumber(object sender, FormatNumberEventArgs e)
{
    if (e.ElementType == ChartElementType.AxisLabels &&
        e.ValueType == ChartValueType.Int64 && 
        e.Format == "MyAxisXCustomFormat")
    {
        e.LocalizedValue = DateTime.FromFileTimeUtc((long)e.Value).ToShortDateString();
    }
}
我爱人 2024-08-15 12:45:58

你有没有尝试过

 yourSeries.XValueType = ChartValueType.Time;

Did you tried to

 yourSeries.XValueType = ChartValueType.Time;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文