Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google Apps Script Chart vAxis.title not working

Something I'm writing in Google Apps Script for Google Sheets generates a chart. In trying to generate a graph I'd like to set axis titles so I can make the charts more understandable for people other than myself. The problem I'm having is that setting vAxis.title in any form doesn't do anything. Once the chart is generated no vertical axis title is made, it's just blank.

var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();
var range = sheet.getRange(1, 1, sheet.getLastRow(), sheet.getLastColumn());
var chartBuilder = sheet.newChart();

chartBuilder.setChartType(Charts.ChartType.SCATTER)
    .addRange(range)
    .setOption("pointSize", 1)
    .setOption("hAxis.title", "X value")
    .setOption("title", "Output")
    .setOption("vAxis.title", "Y output") //Set vertical axis title (not working)
    .setOption("trendlines", {0: {type: "linear", visibleInLegend: true, showR2: true}})
    .setPosition(1, 1, 150, 0);

sheet.insertChart(chartBuilder.build());

Help would be greatly appreciated, Thank you!

like image 339
Napostrophe Avatar asked Mar 01 '26 23:03

Napostrophe


2 Answers

This gist solved a very similar issue for me: https://gist.github.com/tanaikech/4125cc280e15c0fc726cb2fe4f35a3f7

Setting "vAxis" options through setOption("vAxis", {title: "y axis"}) seems to be broken. I tried all types of setting but nothing seemed to work.

But as the gist I link to above suggests, all those settings can be set using setOption("vAxes", {0: {title: "y axis"}})

So in your code change .setOption("vAxis.title", "Y output") to setOption("vAxes", {0: {title: "Y output"}}).

You can set multiple options that way, here is a working example from my code: .setOption("vAxes", {0: {title: "Kilograms", viewWindowMode:'explicit',viewWindow: {max:150,min:50}}})

like image 70
TheIceBear Avatar answered Mar 04 '26 22:03

TheIceBear


I don't know if it can help but I had the same problem, any option related to vAxis was not applying (but hAxis working correctly). I tried the workaround provided by @TheIceBear, but without success too.

I work with two tab open, one with the sheets and one with the code. As I reloaded the sheets (I was working on onOpen event), I noticed the tab with AppsScript did not close (which it always do). I closed it manually and reopen it from sheets menu Extensions->Apps Script, and from there the vAxis options started to apply, with the default way of setting option vAxis directly.

See example below :

  const dateRange = userSheet.getRange('C2:C');
  const differenceRange = userSheet.getRange('F2:F');
  const hAxisOptions = {
    title: 'Period',
    titleTextStyle: {
      bold: true,
    },
  };

  const vAxisOptions = {
    title: 'Difference (h)',
    titleTextStyle: {
      bold: true,
    },
  };

  const chart = userSheet.newChart().asLineChart()
    .addRange(dateRange)
    .addRange(differenceRange)
    .setOption('hAxis', hAxisOptions)
    .setOption('vAxis', vAxisOptions)
    .setOption('height', 400)
    .setOption('width', 800)
    .setOption('useFirstColumnAsDomain', true)
    .setOption('treatLabelsAsText', true)
    .setOption('curveType', 'function')
    .setPosition(1, 7, 0, 0)
    .setOption('title', 'Estimations precision evolution over time')
    .setOption('titleTextStyle', {
      bold: true,
      fontSize: 18,
      color: '#4285f4'
    })
    .build()
  ;

  userSheet.insertChart(chart);

Hope it can help someone !

like image 22
ChristopheH Avatar answered Mar 04 '26 21:03

ChristopheH



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!