【问题标题】:Add javascript function in Dotnet Highchart在 Dotnet Highchart 中添加 javascript 函数
【发布时间】:2015-03-01 10:16:30
【问题描述】:

我想在我生成的图表上添加一个点击事件。我有以下代码:

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'); ");

除了点击事件ChartClickEvent之外,一切都运行良好。当我点击图表时,它没有给出任何结果 - 而不是显示 alert('yyyy')

【问题讨论】:

    标签: javascript jquery asp.net asp.net-mvc dotnethighcharts


    【解决方案1】:

    例如,您可以这样做(基本警报):

    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" });
    

    【讨论】:

      【解决方案2】:

      您仍然需要在 InitChart 中注册 javascript 函数,如下所示:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-11-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-08-18
        • 1970-01-01
        相关资源
        最近更新 更多