Class: DynamicReports::Charts
- Inherits:
-
Object
- Object
- DynamicReports::Charts
- Defined in:
- lib/dynamic_reports/charts.rb
Overview
DynamicReports::Charts
Class used to display different chart types internally. Charts are generated
using Google Charts API
Class Method Summary collapse
-
.bar_column_chart(chart, columns, report, orientation) ⇒ Object
Method to display a bar or column chart for a given chart definition and report.
-
.line_chart(chart, columns, report) ⇒ Object
Method to display a line chart for a given chart definition and report.
-
.pie_chart(chart, columns, report) ⇒ Object
Method to display a pie chart for a given chart definition and report.
-
.random_color ⇒ Object
Method to select a random color from a list of hex codes.
Class Method Details
.bar_column_chart(chart, columns, report, orientation) ⇒ Object
Method to display a bar or column chart for a given chart definition and report
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 |
# File 'lib/dynamic_reports/charts.rb', line 180 def (chart, columns, report, orientation) ::GoogleChart::BarChart.new("#{chart.width}x#{chart.height}", chart.title, orientation, true) do |c| all_data = [] all_labels = [] columns.each do |column| data = [] report.records.each do |record| if record.is_a?(Hash) data << record[column] if record[column].is_a?(Numeric) all_labels << record[chart.label_column.to_s] if chart.label_column elsif record.respond_to?(column.to_sym) data << record.send(column.to_sym) if record.send(column.to_sym).is_a?(Numeric) all_labels << record.send(chart.label_column.to_s) if chart.label_column else data << column if column.is_a?(Numeric) all_labels << chart.label_column.to_s if chart.label_column end end c.data column, data, random_color() unless data.empty? all_data << data end all_data.flatten! if(orientation==:vertical) c.axis :y, :range => [all_data.min,all_data.max], :color => 'ff00ff' unless chart.no_labels else c.axis :x, :range => [all_data.min,all_data.max], :color => 'ff00ff' unless chart.no_labels c.axis :y, :labels => all_labels end c.show_legend = columns.size > 1 return c.to_url(chart.) end end |
.line_chart(chart, columns, report) ⇒ Object
Method to display a line chart for a given chart definition and report
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 |
# File 'lib/dynamic_reports/charts.rb', line 132 def line_chart(chart, columns, report) ::GoogleChart::LineChart.new("#{chart.width}x#{chart.height}", chart.title, false) do |c| all_data = [] columns.each do |column| data = [] report.records.each do |record| if record.is_a?(Hash) data << record[column] if record[column].is_a?(Numeric) elsif record.respond_to?(column.to_sym) data << record.send(column.to_sym) if record.send(column.to_sym).is_a?(Numeric) else data << column if column.is_a?(Numeric) end end c.data column, data, random_color() unless data.empty? all_data << data end all_data.flatten! c.axis :y, :range => [all_data.min,all_data.max], :color => 'ff00ff' unless chart.no_labels c.show_legend = columns.size > 1 return c.to_url(chart.) end end |
.pie_chart(chart, columns, report) ⇒ Object
Method to display a pie chart for a given chart definition and report
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 |
# File 'lib/dynamic_reports/charts.rb', line 158 def pie_chart(chart, columns, report) return if columns.size > 1 || chart.label_column.nil? column = columns.first.to_s ::GoogleChart::PieChart.new("#{chart.width}x#{chart.height}", chart.title, false) do |c| report.records.each do |record| if record.is_a?(Hash) c.data record[chart.label_column.to_s], record[column] if record[column].is_a?(Numeric) elsif record.respond_to?(column.to_sym) c.data record.send(chart.label_column.to_s), record.send(column.to_sym) if record.send(column.to_sym).is_a?(Numeric) else c.data chart.label_column.to_s, column if column.is_a?(Numeric) end end c.show_legend = false c.show_labels = true return c.to_url(chart.) end end |
.random_color ⇒ Object
Method to select a random color from a list of hex codes
Example: random_color()
> “ff0000”
126 127 128 129 |
# File 'lib/dynamic_reports/charts.rb', line 126 def random_color() color_list = %w{000000 0000ff ff0000 ffff00 00ffff ff00ff 00ff00} return color_list[rand(color_list.size)] end |