在饼图中自定义工具提示文本

时间:2016-02-19 18:40:20

标签: javascript charts pie-chart google-chartwrapper google-pie-chart

我在饼图上工作,我想自定义鼠标悬停在饼图上时出现的工具提示。目前,该文字显示50 (50%),我真的希望它说$50 (50%)。谷歌图表API中不清楚如何实现这一目标。我该怎么做呢?目前我的JS代码看起来像这样......

  google.charts.load('current', {'packages':['corechart']});
  google.charts.setOnLoadCallback(drawChart);
  function drawChart() {

    var data = google.visualization.arrayToDataTable([
      ['Effort', 'Amount given'],
      ['first half',     50],
      ['second half',     50]
    ]);

    var options = {
      pieHole: 0.5,
      pieSliceTextStyle: {
        color: 'black',
      },
      legend: 'none'
    };

    var chart = new google.visualization.PieChart(document.getElementById('donut_single'));
    chart.draw(data, options);
  }

JSFiddle

1 个答案:

答案 0 :(得分:1)

以下是如何使用Google图表API指定工具提示的示例。

Here's some relevant documentation.



google.charts.load('current', {
  'packages': ['corechart']
});
google.charts.setOnLoadCallback(drawChart);

function drawChart() {

  data = new google.visualization.DataTable();
  data.addColumn('string', 'Effort');
  data.addColumn('number', 'Amount given');
  data.addColumn({type: 'string', role: 'tooltip'});
  data.addRows([
    ['first half', 50, "$50 (50%)"],
    ['second half', 50, "$50 (50%)"]
  ]);

  var options = {
    pieHole: 0.5,
    pieSliceTextStyle: {
      color: 'black',
    },
    legend: 'none'
  };

  var chart = new google.visualization.PieChart(document.getElementById('donut_single'));
  chart.draw(data, options);
}

<script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
<div id="donut_single" style="width: 900px; height: 500px;"></div>
&#13;
&#13;
&#13;