Highcharts: Ensure y-Axis 0 value is at chart bott

2019-03-14 07:45发布

I'm outputting a series of highcharts on a page. In some instances, all data for the specified time period may come back with 0 values.

In such a case, the chart looks like this: http://jsfiddle.net/charliegriefer/KM2Jx/1/

There is only 1 y-Axis label, 0, and it's in the middle of the chart.

I'd like to force that 0 value for the y-Axis to be at the bottom of the chart, which would be consistent with other charts on the page that do have data.

Have tried various yAxis properties, such as "min: 0", "minPadding: 0", "maxPadding: 0", "startOnTick: true".

Seems it should be pretty straightforward, but I'm at a loss :(

Relevant yAxis code:

yAxis: {
    min: 0, 
    minPadding: 0, 
    startOnTick: true, 
    title: {
        text: ""
    }
},

标签: highcharts
8条回答
女痞
2楼-- · 2019-03-14 08:01

I had also posted this to the HighCharts forum, where I got a response that seems to work. Unfortunately, it will involve me doing some pre-processing of the data prior to rendering the charts in order to check for all "0" data values... but it'll work. Just wish there was something more "built-in" to HighCharts.

So after summing up the "y" values, if I get 0, the y-axis properties should look like this:

yAxis: {
    minPadding: 0, 
    maxPadding: 0,         
    min: 0, 
    max:1,
    showLastLabel:false,
    tickInterval:1,
    title: {
            text: ""
    }
}

See: http://jsfiddle.net/KM2Jx/2/

The keys seem to be the "max" and "showLastLabel" properties.

查看更多
唯我独甜
3楼-- · 2019-03-14 08:05

Since Highcharts 5.0.1, you can also use softMax.

softMax: Number
A soft maximum for the axis. If the series data maximum is greater than this, the axis will stay at this maximum, but if the series data maximum is higher, the axis will flex to show all data.

yAxis: {
    min: 0,
    softMax: 100,
    ...
}

Here is an example with a column chart : http://jsfiddle.net/84LLsepj/

Note that currently, in Highcharts 5.0.7, it doesn't seem to be working for all types of charts. As you can see in this other jsfiddle, it doesn't work with line chart.

查看更多
【Aperson】
4楼-- · 2019-03-14 08:07

Here is a fix I came up with that doesn't require any preprocessing of the data and just let highcharts do it, like it used to before the update. http://jsfiddle.net/QEK3x/

if(chart.yAxis[0].dataMax === 0)
{
   chart.yAxis[0].setExtremes(0, 5);
}

You only need to check the first series axis, as if the first one is not 0 then the problem won't occur. I might submit a pull request for this to high-charts, though I kinda feel like they intended for this functionality for some reason.

查看更多
Melony?
5楼-- · 2019-03-14 08:09

For Highcharts 5.x.x, you should be able to just specify:

yAxis: {
  softMin: 0,
  softMax: 1,
};

This won't work for line chart, which requires that you also set minRange in the same config object as above. Experiment with different values, since defining for example minRange: 6, created a range on yAxis between 0 and 4.

查看更多
走好不送
6楼-- · 2019-03-14 08:18

Only way I could get it to show it "correctly" was by also providing an yAxis max value. I think this is a HighCharts issue when you provide no data elements with a y-value. It draws the best chart it thinks it can.

查看更多
一夜七次
7楼-- · 2019-03-14 08:20

minRange : 1 or whatever you feel so to be displayed in fractions of
softMax : 100 or whatever you feel so

Result

yAxis: {
         minRange : 1,
         softMax: 100
       }
查看更多
登录 后发表回答