通过php从mysql中检索数据以创建flot图

时间:2009-08-21 14:30:17

标签: php flot

您好我正在尝试从mysql数据库中检索数据以创建flot图 任何人都可以带我完成这个程序,或者让我知道该怎么做 感谢

4 个答案:

答案 0 :(得分:22)

你可能想要这样的东西。我没有使用过flot,但我查看了示例here

<?php
//create array of pairs of x and y values
$dataset1 = array();
while ($row = mysql_fetch_assoc()) { //or whatever
    $dataset1[] = array( $row['xvalue'], $row['yvalue'] );
}
?>

<script type="text/javascript">
    //put array into javascript variable
    var dataset1 = <?php echo json_encode($dataset1); ?>;

    //plot
    $(function () {
         $.plot($("#placeholder"), [ dataset1 ]);
    });
</script>

答案 1 :(得分:3)

添加@Tom Haigh的示例:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>Flot Examples</title>
    <link href="layout.css" rel="stylesheet" type="text/css">
    <!--[if lte IE 8]><script language="javascript" type="text/javascript" src="../excanvas.min.js"></script><![endif]-->
    <script language="javascript" type="text/javascript" src="../jquery.js"></script>
    <script language="javascript" type="text/javascript" src="../jquery.flot.js"></script>
 </head>
    <body>
    <h1>Flot Examples</h1>

    <div id="placeholder" style="width:600px;height:300px;"></div>

<?php

$server = "localhost";
    $user="user";
    $password="password";  
    $database = "some_database";

    $connection = mysql_connect($server,$user,$password);
    $db = mysql_select_db($database,$connection);

query = "SELECT x_axis_values, y_axis_values FROM some_table";
    $result = mysql_query($query);        

    while($row = mysql_fetch_assoc($result))
    {
        $dataset1[] = array($row['x_axis_value'],$row['y_axis_value']);
    }

?>


<script type="text/javascript">
$(function () {
    var dataset1 = <?php echo json_encode($dataset1); ?>;

    $.plot($("#placeholder"), [ dataset1 ]);
});
</script>

 </body>
</html>

答案 2 :(得分:2)

正如@Tom Haigh说工作得很好,但是你需要添加另一个代码才能正常工作,我正在使用这个例子,但我发现它在源代码中添加了结果引号“所以为了避免这个,只需添加: intval到数组,例如:

<?php
$query = "SELECT valx, valy FROM chart";
    $result = mysql_query($query);        

    while($row = mysql_fetch_assoc($result))
    {
        $d2[] = array (intval($row['valx']),intval($row['valy']));
    }
?>

答案 3 :(得分:0)

这在很大程度上取决于您的环境和要求。你可以使用很多免费工具。一个例子是Flot,它允许您使用jQuery来构建图形。有关Google代码页上文档的链接。