0

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?

4

1 回答 1

2

目前,chartkick 无法接受函数,因为它使用 json 作为参数的方式。我发现了这个讨论这个的github讨论。他们提到一个可能的解决方案是将函数作为字符串传递,然后修改 json 的解析方式,类似于本文

另一种可能的解决方案,因为我认为chartkick 对其js 使用highcharts 的印象是,在页面/图表已经加载后更新事件。我相信它看起来像这样

$("#container").highcharts().series[0].update({
  events:{
    click: function (event, i) {
      alert(event.point.name);
    }
  }
})
于 2014-05-02T12:37:14.837 回答