2013-12-21 39 views
0

我想將這個庫包含到我的rails應用程序中,我剛開始編輯highcharts原始代碼以使用我的數據填充數據庫,目前我有以下代碼:向Highstock填充數據

視圖內:

<div id="line-basic-1" style="height: 500px; min-width: 500px"></div> 
     <script> 
      $(function() { 

       $.getJSON(<%= @yearly_complaints.to_json.html_safe %>, function(data) { 
        // Create the chart 
        $('#line-basic-1').highcharts('StockChart', { 


         rangeSelector : { 
          selected : 1 
         }, 

         title : { 
          text : 'AAPL Stock Price' 
         }, 

         series : [{ 
          name : 'AAPL', 
          data : data, 
          tooltip: { 
           valueDecimals: 2 
          } 
         }] 
        }); 
       }); 

      }); 
     </script> 

在控制器內部:

def dashboard 
    yearly_chart 
    end 

    def yearly_chart 
    start_year = Date.today.beginning_of_year 
    end_year = Date.today 
    all_tickets = Ticket.all # or Ticket.where(created_at: <from>..<to>) 

    count_for_year = (start_year..end_year).map do |year| 
     [ year, all_tickets.select{|t| t.created_at.to_date == year }.count ] 
    end 
    @yearly_complaints = count_for_year 
    end 

錯誤通過服務器日誌控制檯提出:

