简体   繁体   English

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

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

I have a bar chart which shows different categories on the Y axis. 我有一个条形图,显示Y轴上的不同类别。

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. 任何帮助都感激不尽。

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM