我想在我使用Highcharts的rails项目中通过图表表示数据。我能够将其集成到项目中。如果y轴值为零,我不想绘制该图。 Image reference of the current scenario
我的代码,
@chart_data = [[2015, 20], [2016, 31], [2017, 5], [2018, 19], [2019, 0]]
= column_chart @chart_data, height: '375px', library: {
xAxis: {
title: { text: 'Years'}
},
yAxis: {
stackLabels: {
enabled: true, style: {color: '#8b9196'}
},
title: {
text: 'Turn over rate')
}
},
plotOptions: {
column: {
dataLabels: {
enabled: true, style: { color: 'white'}
}
}
},
legend: {
align: 'right', x: 0, verticalAlign: 'top', y: -10, floating: true, backgroundColor: 'white', borderColor: '#CCC', borderWidth: 1, shadow: false
}
}
我正在寻找没有用的JavaScript格式的解决方案。根据搜索,应添加以下代码,如下所示
plotOptions: {
column: {
dataLabels: {
enabled: true, style: { color: 'white'},
formatter: %|function(){
if (this.y > 0)
return this.y;
}|
}
}
}
它抛出错误refer to the image here。我无法找出获得期望结果的方法。我正在Rails中寻求帮助,因为该图表已添加到视图中。
我正在使用带有highcharts库的chartkick gem解决相同的问题。由于在ruby代码上调用js函数无效,因此我将其添加到application.js文件中,因此在加载图表时,此配置将应用于图表。如果只想在几个图表中使用该配置,则只需要在所需的视图中进行配置即可。
const Chartkick = require("chartkick");
Chartkick.use(require("highcharts"));
Chartkick.options = {
library: {
plotOptions: {
column: {
dataLabels: {
formatter: function() {
return (this.y!=0) ? this.y : "";
}
}
}
}
}
}