如何在一个页面上创建多个融合图表?

时间:2016-08-05 18:03:54

标签: php mysql fusioncharts

我有以下代码产生这个:

enter image description here

如果你查看我的代码,我实际上是在尝试重新绘制2个图表,并将它们放在单独的表行中,这样它们就会成为另一个表格的顶部。这就是为什么表格线在图表底部聚集的原因。当我删除第一张图表的所有代码时,我的第二张图表显示在网页上,但如果它在那里则不会显示。有什么我想念的吗?

我在想,也许我必须在每个图形之后包含行:dbhandle->close,但这没有多大意义,因为它只是与MySQL数据库的连接。

    <?php

      include("includes/fusioncharts.php");
      // variables declared before
      $dbhandle = new mysqli($hostdb, $userdb, $passdb, $namedb);

       // Render an error message, to avoid abrupt failure, if the database connection parameters are incorrect
       if ($dbhandle->connect_error) {
            exit("There was an error with your connection: ".$dbhandle->connect_error);
       }
    ?>

    <html>
       <head>
        <title>FusionCharts XT - Column 2D Chart - Data from a database</title>
        <link  rel="stylesheet" type="text/css" href="css/style.css" />

        <script src="fusioncharts/js/fusioncharts.js"></script>
        </head>
        <body>

    <?php

        $strQuery = "SELECT Department, SUM(Quantity) AS Quantity FROM Scrap GROUP BY Department ORDER BY Department";

        // Execute the query, or else return the error message.
        $result = $dbhandle->query($strQuery) or exit("Error code ({$dbhandle->errno}): {$dbhandle->error}");

        // If the query returns a valid response, prepare the JSON string
        if ($result) {
                // The `$arrData` array holds the chart attributes and data
                $arrData = array(
                "chart" => array(
                    "caption" => "Scrap by Department",
                    "paletteColors" => "#0075c2",
                    "bgColor" => "#ffffff",
                    "borderAlpha"=> "20",
                    "canvasBorderAlpha"=> "0",
                    "usePlotGradientColor"=> "0",
                    "plotBorderAlpha"=> "10",
                    "showXAxisLine"=> "1",
                    "xAxisLineColor" => "#999999",
                    "showValues"=> "0",
                    "divlineColor" => "#999999",
                    "divLineIsDashed" => "1",
                    "showAlternateHGridColor" => "0"
                )
                );

                $arrData["data"] = array();

        // Push the data into the array

                while($row = mysqli_fetch_array($result)) {
                array_push($arrData["data"], array(
                "label" => $row["Department"],
                "value" => $row["Quantity"],
                //"link" => "deptDrillDown.php?Department=".$row["Department"]
                )
                );
                }

        $jsonEncodedData = json_encode($arrData);

        $columnChart = new FusionCharts("column2D", "myFirstChart" , 600, 300, "chart-1", "json", $jsonEncodedData);

        // Render the chart
        $columnChart->render();
        }

        $strQuery2 = "SELECT ScrapDate, SUM(Quantity) AS Quantity FROM Scrap WHERE Department = 'NW' GROUP BY ScrapDate ORDER BY ScrapDate";

        // Execute the query, or else return the error message.
        $result2 = $dbhandle->query($strQuery2) or exit("Error code ({$dbhandle->errno}): {$dbhandle->error}");

        // If the query returns a valid response, prepare the JSON string
        if ($result2) {
                // The `$arrData` array holds the chart attributes and data
                $arrData2 = array(
                "chart" => array(
                    "caption" => "By Week",
                    "paletteColors" => "#0075c2",
                    "bgColor" => "#ffffff",
                    "borderAlpha"=> "20",
                    "canvasBorderAlpha"=> "0",
                    "usePlotGradientColor"=> "0",
                    "plotBorderAlpha"=> "10",
                    "showXAxisLine"=> "1",
                    "xAxisLineColor" => "#999999",
                    "showValues"=> "0",
                    "divlineColor" => "#999999",
                    "divLineIsDashed" => "1",
                    "showAlternateHGridColor" => "0"
                )
                );

                $arrData2["data"] = array();

        // Push the data into the array

                while($row = mysqli_fetch_array($result2)) {
                array_push($arrData2["data"], array(
                "label" => $row["ScrapDate"],
                "value" => $row["Quantity"],
                //"link" => "deptDrillDown.php?Department=".$row["Department"]
                )
                );
                }

        $jsonEncodedData2 = json_encode($arrData2);

        $columnChart2 = new FusionCharts("column2D", "myFirstChart" , 600, 300, "chart-2", "json", $jsonEncodedData2);

        // Render the chart
        $columnChart2->render();

        // Close the database connection
        $dbhandle->close();

        }
?>


<table border='1'>
<tr>
        <td>
                <div id="chart-1"><!-- Fusion Charts will render here--></div>
        </td>
</tr>
<tr>
        <td>
                <div id="chart-2"><!-- Fusion Charts will also be rendered here--></div>
        </td>
</tr>
</table>
   </body>
</html>

解决: 我查找了FusionCharts对象的构造函数,第二个参数是图表的唯一标识符。由于我之前复制和粘贴,我没想到要改变它。我将其设置为图表2,现在两个图表都正确呈现。

以前是这样的:

$columnChart2 = new FusionCharts("column2D", "myFirstChart" , 600, 300, "chart-2", "json", $jsonEncodedData2);

这是更新的代码:

$columnChart2 = new FusionCharts("column2D", "mySecondChart" , 600, 300, "chart-2", "json", $jsonEncodedData2);

0 个答案:

没有答案