igs_pie_chart 0.0.7 → 0.0.8

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,104 @@
1
+ class Igs::PieChart
2
+ attr_reader :title,:size,:endomargin,:target_element,:data
3
+ # data=[100,299,234,55]
4
+ def initialize(title,size,endomargin=0,target_element='body',data)
5
+ @title = title
6
+ @size = size
7
+ @endomargin = endomargin
8
+ @data = data.values
9
+ @labels = data.keys
10
+ @target_element = target_element
11
+ @default_attribution=false
12
+ end
13
+
14
+ def script
15
+ begin
16
+ default_attribution
17
+
18
+ path = File.expand_path("../../../templates/_script.html.erb", __FILE__)
19
+ output = ERB.new(File.read(path)).result(binding)
20
+ rescue Exception => e
21
+ STDERR.puts "Erro ao renderizar JavaScript! #{e}"
22
+ output = nil
23
+ end
24
+ return output
25
+ end
26
+
27
+ def style
28
+ begin
29
+ default_attribution
30
+
31
+ path = File.expand_path("../../../templates/_style.css.erb", __FILE__)
32
+ output = ERB.new(File.read(path)).result(binding)
33
+ rescue Exception => e
34
+ STDERR.puts "Erro ao renderizar CSS! #{e}"
35
+ output = nil
36
+ end
37
+ return output
38
+ end
39
+
40
+ def labels
41
+ begin
42
+ default_attribution
43
+
44
+ path = File.expand_path("../../../templates/_labels.html.erb", __FILE__)
45
+ output = ERB.new(File.read(path)).result(binding)
46
+ rescue Exception => e
47
+ STDERR.puts "Erro ao renderizar Labels! #{e}"
48
+ output = nil
49
+ end
50
+ return output
51
+ end
52
+
53
+ def render
54
+ rendering = '<style>'
55
+ rendering+= self.style
56
+ rendering+= '</style>'
57
+ rendering+= self.script
58
+ rendering+= self.labels
59
+
60
+ return rendering
61
+ end
62
+
63
+ def to_s
64
+ render
65
+ end
66
+
67
+ def data=(data)
68
+ @data=data
69
+ end
70
+
71
+ def width=(width)
72
+ @width = width
73
+ end
74
+
75
+ def height=(height)
76
+ @height = height
77
+ end
78
+
79
+ private
80
+ def default_attribution
81
+
82
+ if @default_attribution == true
83
+ return
84
+ end
85
+
86
+ @default_attribution=true
87
+ #Default value attribution
88
+ dsize = 300
89
+ @width = dsize if @width == nil
90
+ @height = dsize if @height == nil
91
+ @endomargin = 0 if @endomargin == nil
92
+ @width = @height = @size if @size!=nil
93
+
94
+ if @data==nil
95
+ @data=[1,1,1]
96
+ end
97
+
98
+ if @endomargin >= 1
99
+ @endomargin = 0
100
+ STDERR.puts "@endomargin must be less than 1"
101
+ end
102
+ end
103
+
104
+ end
data/lib/igs_pie_chart.rb CHANGED
@@ -1,42 +1,31 @@
1
- #encoding: utf-8
2
- require 'erb'
3
-
4
- class IgsPieChart
5
-
6
- # data=[100,299,234,55]
7
- def initialize(size,endomargin=0,target_element='body',data)
8
- @size = size
9
- @endomargin = endomargin
10
- @data = data.values
11
- @labels = data.keys
12
- @target_element = target_element
13
- end
14
-
15
- def render
16
- begin
17
- path = File.expand_path("../../templates/pie_chart.html.erb", __FILE__)
18
- rendering = ERB.new(File.read(path)).result(binding)
19
- rescue Exception => e
20
- STDERR.puts "Erro ao renderizar JavaScript! #{e}"
21
- rendering = nil
22
- end
23
- return rendering
1
+ begin
2
+ mod = Required::Module::const_get "Igs"
3
+ #It exists
4
+ rescue NameError
5
+ module Igs
24
6
  end
7
+ end
25
8
 
26
- def to_s
27
- render
9
+ begin
10
+ if Rails.version.split('.').first.to_i >= 3
11
+ require 'd3_rails'
12
+ else
13
+ STDERR.puts "IGS Pie Chart does not support Rails versions below 3.x"
28
14
  end
15
+ rescue NameError
16
+ #TODO: change this to use a logging api
17
+ puts "Not a Rails application, nothing to do here!"
18
+ end
29
19
 
30
- def data=(data)
31
- @data=data
32
- end
33
-
34
- def width=(width)
35
- @width = width
36
- end
20
+ #encoding: utf-8
21
+ require 'erb'
22
+ require 'igs_pie_chart/pie_chart'
37
23
 
38
- def height=(height)
39
- @height = height
24
+ unless "".respond_to? 'camelize'
25
+ class String
26
+ def camelize
27
+ return self if self !~ /_/ && self =~ /[A-Z]+.*/
28
+ split('_').map{|e| e.capitalize}.join
29
+ end
40
30
  end
