analytics_charts 0.0.4 → 0.0.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +16 -16
- data/lib/analytics_charts/custom_pie.rb +58 -33
- data/lib/analytics_charts/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 5f62eba893dde452e53244820cf1b40be9113dc5
|
4
|
+
data.tar.gz: ebb5743f68c3f9ee133c98664b10288032a37850
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 200aba6ecbc0c3ef2a84053e0389b046a810ab301b95f84eccc02cebb1dfa96bb97fc1224db272df30c89fd5bfd8028fe741db0a53d0922bd500f38fa7dcb822
|
7
|
+
data.tar.gz: 455a146f16bb27531f03836fb264a1496bf9e78b9d2024aabb4e7721eaecde342da2e163f23a1f1f156658929eadf72d8124c0d17f77b5f23601820b084b1106
|
data/README.md
CHANGED
@@ -1,6 +1,7 @@
|
|
1
1
|
# AnalyticsCharts
|
2
2
|
|
3
3
|
Custom library for a pie chart. Originated from the gruff library, but branched out.
|
4
|
+
History of commits before can be found in my forked gruff repository.
|
4
5
|
|
5
6
|
## Installation
|
6
7
|
|
@@ -18,22 +19,21 @@ Or install it yourself as:
|
|
18
19
|
|
19
20
|
## Usage
|
20
21
|
|
21
|
-
|
22
|
-
require '
|
23
|
-
g = AnalyticsCharts::CustomPie.new('./yourImage.png')
|
24
|
-
hash = {
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
g.
|
32
|
-
g.insert_pie_data("ndaf
|
33
|
-
g.insert_pie_data("ndaf
|
34
|
-
g.insert_pie_data("ndaf
|
35
|
-
g.
|
36
|
-
g.write
|
22
|
+
|
23
|
+
require 'analytics_charts'
|
24
|
+
g = AnalyticsCharts::CustomPie.new('./yourImage.png')
|
25
|
+
hash = {
|
26
|
+
'fill' => 'blue',
|
27
|
+
'font_family' => 'Helvetica',
|
28
|
+
'pointsize' => 64,
|
29
|
+
'font_weight' => 700
|
30
|
+
}
|
31
|
+
g.set_pie_geometry(1242,620, 250)
|
32
|
+
g.insert_pie_data("ndaf me3", 15.1,1)
|
33
|
+
g.insert_pie_data("ndaf me4", 15,3)
|
34
|
+
g.insert_pie_data("ndaf me2", 15.7,2)
|
35
|
+
g.insert_pie_data("ndaf me1", 300.22,0)
|
36
|
+
g.write
|
37
37
|
|
38
38
|
## Contributing
|
39
39
|
|
@@ -5,17 +5,30 @@ class AnalyticsCharts::CustomPie
|
|
5
5
|
attr_accessor :pie_center_x
|
6
6
|
attr_accessor :pie_center_y
|
7
7
|
attr_accessor :pie_radius
|
8
|
-
|
9
|
-
|
8
|
+
attr_accessor :label_hash
|
9
|
+
attr_accessor :pie_label_hash
|
10
|
+
attr_accessor :label_start_x
|
11
|
+
attr_accessor :label_start_y
|
12
|
+
attr_accessor :label_offset
|
13
|
+
def initialize(image_path, label_hash, pie_label_hash)
|
10
14
|
@base_image = Image.read(image_path)[0]
|
11
15
|
@d = Draw.new
|
12
16
|
@data = Hash.new # Value is array with two items
|
13
17
|
@aggregate = Array([0,0,0,0]) # Cluster brands into categories
|
14
18
|
@columns = @base_image.columns
|
15
19
|
@rows = @base_image.rows
|
20
|
+
@label_hash = Hash.new
|
21
|
+
@pie_label_hash = Hash.new
|
22
|
+
@label_hash = label_hash if label_hash
|
23
|
+
@pie_label_hash = pie_label_hash if pie_label_hash
|
16
24
|
set_pie_colors(%w(#AD1F25 #BE6428 #C1B630 #1E753B))
|
17
25
|
end
|
18
26
|
|
27
|
+
def set_label_values(label_start_x, label_start_y, label_offset)
|
28
|
+
@label_start_x = label_start_x
|
29
|
+
@label_start_y = label_start_y
|
30
|
+
@label_offset = label_offset
|
31
|
+
end
|
19
32
|
def set_pie_geometry(x, y, radius)
|
20
33
|
@pie_center_x = x
|
21
34
|
@pie_center_y = y
|
@@ -130,35 +143,42 @@ class AnalyticsCharts::CustomPie
|
|
130
143
|
end
|
131
144
|
|
132
145
|
def draw_labels
|
133
|
-
@d.pointsize = 56
|
134
146
|
@d.align = LeftAlign
|
135
147
|
sorted_data = @data.sort_by{|key,value| -value[1]} # Sort by descending quality
|
136
|
-
x_offset =
|
137
|
-
y_offset =
|
148
|
+
x_offset = @label_start_x
|
149
|
+
y_offset = @label_start_y
|
138
150
|
for data in sorted_data
|
139
151
|
if data[1][0] > 0 # Amount > 0
|
140
152
|
font_weight = 900 # Very Bold
|
141
153
|
text = data[0] + ' <--'
|
142
154
|
else
|
143
155
|
text = data[0]
|
144
|
-
font_weight = 900 #
|
156
|
+
font_weight = 900 # Very Bold
|
145
157
|
end
|
146
158
|
case data[1][1]
|
147
159
|
when 3
|
148
|
-
|
160
|
+
# label_hash gets merged and overrided by fill and font_weight.
|
161
|
+
insert_text(x_offset, y_offset, text,
|
162
|
+
@label_hash.merge({'fill'=> '#1E753B', 'font_weight'=> font_weight }))
|
149
163
|
when 2
|
150
|
-
insert_text(x_offset, y_offset, text,
|
164
|
+
insert_text(x_offset, y_offset, text,
|
165
|
+
@label_hash.merge({'fill'=> '#C1B630', 'font_weight'=> font_weight }))
|
151
166
|
when 1
|
152
|
-
insert_text(x_offset, y_offset, text,
|
167
|
+
insert_text(x_offset, y_offset, text,
|
168
|
+
@label_hash.merge({'fill'=> '#BE6428', 'font_weight'=> font_weight }))
|
153
169
|
when 0
|
154
|
-
insert_text(x_offset, y_offset, text,
|
170
|
+
insert_text(x_offset, y_offset, text,
|
171
|
+
@label_hash.merge({'fill'=> '#AD1F25', 'font_weight'=> font_weight }))
|
155
172
|
end
|
156
|
-
y_offset +=
|
173
|
+
y_offset += @label_offset
|
157
174
|
end
|
158
175
|
end
|
159
176
|
|
160
177
|
def draw_pie_label(center_x, center_y, angle, radius, percent)
|
161
|
-
|
178
|
+
#気を付けて、get_type_metrics depends on font and pointsize, image res so need to set those first
|
179
|
+
# See more at http://studio.imagemagick.org/RMagick/doc/draw.html#get_type_metrics
|
180
|
+
@d.font = @pie_label_hash['font'] if @pie_label_hash['font']
|
181
|
+
@d.pointsize = @pie_label_hash['pointsize'] if @pie_label_hash['pointsize']
|
162
182
|
width = @d.get_type_metrics(@base_image, percent.to_s).width
|
163
183
|
ascent = @d.get_type_metrics(@base_image, percent.to_s).ascent
|
164
184
|
descent = @d.get_type_metrics(@base_image, percent.to_s).descent
|
@@ -181,31 +201,36 @@ class AnalyticsCharts::CustomPie
|
|
181
201
|
y -= (ascent / 2.0 - descent) # descent to account for '$' descent,
|
182
202
|
# descent value retrieved is negative, so sub instead of add
|
183
203
|
end
|
184
|
-
|
185
204
|
@d.align = CenterAlign
|
186
|
-
|
205
|
+
# Provide default fill of black
|
206
|
+
insert_text(x, y, percent, {'fill'=> 'black'}.merge(@pie_label_hash))# {'fill'=> 'black', 'font_weight'=> 700, 'pointsize'=>48})
|
187
207
|
end
|
188
208
|
|
189
209
|
def set_feature(feature, attribute)
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
|
194
|
-
|
195
|
-
|
196
|
-
|
197
|
-
|
198
|
-
|
199
|
-
|
200
|
-
|
201
|
-
|
202
|
-
|
203
|
-
|
204
|
-
|
205
|
-
|
206
|
-
|
207
|
-
|
208
|
-
|
210
|
+
begin
|
211
|
+
case feature
|
212
|
+
when 'fill'
|
213
|
+
@d.fill = attribute
|
214
|
+
when 'font'
|
215
|
+
@d.font = attribute
|
216
|
+
when 'font_family'
|
217
|
+
@d.font_family = attribute
|
218
|
+
when 'font_stretch'
|
219
|
+
@d.font_stretch = attribute
|
220
|
+
when 'font_style'
|
221
|
+
@d.font_style = attribute
|
222
|
+
when 'font_weight'
|
223
|
+
@d.font_weight = attribute
|
224
|
+
when 'stroke'
|
225
|
+
@d.stroke = attribute
|
226
|
+
when 'pointsize'
|
227
|
+
@d.pointsize = attribute
|
228
|
+
when 'text_undercolor'
|
229
|
+
@d.undercolor = attribute
|
230
|
+
end
|
231
|
+
rescue
|
232
|
+
puts "Tried to set #{feature} to #{attribute}"
|
233
|
+
puts $!, $@
|
209
234
|
end
|
210
235
|
end
|
211
236
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: analytics_charts
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- STEPHEN YU
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-05-
|
11
|
+
date: 2015-05-25 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|