C# 如何更改图表“系列”中特定数据点的颜色?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15403711/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 16:41:34  来源:igfitidea点击:

How to change colors of specific data points in a "series" of a chart?

c#charts

提问by vish213

screenshot of currently appearing chartI don't know if can make it clear otherwise, So here is the code:-

当前出现的图表的屏幕截图我不知道是否可以说清楚,所以这里是代码:-

    Series series2 = null;
    for (int i = 0; i < 4; i++)
    {
        series2 = chart2.Series.Add(subjectnames[i]);
    }
    for (int i = 0; i < 4; i++)
    {
        series2.Points.Add(attemptper[i]);
        series2.Points.Add(correctper[i]);
    }

Now, I just want to display "attemptper" bars in different color than "correctper" bars. By default they are appearing in greyish-blue color. How do I get it done?

现在,我只想用与“更正者”条不同的颜色显示“尝试者”条。默认情况下,它们显示为灰蓝色。我该如何完成?

The charts appear all in bluish color. I want them to appear in different colors. I think I haven't added the data correctly to the chart for this to achieve?

图表全部显示为蓝色。我希望它们以不同的颜色出现。我想我没有正确地将数据添加到图表中以实现这一目标?

采纳答案by wsclaassen

I would suggest adding an extra series to your chart and changing the color set on that.

我建议在您的图表中添加一个额外的系列并更改其上的颜色设置。

回答by Brian

You change the color on the series point(index) like this:

您可以像这样更改系列点(索引)上的颜色:

        Chart1.Series[1].Points[0].Color = Color.Red

Getting the chart to repaint might also take some code, depending on what you're doing. For me, I wanted to animate my chart, building the data point (a column) dynamically as the program ran, showing the status of some work I was doing, which required this:

重新绘制图表可能还需要一些代码,具体取决于您在做什么。对我来说,我想动画我的图表,在程序运行时动态构建数据点(一列),显示我正在做的一些工作的状态,这需要:

        Chart1.Series.ResumeUpdates()
        Chart1.Series[1].Points.Item[0].YValues = Double(1) {MyNewValue, 0}
        Chart1.Series[1].Points[0].Color = Color.Red
        Chart1.DataBind()
        Chart1.Series.Invalidate()
        Chart1.Series.SuspendUpdates()