MS Chart: How can you change the color of each label on the Axis of a Bar Chart?

人走茶凉 提交于 2019-12-23 09:12:50

问题


I have a bar chart which shows different categories on the Y axis.

I can change the color of all of them on the axis at the same time by using:

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

However it doesn't allow me to set the color for each of them.

Any help will be much appreciated.


回答1:


You can try adding custom labels to the chart, and that will allow you to modify each one individually.

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:


Ok The only solution I have found is to create a custom label and set the color that way:

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


来源:https://stackoverflow.com/questions/9637561/ms-chart-how-can-you-change-the-color-of-each-label-on-the-axis-of-a-bar-chart

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!