在 C# 图表中选择性地隐藏系列
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15012718/
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
Selectively hiding series in a C# chart
提问by Xantham
Lets say I have a chart with 2 series on it. Then for each series, I have a checkbox to say whether I want to see them or not. Assume that I originally plot both, and afterwards, wanted to hide either of them. What is the best way to do this?
假设我有一个图表,上面有 2 个系列。然后对于每个系列,我都有一个复选框来说明我是否想看它们。假设我最初绘制了两者,然后想隐藏它们中的任何一个。做这个的最好方式是什么?
I know I could just Clear()
it and then AddXY()
them back in, but is there a faster way to do it?
我知道我可以Clear()
,然后AddXY()
他们又回来了,但是有没有更快的方法呢?
My attempted ideas:
我尝试的想法:
1. Set a visibility property to true/false depending on checkbox.
There is No visibility Property
2. Copy Points Collection to a variable, clear, and put back in.
Series[].Points
is read-only
3. Copy Series to a variable, clear the points, and put back in.
Apparently it stores the Series as a reference when I try this, and I cannot find a copy command.
1. 根据复选框将可见性属性设置为 true/false。
没有可见性属性
2. 将点集合复制到变量,清除,然后放回。
Series[].Points
是只读的
3. 将系列复制到变量,清除点,然后放回。
显然它将系列存储为参考当我尝试这个时,我找不到复制命令。
So I am apparently going about this the wrong way. How would you dynamically allow chart to have different series hidden?
所以我显然是以错误的方式解决这个问题的。您将如何动态允许图表隐藏不同的系列?
采纳答案by Larry
To hide a series in MSChart, use the Enabled
property this way :
要在 MSChart 中隐藏系列,请按Enabled
以下方式使用该属性:
msChart.Series["Series"].Enabled = false;
and to show it again :
并再次显示它:
msChart.Series["Series"].Enabled = true;
So you dont need to remove points and re-add them.
所以你不需要删除点并重新添加它们。
回答by Hyman
There's another way to do this without hiding the legend.
还有另一种方法可以在不隐藏图例的情况下做到这一点。
Simply say:
简单地说:
Chart.Series[0].Color = Color.FromArgb(0, 0, 0, 0); //sets color to transparent
You can reset to Color.Empty later on to restore the default colour.
您可以稍后重置为 Color.Empty 以恢复默认颜色。
Only disadvantage here is that unless the last item in the series is hidden, the other lines will be recolored
这里唯一的缺点是,除非隐藏系列中的最后一项,否则其他行将重新着色