Completed 200 OK in 83ms (Views: 58.9ms | ActiveRecord: 2.5ms) 
Invalid request: HTTP element REQUEST_PATH is longer than the 1024 allowed length. 
     /home/dexter/.rvm/gems/ruby-1.9.3-p194/gems/thin-1.6.1/lib/thin/request.rb:84:in `execute' 
     /home/dexter/.rvm/gems/ruby-1.9.3-p194/gems/thin-1.6.1/lib/thin/request.rb:84:in `parse' 
     /home/dexter/.rvm/gems/ruby-1.9.3-p194/gems/thin-1.6.1/lib/thin/connection.rb:41:in `receive_data' 
     /home/dexter/.rvm/gems/ruby-1.9.3-p194/gems/eventmachine-1.0.3/lib/eventmachine.rb:187:in `run_machine' 
     /home/dexter/.rvm/gems/ruby-1.9.3-p194/gems/eventmachine-1.0.3/lib/eventmachine.rb:187:in `run' 
     /home/dexter/.rvm/gems/ruby-1.9.3-p194/gems/thin-1.6.1/lib/thin/backends/base.rb:73:in `start' 
     /home/dexter/.rvm/gems/ruby-1.9.3-p194/gems/thin-1.6.1/lib/thin/server.rb:162:in `start' 
     /home/dexter/.rvm/gems/ruby-1.9.3-p194/gems/rack-1.4.5/lib/rack/handler/thin.rb:13:in `run' 
     /home/dexter/.rvm/gems/ruby-1.9.3-p194/gems/rack-1.4.5/lib/rack/server.rb:268:in `start' 
     /home/dexter/.rvm/gems/ruby-1.9.3-p194/gems/railties-3.2.14/lib/rails/commands/server.rb:70:in `start' 
     /home/dexter/.rvm/gems/ruby-1.9.3-p194/gems/railties-3.2.14/lib/rails/commands.rb:55:in `block in <top (required)>' 
     /home/dexter/.rvm/gems/ruby-1.9.3-p194/gems/railties-3.2.14/lib/rails/commands.rb:50:in `tap' 
     /home/dexter/.rvm/gems/ruby-1.9.3-p194/gems/railties-3.2.14/lib/rails/commands.rb:50:in `<top (required)>' 
     script/rails:6:in `require' 
     script/rails:6:in `<main>' 

Html頁面源:

<div id="line-basic-1" style="height: 500px; min-width: 500px"></div> 
     <script> 
      $(function() { 

       $.getJSON([["2013-01-01",0],["2013-01-02",0],["2013-01-03",0],["2013-01-04",0],["2013-01-05",0],["2013-01-06",0],["2013-01-07",0],["2013-01-08",0],["2013-01-09",0],["2013-01-10",0],["2013-01-11",0],["2013-01-12",0],["2013-01-13",0],["2013-01-14",0],["2013-01-15",0],["2013-01-16",0],["2013-01-17",0],["2013-01-18",0],["2013-01-19",0],["2013-01-20",0],["2013-01-21",0],["2013-01-22",0],["2013-01-23",0],["2013-01-24",0],["2013-01-25",0],["2013-01-26",0],["2013-01-27",0],["2013-01-28",0],["2013-01-29",0],["2013-01-30",0],["2013-01-31",0],["2013-02-01",0],["2013-02-02",0],["2013-02-03",0],["2013-02-04",0],["2013-02-05",0],["2013-02-06",0],["2013-02-07",0],["2013-02-08",0],["2013-02-09",0],["2013-02-10",0],["2013-02-11",0],["2013-02-12",0],["2013-02-13",0],["2013-02-14",0],["2013-02-15",0],["2013-02-16",0],["2013-02-17",0],["2013-02-18",0],["2013-02-19",0],["2013-02-20",0],["2013-02-21",0],["2013-02-22",0],["2013-02-23",0],["2013-02-24",0],["2013-02-25",0],["2013-02-26",0],["2013-02-27",0],["2013-02-28",0],["2013-03-01",0],["2013-03-02",0],["2013-03-03",0],["2013-03-04",0],["2013-03-05",0],["2013-03-06",0],["2013-03-07",0],["2013-03-08",0],["2013-03-09",0],["2013-03-10",0],["2013-03-11",0],["2013-03-12",0],["2013-03-13",0],["2013-03-14",0],["2013-03-15",0],["2013-03-16",0],["2013-03-17",0],["2013-03-18",0],["2013-03-19",0],["2013-03-20",0],["2013-03-21",0],["2013-03-22",0],["2013-03-23",0],["2013-03-24",0],["2013-03-25",0],["2013-03-26",0],["2013-03-27",0],["2013-03-28",0],["2013-03-29",0],["2013-03-30",0],["2013-03-31",0],["2013-04-01",0],["2013-04-02",0],["2013-04-03",0],["2013-04-04",0],["2013-04-05",0],["2013-04-06",0],["2013-04-07",0],["2013-04-08",0],["2013-04-09",0],["2013-04-10",0],["2013-04-11",0],["2013-04-12",0],["2013-04-13",0],["2013-04-14",0],["2013-04-15",0],["2013-04-16",0],["2013-04-17",0],["2013-04-18",0],["2013-04-19",0],["2013-04-20",0],["2013-04-21",0],["2013-04-22",0],["2013-04-23",0],["2013-04-24",0],["2013-04-25",0],["2013-04-26",0],["2013-04-27",0],["2013-04-28",0],["2013-04-29",0],["2013-04-30",0],["2013-05-01",0],["2013-05-02",0],["2013-05-03",0],["2013-05-04",0],["2013-05-05",0],["2013-05-06",0],["2013-05-07",0],["2013-05-08",0],["2013-05-09",0],["2013-05-10",0],["2013-05-11",0],["2013-05-12",0],["2013-05-13",0],["2013-05-14",0],["2013-05-15",0],["2013-05-16",0],["2013-05-17",0],["2013-05-18",0],["2013-05-19",0],["2013-05-20",0],["2013-05-21",0],["2013-05-22",0],["2013-05-23",0],["2013-05-24",0],["2013-05-25",0],["2013-05-26",0],["2013-05-27",0],["2013-05-28",0],["2013-05-29",0],["2013-05-30",0],["2013-05-31",0],["2013-06-01",0],["2013-06-02",0],["2013-06-03",0],["2013-06-04",0],["2013-06-05",0],["2013-06-06",0],["2013-06-07",0],["2013-06-08",0],["2013-06-09",0],["2013-06-10",0],["2013-06-11",0],["2013-06-12",0],["2013-06-13",0],["2013-06-14",0],["2013-06-15",0],["2013-06-16",0],["2013-06-17",0],["2013-06-18",0],["2013-06-19",0],["2013-06-20",0],["2013-06-21",0],["2013-06-22",0],["2013-06-23",0],["2013-06-24",0],["2013-06-25",0],["2013-06-26",0],["2013-06-27",0],["2013-06-28",0],["2013-06-29",0],["2013-06-30",0],["2013-07-01",0],["2013-07-02",0],["2013-07-03",0],["2013-07-04",0],["2013-07-05",0],["2013-07-06",0],["2013-07-07",0],["2013-07-08",0],["2013-07-09",0],["2013-07-10",0],["2013-07-11",0],["2013-07-12",0],["2013-07-13",0],["2013-07-14",0],["2013-07-15",0],["2013-07-16",0],["2013-07-17",0],["2013-07-18",0],["2013-07-19",0],["2013-07-20",0],["2013-07-21",0],["2013-07-22",0],["2013-07-23",0],["2013-07-24",0],["2013-07-25",0],["2013-07-26",0],["2013-07-27",0],["2013-07-28",0],["2013-07-29",0],["2013-07-30",0],["2013-07-31",0],["2013-08-01",0],["2013-08-02",0],["2013-08-03",0],["2013-08-04",0],["2013-08-05",0],["2013-08-06",0],["2013-08-07",0],["2013-08-08",0],["2013-08-09",0],["2013-08-10",0],["2013-08-11",0],["2013-08-12",0],["2013-08-13",0],["2013-08-14",0],["2013-08-15",0],["2013-08-16",0],["2013-08-17",0],["2013-08-18",0],["2013-08-19",0],["2013-08-20",0],["2013-08-21",0],["2013-08-22",0],["2013-08-23",0],["2013-08-24",0],["2013-08-25",0],["2013-08-26",0],["2013-08-27",0],["2013-08-28",0],["2013-08-29",0],["2013-08-30",0],["2013-08-31",0],["2013-09-01",0],["2013-09-02",0],["2013-09-03",0],["2013-09-04",0],["2013-09-05",0],["2013-09-06",0],["2013-09-07",0],["2013-09-08",0],["2013-09-09",0],["2013-09-10",0],["2013-09-11",0],["2013-09-12",0],["2013-09-13",0],["2013-09-14",0],["2013-09-15",0],["2013-09-16",0],["2013-09-17",0],["2013-09-18",0],["2013-09-19",0],["2013-09-20",0],["2013-09-21",0],["2013-09-22",0],["2013-09-23",0],["2013-09-24",0],["2013-09-25",0],["2013-09-26",0],["2013-09-27",0],["2013-09-28",0],["2013-09-29",0],["2013-09-30",0],["2013-10-01",0],["2013-10-02",0],["2013-10-03",0],["2013-10-04",0],["2013-10-05",0],["2013-10-06",0],["2013-10-07",0],["2013-10-08",0],["2013-10-09",0],["2013-10-10",0],["2013-10-11",0],["2013-10-12",0],["2013-10-13",0],["2013-10-14",0],["2013-10-15",0],["2013-10-16",0],["2013-10-17",0],["2013-10-18",0],["2013-10-19",0],["2013-10-20",0],["2013-10-21",0],["2013-10-22",0],["2013-10-23",0],["2013-10-24",0],["2013-10-25",0],["2013-10-26",0],["2013-10-27",0],["2013-10-28",0],["2013-10-29",0],["2013-10-30",0],["2013-10-31",0],["2013-11-01",0],["2013-11-02",0],["2013-11-03",0],["2013-11-04",0],["2013-11-05",0],["2013-11-06",0],["2013-11-07",0],["2013-11-08",0],["2013-11-09",0],["2013-11-10",0],["2013-11-11",0],["2013-11-12",0],["2013-11-13",0],["2013-11-14",0],["2013-11-15",0],["2013-11-16",0],["2013-11-17",0],["2013-11-18",0],["2013-11-19",0],["2013-11-20",0],["2013-11-21",0],["2013-11-22",0],["2013-11-23",0],["2013-11-24",0],["2013-11-25",0],["2013-11-26",0],["2013-11-27",0],["2013-11-28",0],["2013-11-29",0],["2013-11-30",0],["2013-12-01",0],["2013-12-02",0],["2013-12-03",0],["2013-12-04",0],["2013-12-05",0],["2013-12-06",0],["2013-12-07",0],["2013-12-08",0],["2013-12-09",0],["2013-12-10",0],["2013-12-11",0],["2013-12-12",0],["2013-12-13",0],["2013-12-14",0],["2013-12-15",0],["2013-12-16",0],["2013-12-17",0],["2013-12-18",3],["2013-12-19",0],["2013-12-20",1],["2013-12-21",0]], function(data) { 
        // Create the chart 
        $('#line-basic-1').highcharts('StockChart', { 


         rangeSelector : { 
          selected : 1 
         }, 

         title : { 
          text : 'AAPL Stock Price' 
         }, 

         series : [{ 
          name : 'AAPL', 
          data : data, 
          tooltip: { 
           valueDecimals: 2 
          } 
         }] 
        }); 
       }); 

      }); 
     </script> 

回答

2

$.getJSON()第一個參數必須是一個URL(http://api.jquery.com/jQuery.getJSON/)。 因此,您可以先在這裏放入一個響應帶數組的GET請求的URL,或者刪除這個ajax調用,並將<%= @yearly_complaints.to_json.html_safe %>的結果直接放入一個javascript變量(將在data這裏)。

+0

你能告訴我如何將它移動到一個.json文件,所以我可以使用它getjson? –