在Google饼图中显示标签和百分比

时间:2018-12-10 22:11:26

标签: javascript charts google-visualization

我想在Google饼图中显示标签百分比。有什么办法吗?在docs中,我发现可以使用pieSliceText选项修改文本。可能的值为:

label-显示数据名称(例如Apples

value-显示绝对值(例如7

percentage-显示百分比值(例如50%

value-and-percentage-同时显示值和百分比(例如7 (50%)

但是是否有类似label-and-percentage的东西来显示类似Apples (50%)的东西?

1 个答案:

答案 0 :(得分:2)

将同时显示标签和百分比的唯一配置选项是图例...

legend: {
  position: 'labeled'
},

请参阅以下工作片段...

google.charts.load('current', {
  packages: ['corechart']
}).then(function () {
  var data = google.visualization.arrayToDataTable([
    ['Tasks', 'Completed'],
    ['Morning', 28],
    ['Afternoon', 43],
    ['Evening', 80],
    ['Night', 161]
   ]);

  var options = {
    width: 900,
    height: 400,
    title: 'Tasks Completed',
    pieHole: 0.5,
    colors: ['#008000', '#ffbf00', '#FF0000','#4E6282'],
    pieSliceText: 'value',
    sliceVisibilityThreshold :0,
    fontSize: 17,
    legend: {
      position: 'labeled'
    },
  };

  var chart = new google.visualization.PieChart(document.getElementById('chart_div'));
  chart.draw(data, options);
});
<script src="https://www.gstatic.com/charts/loader.js"></script>
<div id="chart_div"></div>