简体   繁体   中英

Highcharts - toggle auto scale

I'm trying to make a toggle button where you can freeze the scale. If you click again the scale is recomputed automaticlly (which is default in highcharts).

Accoridng to the API reference, if y-axis = {max:null} then the scale is automatic. But if I lock the scale I must extract the y-axis scale somehow so I can manually set the y-axis to the current value. And when I toggle back the y-axis is set to null.

Is there anyway to extract the y-axis scale when it is set to null? I haven't been able to find any in API.

Here is my highchart object

chartFactory.getBarChart = function(data, duration){
  //console.log(chartData);
  var chart = $('.chart').highcharts({
    chart: {
      renderTo: 'chartContainer',
      type: 'column',
      height: windowHeight - 220, //Set chartsize depending on size of window
      reflow: true
    },
    title: {
      text: "title"
    },
    subtitle: {
      text: "Source: "
    },
    xAxis: {
      categories: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun','Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'],
    },
    yAxis: {
      min: 0,
      max: null,
      title: {
        text: 'Energy (kWh)'
      }
    },
    "tooltip": {},
    plotOptions: {
      column: {
        animation: {
          duration: duration
        },
        stacking: 'normal',
        dataLabels: {
          enabled: true,
          color: (Highcharts.theme && Highcharts.theme.dataLabelsColor) || 'white'
        }
      }
    },
    series: data //Calculated data
  })
  .highcharts(); // return chart
}

Sure, you can extract the current chart settings by calling chart.highcharts(), it contains each axis which will have the effective min and max as well as the data's min/max set based on the series that are using it.

yAxis[0] would work if you only have one, but setting an id on the Axis might be clearer:

 var chart = highcharts({...
     yAxis: {            
        id: 'my_y',
     ...

 yAxis = chart.highcharts().get('my_y');
 yAxis.getExtremes();  // {min:, max:, dataMin:, dataMax:}
 yAxis.setExtremes(newmin, newmax);

fiddle

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM