A Rails/OFC/Ajax example
Following this nice work from Pullmonkey, here is a small example on how to use OFCII to dynamically load a chart. We will display a line chart and then dynamically add one more line.
Controller code (test_it_controller.rb):
class TestItController < ApplicationController
def index_js_3_line
title = Title.new("Multiple Lines")
data1 = [5, 3, 4, 1, 3, 2, 5, 4, 3, 3]
data2 = [12, 9, 9, 7, 8, 8, 9, 9, 8, 9]
data3 = [16, 14, 17, 18, 14, 15, 16, 18, 15, 15]
line_dot = LineDot.new
line_dot.text = "Line Dot"
line_dot.width = 4
line_dot.colour = '#DFC329'
line_dot.dot_size = 5
line_dot.values = data1
line_hollow = LineHollow.new
line_hollow.text = "Line Hollow"
line_hollow.width = 1
line_hollow.colour = '#6363AC'
line_hollow.dot_size = 5
line_hollow.values = data2
line = Line.new
line.text = "Line"
line.width = 1
line.colour = '#5E4725'
line.dot_size = 5
line.values = data3
y = YAxis.new
y.set_range(0,20,5)
x_legend = XLegend.new("MY X Legend")
x_legend.set_style('{font-size: 20px; color: #778877}')
y_legend = YLegend.new("MY Y Legend")
y_legend.set_style('{font-size: 20px; color: #770077}')
chart = OpenFlashChart.new
chart.set_title(title)
chart.set_x_legend(x_legend)
chart.set_y_legend(y_legend)
chart.y_axis = y
chart.add_element(line_dot)
chart.add_element(line_hollow)
@chart = chart
end
def some_server_data_line
title = Title.new("Multiple Lines")
data1 = [5, 3, 4, 1, 3, 2, 5, 4, 3, 3]
data2 = [12, 9, 9, 7, 8, 8, 9, 9, 8, 9]
data3 = [16, 14, 17, 18, 14, 15, 16, 18, 15, 15]
line_dot = LineDot.new
line_dot.text = "Line Dot"
line_dot.width = 4
line_dot.colour = '#DFC329'
line_dot.dot_size = 5
line_dot.values = data1
line_hollow = LineHollow.new
line_hollow.text = "Line Hollow"
line_hollow.width = 1
line_hollow.colour = '#6363AC'
line_hollow.dot_size = 5
line_hollow.values = data2
line = Line.new
line.text = "Line"
line.width = 1
line.colour = '#5E4725'
line.dot_size = 5
line.values = data3
y = YAxis.new
y.set_range(0,20,5)
x_legend = XLegend.new("MY X Legend")
x_legend.set_style('{font-size: 20px; color: #778877}')
y_legend = YLegend.new("MY Y Legend")
y_legend.set_style('{font-size: 20px; color: #770077}')
chart =OpenFlashChart.new
chart.set_title(title)
chart.set_x_legend(x_legend)
chart.set_y_legend(y_legend)
chart.y_axis = y
chart.add_element(line_dot)
chart.add_element(line_hollow)
chart.add_element(line)
render :text => chart.to_s
end
endAnd in the view (index_js_3_line.html.erb):
<html>
<head>
<%= javascript_include_tag :defaults, 'swfobject' %>
</head>
<body>
<%= @chart.js_open_flash_chart_object("my_chart_js_1", 550,300) %>
<br/><br/>
<%= @chart.link_to_ofc_load("Load Original Chart", "my_chart_js_1") %> ||
<%= @chart.link_to_remote_ofc_load("Load Chart from server data", "my_chart_js_1", "/test_it/some_server_data_line") %>
</body>
</html>You can see this example in live here.
Actually there is some “cheating” involved. Indeed we simply load a first chart and then reload a new chart identical with one more line. Next step is to load once the whole graph and to dynamically modify the JSON Data to display one line or another.
H
Posted in Ruby on Rails | no comments | atom
Some News and Tech Links about Ruby and Rails
Some News and Tech Links about Ruby and Rails
You are looking for info about Rails ? Here are some useful links.
News
Railsinside
Rubyinside
Railsenvy
Nuby on rails
locomotivation
peepcode
Stone age blog (in French)
I assume you already know the official website.
API
http://api.rubyonrails.org/
http://www.ruby-doc.org/core-1.8.6/index.html
http://start.gotapi.com/
http://www.railsbrain.com
NB Some people find api.rubyonrails.org hard to use. I just use the google toolbar to make a “search on this website” and I get the info I was looking for in 0.1 sec. Equivalently you can search on google using site:api.rubyonrails.org.
Book
http://pragprog.com/titles/rails3/agile-web-development-with-rails-third-edition
Posted in Ruby on Rails | no comments | atom
Installing Typo : mails
To install Typo I used the experienced user method. Meaning I installed it as if I was developping it. The setup is the same as Thinkosphere website. The code is inside a svn repository and the deployment is done thanks to Capistrano.
Installation was pretty easy until it came to mailing capabilities which were not working. My old friend Google sent me to this blog : locomotivation.com/trackbacks The same tunings solved the problem. Indeed I am also working on Rails 2.x and this problem is due to a modification in Rails2.
However I add some more difficulties. Here is how you can lose 2 hours. The mailing was perfectly working in development environment but not in production. After 2 hours of Rails debugging into the mail sending low level code and a discussion with my host to get the smtp log, the solution appeared clearly : everything was working perfectly… but emails from the production blog were stopped by my spam filter while emails from the dev blog were not ! That’s a tricky situation.
So when working with mails do not forget your antispam !
Posted in Software | no comments | atom
home
about
archives
