Created
July 11, 2012 23:14
-
-
Save zyphlar/3094382 to your computer and use it in GitHub Desktop.
Helper and model methods for creating GoogleVisualr charts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# This is a Rails helper for generating a single chart for a column from a data set. | |
# blood_tests is the data set, and column_name is the column from the set to use. | |
# It calls generate_chart_row for each record, passing the column_name argument. | |
# It also does some stuff with vertical axis labels and chart options. | |
def generateChart(blood_tests, column_name) | |
data_table = GoogleVisualr::DataTable.new | |
# Add Column Headers | |
data_table.new_column('string', 'Date' ) | |
data_table.new_column('number', column_name.to_s.capitalize) | |
data_table.new_column('boolean' , nil, nil, 'scope') | |
data_table.new_column('string' , nil, nil, 'tooltip') | |
chart_entries = Array.new | |
# Add Rows and Values | |
blood_tests.each do |test| | |
chart_entries.concat(test.generate_chart_row(column_name)) | |
end | |
data_table.add_rows(chart_entries) | |
if(column_name == :hba1c) then | |
vaxes = [{:format => "#'%'"}] | |
else | |
vaxes = [{:format => "# 'mg/dL'"}] | |
end | |
option = { :width => 800, :height => 100, :areaOpacity => 0, :vAxes => vaxes, :colors => ['red'], :legend => {:position => 'none'}, :title => column_name.to_s.capitalize} | |
chart = GoogleVisualr::Interactive::AreaChart.new(data_table, option) | |
return chart | |
end | |
# END HELPER |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# This goes in the model of the item to be charted. | |
# It's set up for blood tests right now but could do anything based on the column names you specify. | |
# These methods are all called via the helper above. | |
def generate_chart_row(column_name) | |
return [[self.date.to_s, | |
self[column_name], range(column_name), tooltip(column_name)]] | |
end | |
def range(column_name) | |
case column_name | |
when :triglycerides | |
tri_range(self[column_name]) | |
when :cholesterol | |
cho_range(self[column_name]) | |
when :hdl | |
hdl_range(self[column_name]) | |
when :ldl | |
ldl_range(self[column_name]) | |
when :glucose | |
glu_range(self[column_name]) | |
when :hba1c | |
hba_range(self[column_name]) | |
end | |
end | |
def tooltip(column_name) | |
case column_name | |
when :hba1c | |
column_name.to_s.capitalize+':\r\n'+self[column_name].to_s+"%" | |
else | |
column_name.to_s.capitalize+':\r\n'+self[column_name].to_s+" mg/dL" | |
end | |
end | |
def tri_range(val) | |
if(val > 200) | |
return true | |
else | |
return false | |
end | |
end | |
# END MODEL |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!-- This is a Rails view for generating multiple charts, one per column, for a given Rails data set using the helper and model methods below. --> | |
<!-- This all relies on the GoogleVisualr gem. --> | |
<div id='chart1'></div> | |
<div id='chart2'></div> | |
<div id='chart3'></div> | |
<div id='chart4'></div> | |
<div id='chart5'></div> | |
<div id='chart6'></div> | |
<%= render_chart(generateChart(@blood_tests, :triglycerides), 'chart1') %> | |
<%= render_chart(generateChart(@blood_tests, :cholesterol), 'chart2') %> | |
<%= render_chart(generateChart(@blood_tests, :hdl), 'chart3') %> | |
<%= render_chart(generateChart(@blood_tests, :ldl), 'chart4') %> | |
<%= render_chart(generateChart(@blood_tests, :glucose), 'chart5') %> | |
<%= render_chart(generateChart(@blood_tests, :hba1c), 'chart6') %> | |
<!-- END VIEW --> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment