C# 仅在.NET图表的X轴中使用自定义标签
我正在用C#制作一个X轴间隔为周的.NET线条图。对于我的项目,我只想使用自定义标签,但现在我仍然想要网格线。有人知道在保留自定义标签的同时隐藏默认X轴标签的方法吗 我试过这个:C# 仅在.NET图表的X轴中使用自定义标签,c#,.net,charts,linechart,axis-labels,C#,.net,Charts,Linechart,Axis Labels,我正在用C#制作一个X轴间隔为周的.NET线条图。对于我的项目,我只想使用自定义标签,但现在我仍然想要网格线。有人知道在保留自定义标签的同时隐藏默认X轴标签的方法吗 我试过这个: Chart4.ChartAreas[0].AxisX.LabelStyle.Enabled = false; 明显的结果是没有应用任何标签,这不是我想要做的 编辑: 生成原始行的代码如下所示: Chart4.ChartAreas["ChartArea1"].AxisX.LabelStyle.Format = "M";
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);
自定义标签的代码如下:
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);
图表如下所示:
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);
它应该是这样的:好吧,我查看了。为了使自定义标签代替普通标签显示,我将
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);
您可能需要查看柱状图