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

标签 c# .net charts linechart axis-labels

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

我试过这个:

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

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

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

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);

图表如下所示:Chart with dates

它应该是这样的:chart without dates

最佳答案

好吧,我查看了 MSDN 上的 Label 控件。为了使自定义标 checkout 现在正常标签的位置,我将 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);

关于c# - 在 .NET 图表的 X 轴中仅使用自定义标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13996281/

相关文章:

c# - 应该使用什么方法来并行执行一项任务多次?

c# - 使用 Entity Framework 存储类别的枚举或实际表?

c# - C# datagridview 中的存储过程而不是列表框

.net - 如何避免 WPF 全屏应用程序中的闪烁?

javascript - 在 donut 中心显示文本

excel - VBA - 将图表从 Excel 粘贴到 PowerPoint 时保持源格式

c# - 应用程序服务器上安装的 IBM MQ .Net 客户端使用什么证书名称?

c# - 身份验证失败时重定向到母版页中的页面不起作用

.net - NET中写入文件时的填充编号

Excel雷达图-更改轴限制值