开发者

How to Line Chart this data?

I would like to create line chart for following data. I would also like to have the ability to hover over each data point to look at the x and y value at that data point. I have the following data structure:

x[0] = [23 4 2 2 4 4 5 3 334 2]

y[0] = [6 24 1 2 2 5 1 3 8 0]

x[1] = [5 6 8 6 3 4 6 3 3]

y[1] = [9 7 8 6 3 4 1 9 2]

x[2] = [6 9 9 6 2 5 8 3]

y[2] = [1 0 2 5 6 2 1 5]

... so that I will have 3 lines on the same chart.

I played with "Seer" without much success. Can anyone开发者_C百科 provide any recommendations / examples / references for plotting similar data using Seer or anything else?

Thanks.


Give the lazy_high_charts gem a try.

#app/views/layouts/appliction.*
= javascript_include_tag 'highcharts.js'

#Gemfile
gem 'lazy_high_charts'

# my_controller#my_action
x_0 = [23, 4, 2, 2, 4, 4, 5, 3, 334, 2]
y_0= [6, 24, 1, 2, 2, 5, 1, 3, 8, 0]

x_1 = [5, 6, 8, 6, 3, 4, 6, 3, 3]
y_1 = [9, 7, 8, 6, 3, 4, 1, 9, 2]

x_2 = [6, 9, 9, 6, 2, 5, 8, 3]
y_2 = [1, 0, 2, 5, 6, 2, 1, 5]


data_0  = x_0.zip(y_0)
data_1 = x_1.zip(y_1)
data_2  = x_2.zip(y_2)

@h = LazyHighCharts::HighChart.new('graph') do |f|
    f.series(:name => "xy0", :data => data_0)
    f.series(:name => "xy1", :data => data_1)
    f.series(:name => "xy3", :data => data_2)
    f.chart({:defaultSeriesType=>"line" })
    f.yAxis(:title => { :text => "y axis values" } )
    f.xAxis(:title => { :text => "x axis values"} )
    f.title(:text => "XY Graph")
    f.plotOptions({}) # override the default values that lazy_high_charts puts there
    f.legend({}) # override the default values 
end

#app/views/my_controller/my_action
= high_chart("chart", @h)

Caveat:

HighCharts is only free for non-commercial use. That may or may not be a dealbreaker for you.


I've really liked jQuery flot for this kind of thing:

http://code.google.com/p/flot/

Check out the example here:

http://flot.googlecode.com/svn/trunk/README.txt

In your controller or view, you can use Ruby's zip to zip together arrays of x and y values if you need to:

> a = [1,2,3]
 => [1, 2, 3] 
> b = [5,6,7]
 => [5, 6, 7] 
> a.zip(b)
 => [[1, 5], [2, 6], [3, 7]]
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