无法获取数据显示在图表上

时间:2013-06-14 17:17:06

标签: javascript ajax api highcharts

我在mtgox api的控制台中正确接收数据。但是,我无法将数据显示在我的图表上。我正在使用highcharts。任何帮助表示赞赏。我的代码发布在下面。感谢。

Javascript代码:

$(document).ready(function() {

  $.ajax({
    url: "/chart/ajax_get_chart", // the URL of the controller action method
    dataType: "json",
    type: "GET",
    success: function(result) {
      var result = JSON.parse(result);
      var date = new Array();
      var price = new Array();
      for (var i = 0; i < result.length; i++) {
        date.push(result[i]['date']);
        price.push(result[i]['price']);
      }

      $(function () {
        console.log(date);
        $('#container').highcharts({
            chart: {
                type: 'line',
                marginRight: 130,
                marginBottom: 25
            },
            title: {
                text: 'Bitcoin Price',
                x: -20 //center
            },
            subtitle: {
                text: 'Source: MtGox.com',
                x: -20
            },
            xAxis: {
                categories: 'datetime'
            },
            yAxis: {
                title: {
                    text: 'Price'
                },
                plotLines: [{
                    value: 'Price',
                    width: 1,
                    color: '#808080'
                }]
            },
            tooltip: {
                valueSuffix: '°C'
            },
            legend: {
                layout: 'vertical',
                align: 'right',
                verticalAlign: 'top',
                x: -10,
                y: 100,
                borderWidth: 0
            },
            series: [{
                name: 'Bitcoin',
                data: price
            }]
        });
      });
    }  
  });

}); 

型号:

public function ajax_get_chart() {

    $quotes = $this->rest->get('api/1/BTCUSD/trades/fetch');

    $series_data = array();

    $results = $quotes->return;

    foreach ($results as $quote)
    {
      $series_tmp = array(
        'date'      =>  $quote->date,
        'price'     =>  $quote->price
      );

      $series_data[] = $series_tmp;
    }

    return json_encode($series_data);
  }


}

1 个答案:

答案 0 :(得分:0)

尝试更改此内容:

$('#container').highcharts({
    chart: {
        type: 'line',
        marginRight: 130,
        marginBottom: 25
    },

到此:

var chart = new Highcharts.Chart({
    chart: {
        renderTo: 'container'
        type: 'line',            
        marginRight: 130,
        marginBottom: 25
    },
相关问题