11

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?

I know I could just Clear() it and then AddXY() them back in, but is there a faster way to do it?

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.

So I am apparently going about this the wrong way. How would you dynamically allow chart to have different series hidden?

Xantham
  • 1,829
  • 7
  • 24
  • 42

3 Answers3

28

To hide a series in MSChart, use the Enabled property this way :

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.

Larry
  • 17,605
  • 9
  • 77
  • 106
  • Well, that was simple. I did not think checking for an "enabled", just visible. I could see why they chose this name though. Out of curiosity, how did you initially learn that enabled is what allowed this functionality? – Xantham Feb 21 '13 at 22:04
  • 1
    I remember I was lucky enough to find this Enabled property by playing with the designer :) – Larry Feb 22 '13 at 06:56
  • 2
    @Larry, Thanks for that info. Unfortunately, disable them also stop the chart from showing the legends of the ones i disabled. Is there a way to disable a series but still showing its legend info? Thank you. – dailyUnknown Dec 27 '16 at 22:13
5

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.

Only disadvantage here is that unless the last item in the series is hidden, the other lines will be recolored

Jack
  • 472
  • 6
  • 13
0
chart1.Legends.Clear();

//Hiding the series from the legend will work too:
chart1.Series["0"].IsVisibleInLegend = false;

//disabling the legend:
chart1.Legends[0].Enabled = false;
David
  • 1
  • 2
  • 2
    As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Jeremy Caney Dec 23 '22 at 01:47