FusionCharts无法通过MySQL显示来自MySQL的数据

时间:2014-08-07 12:43:26

标签: php mysql json fusioncharts

我下载了最新版本的FusionCharts。它没有旧版本的文件夹代码。我可以用example显示图表但不使用MySQL。我尝试使用json_encode()但它不显示图表。

这是我的数据库。

CREATE TABLE `customer` (
`CustomerID` varchar(4) NOT NULL,
`Name` varchar(50) NOT NULL,
`Email` varchar(50) NOT NULL,
`CountryCode` varchar(2) NOT NULL,
`Budget` double NOT NULL,
`Used` double NOT NULL,
PRIMARY KEY (`CustomerID`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

-- 
-- Dumping data for table `customer`
-- 

INSERT INTO `customer` VALUES ('C001', 'Win Weerachai', 'win.weerachai@thaicreate.com', 'TH', 1000000, 600000);
INSERT INTO `customer` VALUES ('C002', 'John Smith', 'john.smith@thaicreate.com', 'EN', 2000000, 800000);
INSERT INTO `customer` VALUES ('C003', 'Jame Born', 'jame.born@thaicreate.com', 'US', 3000000, 600000);
INSERT INTO `customer` VALUES ('C004', 'Chalee Angel', 'chalee.angel@thaicreate.com', 'US', 4000000, 100000);

当我运行php json_encode()时,它可以像这样显示

[{"0":"C001","CustomerID":"C001","1":"1000000","Budget":"1000000"},{"0":"C002","CustomerID":"C002","1":"2000000","Budget":"2000000"},{"0":"C003","CustomerID":"C003","1":"3000000","Budget":"3000000"},{"0":"C004","CustomerID":"C004","1":"4000000","Budget":"4000000"}]

但是当我像这段代码一样使用JSON和FusionCharts时,它不显示图表。

<html>
    <head>

        <title>My first chart using FusionCharts Suite XT</title>
        <script type="text/javascript" src="fusioncharts/fusioncharts.js"></script>
        <script type="text/javascript" src="fusioncharts/themes/fusioncharts.theme.zune.js"></script>
        <script type="text/javascript">
            FusionCharts.ready(function(){
        var revenueChart = new FusionCharts({
        type: "column2d",
        renderAt: "chartContainer",
        width: "500",
        height: "300",
        dataFormat: "json",
        dataSource: {
        "chart": {
        "caption": "Monthly revenue for last year",
        "subCaption": "Harry's SuperMart",
        "xAxisName": "Month",
        "yAxisName": "Revenues (In USD)",
        "theme": "zune"
        },

    <?
    $objConnect = mysql_connect("localhost","root","1234") or die("Error Connect to Database");
    $objDB = mysql_select_db("customer");
    $strSQL = "SELECT CustomerID, Budget FROM customer";
    $objQuery = mysql_query($strSQL) or die ("Error Query [".$strSQL."]");

    $json = array();

    while($r=mysql_fetch_array($objQuery)){

    $json[] = $r;

    }

    //Display the JSON data
    echo $json_data=json_encode($json); 

    mysql_close($objConnect);

    ?>

       });
        revenueChart.render("chartContainer");
        });

        </script>
        </head>
        <body>
        <div id="chartContainer">FusionCharts XT will load here!</div>
        </body>
        </html>

如何用PHP将数据从MySQL显示到FusionCharts?

1 个答案:

答案 0 :(得分:0)

FusionCharts已更新其PHP包装器,您可以从链接https://github.com/fusioncharts/php-wrapper

下载它

在您的情况下,有一个非常类似于您的示例,其中使用从数据库获取并转换为JSON格式的数据显示柱形图。 Load chart data for column chart from a database

如果这个答案有帮助,请告诉我。