New line for legend in google chart (PieChart)

Viewed 2509

I am using Google chart (piechart) and want to start each legend on a new line - NOT like on the picture below:

Piechart

here is my code:

 var data = new google.visualization.arrayToDataTable(chartData, false);

                        var optionsErrorDetails = {
                            title: 'My Piechart',
                            fontSize: 10
                            ,

                            pieHole: 0.3,

                            legend:
                                {
                                    position: 'top',
                                    alignment: 'start',

                                textStyle: {
                                    fontSize: 8
                                },

                            },


                        };

                        var chartErrorDetails = new google.visualization.PieChart(document.getElementById('errorDetails'));

                        chartErrorDetails.draw(data, optionsErrorDetails);
2 Answers

the only way to guarantee each legend label is displayed on a new line is to position the legend on the right,
see following working snippet...

google.charts.load('current', {
  packages: ['corechart']
}).then(function () {
  var chartData = [
    ['Category', 'Value'],
    ['Category Alpha', 46.7],
    ['Category Beta', 53.3],
  ];

  var data = google.visualization.arrayToDataTable(chartData, false);

  var optionsErrorDetails = {
    title: 'My Piechart',
    fontSize: 10,
    pieHole: 0.3,
    legend: {
      position: 'right',
      textStyle: {
        fontSize: 8
      },
    },
  };

  var chartErrorDetails = new google.visualization.PieChart(document.getElementById('errorDetails'));

  chartErrorDetails.draw(data, optionsErrorDetails);
});
#errorDetails {
  display: inline-block;
}
<script src="https://www.gstatic.com/charts/loader.js"></script>
<div id="errorDetails"></div>


there is a maxLines option that works when legend.position is 'top'.
but this only allows additional lines, it doesn't force them,
from the docs...

legend.maxLines - Maximum number of lines in the legend. Set this to a number greater than one to add lines to your legend. Note: The exact logic used to determine the actual number of lines rendered is still in flux.


note: arrayToDataTable is a static method, and doesn't require the new keyword...

var data = google.visualization.arrayToDataTable(chartData, false);

EDIT

you could try moving the labels manually,
see following working snippet for a rough example...

google.charts.load('current', {
  packages: ['corechart']
}).then(function () {
  var chartData = [
    ['Category', 'Value'],
    ['Category C', 50],
    ['Category B', 50],
    ['Category A', 50],
  ];

  var data = google.visualization.arrayToDataTable(chartData, false);

  var optionsErrorDetails = {
    chartArea: {
      bottom: 72
    },
    title: 'My Piechart',
    fontSize: 10,
    pieHole: 0.3,
    legend: {
      position: 'bottom',
      alignment: 'middle',
      textStyle: {
        fontSize: 8
      },
    },
  };

  var container = document.getElementById('errorDetails');
  var chartErrorDetails = new google.visualization.PieChart(container);

  google.visualization.events.addListener(chartErrorDetails, 'ready', function () {
    var circles = container.getElementsByTagName('circle');
    var labels = container.getElementsByTagName('text');
    var labelIndex = -1;
    var fontSize;
    var radius;
    var xCoordCircle;
    var xCoordLabel;
    var yCoordLabel;
    var yCoordCircle;
    Array.prototype.forEach.call(labels, function(label) {
      if ((label.getAttribute('text-anchor') === 'start') && (label.getAttribute('fill') !== '#ffffff')) {
        labelIndex++;
        if (labelIndex === 0) {
          radius = parseFloat(circles[labelIndex].getAttribute('r'));
          xCoordCircle = circles[labelIndex].getAttribute('cx');
          xCoordLabel = label.getAttribute('x');
        } else {
          fontSize = parseFloat(label.getAttribute('font-size')) * labelIndex;
          yCoordLabel = parseFloat(label.getAttribute('y'));
          label.setAttribute('x', xCoordLabel);
          label.setAttribute('y', (yCoordLabel - fontSize - (radius * labelIndex)));
          yCoordCircle = parseFloat(circles[labelIndex].getAttribute('cy'));
          circles[labelIndex].setAttribute('cx', xCoordCircle);
          circles[labelIndex].setAttribute('cy', yCoordCircle - fontSize - (radius * labelIndex));
        }
      }
    });
  });

  chartErrorDetails.draw(data, optionsErrorDetails);
});
<script src="https://www.gstatic.com/charts/loader.js"></script>
<div id="errorDetails"></div>

I don't know if it is any help, but for Highcharts, the way to have the legend items show in vertical, you must say: layout: 'vertical'. Possibly this works the same way for google charts. (see below)

                   legend: {
                    layout: 'vertical',
                    align: 'top',
                    verticalAlign: 'top',
                    fontweight: 'normal',
                    margin: -4,
                    padding: 0
                },
Related