创建在PHP页面jqplot图 [英] Creating jqplot graph in php page

查看:99
本文介绍了创建在PHP页面jqplot图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试着这样做使用Apache服务器的一个小项目来学习PHP的。我有,我想用我的数据从一个MySQL查询拉来显示条形图jqplot一个PHP页面。我已经有一个查询工作给我我想要的数据。问题是我不知道如何来实现它变成jqplot图。即时猜测我需要做一个AJAX调用,但如果我能避免,我想。我的PHP页面code到目前为止在这里 http://snipt.org/oknnl2 。 在JavaScript的条形图是在这里 http://snipt.org/oknoi3
我想在图表中的管线177 DIV ID = chartdiv这就是我想象像6图表呈现。如果我能得到做这一块一定的帮助,我确定我可以使用相同的工艺打造了别人。

Im trying to learn php by doing a little project using apache server. I have a php page where I want to display a bar chart with jqplot using data i pull from a MySql query. I already have a query working giving me the data i want. The problem is i dont know how to implement this into a jqplot graph. Im guessing i need to make an ajax call but if i can avoid that i would like to. my php page code so far is here http://snipt.org/oknnl2. the javascript for the bar chart is here http://snipt.org/oknoi3.
i want the chart to render in div id=chartdiv thats on line 177. I have to visualize like 6 charts. if i can get some help on doing this one, im sure i can use the same process to build the others.

推荐答案

PHP不能创建JavaScript的阴谋并将其发送给客户端下行,但你没有做一个实际的AJAX调用加载页面后无论是。简单的JavaScript一旦页面加载就足够了。如果你找回你需要在PHP层中的数据,然后可以使它的JavaScript可以在客户端收到的HTML。步骤来做到这一点:

PHP cannot create the javascript plot and send it downstream to the client, but you don't have to make an actual AJAX call after the page is loaded either. Simple javascript once the page loads will suffice. If you retrieve the data you need at the PHP level, you can then make it available to javascript in the HTML received by the client. The steps to make this happen:

  1. 首先,使用PHP来检索您的MySQL数据库所需要的数据。
  2. 然后,输出您检索使用JavaScript的PHP里面的情节数据 code座作为PHP发送到客户端的HTML的一部分。
  3. 执行与在页面加载接种由PHP数据的JavaScript。
  1. First, use PHP to retrieve the data you need from the MySQL database.
  2. Then, output the plot data you retrieved using PHP inside a javascript code block as part of the HTML that PHP sends to the client.
  3. Execute the javascript with the data seeded by PHP on page load.

所以,一个简​​单的例子:

So, a simplified example:

<?php

// Retrieve plot data from mysql
$q = '...';
$r = mysql_query($q);

$plot_row1 = array();
$plot_row2 = array();
$plot_row3 = array();

while ($row = mysql_fetch_array($r)) {
    // append values to $plot_row1, $plot_row2 and $plot_row3 arrays
}

$my_page_title = 'My first PHP/JS Combo Foray';

?>

<html>
<head>
    <script type="text/javascript" src="/scripts/jquery-1.5.2.min.js"></script>
    <script type="text/javascript" src="/scripts/my_plotter.js"></script>
</head>
<body>

<h1><?php echo $my_page_title; ?></h1>

<div id="chartdiv">
    Hold on, javascript is loading the plot ...
</div>

</body>
</html>


<script type="text/javascript">
$(document).ready(function() {

    // we're combining the php array elements into a comma separated list
    // so that when the code is output javascript thinks it's an array.
    // if the $plot_row1 = array(1, 2, 3) then we'd get this:
    // 
    // row1 = [1, 2, 3];
    // 
    // if you needed quotes around the imploded php array values (if they
    // are strings where javascript is concerned) you could do this instead:
    // 
    // row1 = ["<?php echo substr(implode('","', $plot_row1), 0, -2); ?>"];

    row1 = [ <?php echo rtrim(implode(',', $plot_row1), ','); ?> ];
    row2 = [ <?php echo rtrim(implode(',', $plot_row2), ','); ?> ];
    row3 = [ <?php echo rtrim(implode(',', $plot_row3), ','); ?> ];

    // call your js function that creates the plot
    showBrittleness(row1,row2,row3);

    // add whatever js code you need to append the plot to $('#chartdiv')
}
</script>

更新

据的 jqplot文档一个粗略的检查,如果的JavaScript,你从这个链接编辑行12:

According to a cursory examination of the jqplot docs, if you edit line 12 of the javascript you link from this:

var plot1 = $.jqplot('chart1', [s1, s2], {

要这样:

var plot1 = $.jqplot('chartdiv', [s1, s2], {

您的功能应该呈现在chartdivid元素的情节。看来第一个参数$ .jqplot功能的元素在其中创建它...

Your function should render the plot in the 'chartdiv' id element. It seems the first argument to the $.jqplot function is the element in which to create it ...

这篇关于创建在PHP页面jqplot图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