为啥我需要将 DataView(而不仅仅是 DataTable)传递给 PieChart.draw()?
Posted
技术标签:
【中文标题】为啥我需要将 DataView(而不仅仅是 DataTable)传递给 PieChart.draw()?【英文标题】:Why do I need to pass a DataView (instead of just a DataTable) to PieChart.draw()?为什么我需要将 DataView(而不仅仅是 DataTable)传递给 PieChart.draw()? 【发布时间】:2013-02-14 00:55:10 【问题描述】:我搜索了很多,终于能够运行我的谷歌图表代码。这是我同时使用数据视图和数据表的代码。
//这是我的chartDraw.php代码
<html>
<head>
<!--Load the AJAX API -->
<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript">
function drawChart()
var jsonData = $.ajax(
url:"getdata.php",
dataType:"json",
async:false
).responseText;
//This is used when you hard code your values: static data. Here I am taking data from database so commented it.
/*var jsonData='"cols":["label":"User ID","type":"string","label":"Group Name","type":"string","label":"Requested Nodes","type":"number","label":"Actual PE","type":"number"],"rows":["c":["v":"user 1","v":"ufhpc","v":1,"v":5.000],"c":["v":"user2","v":"ufhpc","v":1,"v":7.000]]';
*/
//Create our data table out of JSON data loaded from server
var data=new google.visualization.DataTable(jsonData);
//PieCharts expects 2 columns of data: a label and a value, so we need to use a DataView to restrict to 2 columns
var view=new google.visualization.DataView(data);
view.setColumns([0,3]);
//Instantiate and draw our chart, passing in some options
var chart=new google.visualization.PieChart(document.getElementById('chart_div'));
chart.draw(view,width:400,height:240);
//Load the visualization API and the piechart package
google.load('visualization','1','packages':['corechart']);
//Set a callback to run when the google visualization API is loaded
google.setOnLoadCallback(drawChart);
</script>
</head>
<body>
<!--Div that will hold the pie chart -->
<div id="chart_div"></div>
</body>
</html>
//getdata.php:包含连接和查询
<?php
mysql_connect('localhost','user','password');
mysql_select_db('dbname');
$sqlquery1="select userid,group_name,req_nodes,actualPE from jobs where userid='zhang' limit 200";
$sqlresult1=mysql_query($sqlquery1);
$table=array();
$table['cols']=array(
array('label'=> 'User ID', type=>'string'),
array('label'=>'Group Name', type=>'string'),
array('label'=>'Requested Nodes', type=>'number'),
array('label'=>'Actual PE', type=>'number')
);
$rows=array();
while($r=mysql_fetch_assoc($sqlresult1))
$temp=array();
$temp[]=array('v' => $r['userid']);
$temp[]=array('v' => $r['group_name']);
$temp[]=array('v' =>(int) $r['req_nodes']);
$temp[]=array('v' =>(float) $r['actualPE']);
$rows[]=array('c' => $temp);
$table['rows']=$rows;
$jsonTable = json_encode($table);
//this print statement just for testing
print $jsonTable;
?>
DataView
类和DataTable
构造函数有什么区别?
如果我不使用DataView
,它不会打印。
【问题讨论】:
你忘了问问题 这只是一个答案。我解决了我的问题,所以发布了它。但是我仍然添加了一个问题.. 这个网站是用来提问和回答的。不是教程。 对此我很抱歉。但我添加了一个问题。 【参考方案1】:使用谷歌:
Google visualization API.
堆栈溢出不适用于教程,而是用于生产性问题。如果您想创建教程,您可能需要使用名为 The Code Player 的网站。
【讨论】:
以上是关于为啥我需要将 DataView(而不仅仅是 DataTable)传递给 PieChart.draw()?的主要内容,如果未能解决你的问题,请参考以下文章
为啥有 Mongoose 模型而不仅仅是模式/文档? [复制]
为啥 gulp 需要安装 --save-dev 而不仅仅是 --save
SlickGrid:使用 DataView 而不是原始数据的简单示例?
为啥 std::vector 需要 is_trivial 进行按位移动,而不仅仅是 is_trivially_copyable?