高图表实时数据从PHP文件多个系列

时间:2018-02-23 20:39:33

标签: javascript php ajax rest highcharts

我是js和php的新手,我正在尝试从php文件的交换中加载多个btc价格系列的实时图表。我已经尝试过但失败了,我只能加载一个数据系列,它们都是单独工作但是将它们全部加载为图表上的单独点同时使我无法实现。

BITSTAMP.PHP - 结果:[1519417809000,9932.83]

class ViewController: UIViewController {
    var faceView: FaceView?

    // add some code somewhere, maybe in viewDidLoad, to assign something to faceView

    private func updateUI() {
        switch self.expression.eyes {
    case .open:
        self.faceView?.eyesOpen = true
    // ... etc ...
    }
}

chart.js之

<?php
header("Content-type: text/json");
function bitstampbtcusdgetprice($url)

{   $decode = file_get_contents($url);
    return json_decode($decode, true); }

$x = time() * 1000;
$y = 
$btcusdgetprice('https://www.bitstamp.net/api/v2/ticker/btcusd');
$btcusdtickerprice = round(($y["last"]), 2);
$ret = array($x, $btcusdtickerprice);
echo json_encode($ret);
?>

2 个答案:

答案 0 :(得分:0)

我不是Highcharts的专家,但我看到的是你只用点填充chart.series [0],所以只打印一个系列。你必须有每个交易所的系列。

更新

尝试为PHP文件中的每个交换创建一个数组,并通过AJAX接收,然后为chart.series [1]和chart.series [2]创建点。​​

答案 1 :(得分:-1)

得到它,继续更新的代码。我有其他系列我尝试在错误的地方导入。

function requestData() {
$.ajax({
    url: 'php/binance.php',
    success: function(point) {
        var series = chart.series[0],
            shift0 = series.data.length > 50; // shift if the series is 
                                             // longer than 20

        // add the point
        chart.series[0].addPoint(point, true, shift0);

        // call it again after one second   
    },
    cache: false
});
$.ajax({
    url: 'php/bitstamp.php',
    success: function(point) {
        var series = chart.series[1],
            shift1 = series.data.length > 50; // shift if the series is 
                                             // longer than 20

        // add the point
        chart.series[1].addPoint(point, true, shift1);

        // call it again after one second  
    },
    cache: false
});
    $.ajax({
    url: 'php/bittrex.php',
    success: function(point) {
        var series = chart.series[2],
            shift2 = series.data.length > 50; // shift if the series is 
                                             // longer than 20

        // add the point
        chart.series[2].addPoint(point, true, shift2);

        // call it again after one second
        setTimeout(requestData, 10000);    
    },
    cache: false
});
}