首页 > 解决方案 > 无法为谷歌饼图增加价值

问题描述

 $(document).ready(function(){
 name1= $("#party1_name").val();
 count1=$("#party1_count").val();
 name2= $("#party2_name").val();
var  count2=$("#party2_count").val();
 name3= $("#party3_name").val();
 count3=$("#party3_count").val();




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

// Draw the chart and set the chart values
function drawChart() {
  alert("Value: " + count2);
 var value1=count1;
 var value2=2;
  var data = google.visualization.arrayToDataTable([
  ['Task', 'Hours per Day'],
  [name1, value1],
  [name2, value2],
  [name3, 4],
  ['Gym', 2],
  ['Sleep', 8]
]);

  // Optional; add a title and set the width and height of the chart
  var options = {'title':'Result Analysis', 'width':550, 'height':400};

  // Display the chart inside the <div> element with id="piechart"
  var chart = new google.visualization.PieChart(document.getElementById('piechart'));
  chart.draw(data, options);
}
 });

当 m 从输入字段中获取数据并将数据添加到 dataarray 时,例如 value1 变量。图表中不显示任何内容。但是当添加 value2 变量时它的工作。我已经使用警报进行了测试。我得到了正确的值,但没有显示数据。

这就是如何将数据添加到输入字段。

 <input type="text" id="party1_name" value="<?=$party_name1?>">
 <input type="number" id="party1_count" value="<?=$count_part1?>">
 <input type="text" id="party2_name" value="<?=$party_name2?>">
 <input type="text" id="party2_count" value="<?=$count_part2?>">
 <input type="text" id="party3_name" value="<?=$party_name3?>">
 <input type="text" id="party3_count" value="<?=$count_part3?>">

这是图表输出

标签: javascriptphp

解决方案


推荐阅读