Class DynamicReports::Charts

  1. lib/dynamic_reports/charts.rb
Parent: Object

DynamicReports::Charts

Class used to display different chart types internally.  Charts are generated
using Google Charts API

Public class methods

bar_column_chart (chart, columns, report, orientation)

Method to display a bar or column chart for a given chart definition and report

[show source]
# File lib/dynamic_reports/charts.rb, line 180
      def bar_column_chart(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.options)
        end
      end
line_chart (chart, columns, report)

Method to display a line chart for a given chart definition and report

[show source]
# 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.options)
        end
      end
pie_chart (chart, columns, report)

Method to display a pie chart for a given chart definition and report

[show source]
# 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.options)
        end
      end
random_color ()

Method to select a random color from a list of hex codes

Example: random_color()

> “ff0000“

[show source]
# 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