【发布时间】:2011-07-27 09:15:21
【问题描述】:
在一个 winfow 应用程序中,我有一个带有 2 个图表区域的 ms 图表。 第一个图表区域包含 4 个系列(堆叠和条形)
我需要更改某些特定点的 X 轴标签颜色,但在 VS 2010 中,我只能更改轴标签文本,而不能更改颜色。
有没有办法做到这一点?
【问题讨论】:
标签: c# .net winforms user-interface charts
在一个 winfow 应用程序中,我有一个带有 2 个图表区域的 ms 图表。 第一个图表区域包含 4 个系列(堆叠和条形)
我需要更改某些特定点的 X 轴标签颜色,但在 VS 2010 中,我只能更改轴标签文本,而不能更改颜色。
有没有办法做到这一点?
【问题讨论】:
标签: c# .net winforms user-interface charts
在此链接中: http://msdn.microsoft.com/en-us/library/dd456628.aspx
你会发现使用 LabelStyle 类来改变轴的标签。使用 LabelStyle.ForeColor 属性来改变标签的颜色。
【讨论】:
我知道这对于 OP 来说为时已晚,但它可能对寻找如何执行此操作的其他人有用。
自定义标签允许您设置颜色,问题是如果您添加一个自定义标签,那么所有标准标签都会消失,因此您必须为整个轴创建自定义标签,然后为您想要的那个设置颜色与众不同。
此代码假定每个 X 值都需要一个标签。如果您有大量 X 值,则需要调整代码。
double offset = 0.5;//Choose an offset that is 1/2 of the range between x values
for (int i = 0; i < chart1.Series[0].Points.Count; i++)
{
var customLabel = new CustomLabel();
//NOTE: the custom label will appear at the mid-point between the FromPosition and the ToPosition
customLabel.FromPosition = chart1.Series[0].Points[i].XValue - offset; //set beginning position (uses axis values)
customLabel.ToPosition = chart1.Series[0].Points[i].XValue + offset; //set ending position (uses axis values)
customLabel.Text = chart1.Series[0].Points[i].XValue.ToString(); //set the text to display, you may want to format this value
if (i == 3)
{
customLabel.ForeColor = Color.Green;//only change the 3rd label to be green, the rest will default to black
}
chart1.ChartAreas[0].AxisX.CustomLabels.Add(customLabel);
}
【讨论】: