Use OFC in your model
I am only now upgrading my OFC plugin version in my Thinkosphere project. Last update was before all the refactoring made by dfl.
From this refactoring until now, you need to add the line ‘include OpenFlashChart’ in your model to properly use OFC. You do not need to do that in your controllers because OFC is already required for all controllers and views thanks to the following lines in init.rb:
require 'open_flash_chart'
ActionView::Base.send :include, OpenFlashChart::View
OpenFlashChart::Base.send :include, OpenFlashChart::View
ActionController::Base.send :include, OpenFlashChart::Controller
ActionController::Base.send :include, OpenFlashChartSo if you want to use OFC in your model (what you should want) simply add the following lines in init.rb:
ActiveRecord::Base.send :include, OpenFlashChart::View
ActiveRecord::Base.send :include, OpenFlashChart::Controller
ActiveRecord::Base.send :include, OpenFlashChartNotice that this will work for your ActiveRecord model not the others.
Then why would you want to use OFC in your model ? Probably because you want to use a “fat model, skinny controller” policy. Doing so will allow you to easily reuse your code needed to prepare the graphs. If you have lots of graphs in your application that will be very efficient.
This code update is currently available on my github fork.
Update: It is now available on OFC home.
This makes me think that a small demonstration of use of OFC with models seems needed in the OFC test app. If someone feels like wanting to do it, do not hesitate ! I am specifically aiming at Anthony Navarre who left a nice code snippet as a comment on Charlie’s blog ;-)
Posted in Ruby on Rails | no comments |
News from Open Flash Chart
In November, Open Flash Chart was updated with some new features and bugfixes. OFC rails plugin was also updated thanks to Charlie, Zuk and Otavio. I have just updated the OFC test app. The list of changes brought by OFC new version is given at the end of this post.
Moreover, some new examples of OFC use are available around:
- 2 from Giovanni: a radar chart with line and a chart with error margin.
- 1 from Anthony Navarre as a comment on Charlie’s blog on how to use a RESTful implementation with OFC.
For reference, here is the list of changes of the latest OFC version:
- Add more font options to the title http://teethgrinder.co.uk/open-flash-chart-2/title.php.
- Moved a lot of the source code files around so they make more sense.
- Added dashed lines, dotted lines in line styles http://teethgrinder.co.uk/open-flash-chart-2/line-dash.php
- saveimage was changed to postimage http://teethgrinder.co.uk/open-flash-chart-2/adv-upload-image.php, this better reflects what the function does.
- Fixed the area chart so it fills correctly.
- Fixed a bug in area hollow so it now will accept point objects.
- utf8 tutorial http://teethgrinder.co.uk/open-flash-chart-2/utf8.php page.
- Better right click text customization (by Greg Barker)
- Hacky little fix for Y Axis labels going off screen when no title or key set (by Jerremy Koot)
- Y Axis Labels http://teethgrinder.co.uk/open-flash-chart-2/y-axis.php, now support vertical text (by Jerremy Koot)
Posted in Ruby on Rails | no comments |
A test and example app for Open Flash Chart Rails plugin
On October 24th, David Lowenfels aka dfl added the beginning of a test app in OFC plugin. As I had myself a local test app used to play around with OFC I thought it would be nice to add my test code in the OFC plugin. This code is actually made mainly by Charlie’s examples taken from his blog and my own examples.
A test app would be useful for 2 main reasons. The first one, as it says, is for testing purposes. The recent changes brought to OFC plugin were great but they were lacking some tests (functional tests and backward compatibility tests). So more tests are definitely necessary. The other reason which is almost more important is the need for working examples. The examples exist on Charlie’s blog and they are very helpful. As they exist why not gather them in an example app ? The examples will be yet more useful and clear.
So first, I worked directly on the app put by dfl in the test directory. But quickly I found it weird to have an application inside a plugin and using this plugin with the application inside etc. You get the kind of recursive situation (not as bad as it seems though!). So I proposed to Charlie to simply make a side test app. For now, the app is here in my repo on github but as it is more natural to put it close to the OFC plugin, Charlie will fork it and the “official” repo will be on his github repo probably here [EDIT: this is done].
[EDIT] If you also have your own OFC test app, and you want to contribute, have a look at this post explaining how to do it with GIT.
Taken from the README :
Both a testing and tutorial application for Rails Open Flash Chart Plugin.
Version 0.2.0
1) Install with:
git clone git://github.com/pullmonkey/ofc_test_app.git 2) Run your server:
script/server -p 3000 3) Go to http://localhost:3000/test_it/all_graphs
You will see all the graphs, like here.
All your comments on how to test a Rails plugin are welcome.
Posted in Ruby on Rails | no comments |
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 : http://www.thinkosphere.com/test_it/index_js_3_line.
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 |
home
about
archives
