Hide Chart Series by clicking on series legend

bones

Well-known member
Joined
Aug 23, 2014
Messages
143
Programming Experience
Beginner
Here's one for you advanced charting guys.....once of which I am NOT..

In my charts I show the series in the legend. It would be a really cool feature if I could allow the user to click on the series in the legend and have it hide, then show that series in the chart.

Is is relatively easy or will it be a lot of work?

Is it even possible?

This is microsoft datavisualization in a forms project... I suspect that makes a different vs ASP or just plain old MSChart...
 
I looked over the link you suggested. I get the concept... I'm not experienced enough to just implement the code required from just that information...

In searching for practical code examples in VB to review and learn from, I came across a charting example that looked promising. To my dismay I discovered, after downloading the code is in C#. Not helpful to me. Even though the download link indicates to me that the download should have VB code I don't see any after unzipping the archive. So..that leads to my question.

Do you know of any sample code in VB that would be similar to this Samples Environments for Microsoft Chart Controls in C#, HTML
 
No, I don't, but I think you're overcomplicating and giving up before trying (to write code).
Have a look at below sample, does this really look complicated at all? Just like I explained, code is from MouseDown event calling HitTest method, investigating the result, and setting a Color for the associated Series object (toggling Transparent/not set).
    Private Sub Chart1_MouseDown(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles Chart1.MouseDown
        Dim result = Me.Chart1.HitTest(e.X, e.Y)
        If result.ChartElementType = ChartElementType.LegendItem Then
            If result.Series.Color = Color.Transparent Then
                result.Series.Color = Color.Empty
            Else
                result.Series.Color = Color.Transparent
            End If
        End If
    End Sub

The only unknown factor for you here would be the return value of HitTest method. Documentation says it is type HitTestResult, including a link to the documetation for that class. Clicking it quickly reveals the relevant properties ChartElementType and Series that is useful here.
 
No, I don't, but I think you're overcomplicating and giving up before trying (to write code).
Have a look at below sample, does this really look complicated at all? Just like I explained, code is from MouseDown event calling HitTest method, investigating the result, and setting a Color for the associated Series object (toggling Transparent/not set).
    Private Sub Chart1_MouseDown(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles Chart1.MouseDown
        Dim result = Me.Chart1.HitTest(e.X, e.Y)
        If result.ChartElementType = ChartElementType.LegendItem Then
            If result.Series.Color = Color.Transparent Then
                result.Series.Color = Color.Empty
            Else
                result.Series.Color = Color.Transparent
            End If
        End If
    End Sub

The only unknown factor for you here would be the return value of HitTest method. Documentation says it is type HitTestResult, including a link to the documetation for that class. Clicking it quickly reveals the relevant properties ChartElementType and Series that is useful here.

:stung: I'll give it a whirl....thank you!
 
It's perfect just as it is.... I click on a series...it vanishes.....click again...it returns....

One thing that happens though is the series color of the next series changes as well....it returns to normal color once the clicked series is returned though... Is that where the return value comes into play or do I need to address that differently?
 
Last edited:
Explain this part to me PLEASE
VB.NET:
If result.Series.Color = [COLOR=#0000cd][B]Color.Transparent[/B][/COLOR] Then
                result.Series.Color = Color.Empty
            Else
result.Series.Color = Color.Transparent

TRANSPARENT is throwing me off....

The series has an assigned color....so explain Transparent as it is used here..

I have a concept of how to keep the color of the next series from changing..but need some clarification on the Transparent statement ..

If TRANSPARENT means the series current status / color... if you please... Then rather than saying color.empty, I would assume if I said series.color = chartarea.color....the issue with the next series changing colors would be solved...

That leaves me wondering what happens when the series is clicked again to return the series color to original...
 
Last edited:
Back
Top