我有以下代码(使用 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");
}
}
}
}
}
%>
我只想将点击事件函数作为参数传递,但rails会解释它。 html_safe
和辅助函数没有用。有建议吗?
最佳答案
目前,chartkick 无法接受函数,因为它使用 json 作为参数的方式。我找到了这个github discussion其中讨论了这一点。他们提到一个可能的解决方案是将函数作为字符串传递,然后修改 json 的解析方式,类似于 article .
另一个可能的解决方案是在页面/图表加载后更新事件,因为我的印象是,chartkick 使用 highcharts 作为其 js。我相信它会看起来像这样
$("#container").highcharts().series[0].update({
events:{
click: function (event, i) {
alert(event.point.name);
}
}
})
关于javascript - Rails 语句中的“Escape” javascript 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23426807/