I'm trying to generate charts using Google Chart API and getting charts as images. Note that I'm rendering the chart on server side and not on browser side (ie not using the Google Visualization API).
In some of my charts, I draw a vertical line, representing a target. As the target is the same for all my different x values, I just need to label it on top of my graph, as shown in the image below:
I can manage to display only the top label, BUT: - How can I remove the small annotation line? Especially when no labels are displayed! - How can I move this label on top of the line instead of right (to prevent overlap with bar labels if e.g. A = 95)? - How can I format this unique label (ie set the text in a box with a background color)
Here is the code I used to generate the chart images:
var annotations = [0,
1, {calc: "stringify", sourceColumn: 1, type: "string", role: "annotation"},
2, {calc: "stringify", sourceColumn: 3, type: "string", role: "annotation"},
];
var dataViewDefinition = Charts.newDataViewDefinition().setColumns(annotations).build();
var data = Charts.newDataTable()
.addColumn(Charts.ColumnType.STRING, 'x')
.addColumn(Charts.ColumnType.NUMBER, 'act')
.addColumn(Charts.ColumnType.NUMBER, 'tgt')
.addColumn(Charts.ColumnType.STRING, 'tgt_lbl')
.addRow(['A', 90, 95, '95'])
.addRow(['B', 80, 95, ''])
.addRow(['C', 100, 95, ''])
.build();
var chart = Charts.newBarChart()
.setDataTable(data)
.setRange(0, 150)
.setOption('series', {
1: { lineWidth: 1, type: 'line'}
})
.setOption('chartArea', {'width': '80%', 'height': '100%'})
.setColors(['#D9D9D9', '#0085AD'])
.setOption('annotations', { textStyle: { color: '#000000' }})
.setDataViewDefinition(dataViewDefinition)
.build();
var folder=DriveApp.getFolderById('Folder ID');
folder.createFile(chart.getAs('image/png')).setName('Image Name');
I saw a trick to remove the annotations line when using the Visualization API by modifying the generated SVG, but how to proceed when using the Chart API ?
Thx for your help!