Class: ActiveReporter::Serializer::Highcharts

Inherits:
Table
  • Object
show all
Defined in:
lib/active_reporter/serializer/highcharts.rb

Instance Attribute Summary

Attributes inherited from Base

#report

Instance Method Summary collapse

Methods inherited from Table

#caption, #each_row, #headers

Methods inherited from Base

#axis_summary, #filter_summary, #human_aggregator_label, #human_aggregator_value_label, #human_category_value_label, #human_dimension_label, #human_dimension_value_label, #human_null_value_label, #human_number_value_label, #human_time_value_label, #initialize, #record_type, #time_formats

Constructor Details

This class inherits a constructor from ActiveReporter::Serializer::Base

Instance Method Details

#aggregator_value(group, aggregator) ⇒ Object

The leaf of report.data is { key: grouper_value, values: [{ key: aggregator, value: y }] }, so pull the requested aggregator’s value out.



89
90
91
92
# File 'lib/active_reporter/serializer/highcharts.rb', line 89

def aggregator_value(group, aggregator)
  entry = Array(group[:values]).detect { |v| v[:key] == aggregator.to_s }
  entry && entry[:value]
end

#categoriesObject



124
125
126
127
128
129
# File 'lib/active_reporter/serializer/highcharts.rb', line 124

def categories
  dimension = report.groupers.first
  dimension.group_values.map do |value|
    human_dimension_value_label(dimension, value)
  end
end

#chart_subtitleObject



135
136
137
# File 'lib/active_reporter/serializer/highcharts.rb', line 135

def chart_subtitle
  filter_summary
end

#chart_titleObject



131
132
133
# File 'lib/active_reporter/serializer/highcharts.rb', line 131

def chart_title
  axis_summary
end

#color_for(dimension, value) ⇒ Object



21
22
23
24
25
# File 'lib/active_reporter/serializer/highcharts.rb', line 21

def color_for(dimension, value)
  # override this if the values of a particular dimension can take on
  # meaningful colors
  color_hash[dimension.name][value]
end

#color_hashObject



10
11
12
13
14
15
16
17
18
19
# File 'lib/active_reporter/serializer/highcharts.rb', line 10

def color_hash
  # ensure we consistently assign the same color to the same dimension-
  # value pair
  @color_hash ||= Hash.new do |h, key|
    color_cycle = colors.cycle
    h[key] = Hash.new do |hh, value|
      hh[value] = color_cycle.next
    end
  end
end

#colorsObject



6
7
8
# File 'lib/active_reporter/serializer/highcharts.rb', line 6

def colors
  ["#7cb5ec", "#434348", "#90ed7d", "#f7a35c", "#8085e9", "#f15c80", "#e4d354", "#2b908f", "#f45b5b", "#91e8e1"]
end

#filters_for(xes) ⇒ Object



118
119
120
121
122
# File 'lib/active_reporter/serializer/highcharts.rb', line 118

def filters_for(xes)
  xes.each_with_object({}) do |(dim, d), h|
    h[dim.name] = d[:key]
  end
end

#highcharts_optionsObject



147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
# File 'lib/active_reporter/serializer/highcharts.rb', line 147

def highcharts_options
  {
    chart: {
      type: "column"
    },
    colors: colors,
    title: {
      text: chart_title
    },
    subtitle: {
      text: chart_subtitle
    },
    series: series,
    xAxis: {
      categories: categories,
      title: {
        text: x_axis_title
      }
    },
    yAxis: {
      allowDecimals: true,
      title: {
        text: y_axis_title
      },
      stackLabels: {
        enabled: report.groupers.length >= 3,
        format: "{stack}",
        rotation: -45,
        textAlign: "left"
      }
    },
    legend: {
      enabled: report.groupers.length >= 2
    },
    tooltip: {},
    plotOptions: {
      series: {
        events: {}
      },
      column: {
        stacking: ("normal" if report.groupers.length >= 2)
      }
    }
  }
end

#seriesObject



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/active_reporter/serializer/highcharts.rb', line 27

def series
  case report.groupers.count
  when 3
    dim1, dim2, dim3 = report.groupers
    report.data.flat_map.with_index do |d3, i|
      d3[:values].flat_map do |d2|
        report.all_aggregators.keys.map do |aggregator|
          name = series_name(human_dimension_value_label(dim2, d2[:key]), aggregator)
          {
            stack: human_dimension_value_label(dim3, d3[:key]),
            name: name,
            (i == 0 ? :id : :linkedTo) => name,
            color: color_for(dim2, d2[:key]),
            data: d2[:values].map do |d1|
              {
                y: aggregator_value(d1, aggregator).to_f,
                tooltip: tooltip_for({ dim1 => d1, dim2 => d2, dim3 => d3 }, aggregator),
                filters: filters_for(dim1 => d1, dim2 => d2, dim3 => d3)
              }
            end
          }
        end
      end
    end
  when 2
    dim1, dim2 = report.groupers
    report.data.flat_map do |d2|
      report.all_aggregators.keys.map do |aggregator|
        {
          name: series_name(human_dimension_value_label(dim2, d2[:key]), aggregator),
          color: color_for(dim2, d2[:key]),
          data: d2[:values].map do |d1|
            {
              y: aggregator_value(d1, aggregator).to_f,
              tooltip: tooltip_for({ dim1 => d1, dim2 => d2 }, aggregator),
              filters: filters_for(dim1 => d1, dim2 => d2)
            }
          end
        }
      end
    end
  when 1
    dim1 = report.groupers.first
    report.all_aggregators.map do |aggregator, aggregator_axis|
      {
        name: human_aggregator_label(aggregator => aggregator_axis),
        data: report.data.map do |d1|
          {
            y: aggregator_value(d1, aggregator).to_f,
            tooltip: tooltip_for({ dim1 => d1 }, aggregator),
            filters: filters_for(dim1 => d1)
          }
        end
      }
    end
  else
    raise ActiveReporter::InvalidParamsError, "report must have <= 3 groupers"
  end
end

#series_name(base, aggregator) ⇒ Object

When a chart plots more than one aggregator we emit a series per aggregator, so disambiguate the series name; for a single aggregator the name stays as just the grouper value (preserving the prior behavior).



97
98
99
100
101
# File 'lib/active_reporter/serializer/highcharts.rb', line 97

def series_name(base, aggregator)
  return base if report.all_aggregators.size <= 1

  "#{base} (#{human_aggregator_label(aggregator => report.all_aggregators[aggregator])})"
end

#tooltip_for(xes, aggregator) ⇒ Object



103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/active_reporter/serializer/highcharts.rb', line 103

def tooltip_for(xes, aggregator)
  lines = []
  xes.each do |dim, d|
    lines << [
      human_dimension_label(dim),
      human_dimension_value_label(dim, d[:key])
    ]
  end
  lines << [
    human_aggregator_label(aggregator => report.all_aggregators[aggregator]),
    human_aggregator_value_label({ aggregator => report.all_aggregators[aggregator] }, aggregator_value(xes[report.groupers.first], aggregator))
  ]
  lines.map { |k, v| "<b>#{k}:</b> #{v}" }.join("<br/>")
end

#x_axis_titleObject



139
140
141
# File 'lib/active_reporter/serializer/highcharts.rb', line 139

def x_axis_title
  human_dimension_label(report.groupers.first)
end

#y_axis_titleObject



143
144
145
# File 'lib/active_reporter/serializer/highcharts.rb', line 143

def y_axis_title
  human_aggregator_label(report.all_aggregators)
end