我正在用 C# 制作 X 轴间隔为数周的 .NET 折线图。对于我的项目,我只想使用自定义标签,但现在我仍然想要网格线。有谁知道在保留自定义标签的同时隐藏默认 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);
图表如下所示:
它应该是这样的:
最佳答案
好吧,我查看了 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/