MsChart:如何在 X 轴上将日历周数显示为标签?

发布于 2024-11-28 14:07:47 字数 444 浏览 3 评论 0原文

我需要在 MSChart 中格式化时间轴的标签,例如“CW21”、“CW22”...我苦苦挣扎了一段时间,但尚未找到任何解决方案。

  1. 有一个 LabelStyle.Format 属性,但这不支持(据我所知)日历周。它使用日期时间格式化字符串。有没有办法引入自定义格式化程序?

  2. 在框架中,我找到了日历.GetWeekOfYear 方法,它将为我提供正确的周数。现在的问题是如何将这些整合在一起?

有什么好的策略可以以通用方式在轴上设置自己的标签吗? (事先不知道轴的最小/最大点)

I need to format the labels of the time axis in a MSChart like "CW21", "CW22", ... I am struggling for a while and could not find any solution yet.

  1. There is a LabelStyle.Format property but this does not support (AFAIK) calendar weeks. It uses the DateTime formatting strings. Is there a way to introduce a custom formatter?

  2. In the framework I found the Calendar.GetWeekOfYear method, which would provide me with the right week number. Now the question is how to bring these together?

Is there any good strategy to setup one's own labels on the axis in a generic manner? (without knowing in advance the min/max points of the axis)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

全部不再 2024-12-05 14:07:47

是的,您可以使用图表.FormatNumber 事件例如:

private void FillChart()
{
    // register the format event
    this.chart1.FormatNumber += new EventHandler<FormatNumberEventArgs>(chart1_FormatNumber);

    // fill the data table with values
    DataTable dt = new DataTable();
    dt.Columns.Add("X", typeof(DateTime));
    dt.Columns.Add("Y", typeof(double));

    dt.Rows.Add(DateTime.Today.AddDays(1), 10);
    dt.Rows.Add(DateTime.Today.AddDays(8), 30);
    dt.Rows.Add(DateTime.Today.AddDays(15), 10);
    dt.Rows.Add(DateTime.Today.AddDays(21), 20);
    dt.Rows.Add(DateTime.Today.AddDays(25), 40);
    dt.Rows.Add(DateTime.Today.AddDays(31), 25);

    // bind the data table to chart
    this.chart1.Series.Clear();

    var series = this.chart1.Series.Add("Series 1");
    series.XValueMember = "X";
    series.YValueMembers = "Y";

    series.ChartType = SeriesChartType.Line;
    this.chart1.DataSource = dt;
    this.chart1.DataBind();

    // set a custom format to recognize the AxisX label in the event handler
    this.chart1.ChartAreas[0].AxisX.LabelStyle.Format = "CustomAxisXFormat"; 

}

void chart1_FormatNumber(object sender, FormatNumberEventArgs e)
{
    if (e.ElementType == ChartElementType.AxisLabels &&
        e.Format == "CustomAxisXFormat")
    {
        if (e.ValueType == ChartValueType.DateTime)
        {
            var currentCalendar = CultureInfo.CurrentCulture.Calendar;
            e.LocalizedValue = "CW" +
                currentCalendar.GetWeekOfYear(DateTime.FromOADate(e.Value),
                System.Globalization.CalendarWeekRule.FirstDay,
                CultureInfo.CurrentCulture.DateTimeFormat.FirstDayOfWeek);
        }
    }
}

结果如下:

在此处输入图像描述

Yes, you can use the Chart.FormatNumber event e.g.:

private void FillChart()
{
    // register the format event
    this.chart1.FormatNumber += new EventHandler<FormatNumberEventArgs>(chart1_FormatNumber);

    // fill the data table with values
    DataTable dt = new DataTable();
    dt.Columns.Add("X", typeof(DateTime));
    dt.Columns.Add("Y", typeof(double));

    dt.Rows.Add(DateTime.Today.AddDays(1), 10);
    dt.Rows.Add(DateTime.Today.AddDays(8), 30);
    dt.Rows.Add(DateTime.Today.AddDays(15), 10);
    dt.Rows.Add(DateTime.Today.AddDays(21), 20);
    dt.Rows.Add(DateTime.Today.AddDays(25), 40);
    dt.Rows.Add(DateTime.Today.AddDays(31), 25);

    // bind the data table to chart
    this.chart1.Series.Clear();

    var series = this.chart1.Series.Add("Series 1");
    series.XValueMember = "X";
    series.YValueMembers = "Y";

    series.ChartType = SeriesChartType.Line;
    this.chart1.DataSource = dt;
    this.chart1.DataBind();

    // set a custom format to recognize the AxisX label in the event handler
    this.chart1.ChartAreas[0].AxisX.LabelStyle.Format = "CustomAxisXFormat"; 

}

void chart1_FormatNumber(object sender, FormatNumberEventArgs e)
{
    if (e.ElementType == ChartElementType.AxisLabels &&
        e.Format == "CustomAxisXFormat")
    {
        if (e.ValueType == ChartValueType.DateTime)
        {
            var currentCalendar = CultureInfo.CurrentCulture.Calendar;
            e.LocalizedValue = "CW" +
                currentCalendar.GetWeekOfYear(DateTime.FromOADate(e.Value),
                System.Globalization.CalendarWeekRule.FirstDay,
                CultureInfo.CurrentCulture.DateTimeFormat.FirstDayOfWeek);
        }
    }
}

Here's the result:

enter image description here

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