Class: HighChartSeries

Inherits:
Object
  • Object
show all
Defined in:
lib/high_chart_series.rb

Constant Summary collapse

PIE_CHART_TYPE =
"pie"

Class Method Summary collapse

Class Method Details

.multi_axis_series(series_name_attributes, attributes_of_interest, chart_data_arr, series_options = [{}, {"type" => "line", "yAxis"=>1}, {"type" => "line", "yAxis"=>2}]) ⇒ Object

for building multiple, non-identical series for combo line, bar charts series_name_attributes - attributes to get “name” keys from attributes_of_interest - array of strings naming the attributes to examine chart_data - the data to build the chart from, an array of arrays, if you will series_options - array of hashes to be passed to single_series



111
112
113
114
115
116
117
118
119
120
# File 'lib/high_chart_series.rb', line 111

def multi_axis_series(series_name_attributes, attributes_of_interest, chart_data_arr, series_options=[{}, {"type" => "line", "yAxis"=>1}, {"type" => "line", "yAxis"=>2}])
  series = []

  chart_data_arr.each_with_index do |chart_data, index|
    inner_series = HighChartSeries.single_series(series_name_attributes[index], attributes_of_interest[index], chart_data, series_options[index])
    series << inner_series
  end

  return series
end

.multi_series(series_name_attribute, attribute_of_interest, chart_data, series_options = {}) ⇒ Object

builds an array of series to make a chart more interesting series_name_attribute - string of method to call to get series name attribute_of_interest - method with the numeric data to build the chart chart_data - array of query results with which to build a series



12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/high_chart_series.rb', line 12

def multi_series(series_name_attribute, attribute_of_interest, chart_data, series_options={})
  series = []

  for datum in chart_data
    if datum.kind_of?(Hash)
      series << self.single_series(datum.fetch(series_name_attribute), attribute_of_interest, datum.fetch(attribute_of_interest))
    else
      series << self.single_series(datum.send(series_name_attribute.to_sym), attribute_of_interest, datum.send(attribute_of_interest.to_sym))
    end
  end

  return series
end

.pie_series(series_name, key, value, chart_series_data) ⇒ Object

generates a hash for a pie chart

{:type => 'pie', :name => "MyChart", :data => [ [ "event1", 1], ["event2", 2] ] }


52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/high_chart_series.rb', line 52

def pie_series(series_name, key, value, chart_series_data)
  final_series = []
  series_data = []

  for datum in chart_series_data
      series_data << [datum.fetch(key), datum.fetch(value)]
  end

  final_series << {:type => PIE_CHART_TYPE, :name => series_name, :data => series_data}

  return final_series
end

.pivot_series(series_name_attribute, attribute_of_interest, chart_data, options = {}) ⇒ Object

accommodates multi-dimensional pivot tables based on Ruport pivot tables also generates your x-axis labels



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/high_chart_series.rb', line 68

def pivot_series(series_name_attribute, attribute_of_interest, chart_data, options={})
  series = []
  x_labels = []
  ignore = nil

  if options && options[:ignore]
    ignore = options[:ignore]
  end

  sample_data_hash = chart_data[0].send(attribute_of_interest.to_sym)
  series_builder = {}

  unless sample_data_hash.is_a?(Hash)
    raise HighChartError, "Please use the multi_series method.  This is not the appropriate chart type for you."
  end

  for datum in chart_data
    data_hash = datum.send(attribute_of_interest)

    data_hash.each do |k, v|
      unless k.to_s == ignore || (ignore && ignore.include?(k.to_s))
        series_builder[k] ||= []
        series_builder[k] << v
      end

      unless x_labels.include?(datum.send(series_name_attribute))
        x_labels << datum.send(series_name_attribute)
      end
    end
  end

  series_builder.each do |k, v|
    series << {:name => k, :data => v}
  end

  return x_labels, series
end

.single_series(series_name, attribute_of_interest, chart_series_data, series_options = {}) ⇒ Object

will generate a hash {:name => “My Wonderful Series”, :data => [0, 1, 2, 3] series_options => used to define individual options for series



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/high_chart_series.rb', line 29

def single_series(series_name, attribute_of_interest, chart_series_data, series_options={})
  series_data = []

  if chart_series_data.kind_of?(Array)
    for datum in chart_series_data
      series_data << datum.send(attribute_of_interest).to_f
    end
  else
    series_data << chart_series_data.to_f
  end

  unless series_options == nil
    final_series = {:name => series_name, :data => series_data}.merge(series_options)
  else
    final_series = {:name => series_name, :data => series_data}
  end


  return final_series
end