douhu2131 2015-06-30 07:23
浏览 280

在Chart中显示mysql数据库中的数据

I'm using AmCharts to create a pie chart.
I'm trying to assign data from my mysql database to the variable chartData that has the fields country and liters. How can I assign my mysql data to chartdata?

 <script>
    var chart;
    var legend;



    var chartData = [{
    "country": "Czech Republic",
    "litres": 156.9
    },
    {
    "country": "Ireland",
    "litres": 131.1
    },
    {
    "country": "Germany",
    "litres": 115.8
    },
    {
    "country": "Australia",
    "litres": 109.9
    },
    {
    "country": "Austria",
    "litres": 108.3
    },
    {
    "country": "UK",
    "litres": 65
    },
    {
    "country": "Belgium",
    "litres": 50
    }
    ];

     AmCharts.ready(function () {
     // PIE CHART
    chart = new AmCharts.AmPieChart();
    chart.dataProvider = chartData;
    chart.titleField = "country";
    chart.valueField = "litres";



  // LEGEND
   legend = new AmCharts.AmLegend();
        legend.align = "center";
        legend.markerType = "circle";
        chart.balloonText = "[[title]]<br><span style='font-size:14px'><b>[[value]]</b> ([[percents]]%)</span>";
        chart.addLegend(legend);

        // WRITE
        chart.write("chartdiv");
    });
// changes label position (labelRadius)
function setLabelPosition() {

if (document.getElementById("rb1").checked) {


 chart.labelRadius = 30;


 chart.labelText = "[[title]]: [[value]]";


     } else {


 chart.labelRadius = -30;

    chart.labelText = "[[percents]]%";
    }
    chart.validateNow();
}


 // makes chart 2D/3D


function set3D() {
   if (document.getElementById("rb3").checked) {
        chart.depth3D = 10;
        chart.angle = 10;
    } else {
        chart.depth3D = 0;
        chart.angle = 0;
    }
    chart.validateNow();
}

// changes switch of the legend (x or v)
  function setSwitch() {
    if (document.getElementById("rb5").checked) {
    legend.switchType = "x";
 } else {
   legend.switchType = "v";
    }
 legend.validateNow();

}



     </script>
  • 写回答

2条回答 默认 最新

  • duangan6133 2015-06-30 08:47
    关注

    You cannot directly access your mysql database from javascript. You will have to ask your server for some data. Typicaly, you will want to get JSON formated data (using ajax) :

    Javascript (using .getJSON()):

    $.getJSON('/get-my-data.php', function(json) {
        var chart;
        var legend;
    
        var chartData = json;
    
        AmCharts.ready(function () {
            chart = new AmCharts.AmPieChart();
            chart.dataProvider = chartData;
    
            // code ...
        });
    });
    

    get-my-data.php (using mysqli) :

    $mysqli = new mysqli("localhost", "my_user", "my_password", "my_database");
    
    $stats = array();
    
    $query = "
        SELECT `country`, `litres`
        FROM `mytable`
    ";
    $statement = $mysqli->prepare($query);
    
    $result = $statement->get_result();
    while ($data = $result->fetch_assoc())
    {
        $stats[] = $data;
    }
    
    echo json_encode($stats);
    
    评论

报告相同问题?

悬赏问题

  • ¥15 BP神经网络控制倒立摆
  • ¥20 要这个数学建模编程的代码 并且能完整允许出来结果 完整的过程和数据的结果
  • ¥15 html5+css和javascript有人可以帮吗?图片要怎么插入代码里面啊
  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算