Search code examples
webchartsmschartinteractive

How to make Chart Legend Items interactive in mschart in a web application


How to make Chart Legend Items interactive in mschart in a web application. I have tried using HitTestResult class. However to get the coordinates for X and Y locations of click, MouseEventArgs class is not supported for charts.Could someone please answer this and preferably share a code snippet.


Solution

  • From the question I guess you want to hide/unhide series on click of legend items. HitResult is used with the desktop version, where we get access to clicked co-ordinates using MouseEventArgs object. However, to achieve the same on web chart, you can follow the below,

    1. Associate series details with Legend Item's post back property when the chart is being built legendItem1.PostBackValue = ser.Name & ";" & Chart1.Legends(ChartArea1).CustomItems.Count - 1

    2. This post back can be used to access the clicked series on Chart's click event,

      Protected Sub Chart1_Click(ByVal sender As Object, ByVal e As ImageMapEventArgs) Dim pointData As String() = e.PostBackValue.Split(";"c) Dim selectedSeries As Series = Chart1.Series(pointData(0)) Dim selectedlegendItem As LegendItem = Chart1.Legends("Default").CustomItems(pointData(1))

      If selectedSeries IsNot Nothing Then
          If selectedSeries.Enabled Then
              selectedSeries.Enabled = False
          Else
              selectedSeries.Enabled = True
          End If
      End If        
      

      End Sub