简体   繁体   English

有选择地隐藏 C# 图表中的系列

[英]Selectively hiding series in a C# chart

Lets say I have a chart with 2 series on it.假设我有一张图表,上面有 2 个系列。 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?做这个的最好方式是什么?

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. 1. 根据复选框将可见性属性设置为真/假。
There is No visibility Property没有可见性属性

2. Copy Points Collection to a variable, clear, and put back in. 2. 将 Points Collection 复制到变量中,清除并放回。
Series[].Points is read-only Series[].Points是只读的

3. Copy Series to a variable, clear the points, and put back in. 3. 将 Series 复制到一个变量,清除点,然后放回。
Apparently it stores the Series as a reference when I try this, and I cannot find a copy command.显然,当我尝试这样做时,它将系列存储为参考,但我找不到复制命令。

So I am apparently going about this the wrong way.所以我显然以错误的方式解决这个问题。 How would you dynamically allow chart to have different series hidden?您将如何动态地允许图表隐藏不同的系列?

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. 所以你不需要删除点并重新添加它们。

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 这里唯一的缺点是,除非隐藏系列中的最后一项,否则其他线将重新着色

form.chart1.Legends.Clear(); form.chart1.Legends.Clear();

//Hiding the series from the legend will work too: //隐藏图例中的系列也可以:

form.chart1.Series["0"].IsVisibleInLegend = false; form.chart1.Series["0"].IsVisibleInLegend = false;

//disabling the legend: //禁用图例:

form.chart1.Legends[0].Enabled = false; form.chart1.Legends[0].Enabled = false;

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

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