igs_bar_chart 0.0.0
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.
- data/lib/igs_bar_chart/bar_chart.rb +85 -0
- data/lib/igs_bar_chart.rb +34 -0
- data/templates/_labels.html.erb +14 -0
- data/templates/_script.html.erb +59 -0
- data/templates/_style.css.erb +23 -0
- data/test/test_igs_bar_chart.rb +43 -0
- metadata +75 -0
@@ -0,0 +1,85 @@
|
|
1
|
+
class Igs::BarChart
|
2
|
+
attr_reader :title,:size,:target_element,:data
|
3
|
+
# data=[100,299,234,55]
|
4
|
+
def initialize(title,width,height,target_element='body',data)
|
5
|
+
@title = title
|
6
|
+
@width = width
|
7
|
+
@height = height
|
8
|
+
@data = data.values
|
9
|
+
@labels = data.keys
|
10
|
+
@target_element = target_element
|
11
|
+
@default_attribution=false
|
12
|
+
end
|
13
|
+
|
14
|
+
def script
|
15
|
+
return eval_erb('script.html')
|
16
|
+
end
|
17
|
+
|
18
|
+
def style
|
19
|
+
return eval_erb('style.css')
|
20
|
+
end
|
21
|
+
|
22
|
+
def labels
|
23
|
+
return eval_erb('labels.html')
|
24
|
+
end
|
25
|
+
|
26
|
+
def render
|
27
|
+
rendering = "<style>\n"
|
28
|
+
rendering+= self.style
|
29
|
+
rendering+= "</style>\n"
|
30
|
+
rendering+= self.script
|
31
|
+
#rendering+= self.labels
|
32
|
+
|
33
|
+
return rendering
|
34
|
+
end
|
35
|
+
|
36
|
+
def to_s
|
37
|
+
render
|
38
|
+
end
|
39
|
+
|
40
|
+
def data=(data)
|
41
|
+
@data=data
|
42
|
+
end
|
43
|
+
|
44
|
+
def width=(width)
|
45
|
+
@width = width
|
46
|
+
end
|
47
|
+
|
48
|
+
def height=(height)
|
49
|
+
@height = height
|
50
|
+
end
|
51
|
+
|
52
|
+
private
|
53
|
+
|
54
|
+
def eval_erb(partial)
|
55
|
+
begin
|
56
|
+
default_attributions
|
57
|
+
|
58
|
+
path = File.expand_path("../../../templates/_#{partial}.erb", __FILE__)
|
59
|
+
output = ERB.new(File.read(path)).result(binding)
|
60
|
+
rescue Exception => e
|
61
|
+
STDERR.puts "Erro ao renderizar '#{partial}'! #{e}"
|
62
|
+
output = nil
|
63
|
+
end
|
64
|
+
return output
|
65
|
+
end
|
66
|
+
|
67
|
+
def default_attributions
|
68
|
+
|
69
|
+
if @default_attribution == true
|
70
|
+
return
|
71
|
+
end
|
72
|
+
|
73
|
+
@default_attribution=true
|
74
|
+
#Default value attribution
|
75
|
+
@width = 500 if @width == nil
|
76
|
+
@height = 100 if @height == nil
|
77
|
+
#@width = @height = @size if @size!=nil
|
78
|
+
|
79
|
+
if @data==nil
|
80
|
+
@data=[1,1,1]
|
81
|
+
end
|
82
|
+
|
83
|
+
end
|
84
|
+
|
85
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
#encoding: utf-8
|
2
|
+
|
3
|
+
begin
|
4
|
+
mod = Required::Module::const_get "Igs"
|
5
|
+
#It exists
|
6
|
+
rescue NameError
|
7
|
+
module Igs
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
begin
|
12
|
+
if Rails.version.split('.').first.to_i >= 3
|
13
|
+
require 'd3_rails'
|
14
|
+
else
|
15
|
+
STDERR.puts "IGS Pie Chart does not support Rails versions below 3.x"
|
16
|
+
end
|
17
|
+
rescue NameError
|
18
|
+
#TODO: change this to use a logging api
|
19
|
+
puts "Not a Rails application, nothing to do here!"
|
20
|
+
puts " . . "
|
21
|
+
puts " ____ \n\n"
|
22
|
+
end
|
23
|
+
|
24
|
+
require 'erb'
|
25
|
+
require 'igs_bar_chart/bar_chart'
|
26
|
+
|
27
|
+
unless "".respond_to? 'camelize'
|
28
|
+
class String
|
29
|
+
def camelize
|
30
|
+
return self if self !~ /_/ && self =~ /[A-Z]+.*/
|
31
|
+
split('_').map{|e| e.capitalize}.join
|
32
|
+
end
|
33
|
+
end
|
34
|
+
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
|
+
<%= @data[i] %>
|
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,59 @@
|
|
1
|
+
<script type="text/javascript">
|
2
|
+
function colorfy (number) {
|
3
|
+
number=number*10
|
4
|
+
while(number>255){
|
5
|
+
number=number/1.1
|
6
|
+
}
|
7
|
+
number=parseInt(number);
|
8
|
+
return number;
|
9
|
+
}
|
10
|
+
//Width and height
|
11
|
+
var w = <%=@width%>;
|
12
|
+
var h = <%=@height%>;
|
13
|
+
var barPadding = 2;
|
14
|
+
|
15
|
+
var dataset = <%=@data%>;
|
16
|
+
|
17
|
+
//Create SVG element
|
18
|
+
var svg = d3.select("<%=@target_element%>")
|
19
|
+
.append("svg")
|
20
|
+
.attr("width", w)
|
21
|
+
.attr("height", h);
|
22
|
+
|
23
|
+
svg.selectAll("rect")
|
24
|
+
.data(dataset)
|
25
|
+
.enter()
|
26
|
+
.append("rect")
|
27
|
+
.attr("x", function(d, i) {
|
28
|
+
return i * (w / dataset.length);
|
29
|
+
})
|
30
|
+
.attr("y", function(d) {
|
31
|
+
return h - (d * 4);
|
32
|
+
})
|
33
|
+
.attr("width", w / dataset.length - barPadding)
|
34
|
+
.attr("height", function(d) {
|
35
|
+
return d * 4;
|
36
|
+
})
|
37
|
+
.attr("fill", function(d) {
|
38
|
+
return "rgb(0, 0, " + colorfy(d) + ")";
|
39
|
+
});
|
40
|
+
|
41
|
+
svg.selectAll("text")
|
42
|
+
.data(dataset)
|
43
|
+
.enter()
|
44
|
+
.append("text")
|
45
|
+
.text(function(d) {
|
46
|
+
return d;
|
47
|
+
})
|
48
|
+
.attr("text-anchor", "middle")
|
49
|
+
.attr("x", function(d, i) {
|
50
|
+
return i * (w / dataset.length) + (w / dataset.length - barPadding) / 2;
|
51
|
+
})
|
52
|
+
.attr("y", function(d) {
|
53
|
+
return h - (d * 4) + 14;
|
54
|
+
})
|
55
|
+
.attr("font-family", "sans-serif")
|
56
|
+
.attr("font-size", "11px")
|
57
|
+
.attr("fill", "white");
|
58
|
+
|
59
|
+
</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
|
+
}
|
@@ -0,0 +1,43 @@
|
|
1
|
+
require 'test/unit'
|
2
|
+
require 'igs_bar_chart'
|
3
|
+
require 'erb'
|
4
|
+
|
5
|
+
class IgsBarChartTest < Test::Unit::TestCase
|
6
|
+
|
7
|
+
include Igs
|
8
|
+
|
9
|
+
HTML_TEST_OUTPUT='test.output.html'
|
10
|
+
JS_TEST_OUTPUT='test.output.js'
|
11
|
+
|
12
|
+
def test_pie_chart_rendering
|
13
|
+
puts "Purging #{HTML_TEST_OUTPUT}..."+`rm #{HTML_TEST_OUTPUT}`
|
14
|
+
puts "Purging #{JS_TEST_OUTPUT}..."+`rm #{JS_TEST_OUTPUT}`
|
15
|
+
|
16
|
+
#these two lines does the trick!
|
17
|
+
bar = BarChart.new('My colorful chart',500,600,'body',{'one'=>1.1,'two'=>2.2,'tree'=>3.3,'five'=>5.5,'eight'=>8.8,'thirteen'=>13.13,'twenty_one'=>21.21,'thirty_four'=>34.34,'fifty_five'=>55.55,'eighty_nine'=>89.89,'a_hundread_forty_four'=>144.144})
|
18
|
+
@render = bar.render
|
19
|
+
|
20
|
+
assert_not_equal nil, @render
|
21
|
+
|
22
|
+
File.open('test.output.js', 'w') do |f|
|
23
|
+
f.write @render
|
24
|
+
end
|
25
|
+
|
26
|
+
path = File.expand_path("../../templates/test_bar_chart.html.erb", __FILE__)
|
27
|
+
html_render = ERB.new(File.read(path)).result(binding)
|
28
|
+
|
29
|
+
File.open(HTML_TEST_OUTPUT, 'w') do |f|
|
30
|
+
f.write html_render
|
31
|
+
end
|
32
|
+
|
33
|
+
begin
|
34
|
+
`open #{HTML_TEST_OUTPUT}`
|
35
|
+
rescue Exception => e
|
36
|
+
puts "Ouch! #{e}"
|
37
|
+
end
|
38
|
+
|
39
|
+
`google-chrome #{HTML_TEST_OUTPUT}`
|
40
|
+
|
41
|
+
end
|
42
|
+
|
43
|
+
end
|
metadata
ADDED
@@ -0,0 +1,75 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: igs_bar_chart
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Lucas N. Martins
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-09-14 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: d3_rails
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '2.10'
|
22
|
+
- - <
|
23
|
+
- !ruby/object:Gem::Version
|
24
|
+
version: '3.0'
|
25
|
+
type: :runtime
|
26
|
+
prerelease: false
|
27
|
+
version_requirements: !ruby/object:Gem::Requirement
|
28
|
+
none: false
|
29
|
+
requirements:
|
30
|
+
- - ! '>='
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '2.10'
|
33
|
+
- - <
|
34
|
+
- !ruby/object:Gem::Version
|
35
|
+
version: '3.0'
|
36
|
+
description: Bar Chart is a gem for creating bar charts (duh!). It uses D3 (Data Driven
|
37
|
+
Documents) to aggregate the data and render the SVG awesomeness.
|
38
|
+
email: lucas.martins@innovit.com.br
|
39
|
+
executables: []
|
40
|
+
extensions: []
|
41
|
+
extra_rdoc_files: []
|
42
|
+
files:
|
43
|
+
- lib/igs_bar_chart.rb
|
44
|
+
- lib/igs_bar_chart/bar_chart.rb
|
45
|
+
- templates/_labels.html.erb
|
46
|
+
- templates/_script.html.erb
|
47
|
+
- templates/_style.css.erb
|
48
|
+
- test/test_igs_bar_chart.rb
|
49
|
+
homepage: https://github.com/lucasmartins/igs_bar_chart
|
50
|
+
licenses:
|
51
|
+
- MIT
|
52
|
+
post_install_message:
|
53
|
+
rdoc_options: []
|
54
|
+
require_paths:
|
55
|
+
- lib
|
56
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ! '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: 1.9.1
|
62
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
63
|
+
none: false
|
64
|
+
requirements:
|
65
|
+
- - ! '>='
|
66
|
+
- !ruby/object:Gem::Version
|
67
|
+
version: '0'
|
68
|
+
requirements: []
|
69
|
+
rubyforge_project:
|
70
|
+
rubygems_version: 1.8.23
|
71
|
+
signing_key:
|
72
|
+
specification_version: 3
|
73
|
+
summary: Bar Chart is a gem for creating bar charts (duh!).
|
74
|
+
test_files:
|
75
|
+
- test/test_igs_bar_chart.rb
|