41
-
42
- end
31
+ end
@@ -0,0 +1,14 @@
1
+ <ul class="labels">
2
+ <% @labels.each_with_index do |l,i| %>
3
+ <% l = l.gsub('_',' ') %>
4
+ <li>
5
+ <span id="<%=@target_element%>_color_<%=i%>">
6
+ &nbsp;<%= @data[i] %>&nbsp;
7
+ </span>
8
+ <%= l.to_s.capitalize %>
9
+ <script type="text/javascript">
10
+ document.getElementById('<%=@target_element%>_color_<%=i%>').setAttribute('style','background-color:'+color(<%=i%>)+';');
11
+ </script>
12
+ </li>
13
+ <% end %>
14
+ </ul>
@@ -0,0 +1,36 @@
1
+ <script type="text/javascript">
2
+
3
+ var width = <%=@width%>,height = <%=@height%>,
4
+ outerRadius = Math.min(width, height) / 2,
5
+ innerRadius = outerRadius * <%= @endomargin%>,
6
+ data = <%=@data%>,
7
+ color = d3.scale.category20(),
8
+ donut = d3.layout.pie(),
9
+ arc = d3.svg.arc().innerRadius(innerRadius).outerRadius(outerRadius);
10
+
11
+ var vis = d3.select("<%=@target_element%>")
12
+ .append("svg")
13
+ .data([data])
14
+ .attr("width", width)
15
+ .attr("height", height);
16
+
17
+ var arcs = vis.selectAll("g.arc")
18
+ .data(donut)
19
+ .enter().append("g")
20
+ .attr("class", "arc")
21
+ .attr("transform", "translate(" + outerRadius + "," + outerRadius + ")");
22
+
23
+ arcs.append("path")
24
+ .attr("fill", function(d, i) { return color(i); })
25
+ .attr("d", arc);
26
+
27
+ arcs.append("text")
28
+ .attr("transform", function(d) { return "translate(" + arc.centroid(d) + ")"; })
29
+ .attr("dy", ".35em")
30
+ .attr("class", "pie-chart-text-label")
31
+ .attr("text-anchor", "middle")
32
+ .attr("style", "stroke-opacity:0.0;fill:#FFFFFF;font-weight:600;")
33
+ .attr("display", function(d) { return d.value > 20 ? null : "none"; })
34
+ .text(function(d, i) { return d.value.toFixed(1); });
35
+
36
+ </script>
@@ -0,0 +1,23 @@
1
+ .labels span {
2
+ width: 2em;
3
+ height: 2em;
4
+ border: 1px solid black;
5
+ margin-right: 0.5em;
6
+ }
7
+ .labels{
8
+ list-style-type: none;
9
+ }
10
+ .labels li{
11
+ margin-top: 0.2em;
12
+ }
13
+ text.pie-chart-text-label{
14
+ /*color:white;
15
+ #font-weight: bold;*/
16
+ text-shadow: -1px 0 black, 0 1px black, 1px 0 black, 0 -1px black;
17
+ }
18
+
19
+ .labels li span {
20
+ color:white;
21
+ font-weight: bold;
22
+ text-shadow: -1px 0 black, 0 1px black, 1px 0 black, 0 -1px black;
23
+ }
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: igs_pie_chart
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.7
4
+ version: 0.0.8
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-09-06 00:00:00.000000000 Z
12
+ date: 2012-09-10 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: d3_rails
@@ -41,8 +41,11 @@ extensions: []
41
41
  extra_rdoc_files: []
42
42
  files:
43
43
  - lib/igs_pie_chart.rb
44
- - templates/pie_chart.html.erb
45
- homepage: http://gitlab.lan.innovit.com.br/igs_pie_chart/wikis/index
44
+ - lib/igs_pie_chart/pie_chart.rb
45
+ - templates/_labels.html.erb
46
+ - templates/_script.html.erb
47
+ - templates/_style.css.erb
48
+ homepage: https://github.com/lucasmartins/igs_pie_chart
46
49
  licenses: []
47
50
  post_install_message:
48
51
  rdoc_options: []
@@ -54,6 +57,9 @@ required_ruby_version: !ruby/object:Gem::Requirement
54
57
  - - ! '>='
55
58
  - !ruby/object:Gem::Version
56
59
  version: '0'
60
+ segments:
61
+ - 0
62
+ hash: 427138969806614625
57
63
  required_rubygems_version: !ruby/object:Gem::Requirement
58
64
  none: false
59
65
  requirements:
@@ -1,55 +0,0 @@
1
- <%
2
- #Default value attribution
3
- dsize = 300
4
- @width = dsize if @width == nil
5
- @height = dsize if @height == nil
6
- @endomargin = 0 if @endomargin == nil
7
- @width = @height = @size if @size!=nil
8
-
9
- if @data==nil
10
- @data=[1,1,1]
11
- end
12
-
13
- if @endomargin >= 1
14
- @endomargin = 0
15
- puts "@endomargin deve ser menor que 1"
16
- end
17
-
18
- @endomargin = 0.3
19
-
20
- %>
21
- <script type="text/javascript">
22
-
23
- var width = <%=@width%>,height = <%=@height%>,
24
- outerRadius = Math.min(width, height) / 2,
25
- innerRadius = outerRadius * <%= @endomargin%>,
26
- data = <%=@data%>,
27
- color = d3.scale.category20(),
28
- donut = d3.layout.pie(),
29
- arc = d3.svg.arc().innerRadius(innerRadius).outerRadius(outerRadius);
30
-
31
- var vis = d3.select("<%=@target_element%>")
32
- .append("svg")
33
- .data([data])
34
- .attr("width", width)
35
- .attr("height", height);
36
-
37
- var arcs = vis.selectAll("g.arc")
38
- .data(donut)
39
- .enter().append("g")
40
- .attr("class", "arc")
41
- .attr("transform", "translate(" + outerRadius + "," + outerRadius + ")");
42
-
43
- arcs.append("path")
44
- .attr("fill", function(d, i) { return color(i); })
45
- .attr("d", arc);
46
-
47
- arcs.append("text")
48
- .attr("transform", function(d) { return "translate(" + arc.centroid(d) + ")"; })
49
- .attr("dy", ".35em")
50
- .attr("class", "pie-chart-text-label")
51
- .attr("text-anchor", "middle")
52
- .attr("display", function(d) { return d.value > .15 ? null : "none"; })
53
- .text(function(d, i) { return d.value.toFixed(2); });
54
-
55
- </script>