LazyHighcharts散点图问题

时间:2014-03-03 04:20:58

标签: ruby-on-rails ruby-on-rails-3.2

我在分散高图表图表中显示数据时出现问题。我试图将速度作为x轴并建立一个y。如果我从f.series中删除变量并放置data => [[3,120]]它有效,但变量给我一个空白图。

  @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

1 个答案:

答案 0 :(得分:0)

数据需要是[[x1,y1],[x2,y2]]之类的点数组。不是建立速度并建立为单独的数组,而是创建一个数组数组:

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

然后,将图表设置为:

data: data