05 Jan
Open Flash Chart II – X Axis Date and Time
I was asked how to display date and time for the x axis as seen in this teethgrinder example - So here it goes.
Here is the graph we are after in this example:
More Open Flash Chart II examples.
And here is the code (the controller):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
class TestItController < ApplicationController def index @graph = open_flash_chart_object(550,300,"/test_it/chart") end def chart data1 = [] data2 = [] year = Time.now.year 31.times do |i| x = "#{year}-1-#{i+1}".to_time.to_i y = (Math.sin(i+1) * 2.5) + 10 data1 << ScatterValue.new(x,y) data2 << (Math.cos(i+1) * 1.9) + 4 end dot = HollowDot.new dot.size = 3 dot.halo_size = 2 dot.tooltip = "#date:d M y#<br>Value: #val#" line = ScatterLine.new("#DB1750", 3) line.values = data1 line.default_dot_style = dot x = XAxis.new x.set_range("#{year}-1-1".to_time.to_i, "#{year}-1-31".to_time.to_i) x.steps = 86400 labels = XAxisLabels.new labels.text = "#date: l jS, M Y#" labels.steps = 86400 labels.visible_steps = 2 labels.rotate = 90 x.labels = labels y = YAxis.new y.set_range(0,15,5) chart = OpenFlashChart.new title = Title.new(data2.size) chart.title = title chart.add_element(line) chart.x_axis = x chart.y_axis = y render :text => chart, :layout => false end end |
And in your view (index.html.erb):
1 2 3 4 |
<script type="text/javascript" src="/javascripts/swfobject.js"></script> <%= @graph %> |
Good Luck!