FusionCharts组合图表MySQL

时间:2016-03-18 20:18:52

标签: php mysql arrays charts fusioncharts

我被要求创建一个带有2个Y轴和1个行的组合图表(mscolumn3dlinedy根据文档(http://www.fusioncharts.com/dev/chart-attributes.html?chart=mscolumn3dlinedy)可以从MySQL检索数据并且我很难做到这一点。在FusionCharts文档中,有一些使用MySQL和PHP制作图表的示例,但唯一的例子是2d柱形图,其结构非常简单。

我的问题是,在那个示例(http://www.fusioncharts.com/dev/using-with-server-side-languages/php/creating-charts-with-data-from-a-database.html)中,他们使用while迭代器将MySQL数据放入数组并查看我必须制作的组合图表,它应该使用更多数组,或者在至少这就是我的想法。所以我真的不知道如何做这个特定的图表。

我有一个包含6个字段的表: DT(数据时间) TP(int值) OPEN_AMT(int值) IN_AMT(null) OUT_AMT(null) END_AMT(int value)

图表必须看起来像 this

我得到的图表是这个

它是空的,但我从DataBase获取数据。如果你查看我的页面的源代码,你可以看到图表的结构有点乱,但数据就在那里。 (http://sisfacil.cl/securestart/graficos.php

我一直在努力查看文档,但我找不到任何可以帮助我的内容。

图表的代码是这个

<?php 
    $stmt = "SELECT * FROM datos_grafico_01
            WHERE year(DT)=2016
            AND month(DT)=3";

    $result = $mysqli->query($stmt) or exit("Error code ({$mysqli->errno}): {$mysqli->error}");

    if ($result) {
        $arrData = array(
            "chart" => array(
                "caption" => "Merchandise",
                "xAxisname" => "Días de Marzo",
                "pYAxisName" => "$",
                "sYAxisName" => "$",
                "numberPrefix" => "$",
                "sNumberSuffix" => "$",
                "sYAxisMaxValue" => "6000000000",
                "paletteColors" => "#0075c2,#1aaf5d,#f2c500",
                "bgColor" => "#ffffff",
                "showBorder" => "0",
                "showCanvasBorder" => "0",
                "usePlotGradientColor" => "0",
                "plotBorderAlpha" => "10",
                "legendBorderAlpha" => "0",
                "legendBgAlpha" => "0",
                "legendShadow" => "0",
                "showHoverEffect" => "1",
                "valueFontColor" => "#ffffff",
                "rotateValues" => "1",
                "placeValuesInside" => "1",
                "divlineColor" => "#999999",
                "divLineDashed" => "1",
                "divLineDashLen" => "1",
                "divLineGapLen" => "1",
                "canvasBgColor" => "#ffffff",
                "captionFontSize" => "14",
                "subcaptionFontSize" => "14",
                "subcaptionFontBold" => "0"
                )/*
            "categories" => array(
              "category" => array(
                "label" => "2",
                "label" => "3",
                "label" => "4",
                "label" => "5",
                "label" => "6",
                "label" => "7",
                "label" => "8",
                "label" => "9",
                "label" => "10",
                "label" => "11",
                "label" => "12",
                "label" => "13",
                "label" => "14",
                "label" => "15",
                "label" => "16",
                "label" => "17",
                "label" => "18",
                "label" => "19",
                "label" => "20",
                "label" => "21",
                "label" => "22",
                "label" => "23",
                "label" => "24",
                "label" => "25",
                "label" => "26",
                "label" => "27",
                "label" => "28",
                "label" => "29",
                "label" => "30",
                "label" => "31"
                )
            )*/
        );

    $arrData["categories"] = array();
    $cat["category"] = array();

    //Vaciar los datos de mysql en el arreglo
        while($row = mysqli_fetch_array($result)) {
            array_push($cat["category"], array(
                    "label" => $row["DT"]
                )
            );
        }

        array_push($arrData["categories"], array(
                $cat["category"]
            )
        );


    $arrData["dataset"] = array();
    $data1["data"] = array();
    $data2["data"] = array();
    $data3["data"] = array();


    //Vaciar los datos de mysql en el arreglo
        while($row = mysqli_fetch_array($result)) {
            array_push($data1["data"], array(
                    "value" => $row["IN_AMT"]
                    )
            );

            array_push($data2["data"], array(
                    "value" => $row["OUT_AMT"]
                )
            );

            array_push($data3["data"], array(
                    "value" => $row["END_AMT"]
                )
            );
        }

        array_push($arrData["dataset"], array(
            "seriesname" => "INT_AMT",
            $data1["data"],
            "seriesname" => "OUT_AMT",
            $data2["data"],
            "seriesname" => "END_AMT",
            "renderAs"=> "line",
            "parentYAxis"=> "S",
            "showValues"=> "0",
            $data3["data"]
            )
        );

        $jsonEncodedData = json_encode($arrData);

        $myChart = new FusionCharts("mscolumn3dlinedy", "MySQLChart", 800, 600, "chartContainer", "json", $jsonEncodedData);

        $myChart->render();

        $mysqli->close();
    }

?>

请注意,我正在使用数组从下面的数据库中检索第一个类别部分。

我不是特别是编程方面的专家,所以我可以使用任何帮助,特别是图表的结构和代码制作。

例如,结构应该在数据集部分中看起来像这样:

"dataset": [
    {
        "seriesname": "Food Products",
        "data": [
            {
                "value": "11000"
            },
            {
                "value": "14000"
            },
            {
                "value": "10500"
            },
            {
                "value": "15000"
            }
        ]
    },
    {
        "seriesname": "Non-Food Products",
        "data": [
            {
                "value": "14400"
            },
            {
                "value": "14800"
            },
            {
                "value": "8300"
            },
            {
                "value": "11800"
            }
        ]
    },
    {
        "seriesname": "Profit %",
        "renderAs": "line",
        "parentYAxis": "S",
        "showValues": "0",
        "data": [
            {
                "value": "14"
            },
            {
                "value": "16"
            },
            {
                "value": "15"
            },
            {
                "value": "17"
            }
        ]
    }

但我的,在渲染图表时只显示类似

的内容
"dataset":[{"seriesname":"END_AMT","0":[],"1":[],"renderAs":"line","parentYAxis":"S","showValues":"0","2":[]}]}});
        });

