更改嵌入式组合图中数据标签的位置

时间:2018-10-09 18:49:09

标签: google-apps-script google-sheets google-visualization embedded-resource

我正在尝试使用Google App脚本修改Google表格中的图表。我想出了如何做我需要做的大多数事情,除了一项。我需要更改数据标签在图表中的位置。该图表是具有折线图和条形图的组合图。

以下代码显示了dataLabels,但我找不到如何更改其位置(例如,“中心”,“下方”等)。

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);
}

显然现在上面有文档,但是有些人找到了显示dataLabel(Programatically show data labels in Google Spreadsheet Embedded Line Chart)的方法。我想知道是否有人知道如何更改数据标签的位置。

1 个答案:

答案 0 :(得分:1)

答案是

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();

我按照此答案(Google Apps Script: How to set "Use column A as labels" in chart embedded in spreadsheet?)上的说明进行操作。只需发布图表并检查代码即可。

这是我找到的属性的列表:

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
}