2

I want to add a click event on my generated chart. I have the code below:

Highcharts chart = new Highcharts("chart");
chart.InitChart(new DotNet.Highcharts.Options.Chart { DefaultSeriesType = ChartTypes.Bar, Height = 500 })

   .SetTitle(new Title { Text = "Nombre de resultat par mois" })
    // .SetSubtitle(new Subtitle { Text = "Accounting" })
   .SetXAxis(new XAxis { Categories = new[] { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" } })
   .SetYAxis(new YAxis { Title = new YAxisTitle { Text = "Nombre de resultats" } })
   .SetTooltip(new Tooltip
   {
       Enabled = true,
       Formatter = @"function() { return '<b>'+ this.series.name +'</b><br/>'+ this.x +': '+ this.y; }"
   })
   .SetPlotOptions(new PlotOptions
   {
       Line = new PlotOptionsLine
       {
           Point = new PlotOptionsLinePoint { Events = new PlotOptionsLinePointEvents { Click = "ChartClickEvent" } },
           DataLabels = new PlotOptionsLineDataLabels
           {
               Enabled = true
           },
           EnableMouseTracking = false
       }
   })
  .SetSeries(new Series { Name = "MySeries", Data = new Data(new object[] { 29.9, 71.5, 106.4, 129.2, 144.0, 148.5, 216.4, 194.1, 95.6, 54.4, 55, 66 }) })
  .AddJavascripFunction("ChartClickEvent", @"alert('yyyy'); ");

Everything is working well except from the click event ChartClickEvent. When I click on the chart, it just gives no result - instead of showing alert('yyyy').

ekad
  • 14,436
  • 26
  • 44
  • 46

2 Answers2

1

You can do this, for instance (basic alert):

Highcharts chart = new Highcharts("chart")
   .SetPlotOptions(new PlotOptions
   {
       Series = new PlotOptionsSeries
       {
           Point = new PlotOptionsSeriesPoint
           {
               Events = new PlotOptionsSeriesPointEvents { Click = "ChartClickEvent" }
           }
       }
   })
  .AddJavascripFunction("ChartClickEvent", @"alert(this.series.name);", new string[] { "event" });
Francisco
  • 4,104
  • 3
  • 24
  • 27
0

You will still need to register the javascript function in your InitChart, like this:

.InitChart(new DotNet.Highcharts.Options.Chart { 
    DefaultSeriesType = ChartTypes.Bar, 
    Height = 500,
    Events = new ChartEvents { Click = "ChartClickEvent" }
})
Tegyr
  • 129
  • 1
  • 6