Skip to content

Instantly share code, notes, and snippets.

@zamith
Last active August 29, 2015 14:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zamith/489eea929f1ce0dd637c to your computer and use it in GitHub Desktop.
Save zamith/489eea929f1ce0dd637c to your computer and use it in GitHub Desktop.
def index
@plans = Plan.all
@graph = Graph.new(@plans)
end
class Graph
GRAPHS = {
roi: ReturnInvestment,
extra_pay: ExtraPay,
profit: Profit
}
private_constant :GRAPHS
def initialize(plans)
@plans = plans
end
def data_for(graph_identifier = :roi)
GRAPHS[graph_identifier].new.data(@plans)
end
end
class ReturnInvestiment
def data(plans)
plans.each_with_object({}) do |plan, graph_data|
graph_data[plan.name] = plan.return_of_investiment.round(2)
end
end
end
class ExtraPay
def data(plans)
plans.each_with_object({}) do |plan, graph_data|
graph_data[plan.name] = plan.extra_pay_total.round(2)
end
end
end
class Profit
def data(plans)
plans.each_with_object({}) do |plan, graph_data|
graph_data[plan.name] = plan.profit.round(2)
end
end
end
<%= javascript_include_tag "http://www.google.com/jsapi", "chartkick" %>
<div class="container">
<div class="row">
<div class="col-md-offset-2 col-md-8">
<div class="page-header">
<h3>Financials</h3>
</div>
<% if @plans.empty? %>
<div>
No plans available.
</div>
<% else %>
<%= pie_chart @graph.data_for(:profit) %>
<p class="chart-label">Profit by plan chart<p>
<%= column_chart @graph.data_for(:roi) %>
<p class="chart-label">Return of investiment chart<p>
<%= column_chart @graph.data_for(:extra_pay) %>
<p class="chart-label">Extra pay by plan chart<p>
<% end %>
</div>
</div>
</div>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment