2014-03-03 44 views
0

我在我的散點圖表中顯示數據時出現問題。我試圖將速度作爲x軸並建立y軸。如果我從f.series中刪除變量並放置數據=> [[3,120]],它會起作用,但變量會給我一個空白圖。LazyHighcharts散點圖問題

@planter_speed = PlanterSpeed.find(params[:id]) 

    @data = CSV.open(@planter_speed.file.path, :headers => false, 
          :encoding => 'ISO-8859-1') 
    speed = [] 
    established = [] 
    @data.each do |row| 
    speed << row[0] 
    established << row[1].to_i 
    end 

    @graph = LazyHighCharts::HighChart.new('graph') do |f| 
    f.chart(:height => '300') 
    f.xAxis(:title => {:text => 'Speed km/hr', 
     :margin => 20, style: { color: '#333'}}, :min => 0, :max => 16) 
    f.yAxis [min: 0, max: 120] 
    f.series(:type => 'scatter', :name => 
     '% Established over planted', 
      data: [[speed, established]], :color => '#00463f') 
    f.plotOptions({line: {enableMouseTracking: false}}) 
    f.legend({:align => 'center', :verticalAlign=> 'top', 
      :y => 0, :borderWidth => 0, style: {color: "#333"}}) 
    end 

回答

0

數據需要是像[[x1,y1],[x2,y2]]這樣的點數組。相反,建設速度快,並設立爲單獨的陣列,使數組的數組:

data = [] 
    @data.each do |row| 
    data << [row[0].to_i, row[1].to_i] 
    end 

然後,設置圖表,就像:

data: data 
+0

十分感謝Steakchaser。我還必須將[0] .to_i排列以顯示速度結果。 – DollarChills