【问题标题】:Can I access values generated by google charts我可以访问谷歌图表生成的值吗
【发布时间】:2015-08-10 09:58:01
【问题描述】:

目前我将变量传递给 api,如下所示:

<script type="text/javascript">
 mobileNav();
 // Load the Visualization API and the piechart package.
 google.load('visualization', '1.0', {'packages':['corechart']});

 // Set a callback to run when the Google Visualization API is loaded.
 google.setOnLoadCallback(drawChart);

 //Callback that creates and populates a data table,
 // instantiates the pie chart, passes in the data and
 // draws it.
 function drawChart() {

  // Create the data table.
  var data = new google.visualization.DataTable();
  data.addColumn('string', 'Date');
  data.addColumn('number', 'Attendance');
  data.addRows([
    ['Present', <?php echo $present; ?>],
    ['Absent', <?php echo $absent; ?>],
    ['Directed Study', <?php echo $directed; ?>],
    ['Medical Certificate', <?php echo $medical; ?>],
    ['Self Certificate', <?php echo $selfcert; ?>],
    ['Authorised Absence', <?php echo $authorised; ?>],
  ]);

  // Set chart options
  var options = {'title':'Your Attendance'};


  // Instantiate and draw our chart, passing in some options.
  var chart = new      google.visualization.PieChart(document.getElementById('chart_div'));
  chart.draw(data, options);

我得到了结果,一切都很好。我想访问饼图生成的 % 值。有什么办法可以实现吗?

【问题讨论】:

    标签: javascript google-visualization googlevis


    【解决方案1】:

    如果您想访问饼图生成的值,则需要在 dataTable() 上检索数据。

    您需要使用 getFormattedValue 方法。

    文档: 数据表 API:https://developers.google.com/chart/interactive/docs/reference#DataTable

    方法:getFormattedValue(rowIndex, columnIndex)

    【讨论】:

    • 所有这一切都是返回放入图表中的相同数据。即 2 我正在寻找类似“13%”的地方
    • 你测试过:data.getNumberOfRows() 吗?
    • 以下是写入日志的以下函数的结果。 'data.getNumberOfRows() -> 6 | data.getNumberOfColumns() -> 2 | console.log(data.getFormattedValue(1,0)); -> 缺席 | console.log(data.getFormattedValue(1,1)); -> 2'
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-29
    • 1970-01-01
    • 2019-01-29
    • 2011-10-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多