Class: Repor::Serializers::HighchartsSerializer
- Inherits:
-
TableSerializer
- Object
- BaseSerializer
- TableSerializer
- Repor::Serializers::HighchartsSerializer
- Defined in:
- lib/repor/serializers/highcharts_serializer.rb
Instance Attribute Summary
Attributes inherited from BaseSerializer
Instance Method Summary collapse
- #categories ⇒ Object
- #chart_subtitle ⇒ Object
- #chart_title ⇒ Object
- #color_for(dimension, value) ⇒ Object
- #color_hash ⇒ Object
- #colors ⇒ Object
- #filters_for(xes) ⇒ Object
- #highcharts_options ⇒ Object
- #series ⇒ Object
- #tooltip_for(xes) ⇒ Object
- #x_axis_title ⇒ Object
- #y_axis_title ⇒ Object
Methods inherited from TableSerializer
Methods inherited from BaseSerializer
#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 Repor::Serializers::BaseSerializer
Instance Method Details
#categories ⇒ Object
90 91 92 93 94 95 |
# File 'lib/repor/serializers/highcharts_serializer.rb', line 90 def categories dimension = report.groupers.first dimension.group_values.map do |value| human_dimension_value_label(dimension, value) end end |
#chart_subtitle ⇒ Object
101 102 103 |
# File 'lib/repor/serializers/highcharts_serializer.rb', line 101 def chart_subtitle filter_summary end |
#chart_title ⇒ Object
97 98 99 |
# File 'lib/repor/serializers/highcharts_serializer.rb', line 97 def chart_title axis_summary end |
#color_for(dimension, value) ⇒ Object
20 21 22 23 24 |
# File 'lib/repor/serializers/highcharts_serializer.rb', line 20 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_hash ⇒ Object
9 10 11 12 13 14 15 16 17 18 |
# File 'lib/repor/serializers/highcharts_serializer.rb', line 9 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 |
#colors ⇒ Object
4 5 6 7 |
# File 'lib/repor/serializers/highcharts_serializer.rb', line 4 def colors ['#7cb5ec', '#434348', '#90ed7d', '#f7a35c', '#8085e9', '#f15c80', '#e4d354', '#2b908f', '#f45b5b', '#91e8e1'] end |
#filters_for(xes) ⇒ Object
84 85 86 87 88 |
# File 'lib/repor/serializers/highcharts_serializer.rb', line 84 def filters_for(xes) xes.each_with_object({}) do |(dim, d), h| h[dim.name] = d[:key] end end |
#highcharts_options ⇒ Object
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 |
# File 'lib/repor/serializers/highcharts_serializer.rb', line 113 def { 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 |
#series ⇒ Object
26 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 |
# File 'lib/repor/serializers/highcharts_serializer.rb', line 26 def series case report.groupers.count when 3 dim1, dim2, dim3 = report.groupers report.data.flat_map.with_index do |d3, i| d3[:values].map { |d2| { stack: human_dimension_value_label(dim3, d3[:key]), name: human_dimension_value_label(dim2, d2[:key]), (i == 0 ? :id : :linkedTo) => human_dimension_value_label(dim2, d2[:key]), color: color_for(dim2, d2[:key]), data: d2[:values].map { |d1| { y: d1[:value].to_f, tooltip: tooltip_for(dim1 => d1, dim2 => d2, dim3 => d3), filters: filters_for(dim1 => d1, dim2 => d2, dim3 => d3) }} }} end when 2 dim1, dim2 = report.groupers report.data.map { |d2| { name: human_dimension_value_label(dim2, d2[:key]), color: color_for(dim2, d2[:key]), data: d2[:values].map { |d1| { y: d1[:value].to_f, tooltip: tooltip_for(dim1 => d1, dim2 => d2), filters: filters_for(dim1 => d1, dim2 => d2) }} }} when 1 dim1 = report.groupers.first [{ name: human_aggregator_label(report.aggregator), data: report.data.map { |d1| { y: d1[:value].to_f, tooltip: tooltip_for(dim1 => d1), filters: filters_for(dim1 => d1) }} }] else raise Repor::InvalidParamsError, "report must have <= 3 groupers" end end |
#tooltip_for(xes) ⇒ Object
69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/repor/serializers/highcharts_serializer.rb', line 69 def tooltip_for(xes) lines = [] xes.each do |dim, d| lines << [ human_dimension_label(dim), human_dimension_value_label(dim, d[:key]) ] end lines << [ human_aggregator_label(report.aggregator), human_aggregator_value_label(report.aggregator, xes[report.groupers.first][:value]) ] lines.map { |k, v| "<b>#{k}:</b> #{v}" }.join('<br/>') end |
#x_axis_title ⇒ Object
105 106 107 |
# File 'lib/repor/serializers/highcharts_serializer.rb', line 105 def x_axis_title human_dimension_label(report.groupers.first) end |
#y_axis_title ⇒ Object
109 110 111 |
# File 'lib/repor/serializers/highcharts_serializer.rb', line 109 def y_axis_title human_aggregator_label(report.aggregator) end |