From ec29d4d6c501dc2c86b8c9f8ad94edb3d12487e3 Mon Sep 17 00:00:00 2001 From: Per Cederqvist <ceder@lysator.liu.se> Date: Sun, 20 Aug 2006 13:45:13 +0000 Subject: [PATCH] Generate a gruff graph of the simulation. --- app/controllers/simulation_controller.rb | 33 +++++++++++++++++++ .../simulation_graph_controller.rb | 32 ------------------ app/views/simulation/simulate.rhtml | 3 ++ config/environment.rb | 3 +- config/routes.rb | 2 ++ .../simulation_graph_controller_test.rb | 24 -------------- 6 files changed, 40 insertions(+), 57 deletions(-) delete mode 100644 app/controllers/simulation_graph_controller.rb delete mode 100644 test/functional/simulation_graph_controller_test.rb diff --git a/app/controllers/simulation_controller.rb b/app/controllers/simulation_controller.rb index d64f70d..4ceb44e 100644 --- a/app/controllers/simulation_controller.rb +++ b/app/controllers/simulation_controller.rb @@ -81,6 +81,39 @@ class SimulationController < ApplicationController end + def show + simulate + g = Gruff::Line.new + # Uncomment to use your own theme or font + # See http://colourlovers.com or http://www.firewheeldesign.com/widgets/ for color ideas +# g.theme = { +# :colors => ['#663366', '#cccc99', '#cc6633', '#cc9966', '#99cc99'], +# :marker_color => 'white', +# :background_colors => ['black', '#333333'] +# } +# g.font = File.expand_path('artwork/fonts/VeraBd.ttf', RAILS_ROOT) + + g.title = "Simulation" + + datapoints = [] + labels = {} + @actions.each { |data| + date = data[0] + amount = data[3] + + if date.day == 1 + labels[datapoints.size] = "#{date.year}-#{date.month}" + end + datapoints << amount + } + + g.data("Cash", datapoints) + + g.labels = labels + + send_data(g.to_blob, :disposition => 'inline', :type => 'image/png', :filename => "gruff.png") + end + def time_to_date(t) Date.civil(t.year, t.month, t.day) end diff --git a/app/controllers/simulation_graph_controller.rb b/app/controllers/simulation_graph_controller.rb deleted file mode 100644 index c34b042..0000000 --- a/app/controllers/simulation_graph_controller.rb +++ /dev/null @@ -1,32 +0,0 @@ -class SimulationGraphController < ApplicationController - - # To make caching easier, add a line like this to config/routes.rb: - # map.graph "graph/:action/:id/image.png", :controller => "graph" - # - # Then reference it with the named route: - # image_tag graph_url(:action => 'show', :id => 42) - - def show - g = Gruff::Line.new - # Uncomment to use your own theme or font - # See http://colourlovers.com or http://www.firewheeldesign.com/widgets/ for color ideas -# g.theme = { -# :colors => ['#663366', '#cccc99', '#cc6633', '#cc9966', '#99cc99'], -# :marker_color => 'white', -# :background_colors => ['black', '#333333'] -# } -# g.font = File.expand_path('artwork/fonts/VeraBd.ttf', RAILS_ROOT) - - g.title = "Gruff-o-Rama" - - g.data("Apples", [1, 2, 3, 4, 4, 3]) - g.data("Oranges", [4, 8, 7, 9, 8, 9]) - g.data("Watermelon", [2, 3, 1, 5, 6, 8]) - g.data("Peaches", [9, 9, 10, 8, 7, 9]) - - g.labels = {0 => '2004', 2 => '2005', 4 => '2006'} - - send_data(g.to_blob, :disposition => 'inline', :type => 'image/png', :filename => "gruff.png") - end - -end diff --git a/app/views/simulation/simulate.rhtml b/app/views/simulation/simulate.rhtml index 00280f9..0da42c4 100644 --- a/app/views/simulation/simulate.rhtml +++ b/app/views/simulation/simulate.rhtml @@ -20,6 +20,9 @@ <% end %> </table> +<%= image_tag graph_url(:action => 'show', :enddate => @end_date) %> + + <p>Transactions:</p> <table border="1"> diff --git a/config/environment.rb b/config/environment.rb index 03e9560..bc6d7c4 100644 --- a/config/environment.rb +++ b/config/environment.rb @@ -50,4 +50,5 @@ end # inflect.uncountable %w( fish sheep ) # end -# Include your application configuration below \ No newline at end of file +# Include your application configuration below +require 'gruff' diff --git a/config/routes.rb b/config/routes.rb index 27fcae8..a9d3b97 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -17,6 +17,8 @@ ActionController::Routing::Routes.draw do |map| # instead of a file named 'wsdl' map.connect ':controller/service.wsdl', :action => 'wsdl' + map.graph "simulation/:action/:enddate/image.png", :controller => "simulation" + # Install the default route as the lowest priority. map.connect ':controller/:action/:id' end diff --git a/test/functional/simulation_graph_controller_test.rb b/test/functional/simulation_graph_controller_test.rb deleted file mode 100644 index 378adf7..0000000 --- a/test/functional/simulation_graph_controller_test.rb +++ /dev/null @@ -1,24 +0,0 @@ -require File.dirname(__FILE__) + '/../test_helper' -require 'simulation_graph_controller' - -# Re-raise errors caught by the controller. -class SimulationGraphController; def rescue_action(e) raise e end; end - -class SimulationGraphControllerTest < Test::Unit::TestCase - - #fixtures :data - - def setup - @controller = SimulationGraphController.new - @request = ActionController::TestRequest.new - @response = ActionController::TestResponse.new - end - - # TODO Replace this with your actual tests - def test_show - get :show - assert_response :success - assert_equal 'image/png', @response.headers['Content-Type'] - end - -end -- GitLab