SO。如果这是TL; DR文本......你们中的一些人可能会要求简化我的问题。它会是:

我做错了什么,当我渲染我的组合图表时,结构搞砸了并且无法正确加载?

如果您需要一些简短的提示,请指出

  • 我从MySQL检索数据。 (见源代码)
  • 我吮吸数组(可能还有编程)
  • FusionChart文档没有MySQL的任何多重/组合图表示例
  • 我认为问题是我制作的阵列。

修改

我设法解决了这个问题。我刚刚添加了array_value来提取array_push上的值。

    <?php 

    $stmt = "SELECT DAY(DT) as DT, IN_AMT, OUT_AMT, END_AMT FROM datos_grafico_01   WHERE year(DT)=2016 AND month(DT)=3 ORDER BY DT ASC";

    $result = $mysqli->query($stmt) or exit("Error code ({$mysqli->errno}): {$mysqli->error}");

    if ($result) {
        $arrData = array(
            "chart" => array(
                "caption" => "Merchandise",
                "xAxisname" => "Días de Marzo",
                "pYAxisName" => "$",
                "sYAxisName" => "$",
                "numberPrefix" => "$",
                "sNumberSuffix" => "$",
                "paletteColors" => "#0075c2,#1aaf5d,#f2c500",
                "bgColor" => "#ffffff",
                "showBorder" => "0",
                "showCanvasBorder" => "0",
                "usePlotGradientColor" => "0",
                "plotBorderAlpha" => "10",
                "legendBorderAlpha" => "0",
                "legendBgAlpha" => "0",
                "legendShadow" => "0",
                "showHoverEffect" => "1",
                "valueFontColor" => "#ffffff",
                "rotateValues" => "1",
                "placeValuesInside" => "1",
                "divlineColor" => "#999999",
                "divLineDashed" => "1",
                "divLineDashLen" => "1",
                "divLineGapLen" => "1",
                "canvasBgColor" => "#ffffff",
                "captionFontSize" => "14",
                "subcaptionFontSize" => "14",
                "subcaptionFontBold" => "0"
                )
        );

    $arrData["categories"] = array();
    $cat["category"] = array();
    $arrData["dataset"] = array();
    $data1["data"] = array();
    $data2["data"] = array();
    $data3["data"] = array();
    //Vaciar los datos de mysql en el arreglo
        while($row = mysqli_fetch_array($result)) {
            array_push($cat["category"], array(
                    "label" => $row["DT"]
                )
            );
            array_push($data1["data"], array(
                    "value" => $row["IN_AMT"]
                    )
            );
            array_push($data2["data"], array(
                    "value" => $row["OUT_AMT"]
                )
            );
            array_push($data3["data"], array(
                    "value" => $row["END_AMT"]
                )
            );
        }
        array_push($arrData["categories"], array(
                "category" => array_values($cat["category"])
            )
        );
        array_push($arrData["dataset"], array(
            "seriesname" => "IN_AMT",
            "data" => array_values($data1["data"])
            )
        );
        array_push($arrData["dataset"], array(
            "seriesname" => "OUT_AMT",
            "data" => array_values($data2["data"])
            )
        );
        array_push($arrData["dataset"], array(
            "seriesname" => "END_AMT",
            "renderAs"=> "line",
            "parentYAxis"=> "S",
            "showValues"=> "0",
            "data" => array_values($data3["data"])
            )
        );

        $jsonEncodedData = json_encode($arrData);

        $myChart = new FusionCharts("mscolumn3dlinedy", "MySQLChart", 1200, 1000, "chartContainer", "json", $jsonEncodedData);

        $myChart->render();

        $mysqli->close();
    }

?>

有效。另外,我必须更改代码的顺序并简化它,因为我有超过1的时间。无论如何,图表看起来很糟糕,但那是因为表中的数据很乱。

1 个答案:

答案 0 :(得分:0)

您可以参考使用PHP-MySQL创建的多系列图表示例:https://www.dropbox.com/s/ruk3hth9xs6ejou/mschart_php-sql.rar?dl=0

这将使您能够格式化JSON数据,并且对于图表类型的组合,您必须添加相关属性。