How to add data labels to a Google Chart

流过昼夜 提交于 2020-07-22 10:37:26

问题


I've created a pie chart using the Google Chart API but am unable to control which data labels are added. I'd like to be able to add a label for each slice of the pie. Can anyone provide any insight on how to go about doing this?

Code for the chart below:

   <script type="text/javascript" src="https://www.google.com/jsapi"></script>
    <script type="text/javascript">
      google.load("visualization", "1", {packages:["corechart"]});
      google.setOnLoadCallback(drawChart);
      function drawChart() {
        var data = google.visualization.arrayToDataTable([
          ['Resource', 'Percentage'],
          ['Video',     517],
          ['Download',      13],
          ['Tool',      13],
          ['Video and Download',  15],
          ['Video and Tool', 59],
          ['Download Video and Tool',    5]
        ]);

        var options = {
          title: '',
      is3D: 'true',
      colors: ['592C81', '799A3D', '863375', '5B5B5B', '004A97', 'B8BABD' ]
        };

        var chart = new google.visualization.PieChart(document.getElementById('piechart_3d'));
        chart.draw(data, options);
      }
    </script>
  </head>
  <body>
    <div id="piechart_3d" style="width: 900px; height: 500px;"></div>

回答1:


Since there's not enough room in each slice, looks like the best you can do is add

 legend: { position: 'labeled' }

to your options.

Example here.



来源:https://stackoverflow.com/questions/26814107/how-to-add-data-labels-to-a-google-chart

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!