Showing values in center of stacked bar

567 views
Skip to first unread message

gottfried_s

unread,
Sep 10, 2019, 10:28:53 AM9/10/19
to Google Visualization API
This is my code:


google.charts.load('current', {packages: ['corechart', 'bar']});
google.charts.setOnLoadCallback(drawChartp);
    function drawChartp() {
      var data = google.visualization.arrayToDataTable([
        [' ', 'CDU', 'SPD', 'Grüne', 'UWG', 'FDP'],
        ['', 1200, 900, 50, 30, 20],
      ]);
 
  var options_fullStacked = {
  title: 'Stimmenanteil (%)',
        isStacked: 'percent',
    backgroundColor: 'transparent',
        fontSize: 18,
        colors: ['black', '#e2021b', '#1fa12d', '#2382c0','#ffec01'],
height: 150,
width: 800,
        legend: {position: 'none', maxLines: 3},
        hAxis: {
            minValue: 0,
            ticks: [0, .25, .5, .75, 1]
        }
      };

var formatter = new google.visualization.NumberFormat({decimalSymbol: ',', fractionDigits: 0, groupingSymbol: '.'});
formatter.format(data, 1);

    var chart = new google.visualization.BarChart(document.getElementById('piechart'));
    chart.draw(data, options_fullStacked);}

I am a novice with Google chart. How can I acchieve to get the value in the center of the stacked item? This is want I would like to see:

wanted.png


Who can help me?


JENNIFER WOODSON

unread,
Sep 10, 2019, 10:42:33 AM9/10/19
to Google Visualization API
There is not an automatic way to center the labels like that. There is an open issue/feature request here: https://github.com/google/google-visualization-issues/issues/2352

I've previously used the answer in this Stack Overflow post to center labels using a MutationObserver. 

gottfried_s

unread,
Sep 10, 2019, 3:37:45 PM9/10/19
to Google Visualization API
Hi Jen,

thank you for your quick response. Could you please tell me how to display the value at the top of the bar?
Reply all
Reply to author
Forward
0 new messages