Column Chart - Show percentage above each bar

1,609 views
Skip to first unread message

Nicole Goldup

unread,
Oct 28, 2014, 12:05:15 AM10/28/14
to google-visua...@googlegroups.com
Hi there,

page: http://www.hpvregister.org.au/research/coverage-data/hpv-vaccination-coverage-2013

Is it possible to show the percentage value above each bar? e.g. like the attached image

Thanks,
Nicole


image003.jpg

Andrew Gallant

unread,
Oct 28, 2014, 8:55:06 AM10/28/14
to google-visua...@googlegroups.com
You can add an "annotation" column to your data.  You can use a DataView to calculate this automatically for you:

var view = new google.visualization.DataView(dataTable);
view.setColumns([0, 1, 2, 3, {
    type: 'string',
    role: 'annotation',
    sourceColumn: 2,
    calc: function (dt, row) {
        return (dt.getValue(row, 2) * 100).toFixed(1) + '%';
    }
}]);

var chart = new google.visualization.ColumnChart(document.getElementById('coverage'));
chart.draw(view, options);

Incidentally, you could take the same approach with the tooltip column as well, since all of the values in that are based on data that exists elsewhere in the DataTable.

Nicole Goldup

unread,
Oct 28, 2014, 6:59:36 PM10/28/14
to google-visua...@googlegroups.com
Thanks Andrew.

It works, however the values are off center. Is it possible to make the percentages centered?

Regards,
Nicole

Andrew Gallant

unread,
Oct 28, 2014, 8:56:28 PM10/28/14
to google-visua...@googlegroups.com
The SVG text nodes that make up the annotations inherit the text-align style of parent elements, which your CSS has set to "left" in layout.css:

#headerCenter, #bannerCenter, #pageTitleCenter, #mainContentCenter, #footerCenter, #contentBottomCenter {
 width
:93.75%; /* (960 / 1024) x 100 */ /*width:81.6666667%; /* (980 / 1200) x 100 */ /*width:61.25%; /* (980 / 1600) x 100 */ /* width:94.230769%; /* (980 / 1040) x 100 */
 margin
:0 auto;
 text
-align:left;
}

You'll need to do some experimentation to find the right settings, but I suggest starting with this:

#coverage {
    text
-align: center;
}

Nicole Goldup

unread,
Oct 29, 2014, 1:26:26 AM10/29/14
to google-visua...@googlegroups.com
Hi Andrew,

I have figured it out. When I decreased the annotation font size, it centered itself.

Regards,
Nicole
Reply all
Reply to author
Forward
0 new messages