dongzhi6927 2017-03-27 12:50
浏览 30

如何动态更新highcharts使用php和ajax?

Welcome!

I'm doing a dynamic curve with highcharts, use ajax returns the data to the series, in the event of chart, use setTimeout to add point to series, now it seems to have a curve, but is will not go ahead

enter image description here

following is the code, I want to know how to make it. like this:

https://www.hcharts.cn/demo/highstock/dynamic-update

Maybe there has no buy and sell in night, there is no curve.

Thank you all for helping.

php code:

/* /index.php/Home/Hq/getdata */

    public function getdata(){
    $goods = I('post.goods');
    $goods = $goods?$goods:'AG';
    $pre = C('DB_PREFIX');
    $model = M();
    $sql = "select id from ".$pre.$goods.' order by id desc limit 0,1';
    //echo($sql);
    $one = $model->query($sql);
    $startid = intval($one[0]['id'])-300;

    $sql = "select mtime,price from ".$pre.$goods.' where id>'.$startid; //选取最近的300条记录

    $list = $model->query($sql);

    foreach($list as $k=>$v){
        $dyn[$k][]=intval($v['mtime'])+8*3600*1000;
        $dyn[$k][]=intval($v['price'])+rand(0,10);/// 这是加个随机数做测试;
    }
   //  echo json_encode($dyn);
   $this->ajaxReturn($dyn);
}

/* /index.php/Home/Hq/getcp */

    public function getcp(){
    ...
    $x = $this->getMillisecond()+8*3600*1000 ;
    $x = sprintf('%-013s', $x);
    $y = intval($cp)+rand(0,10);/// 这是加个随机数做测试;

    $lastone = array($x,$y);
    // echo json_encode($lastone);
    $this->ajaxReturn($lastone);
}

html code:

      $.ajax({
            url: '/index.php/Home/Hq/getdata?goods=' + goodsCode,
            dataType: 'json',
            async: false,
            success: function (result) {
                var data_a = result;
                $('#' + container_id).highcharts('StockChart', {
                    chart: {
                        events: {
                            load: function () {
                                var series = this.series[0];
                                // console.log(series);
                                setInterval(function () {
                                   $.ajax({
                                        url: '/index.php/Home/Hq/getcp',
                                        success: function (point) {
                                                x = point[0];
                                                y = point[1];
                                                series.addPoint([x,y], true, true);
                                        },
                                        cache: false
                                    });
                                },3000);
                            }
                        }
                    },
                    credits: {
                        enabled: false
                    },
                    scrollbar: {
                        enabled: false
                    },

                    series: [{
                        name: '实时行情',
                        data: data_a
                    }],
                    lineWidth: 0.8,
                    navigator: {
                        enabled: false
                    },
                    rangeSelector: {
                        inputEnabled: false,
                        selected: 0,
                        enabled: false
                    },
                    exporting: {
                        enabled: false
                    },
                    xAxis: {
                        labels: {
                            format: '{value:%H:%M}'
                        }
                    },
                    yAxis: {
                        opposite: false,
                        showLastLabel: true
                    },
                    tooltip: {
                        useHTML: true,
                        headerFormat: '<table>',
                        pointFormat: '<tr><td colspan="2">{point.x:%H:%M:%S}</td></tr>' +
                        '<tr><td>数值:</td><td>{point.y}</td></tr>',
                        footerFormat: '</table>',
                        valueDecimals: 2
                    }
                });
            }});
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥100 Jenkins自动化部署—悬赏100元
    • ¥15 关于#python#的问题:求帮写python代码
    • ¥20 MATLAB画图图形出现上下震荡的线条
    • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
    • ¥15 perl MISA分析p3_in脚本出错
    • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
    • ¥15 ubuntu虚拟机打包apk错误
    • ¥199 rust编程架构设计的方案 有偿
    • ¥15 回答4f系统的像差计算
    • ¥15 java如何提取出pdf里的文字?