MS图表:如何更改条形图轴上每个标签的颜色

本文关键字:标签 颜色 图表 何更改 条形图 MS | 更新日期: 2023-09-27 18:27:28

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

我可以通过使用同时更改轴上所有颜色

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

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

任何帮助都将不胜感激。

MS图表:如何更改条形图轴上每个标签的颜色

您可以尝试将自定义标签添加到图表中,这将允许您单独修改每个标签。

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

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

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