这是我尝试过的一个简单示例:
有一个名为 mysql 的表attendence
,我将创建一些数据数组。
<?php
$query = mysql_query("SELECT * FROM attendence");
$results = array(array());
while($line = mysql_fetch_array($query)){
$results[] = $line;
}
$empNames = array();
$attendence = array();
foreach( $results as $g )
{
$empNames[] = $g[EmpId];
$attendence[] = $g[attendence];
}
?>
这是HTML代码:
<div id="content">
<h2>Charts</h2>
<div id="chart1b" style="height:300px; width:500px;"></div>
<br>
<div id="chart1" style="height:300px; width:500px;"></div>
<br>
<div id="chart2" style="height:300px; width:500px;"></div>
</div>
这是我的javascript代码:
<script type="text/javascript">
$(document).ready(function(){
var line1 = <?php echo json_encode($results); ?>;
var plot1b = $.jqplot('chart1b', [line1], {
title: 'Employee vs. Attendence',
series:[{renderer:$.jqplot.BarRenderer}],
axesDefaults: {
tickRenderer: $.jqplot.CanvasAxisTickRenderer ,
tickOptions: {
fontFamily: 'Georgia',
fontSize: '10pt',
angle: -30
}
},
axes: {
yaxis: {
autoscale:true
},
xaxis: {
renderer: $.jqplot.CategoryAxisRenderer
}
}
});
});
</script>
<!-- End code -->
<!-- Chart Two code -->
<script type="text/javascript">
$(document).ready(function(){
var data = <?php echo json_encode($results); ?>;
var plot1 = jQuery.jqplot ('chart1', [data],
{
seriesDefaults: {
// Make this a pie chart.
renderer: jQuery.jqplot.PieRenderer,
rendererOptions: {
// Put data labels on the pie slices.
// By default, labels show the percentage of the slice.
showDataLabels: true
}
},
legend: { show:true, location: 'e' }
});
});
</script>
<!-- End code -->
<!-- Chart three code -->
<script type="text/javascript">
$(document).ready(function(){
var line1 = <?php echo str_replace('"','',json_encode($attendence)); ?>;
var plot3 = $.jqplot('chart2', [line1], {
title: 'Bar Chart with Point Labels',
seriesDefaults: {renderer: $.jqplot.BarRenderer},
series:[
{pointLabels:{
show: true,
labels:<?php echo json_encode($empNames); ?>
}}],
axes: {
xaxis:{renderer:$.jqplot.CategoryAxisRenderer},
yaxis:{padMax:1.3}}
});
});
</script>
<!-- End code -->
好好享受 :)