【问题标题】:Formatting Microsoft Chart Control X Axis labels for specific datapoint为特定数据点格式化 Microsoft Chart Control X 轴标签
【发布时间】:2011-07-27 09:15:21
【问题描述】:

在一个 winfow 应用程序中,我有一个带有 2 个图表区域的 ms 图表。 第一个图表区域包含 4 个系列(堆叠和条形)

我需要更改某些特定点的 X 轴标签颜色,但在 VS 2010 中,我只能更改轴标签文本,而不能更改颜色。

有没有办法做到这一点?

【问题讨论】:

    标签: c# .net winforms user-interface charts


    【解决方案1】:

    在此链接中: http://msdn.microsoft.com/en-us/library/dd456628.aspx

    你会发现使用 LabelStyle 类来改变轴的标签。使用 LabelStyle.ForeColor 属性来改变标签的颜色。

    【讨论】:

    • 我见过,但风格适用于整个轴。我必须将 x 轴标签放在系列中的某个点上,而不是所有点上
    • 没有人可以帮助我吗?这个问题仍然没有答案。
    【解决方案2】:

    我知道这对于 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);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多