Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

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

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 < 0.0001) ? 0.0001 : e.round(3) }

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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;
                }
            }
        }
    }
});

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...