doubingjian2006 2015-11-19 17:33
浏览 46
已采纳

C3:读取PHP生成的JSON

I am a newbie to C3 (and .js) but very impressed and excited with what it can do.

I am using PHP to pull a dataset from MySQL per the tutorial on: http://www.d3noob.org/2013/02/using-mysql-database-as-source-of-data.html

I can successfully generate the JSON file in PHP using the following code:

$myquery = "SELECT  `date`, `close` FROM  `data2`";
$query = mysql_query($myquery);

if ( ! $query ) {
   echo mysql_error();
    die;
}

$data = array();

for ($x = 0; $x < mysql_num_rows($query); $x++) {
   $data[] = mysql_fetch_assoc($query);
}

$output=json_encode($data); 
echo $output;
?>

This generates the following JSON:

[{"date":"1-May-12","close":"58.13"},{"date":"30-Apr-12","close":"53.98"},{"date":"27-Apr-12","close":"67.00"},{"date":"26-Apr-12","close":"89.70"},{"date":"25-Apr-12","close":"99.00"},{"date":"24-Apr-12","close":"130.28"},{"date":"23-Apr-12","close":"166.70"},{"date":"20-Apr-12","close":"234.98"},{"date":"19-Apr-12","close":"345.44"},{"date":"18-Apr-12","close":"443.34"},{"date":"17-Apr-12","close":"543.70"},{"date":"16-Apr-12","close":"580.13"},{"date":"13-Apr-12","close":"605.23"},{"date":"12-Apr-12","close":"622.77"},{"date":"11-Apr-12","close":"626.20"},{"date":"10-Apr-12","close":"628.44"},{"date":"9-Apr-12","close":"636.23"},{"date":"5-Apr-12","close":"633.68"},{"date":"4-Apr-12","close":"624.31"},{"date":"3-Apr-12","close":"629.32"},{"date":"2-Apr-12","close":"618.63"},{"date":"30-Mar-12","close":"599.55"},{"date":"29-Mar-12","close":"609.86"},{"date":"28-Mar-12","close":"617.62"},{"date":"27-Mar-12","close":"614.48"},{"date":"26-Mar-12","close":"606.98"}]

My issue is that the c3.js code I have does not render the chart. Not sure whether this is because the JSON is formatted correctly.

This is code I'm using

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>title</title>

<!-- Here are all the javascripts and css that you need, you can  download them or linked them like here -->
<script type="text/javascript" src="d3.js"></script>
<script type="text/javascript" src="c3.js"></script>
<link href="c3.css" rel="stylesheet" type="text/css">

</head>
<body>
<div id="chart"></div>
<script>

var obj2 = ['<?php echo $output; ?>'];


var chart = c3.generate({
       bindto: '#chart',
       data: {
          json: [
          obj2
          ], // or URL
       keys: {
       //                x: 'name', // it's possible to specify 'x' when category axis
       value: ['close '],
       }   
       },
 axis: {
        x: {
       //     type: 'category'
        }
    }
    });
</script>

</body>
</html>

This is the result I get in the chart: c3 blank chart - http://imgur.com/SHPmVOb

Any help would be gratefully appreciated as I feel as this could be a big blocker to learning C3.

Thanks

  • 写回答

1条回答 默认 最新

  • dox90448 2015-11-19 18:23
    关注

    The problem might be that json_encode returns the JSON as a string. So it's not really JSON you are receiving. USe JSON.parse(your data from json_encode); to get a JS JSON object and go from there.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!