4

I´m trying to modify a chart in Google Sheets using Google App Script. I figured out how to do most of the things I need to do, except for one. I need to change the position of the Data Label in the chart. The chart is a Combo Chart that has line and bar charts.

The following code shows the dataLabels, but I can´t find how to change its position(eg. "center","below", etc..).

function myFunction() {
  var sheet = SpreadsheetApp.getActiveSheet();
  var chart = sheet.getCharts()[0];

  chart = chart.modify()
  .setOption('series',{0: {dataLabel: 'value'},1: {dataLabel: 'value',targetAxisIndex: 1}})
  .build();

  sheet.updateChart(chart);
}

Apparently there is now documentation on it, but some people found a way to display the dataLabel (Programatically show data labels in Google Spreadsheet Embedded Line Chart). I was wondering if any one knows how to change the position of the data label.

TheMaster
  • 45,448
  • 6
  • 62
  • 85
BI ABlab
  • 71
  • 5

1 Answers1

3

The answer is

var vAxes = [{minValue:0,maxValue:''},{minValue:0,maxValue:''}];
var series = {
  0: {dataLabel: 'value',dataLabelPlacement:"insideBase"},
  1: {dataLabel: 'value',targetAxisIndex: 1,dataLabelPlacement:"center"}
};

chart = chart.modify()
.setOption('vAxes',vAxes)
.setOption('series',series)
.build();

I followed the instructions on this answer (Google Apps Script: How to set "Use column A as labels" in chart embedded in spreadsheet?). Just publish the chart and inspect the code.

here a list of the properties I´ve found:

options":{
"treatLabelsAsText":true,
"vAxes":[
{
"minValue":0,
"maxValue":""
},
{
"minValue":0,
"maxValue":""
}
],
"legacyScatterChartLabels":true,
"title":"July/2018, August/2018 and September/2018",
"type":"line",
"lineWidth":2,
"hAxis":{
"useFormatFromData":true,
"title":"Month",
"viewWindow":{
}
},
"series":{
"0":{
"dataLabelPlacement":"insideBase",
"dataLabel":"value",
"annotations":{
"position":"center"
},
"hasAnnotations":true
},
"1":{
"dataLabelPlacement":"below",
"dataLabel":"value",
"annotations":{
"position":"center"
},
"hasAnnotations":true,
"targetAxisIndex":1
}
},
"useFirstColumnAsDomain":true,
"domainAxis":{
"direction":1
},
"width":818,
"booleanRole":"certainty",
"height":506
}
BI ABlab
  • 71
  • 5