Question

I have the following code (using the chartkick gem):

<%= pie_chart [[t('result.positive'), @rating_positive], [t('result.negative'), @rating_negative], [t('result.neutral'), @rating_neutral]],
    colors: ['#B3D986', '#E04848', '#C9C9C9'],
    library: {
      chart: {
        backgroundColor: '#EEEEEE',
        width: 300,
        height: 250
      },
      plotOptions: {
        pie: {
          allowPointSelect: true,
          cursor: 'pointer',
          dataLabels: {
              enabled: false
          },
          showInLegend: true,
          events: {
            click: function(event, i) {
               alert("test");
            }
          }
        }
      }
    }
  %>

I just want to pas the click event function as a parameter, but rails interprets it. html_safeand helper functions are of no use. Suggestions?

Was it helpful?

Solution

Currently, chartkick is unable to accept functions because of the way it uses json for its parameters. I found this github discussion which discusses this. They mention a possible solution is to pass the function in as a string and then to modify how the json is deparsed similarly to this article.

Another possible solution, as I am under the impression that chartkick uses highcharts for its js, is to update the events after the page/chart has already loaded. I believe it would look something like this

$("#container").highcharts().series[0].update({
  events:{
    click: function (event, i) {
      alert(event.point.name);
    }
  }
})
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top