首页 > 解决方案 > GAS:修改图表纵轴最小值和最大值

问题描述

感谢@iansedano 的roundup() 和rounddown() 等效项,以及@Tanaike 过去的帖子,我有一个函数可以计算图表垂直轴的最小值和最大值。但是,当我尝试修改图表时出现错误:

Exception: The parameters (String) don't match the method signature for SpreadsheetApp.Sheet.updateChart

对于修改,我使用了以下文档示例: Class EmbeddedChart

我在以下位置找到的选项: 图表配置选项

我不确定,但问题可能是可以有两个垂直轴,并且必须以某种方式指定左或右?

function test(){
  c_ScaleVerticalAxis('Chart', 'Stock SMA3', 'C6:C1263');
}
function c_ScaleVerticalAxis(sheetName, chartTitle, rangeA1) {
// Author:  Max Hugen
// Date:    2021-08-09
// Purpose: Modify MinValue & MaxValue of Vert.Axis to suit Value Data
// Params:  rangeA1 of Values, eg "C6:C"
  const ss = SpreadsheetApp.getActiveSpreadsheet();
  const sheet = ss.getSheetByName(sheetName);
  const range = sheet.getRange(rangeA1);

  // Ref: @Tanaike, https://stackoverflow.com/a/64887850/190925
  var chart = c_GetChartByTitle(sheetName, chartTitle); 

  var margin = 0.05;   // a margin below/above the min/max data vals
  var decPlaces = 0;
  
  // Ref: @Tanaike, https://stackoverflow.com/a/45203334/190925
  var ar = Array.prototype.concat.apply([], range.getValues()); 
  var minVal = Math.min.apply(null, ar);

  Logger.log('Data minVal:  ' + minVal);
  if (minVal > 100000)  { decPlaces = -5; } else 
  if (minVal > 10000)   { decPlaces = -4; } else
  if (minVal > 1000)    { decPlaces = -3; } else
  if (minVal > 100)     { decPlaces = -2; } else
                        { decPlaces = -1; }

  // Ref: @iansedano, https://stackoverflow.com/a/68710322/190925                      
  minVal = rounddown(minVal*(1-margin), decPlaces); 
  Logger.log('Chart minVal:  ' + minVal);

  var maxVal = Math.max.apply(null, ar);

  Logger.log('Data maxVal:  ' + maxVal);
  if (maxVal > 100000)  { decPlaces = -5; } else 
  if (maxVal > 10000)   { decPlaces = -4; } else
  if (maxVal > 1000)    { decPlaces = -3; } else
  if (maxVal > 100)     { decPlaces = -2; } else
                        { decPlaces = -1; }
  maxVal = roundup(maxVal*(1+margin), decPlaces );
  Logger.log('Chart maxVal:  ' + maxVal);

  chart = chart.modify()
    .setOption('vAxis.minValue',minVal)
    .setOption('vAxis.maxValue',maxVal)
    .build;
  sheet.updateChart(chart);
}

标签: google-apps-scriptgoogle-sheetscharts

解决方案


似乎vAxis.minValue并且vAxis.maxValue仅适用于Continuous Charts

要为Discrete Charts设置最小和最大垂直轴,请分别使用vAxis.viewWindow.minvAxis.viewWindow.max

如配置选项中所述。

在此处输入图像描述


示例离散图表(数据列类型为string

在此处输入图像描述

示例代码:

function updateChart(){
  var sheet = SpreadsheetApp.getActiveSheet();
  var chart = sheet.getCharts()[0];
  var newChart = chart.modify().setOption("vAxis.viewWindow.max", 20).setOption("vAxis.viewWindow.min", 5).build();
  sheet.updateChart(newChart);
}

输出: 在此处输入图像描述


示例连续图(数据列类型为以下之一:numberdate或)datetimetimeofday

在此处输入图像描述

示例代码:

function updateChart1(){
  var sheet = SpreadsheetApp.getActiveSheet();
  Logger.log(sheet.getName());
  var chart = sheet.getCharts()[0];
  var newChart = chart.modify().setOption("vAxis.maxValue", 30).setOption("vAxis.minValue", 0).build();
  sheet.updateChart(newChart);
}

输出:

在此处输入图像描述


推荐阅读