.NET 图表中的 X 轴仅使用自定义标签

2023-12-31

我正在用 C# 制作一个 X 轴间隔为数周的 .N​​ET 折线图。对于我的项目,我只想使用自定义标签,但现在我仍然想要网格线。有谁知道如何隐藏默认 X 轴标签,同时仍保留自定义标签?

我试过这个:

Chart4.ChartAreas[0].AxisX.LabelStyle.Enabled = false;

明显的结果是没有应用标签,这不是我想要做的。

EDIT:生成原始行的代码是这样的:

Chart4.ChartAreas["ChartArea1"].AxisX.LabelStyle.Format = "M";

自定义标签的代码是这样的:

int month = XValues[0].Month;
var XAxis = Chart4.ChartAreas[0].AxisX;

DateTime StartMonthPos = XValues[0];
DateTime EndPos = new DateTime();

foreach (DateTime Date in XValues)
{
    EndPos = Date;

    if (Date.Month != month)
    {
        Chart4.ChartAreas[0].AxisX.CustomLabels.Add(StartMonthPos.ToOADate(), EndPos.ToOADate(), StartMonthPos.ToString("MMMM"), 1, LabelMarkStyle.None);
        StartMonthPos = Date;
    }

    month = Date.Month;
}

XAxis.CustomLabels.Add(StartMonthPos.ToOADate(), EndPos.ToOADate(), StartMonthPos.ToString("MMMM"), 1, LabelMarkStyle.None);

The chart looks like this: Chart with dates

And it should look like this: chart without dates


好吧,我调查了一下Label控制MSDN http://msdn.microsoft.com/en-us/library/dd456628%28v=vs.100%29.aspx。为了使自定义标签出现在普通标签的位置,我设置了RowIndex参数为0,替换默认标签行。自定义行的最终代码如下所示:

    int month = XValues[0].Month;
    var XAxis = Chart4.ChartAreas[0].AxisX;

    DateTime StartMonthPos = XValues[0];
    DateTime EndPos = new DateTime();

    foreach (DateTime Date in XValues)
    {
        EndPos = Date;

        if (Date.Month != month)
        {
           Chart4.ChartAreas[0].AxisX.CustomLabels.Add(StartMonthPos.ToOADate(),
              EndPos.ToOADate(), StartMonthPos.ToString("MMMM"), 0, LabelMarkStyle.None);
            StartMonthPos = Date;
        }

        month = Date.Month;
    }

    XAxis.CustomLabels.Add(StartMonthPos.ToOADate(), EndPos.ToOADate(),
          StartMonthPos.ToString("MMMM"), 0, LabelMarkStyle.None);
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

.NET 图表中的 X 轴仅使用自定义标签 的相关文章

随机推荐