analytics_charts 0.0.6 → 1.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/analytics_charts/custom_module.rb +75 -0
- data/lib/analytics_charts/custom_pie.rb +5 -5
- data/lib/analytics_charts/version.rb +1 -1
- data/lib/analytics_charts.rb +1 -0
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 848a574ba8f5c5be493f38e09db679afdd761f4c
|
4
|
+
data.tar.gz: 77a5ffdf8fdb2bf10d488a5d0c7119ff3182d8cf
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 48126d6bceca1e5b6be4a7fe8836f07f53f76dc0f8d0886ce641c7889983c82f17820f9565200aebc450f0780afb44a8c72817ee8e49466acd3cd85b83b8aab3
|
7
|
+
data.tar.gz: 06010858e65cc939035fd9ba17ec546ce28c0eb560cb5013a5c427b522527fb640387459c98489ab66ad97e4c0d35577c43a6f6c888533020e776167961f7893
|
@@ -0,0 +1,75 @@
|
|
1
|
+
require 'RMagick'
|
2
|
+
|
3
|
+
class AnalyticsCharts::CustomModule
|
4
|
+
include Magick
|
5
|
+
def initialize(image_path, text, output_path)
|
6
|
+
@d = Draw.new
|
7
|
+
@d.fill = 'white'
|
8
|
+
@d.font = 'Helvetica'
|
9
|
+
@d.pointsize = 22
|
10
|
+
@d.font_weight = 500
|
11
|
+
@composite_image = Image.read(image_path)[0]
|
12
|
+
@dummy_image = Image.new(1,1)
|
13
|
+
@composite_columns = @composite_image.columns
|
14
|
+
@composite_rows = @composite_image.rows
|
15
|
+
begin
|
16
|
+
@rows_of_text = tokenize_text_by_lines(text)
|
17
|
+
rescue
|
18
|
+
puts "Had some error in CustomModule, probably a nil calling each"
|
19
|
+
return
|
20
|
+
end
|
21
|
+
@height = @composite_rows + @rows_of_text.size * 22
|
22
|
+
@base_image = Image.new(@composite_columns, @height) {
|
23
|
+
self.background_color = "black"
|
24
|
+
}
|
25
|
+
@base_image.composite!(@composite_image,0,0,OverCompositeOp)
|
26
|
+
#@d.stroke = 'white'
|
27
|
+
@d = @d.stroke_color 'white'
|
28
|
+
@d.stroke_width(1)
|
29
|
+
@d = @d.line(0, @composite_rows, @composite_columns, @composite_rows)
|
30
|
+
@d.draw(@base_image)
|
31
|
+
y_offset = 22 + @composite_rows + @d.get_type_metrics(@dummy_image,"a").height / 2
|
32
|
+
@rows_of_text.each do |text|
|
33
|
+
if text.include? "@$$" # No paragraph break if we insert this uncommonly used word
|
34
|
+
text.sub!("@$$", "")
|
35
|
+
@d.annotate(@base_image, 0 ,0, 22, y_offset, text)
|
36
|
+
next
|
37
|
+
else
|
38
|
+
@d.annotate(@base_image, 0 ,0, 22, y_offset, text)
|
39
|
+
y_offset += 22
|
40
|
+
end
|
41
|
+
end
|
42
|
+
@base_image.write(output_path)
|
43
|
+
end
|
44
|
+
def tokenize_text_by_lines(text)
|
45
|
+
# First split the text by the line carriage element
|
46
|
+
carriage_split_lines = text.split("\r\n")
|
47
|
+
line_tokens = Array.new
|
48
|
+
carriage_split_lines.each do |carriage_split_line|
|
49
|
+
line_wrap_lines = line_wrap(carriage_split_line)
|
50
|
+
line_wrap_lines.each { |line| line_tokens.push(line) }
|
51
|
+
line_tokens.push("\r\n")
|
52
|
+
end
|
53
|
+
line_tokens
|
54
|
+
end
|
55
|
+
def line_wrap(text)
|
56
|
+
tokens = text.split.reverse # Pop stuff off
|
57
|
+
# Safety check, do not allow super long words.
|
58
|
+
tokens.each {|token| return "" if not fits_in_a_line(token) }
|
59
|
+
line_wrap_tokens = Array.new
|
60
|
+
line_builder = ""
|
61
|
+
while tokens.size != 0
|
62
|
+
line_builder = tokens.pop # Pop the first word in a line
|
63
|
+
while tokens.size != 0 and fits_in_a_line(line_builder + " " + tokens.last)
|
64
|
+
line_builder += " " + tokens.pop
|
65
|
+
end
|
66
|
+
line_wrap_tokens.push(line_builder) # Add to list of lines
|
67
|
+
end
|
68
|
+
line_wrap_tokens
|
69
|
+
end
|
70
|
+
|
71
|
+
def fits_in_a_line(text)
|
72
|
+
return @d.get_type_metrics(@dummy_image,text).width < @composite_image.columns - 44
|
73
|
+
end
|
74
|
+
end
|
75
|
+
#Reference: Draw defaults[ font: "Helvetica", pointsize:12, x/y resolution 72 DPI, font_weight: 400]
|
@@ -12,11 +12,11 @@ class AnalyticsCharts::CustomPie
|
|
12
12
|
attr_accessor :label_offset
|
13
13
|
def initialize(image_path, label_hash, pie_label_hash)
|
14
14
|
@base_image = Image.read(image_path)[0]
|
15
|
+
@columns = @base_image.columns
|
16
|
+
@rows = @base_image.rows
|
15
17
|
@d = Draw.new
|
16
18
|
@data = Hash.new # Value is array with two items
|
17
19
|
@aggregate = Array([0,0,0,0]) # Cluster brands into categories
|
18
|
-
@columns = @base_image.columns
|
19
|
-
@rows = @base_image.rows
|
20
20
|
@label_hash = Hash.new
|
21
21
|
@pie_label_hash = Hash.new
|
22
22
|
@label_hash = label_hash if label_hash
|
@@ -56,7 +56,6 @@ class AnalyticsCharts::CustomPie
|
|
56
56
|
|
57
57
|
def draw
|
58
58
|
if @data.size > 0
|
59
|
-
@d = Draw.new
|
60
59
|
@d.stroke_width(@pie_radius)
|
61
60
|
total_sum = 0.0
|
62
61
|
prev_degrees = 60.0
|
@@ -175,13 +174,13 @@ class AnalyticsCharts::CustomPie
|
|
175
174
|
end
|
176
175
|
|
177
176
|
def draw_pie_label(center_x, center_y, angle, radius, percent, index)
|
178
|
-
#気を付けて、get_type_metrics depends on font and pointsize, image res so need to set those first
|
177
|
+
#気を付けて、get_type_metrics depends on font and pointsize, image res, AND font_weight so need to set those first
|
179
178
|
# See more at http://studio.imagemagick.org/RMagick/doc/draw.html#get_type_metrics
|
180
179
|
@d.font = @pie_label_hash['font'] if @pie_label_hash['font']
|
181
180
|
@d.pointsize = @pie_label_hash['pointsize'] if @pie_label_hash['pointsize']
|
182
|
-
width = @d.get_type_metrics(@base_image, percent.to_s).width
|
183
181
|
ascent = @d.get_type_metrics(@base_image, percent.to_s).ascent
|
184
182
|
descent = @d.get_type_metrics(@base_image, percent.to_s).descent
|
183
|
+
width = @d.get_type_metrics(@base_image, percent.to_s).width
|
185
184
|
radians = angle * Math::PI / 180.0
|
186
185
|
x = center_x + radius * Math.cos(radians)
|
187
186
|
# By default, text is centered at bottom, so need to shift vertically to center it
|
@@ -202,6 +201,7 @@ class AnalyticsCharts::CustomPie
|
|
202
201
|
# descent value retrieved is negative, so sub instead of add
|
203
202
|
end
|
204
203
|
@d.align = CenterAlign
|
204
|
+
|
205
205
|
# Provide default fill of black
|
206
206
|
insert_text(x, y, percent, {'fill'=> @colors[index]}.merge(@pie_label_hash))# {'fill'=> 'black', 'font_weight'=> 700, 'pointsize'=>48})
|
207
207
|
end
|
data/lib/analytics_charts.rb
CHANGED
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:
|
4
|
+
version: 1.0.1
|
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-
|
11
|
+
date: 2015-06-10 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -71,6 +71,7 @@ files:
|
|
71
71
|
- analytics_charts.gemspec
|
72
72
|
- appendix.md
|
73
73
|
- lib/analytics_charts.rb
|
74
|
+
- lib/analytics_charts/custom_module.rb
|
74
75
|
- lib/analytics_charts/custom_pie.rb
|
75
76
|
- lib/analytics_charts/version.rb
|
76
77
|
homepage: https://github.com/tryceattack/analytics_charts
|