Highcharts - best way to handle and display zero (or negative) values in a line chart series with logarithmic Y axis

匿名 (未验证) 提交于 2019-12-03 09:18:39

问题:

In my HighChart line graphs, the series data is being fed from my Ruby on Rails application dynamically. Sometimes the series values are zeros or less which is a problem for HighCharts and it throws the following exception:

Highcharts Error #10 Can't plot zero or subzero values on a logarithmic axis 

So as a work-around, I process my ruby array to conditionally replace a zero of less value with an insignificant positive number, .e.g. 0.00001 as shown below:

oil_vol_array = d_array[1].map { |e| (e 

This prevents the exception being thrown, but the display shows the graph starting at 0.0001 if the starting value is zero (understandably so, since I asked it to). A more desirable display would be to start the graph at zero, but HighChart doesn't like it :(

Is there a way that this can be achieved?

回答1:

Have you tried using a label formatter?

var chart = new Highcharts.Chart({      yAxis: {                 labels: {             formatter: function() {                 if(this.value === 0.00001){                     return 0;                 } else {                     return this.value;                 }             }         }     } }); 


回答2:

When dealing with huge numbers, it's better to use the standard label formatter for values other than 0, otherwise your labels gonna be displayed like this: 1000000000... To change this replace 'else' statement to the call to the original label formatter method below:

var chart = new Highcharts.Chart({      yAxis: {                 labels: {             formatter: function() {                 if(this.value === 0.00001){                     return 0;                 } else {                     return Highcharts.Axis.prototype.defaultLabelFormatter.call(this);                 }             }         }     } }); 


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!