dongwende1984 2014-12-05 18:22
浏览 54
已采纳

HIGHCHARTS PIE - 应用AJAX,REFRESH CHART

Im new in JS/AJAX/JSON, I want to apply ajax to my code so my chart can redraw depend on data gets in database. This line of code has no errors, but not refreshing by itself. Please help me to make this chart dynamic. help/Comments/Suggestions are appreciated.

Here's my code: data.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);
        });



    });   

please help me in my project. thankyou

  • 写回答

1条回答 默认 最新

  • doupeng6890 2014-12-08 15:22
    关注

    You need to update data in that chart, for example:

    $(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
        };
    });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗
  • ¥15 使用esm_msa1_t12_100M_UR50S蛋白质语言模型进行零样本预测时,终端显示出了sequence handled的进度条,但是并不出结果就自动终止回到命令提示行了是怎么回事:
  • ¥15 前置放大电路与功率放大电路相连放大倍数出现问题
  • ¥30 关于<main>标签页面跳转的问题
  • ¥80 部署运行web自动化项目
  • ¥15 腾讯云如何建立同一个项目中物模型之间的联系
  • ¥30 VMware 云桌面水印如何添加
  • ¥15 用ns3仿真出5G核心网网元
  • ¥15 matlab答疑 关于海上风电的爬坡事件检测
  • ¥88 python部署量化回测异常问题