带加载插件PHP和JSON的amchart

时间:2017-08-23 18:18:03

标签: php json amcharts

在脚本中我有以下代码:

<script>
var pie_chart = AmCharts.makeChart("chartpie",{
  "type"    : "pie",
  "depth3D": 15,
  "theme": "light",
    "titleField"  : "category",
  "valueField"  : "column-1",
  "angle":30,
  "minRadius" : 50,
  "labelText": "[[percents]]%",
    "dataLoader": {
    "url": "loadPie.php",
    "format": "json"
    }
});
</script>

loadPie.php中,我有以下代码:

<?php
        // Set proper HTTP response headers
        header( 'Content-Type: application/json' );
        $link  = mysqli_connect('localhost', 'pharmana_general', '123456pharmana', 'pharmana_Hareket_db');
         mysqli_set_charset($link , "utf8"); /* Procedural approach */
        $query = "  SELECT SUM(KDV) AS TotalKDV,Ana_Kategori
                    FROM general_Table
                    GROUP BY Ana_Kategori";
        $result = $link->query( $query );
        // Print out rows
        $data = array();
        while ( $row = $result->fetch_assoc() ) {
          $data[] = $row;
        }
        echo json_encode( $data );
?>

这应该有用,但它不会,它只显示空白区域。关于如何解决这个问题的任何建议?

(我在html中包含了这段代码:

<script src="http://www.amcharts.com/lib/3/plugins/dataloader/dataloader.min.js" type="text/javascript"></script> )

1 个答案:

答案 0 :(得分:1)

您需要确保PHP脚本返回的数据中的字段与图表中的titleFieldvalueField匹配,以便DataLoader插件可以使用饼图。您的PHP脚本似乎返回了包含TotalKDVAna_Kategori的行数组,但您的valueFieldtitleField设置为"column-1""category" }, 分别。假设您的数据如下所示:

[{
  "Ana_Kategory": "Category 1",
  "TotalKDV": 10
},{
  "Ana_Kategory": "Category 2",
  "TotalKDV": 20
}]

您的valueField必须为"TotalKDV"titleField必须为"Ana_Kategori"

var pie_chart = AmCharts.makeChart("chartpie",{
  "type"    : "pie",
  "depth3D": 15,
  "theme": "light",
  "titleField"  : "Ana_Kategori",
  "valueField"  : "TotalKDV",
  "angle":30,
  "minRadius" : 50,
  "labelText": "[[percents]]%",
    "dataLoader": {
    "url": "loadPie.php",
    "format": "json"
    }
});