dthl8036 2017-05-18 12:30
浏览 57
已采纳

以json格式返回的数据未正确显示

I am trying to return data in sample probvided as in highcarts i tried alot but my data output is quite diferent then showing wihtin highcart when i try to pu my data the graph data is not showing up

can anyone help me out

here is sample data showing from high chart

?([
[Date.UTC(2013,5,2),0.7695],
[Date.UTC(2013,5,3),0.7648],
[Date.UTC(2013,5,4),0.7645],
[Date.UTC(2013,5,5),0.7638],
[Date.UTC(2013,5,6),0.7549],
[Date.UTC(2013,5,7),0.7562],
[Date.UTC(2013,5,9),0.7574],

This is follwoing out put of my return data

["[Date.UTC(2012,05,18), 14.38],
","
[Date.UTC(2012,05,21), 14.15],
","
[Date.UTC(2012,05,22), 15.11],
","
[Date.UTC(2012,05,23), 14.96],
","
[Date.UTC(2012,05,24), 14.98],
","
[Date.UTC(2012,05,25), 15.05],
","
[Date.UTC(2012,05,28), 14.8],
","
[Date.UTC(2012,05,29), 14.71],
","
[Date.UTC(2012,05,30), 14.72],",

Here is my code

         $chartData = array();
         foreach ($data as $key => $value) {

            $date = explode('-', $value['date']);
            $new_date = $date[0]. "," .$date[1]. "," .$date[2];
            $value = $value['closing_rate'];
            $datetime = 'Date.'. 'UTC('.$new_date.')';
            $chartData[] = "[$datetime, $value]";
        }
        echo json_encode($chartData, JSON_NUMERIC_CHECK);
  • 写回答

1条回答 默认 最新

  • dpk20361 2017-05-18 13:03
    关注

    The sample data you display in the first block contains Javascript code; Date.UTC() is a call of a method UTC of the Javascript Date class. A JSON cannot contain code, it contains only data.

    In the second listing you generate some strings that contain Javascript code. The JSON is properly decoded in Javascript but all it contains is a list of strings that look like the code in the first listing. You don't need the code but its results.

    Date.UTC() is a static method of the Javascript Date class that computes and returns:

    Return value

    A number representing the number of milliseconds in the given Date object since January 1, 1970, 00:00:00, universal time.

    There are many ways to generate the same value in PHP. The easiest one is to let the DateTime class do the heavy lifting:

    // Use the UTC timezone
    $tz = new DateTimeZone('UTC');
    // Prepare the data here
    $chartData = array();
    foreach ($data as $key => $value) {
        $date = new DateTime($value['date'], $tz);
        // DateTime::format('U') returns the number of seconds since Jan 1, 1970
        // multiply by 1000 to get the number of milliseconds
        $chartData[] = array(1000 * $date->format('U'), $value['closing_rate']);
    }
    
    
    echo json_encode($chartData, JSON_NUMERIC_CHECK);
    

    Remark

    If you are running the code on PHP 7 or newer then you can use DateTime::format('v') to get the number of milliseconds:

    foreach ($data as $key => $value) {
        $date = new DateTime($value['date'], $tz);
        $chartData[] = array($date->format('v'), $value['closing_rate']);
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 孟德尔随机化怎样画共定位分析图
  • ¥18 模拟电路问题解答有偿
  • ¥15 Matlab在app上输入带有矩阵形式的初始条件发生错误
  • ¥15 CST仿真别人的模型结果仿真结果S参数完全不对
  • ¥15 误删注册表文件致win10无法开启
  • ¥15 请问在阿里云服务器中怎么利用数据库制作网站
  • ¥60 ESP32怎么烧录自启动程序
  • ¥50 html2canvas超出滚动条不显示
  • ¥15 java业务性能问题求解(sql,业务设计相关)
  • ¥15 52810 尾椎c三个a 写蓝牙地址