Karen Fisher
Karen Fisher

Reputation: 777

How to create data labels on google sheet charts?

Creating an embeded chart in google sheet is quite different form creating it with Chart API. I create a chart by this code:

function new_chart(range, sheet, title, offset_x, offset_y, type, color) {
   var chartBuilder = sheet.newChart();
   chartBuilder.addRange(range)
       .setChartType(eval('Charts.ChartType.' + type))
       .setOption('title', title)
       .setOption('bar.groupWidth', '80%')
       .setOption('width', WIDTH)
       .setOption('height', HIGHT)
       .setOption('legend.textStyle.fontSize', '10')
       .setPosition(1, 1, offset_x, offset_y);
   if (color < 5) {chartBuilder.setOption('colors', [COLORS[color]]);}
   sheet.insertChart(chartBuilder.build());
 }

Mostly, type of chart is COLUMN and I want to add data labels to created charts as it is possible to do manually with 'Advanced edit...'->Data lebels->value, but can't find the way. The nearest guess is to use 'annotations' in options, but here https://developers.google.com/chart/interactive/docs/gallery/columnchart#Configuration_Options nothing about how to make them on. Though a lot of how to style them.
Please, give me a hand how to settle this feature. Thanks!

Upvotes: 4

Views: 1995

Answers (2)

dmusgrave
dmusgrave

Reputation: 121

You can add data labels to the first series on your chart by adding this line when you're setting options in your chart builder:

.setOption('series',{0: {dataLabel: 'value'}})

If you need it for a different series, just change the 0 to the relevant series number. There's no need to do anything with annotations.

You can also do more than one series in the same object, e.g.:

.setOption('series',{0: {dataLabel: 'value'}, 1: {dataLabel: 'value'}})

I can't actually find this in the documentation, but it works.

Upvotes: 1

Lily
Lily

Reputation: 637

You can use .setOption("series",{"0":{"annotations":{"stemColor":"none"},"dataLabel":"value"}}) to instantiate labels on your chart.

Upvotes: 0

Related Questions