【问题标题】:HIGHCHARTS PIE - APPLYING AJAX, REFRESH CHARTHIGHCHARTS PIE - 应用 AJAX,刷新图表
【发布时间】:2014-12-05 18:22:40
【问题描述】:

我是 JS/AJAX/JSON 的新手,我想将 ajax 应用到我的代码中,以便我的图表可以根据数据库中的数据重新绘制。这行代码没有报错,只是自己没有刷新。请帮助我使此图表动态化。帮助/评论/建议表示赞赏。

这是我的代码: 数据.php

$<?php
$con = mysql_connect("localhost","root","");

if (!$con) {
die('Could not connect: ' . mysql_error());
}

mysql_select_db("survey_processor", $con);

$result = mysql_query("select msv_variants.var_text, B.Count from msv_variants, (SELECT           ans_var_id ,count(*) AS Count FROM (select ans_var_id from msv_answers where ans_que_id = '11') as A group by ans_var_id) AS B where msv_variants.var_opt_id = B.ans_var_id AND msv_variants.var_que_id = '11' ");

$rows = array();
while($r = mysql_fetch_array($result)) {
$row[0] = $r[0];
$row[1] = $r[1];
array_push($rows,$row);
}

print json_encode($rows, JSON_NUMERIC_CHECK);

mysql_close($con);
?> 

pie.js

    $(document).ready(function() {
        var options = {
            chart: {
                renderTo: 'container',
                plotBackgroundColor: null,
                plotBorderWidth: null,
                plotShadow: false
            },
            title: {
                text: 'Web Sales & Marketing Efforts'
            },
            tooltip: {
                formatter: function() {
                    return '<b>'+ this.point.name +'</b>: '+ this.percentage +' %';
                }
            },
            plotOptions: {
                pie: {
                    allowPointSelect: true,
                    cursor: 'pointer',
                    dataLabels: {
                        enabled: true,
                        color: '#000000',
                        connectorColor: '#000000',
                        formatter: function() {
                            return '<b>'+ this.point.name +'</b>: '+ this.percentage +' %';
                        }
                    }
                }
            },
            series: [{
                type: 'pie',
                name: 'Browser share',
                data: []
            }]
        }

        $.getJSON("data.php", function(json) {
            options.series[0].data = json;
            chart = new Highcharts.Chart(options);
        });



    });   

请在我的项目中帮助我。谢谢你

【问题讨论】:

    标签: javascript php ajax charts highcharts


    【解决方案1】:

    您需要更新该图表中的数据,例如:

    $(document).ready(function() {
        var options = {
            chart: {
                renderTo: 'container',
                plotBackgroundColor: null,
                plotBorderWidth: null,
                plotShadow: false,
                // add code below:
                events: {
                    load: function() {
                         var series = this.series[0];
                         setTimeout(function() {
                             $.getJSON("data.php", function(json) {
                                 series.setData(json);
                             });
                         }
                    }
                }
            },
            //rest of options and code
        };
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-10-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-02-25
      相关资源
      最近更新 更多