Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to dynamically adjust a Google Sheets chart vertical (y) axis minimum and maximum value to data set?

I'm struggling to adjust a Google Sheets line chart so that the vertical y-axis automatically adjusts to my data set.

For example I do not want the y-axis to start at 0 but rather the minimum value in my data set and also the maximum value.

In my sheet I have a minimum value of 0.712 and a maximum value of 0.811. The ideal is if the charts y-axis can then adjust to start (minimum) from 0.712 minus 10% and y-axis maximum value 0.811 + 10%.

This should create a neat chart for the data I have. I'm struggling to do this and it seems like I cannot enter a formula or something on the chart's customise options under Vertical axis Min and Max. Simply entering hard values will not work since the data I'm charting is very dynamic, so the chart's vertical axis needs to adjust to data.

Any help will be greatly appreciated.

like image 917
Petrus Avatar asked Jun 09 '19 13:06

Petrus


People also ask

How do I automate a chart in Google Sheets?

Creating a new chart happens from the Insert > Chart function. Select the type of chart and the data range, then style the chart as needed. Since the source data comes from the table updated by the form integration, the chart updates in real time.

How do you set a minimum cell value in Google Sheets?

The MIN function is a premade function in Google Sheets, which finds the lowest number in a range. It is typed =MIN and gets a list of cells: =MIN(value1, [value2, ...]) You can select cells one by one, but also ranges, or even multiple ranges.


1 Answers

Below piece of code maintaining min and max of two charts on one sheet.

 function onEdit(e) {
  var sheet = e.source.getActiveSheet();
  mit = sheet.getRange(1,12).getValue();
  mat = sheet.getRange(1,13).getValue();
  mih = sheet.getRange(1,14).getValue();
  mah = sheet.getRange(1,15).getValue();
  var chart = sheet.getCharts()[0];
  chart = chart.modify()
    .setOption('vAxes.0.viewWindow.max', mat)
    .setOption('vAxes.0.viewWindow.min', mit)
    .build();
  sheet.updateChart(chart);
  var chart1 = sheet.getCharts()[1];
  chart1 = chart1.modify()
    .setOption('vAxes.0.viewWindow.max', mah)
    .setOption('vAxes.0.viewWindow.min', mih)
    .build();
  sheet.updateChart(chart1);
}
like image 134
kraf101 Avatar answered Oct 18 '22 05:10

kraf101