MS Chart:如何更改柱状图轴上每个标签的颜色?

6

我有一个条形图,显示了不同类别在Y轴上。

我可以通过使用以下方法同时更改轴上所有类别的颜色:

 chart.ChartAreas["MyChart"].AxisY.LabelStyle.ForeColor = "Red";

然而,它不允许我为它们中的每一个设置颜色。

非常感谢任何帮助。

2个回答

4
你可以尝试向图表添加自定义标签,这将允许你单独修改每个标签。
private void AddCustomLabelAtYValue(double YValue, string Text, Color ForeColor)
{
    double scale = chart.ChartAreas["MyChart"].AxisY.Maximum - 
        chart.ChartAreas["MyChart"].AxisY.Minimum;
    double offset = scale * 0.5;
    CustomLabel customLabel = new CustomLabel(YValue - offset, 
        YValue + offset, Text, 0, LabelMarkStyle.None);
    customLabel.ForeColor = ForeColor;
    chart.ChartAreas["MyChart"].AxisY.CustomLabels.Add(customLabel);
}

2

好的,我找到的唯一解决方案是创建一个自定义标签,并通过这种方式设置颜色:

this._chart.ChartAreas[0].AxisX.CustomLabels.Add(new CustomLabel(position - 1, position + 1, point.AxisLabel, 0, LabelMarkStyle.None));

this._chart.ChartAreas[0].AxisX.CustomLabels[position - 1].ForeColor = GetColor(point.AxisLabel);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接