/
graph_builder.rb
265 lines (214 loc) · 7.96 KB
/
graph_builder.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
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
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
105
106
107
108
109
110
111
112
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
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
class GraphBuilder
def self.calculate_and_build_graphs(report)
charts = nil
if report.share_report?
charts, values = self.generate_share_report(report)
elsif report.value_report?
charts, values = self.generate_value_report(report)
else
throw 'Wrong report type'
end
return values, charts
end
private
def self.generate_value_report(report)
charts = {}
labels = Date.get_date_range_labels report.period_start, report.period_end, report.period_division
chart_values = calculate_and_group_values_by_currencies(report)
chart_values.each do |currency, categories|
chart = OpenFlashChart::OpenFlashChart.new
chart.bg_colour = 0xffffff
# title = Title.new("Raport '#{@report.name}' dla waluty #{currency.long_symbol}")
# chart.title = title
min = nil
max = nil
categories.each do |label, values|
graph = get_graph_object report
max ||= values.max
min ||= values.min
max = values.max if max < values.max
min = values.min if min > values.min
graph.values = values
graph.set_key(label,12)
graph.set_tooltip("#key# <br> Wartość: #val##{currency.symbol} <br> #x_label# ")
graph.colour = COLORS[rand(COLORS.size) ]
chart << graph
end
chart.x_axis = get_x_axis_for_value_report(labels)
chart.y_axis = get_y_axis_for_report(min, max)
charts[currency.long_symbol] = chart
end
pure_values = {}
chart_values.each do |currency, v|
pure_values[currency.long_symbol] = {}
pure_values[currency.long_symbol][:title] = "Raport '#{report.name}' dla waluty #{currency.long_symbol} w okresie #{report.period_start} do #{report.period_end}"
pure_values[currency.long_symbol][:values] = v
pure_values[currency.long_symbol][:date_labels] = labels
end
return charts, pure_values
end
def self.calculate_and_group_values_by_currencies(report)
user = report.user
chart_values = {}
if user.multi_currency_balance_calculating_algorithm == :show_all_currencies
currencies = Currency.for_user_period(user, report.period_start, report.period_end)
else
currencies = [user.default_currency]
end
report.calculate_values.each do |result|
cat_label = result[:category].name
if result[:with_subcategories]
cat_label += ' (+podkategorie)'
end
result[:dates].each do |date_range|
currencies.each do |cur|
chart_values[cur] ||= {}
chart_values[cur][cat_label] ||= []
chart_values[cur][cat_label] << result[:values][date_range].value(cur)
end
end
end
chart_values
end
def self.get_x_axis_for_value_report(labels)
x_axis = OpenFlashChart::XAxis.new
x_axis_labels = OpenFlashChart::XAxisLabels.new
x_axis_labels.labels = labels
if labels.size > 4
x_axis_labels.rotate = 'diagonal'
end
if labels.size > 10
x_axis_labels.steps = labels.size/10
end
x_axis.labels = x_axis_labels
x_axis
end
def self.get_y_axis_for_report(min,max, right = false)
y_axis = if right
OpenFlashChart::YAxisRight.new
else
OpenFlashChart::YAxis.new
end
distance = (max - min).abs
distance = 10 if distance < 10
step = 10**( ( Math.log10( distance ) ).ceil() -1)
steps = distance.to_f/step.to_f
step /= case steps
when 1...3 then 4
when 3..6 then 2
else 1
end
liczba = max.abs / step
liczba = liczba.ceil if max >= 0
liczba = liczba.floor if max < 0
max_max = liczba * step
max_max *= -1 if max < 0
liczba = min.abs / step
liczba = liczba.floor if min >= 0
liczba = liczba.ceil if min < 0
min_min = liczba * step
min_min *= -1 if min < 0
y_axis.set_range(min_min, max_max, step)
y_axis
end
#todo - do some refactor here
def self.generate_share_report(report)
depth = if report.depth == -1
:all
else
report.depth
end
values_in_currencies = report.category.calculate_max_share_values report.max_categories_values_count, depth, report.period_start, report.period_end
get_label = lambda {|hash|
if hash[:category].nil?
'Pozostałe'
else
hash[:category].name + (hash[:without_subcategories]?' (bez podkategorii)':'')
end
}
charts = {}
pure_values = {}
values_in_currencies.each do |cur, values|
title = "Raport '#{report.name}' udziału podkategorii w kategorii #{report.category.name} w okresie #{report.period_start} do #{report.period_end} dla waluty #{cur.long_symbol}"
chart = OpenFlashChart::OpenFlashChart.new
chart.bg_colour = 0xffffff
rejected_values = []
unless values.all? {|val| val[:value].value(cur) >= 0 } || values.all? {|val| val[:value].value(cur) <= 0 }
rejected_values, values = values.partition {|val| val[:value].value(cur) < 0 }
end
graph = get_graph_object report
sum = values.sum{|val| val[:value].value(cur)}
pure_values[cur.long_symbol] = {}
pure_values[cur.long_symbol][:values] = []
pure_values[cur.long_symbol][:title] = title
values.each do |category_hash|
value = category_hash[:value].value(cur)
pure_values[cur.long_symbol][:values] << {:label => get_label.call(category_hash), :value => value, :percent => (value/sum*100).round(2)}
end
rejected_values.each do |category_hash|
value = category_hash[:value].value(cur)
pure_values[cur.long_symbol][:values] << {:label => get_label.call(category_hash), :value => value, :percent => 0}
end
pure_values[cur.long_symbol][:sum] = sum
if report.report_view_type == :pie
graph.values = values.map do |val|
OpenFlashChart::PieValue.new(val[:value].value(cur), get_label.call(val))
end
graph.tooltip = "#percent# <br> #label# <br> Wartość: #val##{cur.symbol} z #{sum}#{cur.symbol}"
if values.size > 15
graph.set_no_labels()
end
graph.colours = COLORS
else
graph.values = values.map do |val|
value = val[:value].value(cur)
bv = OpenFlashChart::BarValue.new(value)
bv.set_tooltip("#{(value/sum*100).round(2)}% <br> #x_label# <br> Wartość: #val##{cur.symbol} z #{sum}#{cur.symbol} ")
bv
end
x_axis = OpenFlashChart::XAxis.new
x_axis_labels = OpenFlashChart::XAxisLabels.new
x_axis_labels.labels = values.map {|val| get_label.call(val)}
x_axis_labels.rotate = 'diagonal'
x_axis.labels = x_axis_labels
chart.x_axis = x_axis
max = values.max{|val1, val2| val1[:value].value(cur) <=> val2[:value].value(cur) }[:value].value(cur)
chart.y_axis = get_y_axis_for_report(0, max)
# chart.y_axis_right = get_y_axis_for_report(0, (max/sum)*100, true) //not working well yet :/
y_legend = OpenFlashChart::YLegend.new("Saldo w #{cur.long_symbol}")
y_legend.style = '{font-size: 22px; color: #000000;}'
chart.y_legend = y_legend
end
chart << graph
charts[cur.long_symbol] = chart
end
return charts, pure_values
end
def self.create_empty_graph
chart = OpenFlashChart::OpenFlashChart.new
chart.bg_colour = 0xeeeeee
title = OpenFlashChart::Title.new("\n\n\n\n\nSzukany raport przedawnił się, \n przeładuj stronę.")
title.style = '{font-size: 30px;}'
chart.title = title
chart << OpenFlashChart::Pie.new
chart.to_s
end
def self.get_graph_object(report)
case report.report_view_type
when :bar then OpenFlashChart::Bar.new
when :pie then OpenFlashChart::Pie.new
when :linear then OpenFlashChart::Line.new
end
end
def self.get_colors
colours = []
0x000000.step(0xFFF0F0, 1500) do |num|
colours << "#%x" % num
end
colours.shuffle
end
COLORS = get_colors
###########################
# dobre kolorki do ustawienia: fdd84e, 6886b4, 72ae6e, d1695e, 8a6eaf, efaa43,
# tlo: 4a465a
end