0

我正在使用lazy_highcharts gem 并尝试配置实时图表。按照此实时图表指南,我在控制器中添加了一个函数,以这种格式呈现 JSON 输出[1340051521000,8]

def live
    x = Time.now.to_i * 1000
    y = Random.rand(11)
    #create an array and echo to JSON
    ret =[x,y]
    x=ActiveSupport::JSON
    @j=x.encode(ret)
    render :json => @j
  end

接下来我有一个名为 live_update.js 的新 JS 文件,我在其中粘贴了步骤 2. 和 3. 包含“requestData()”

我从视图中进行了回调:

 = high_chart("my_id", @h) do |c|
        requestData();

注意:我无法从我的控制器调用它:f.options[:chart][:events][:load] = 'requestData'

当我运行它时,我得到一个错误:chart is not defined,显然它是在 JS 文件中全局定义的。

这是我在浏览器源视图中看到的:

<script type="text/javascript">
            jQuery(function() {
                  // 1. Define JSON options
                  var options = {
                                chart: {"defaultSeriesType":"column","renderTo":"my_id","zoomType":"xy"},
                                        title: {"text":"Test"},
                                        legend: {"layout":"horizontal","style":{}},
                                        xAxis: {"type":"datetime"},
                                        yAxis: {"title":{"text":"Agents"},"labels":{},"min":"0","allowDecimals":false,"gridLineDashStyle":"LongDash"},
                                        tooltip:  {"enabled":true},
                                        credits: {"enabled":false},
                                        plotOptions: {"areaspline":{}},
                                        series: [{"name":"test","data":[]}],
                                        subtitle: {}
                                };

                  // 2. Add callbacks (non-JSON compliant)                
                  requestData();


                  // 3. Build the chart
                    var chart = new Highcharts.Chart(options);
              });
              </script>
4

1 回答 1

0

适用于版本 1.1.6

f.options[:chart][:events] = {
  :load => %| function requestData(){
    $.ajax({
      url:"live",
      success: function(a){
        var c=chart.series[0], d=c.data.length>20;
        chart.series[0].addPoint(a,true,d);
        setTimeout(requestData,16e3)
      }, cache:false
    })
  }|.js_code
}
于 2012-06-27T13:53:25.033 回答