fnando-chart 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/License.txt ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Nando Vieira
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.markdown ADDED
@@ -0,0 +1,178 @@
1
+ Chart
2
+ =====
3
+
4
+ Chart is a wrapper for the excellent [ChartDirector](http://www.advsofteng.com/) library.
5
+ It does not cover all charts; only those I need!
6
+
7
+ Supported charts: grouped bars
8
+
9
+ Installation
10
+ ------------
11
+
12
+ ### Installing ChartDirector
13
+
14
+ 1. Download ChartDirector for Ruby at <http://www.advsofteng.com/download.html>
15
+ 2. Extract the files
16
+ 3. Run `ruby install.rb`
17
+
18
+ ### Installing Chart
19
+
20
+ You can use it as gem or plugin:
21
+
22
+ sudo gem install fnando-chart --source=http://gems.github.com
23
+
24
+ or
25
+
26
+ script/plugin install git://github.com/fnando/chart.git
27
+
28
+ Usage
29
+ -----
30
+
31
+ Just require the Chart library.
32
+
33
+ require "chart"
34
+
35
+ ### Bars
36
+
37
+ # set some data
38
+ data = [3600.99, 1479.00, 900.10]
39
+
40
+ chart = Chart::Bar.new({
41
+ :width => 400,
42
+ :height => 240,
43
+ :data => data,
44
+ :margins => [50, 25],
45
+ :plot_area => [320, 180],
46
+ :labels => %w(Ruby Python Erlang),
47
+ :multicolor => false
48
+ })
49
+
50
+ chart.write("bar.png")
51
+
52
+ Result:
53
+
54
+ ![Bars chart](http://f.simplesideias.com.br/chart/bar.png)
55
+
56
+ ### Multicolor Bars
57
+
58
+ # set some data
59
+ data = [3600.99, 1479.00, 900.10]
60
+
61
+ chart = Chart::Bar.new({
62
+ :width => 400,
63
+ :height => 240,
64
+ :legend => %w(Ruby Python Erlang),
65
+ :data => data,
66
+ :margins => [50, 25],
67
+ :plot_area => [320, 180],
68
+ :labels => %w(Jan Feb Mar),
69
+ :multicolor => true
70
+ })
71
+
72
+ chart.write("multicolorbar.png")
73
+
74
+ Result:
75
+
76
+ ![Multicolor Bars chart](http://f.simplesideias.com.br/chart/multicolorbar.png)
77
+
78
+ ### Grouped Bars
79
+
80
+ # set some data
81
+ in_data = [3600.99, 1479.00, 900.10]
82
+ out_data = [728.77, 356.98, 1100.00]
83
+
84
+ chart = Chart::GroupedBar.new({
85
+ :width => 400,
86
+ :height => 240,
87
+ :colors => [Chart::GREEN, Chart::RED],
88
+ :legend => %w(In Out),
89
+ :data => [in_data, out_data],
90
+ :margins => [50, 25],
91
+ :plot_area => [320, 180],
92
+ :labels => %w(Jan Feb Mar)
93
+ })
94
+
95
+ chart.write("groupedbar.png")
96
+
97
+ Result:
98
+
99
+ ![Grouped Bars chart](http://f.simplesideias.com.br/chart/groupedbar.png)
100
+
101
+ ### Pie
102
+
103
+ # set some data
104
+ data = [1000, 500, 300, 30]
105
+
106
+ # set the legend
107
+ legend = ["Ruby", "Python", "Erlang", "PHP"]
108
+
109
+ chart = Chart::Pie.new({
110
+ :width => 400,
111
+ :height => 250,
112
+ :data => data,
113
+ :margins => [150, 130],
114
+ :legend => legend,
115
+ :radius => 80,
116
+ :position => [310, 80], # [legend_x, legend_y]
117
+ :label_format => "${value|2,.}\n({percent}%)"
118
+ })
119
+
120
+ chart.write("pie.png")
121
+
122
+ Result:
123
+
124
+ ![Pie chart](http://f.simplesideias.com.br/chart/pie.png)
125
+
126
+ To hide the pie label, just set the `label_format` to `nil`:
127
+
128
+ # set some data
129
+ data = [1000, 500, 300, 30]
130
+
131
+ # set the legend
132
+ legend = ["Ruby", "Python", "Erlang", "PHP"]
133
+
134
+ chart = Chart::Pie.new({
135
+ :width => 400,
136
+ :height => 250,
137
+ :data => data,
138
+ :margins => [150, 130],
139
+ :legend => legend,
140
+ :radius => 80,
141
+ :position => [310, 80], # [legend_x, legend_y]
142
+ :label_format => nil
143
+ })
144
+
145
+ chart.write("pie_without_labels.png")
146
+
147
+ Result:
148
+
149
+ ![Pie chart without label](http://f.simplesideias.com.br/chart/pie_without_labels.png)
150
+
151
+ MAINTAINER
152
+ ----------
153
+
154
+ * Nando Vieira ([http://simplesideias.com.br](http://simplesideias.com.br))
155
+
156
+ LICENSE:
157
+ --------
158
+
159
+ (The MIT License)
160
+
161
+ Permission is hereby granted, free of charge, to any person obtaining
162
+ a copy of this software and associated documentation files (the
163
+ 'Software'), to deal in the Software without restriction, including
164
+ without limitation the rights to use, copy, modify, merge, publish,
165
+ distribute, sublicense, and/or sell copies of the Software, and to
166
+ permit persons to whom the Software is furnished to do so, subject to
167
+ the following conditions:
168
+
169
+ The above copyright notice and this permission notice shall be
170
+ included in all copies or substantial portions of the Software.
171
+
172
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
173
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
174
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
175
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
176
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
177
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
178
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/Rakefile ADDED
@@ -0,0 +1,78 @@
1
+ require 'rake'
2
+
3
+ PKG_FILES = %w(init.rb Rakefile chart.gemspec License.txt README.markdown) +
4
+ Dir["lib/**/*"]
5
+
6
+ spec = Gem::Specification.new do |s|
7
+ s.name = "chart"
8
+ s.version = "0.0.1"
9
+ s.summary = "A simple ChartDirector wrapper"
10
+ s.authors = ["Nando Vieira"]
11
+ s.email = ["fnando.vieira@gmail.com"]
12
+ s.homepage = "http://github.com/fnando/chart"
13
+ s.description = ""
14
+ s.has_rdoc = false
15
+ s.files = PKG_FILES
16
+
17
+ s.add_dependency "rubigen"
18
+ end
19
+
20
+ namespace :gem do
21
+ # Thanks to the Merb project for this code.
22
+ desc "Update Github Gemspec"
23
+ task :update_gemspec do
24
+ skip_fields = %w(new_platform original_platform specification_version loaded required_ruby_version rubygems_version platform)
25
+
26
+ result = "# WARNING : RAKE AUTO-GENERATED FILE. DO NOT MANUALLY EDIT!\n"
27
+ result << "# RUN : 'rake gem:update_gemspec'\n\n"
28
+ result << "Gem::Specification.new do |s|\n"
29
+
30
+ spec.instance_variables.each do |ivar|
31
+ value = spec.instance_variable_get(ivar)
32
+ name = ivar.split("@").last
33
+ next if name == "date"
34
+
35
+ next if skip_fields.include?(name) || value.nil? || value == "" || (value.respond_to?(:empty?) && value.empty?)
36
+ if name == "dependencies"
37
+ value.each do |d|
38
+ dep, *ver = d.to_s.split(" ")
39
+ result << " s.add_dependency #{dep.inspect}, #{ver.join(" ").inspect.gsub(/[()]/, "").gsub(", runtime", "")}\n"
40
+ end
41
+ else
42
+ case value
43
+ when Array
44
+ value = name != "files" ? value.inspect : value.inspect.split(",").join(",\n")
45
+ when FalseClass
46
+ when TrueClass
47
+ when Fixnum
48
+ when String
49
+ value = value.inspect
50
+ else
51
+ value = value.to_s.inspect
52
+ end
53
+ result << " s.#{name} = #{value}\n"
54
+ end
55
+ end
56
+
57
+ result << "end"
58
+ File.open(File.join(File.dirname(__FILE__), "#{spec.name}.gemspec"), "w"){|f| f << result}
59
+ end
60
+
61
+ desc "Build gem"
62
+ task :build => [:update_gemspec] do
63
+ system "rm *.gem"
64
+ system "gem build #{spec.instance_variable_get('@name')}.gemspec"
65
+ end
66
+
67
+ desc "Install gem"
68
+ task :install => [:update_gemspec, :build] do
69
+ system "sudo gem install #{spec.instance_variable_get('@name')}"
70
+ end
71
+ end
72
+
73
+ desc "Run all specs"
74
+ task :spec do
75
+ Dir.glob("spec/chart/*_spec.rb").each do |file|
76
+ system "ruby #{file} -c"
77
+ end
78
+ end
data/chart.gemspec ADDED
@@ -0,0 +1,27 @@
1
+ # WARNING : RAKE AUTO-GENERATED FILE. DO NOT MANUALLY EDIT!
2
+ # RUN : 'rake gem:update_gemspec'
3
+
4
+ Gem::Specification.new do |s|
5
+ s.authors = ["Nando Vieira"]
6
+ s.require_paths = ["lib"]
7
+ s.required_rubygems_version = ">= 0"
8
+ s.has_rdoc = false
9
+ s.files = ["init.rb",
10
+ "Rakefile",
11
+ "chart.gemspec",
12
+ "License.txt",
13
+ "README.markdown",
14
+ "lib/chart",
15
+ "lib/chart/bar.rb",
16
+ "lib/chart/base.rb",
17
+ "lib/chart/grouped_bar.rb",
18
+ "lib/chart/pie.rb",
19
+ "lib/chart.rb"]
20
+ s.email = ["fnando.vieira@gmail.com"]
21
+ s.version = "0.0.1"
22
+ s.homepage = "http://github.com/fnando/chart"
23
+ s.name = "chart"
24
+ s.summary = "A simple ChartDirector wrapper"
25
+ s.add_dependency "rubigen", ">= 0"
26
+ s.bindir = "bin"
27
+ end
data/init.rb ADDED
@@ -0,0 +1 @@
1
+ require File.dirname(__FILE__) + "/lib/chart"
data/lib/chart/bar.rb ADDED
@@ -0,0 +1,35 @@
1
+ module Chart
2
+ class Bar < Base
3
+ def instantiate
4
+ # instantiate a new chart
5
+ @object = ChartDirector::XYChart.new(@width, @height) if @width && @height
6
+ end
7
+
8
+ def generate
9
+ # set the plot area
10
+ @object.setPlotArea(@margins[0], @margins[1], @plot_area[0], @plot_area[1], 0xe9e9e9, 0xf5f5f5)
11
+
12
+ # set the legend position
13
+ @object.addLegend(@margins[0], 0, false, "normal", 8).setBackground(ChartDirector::Transparent)
14
+
15
+ # set the top margin
16
+ @object.yAxis.setTopMargin(@plot_top_margin)
17
+
18
+ # set the labels
19
+ @object.xAxis.setLabels(@labels)
20
+
21
+ # add data to layer
22
+ if @multicolor
23
+ @object.addBarLayer3(@data, @colors, @legend, 0)
24
+ else
25
+ @object.addBarLayer(@data, @colors.first)
26
+ end
27
+ end
28
+
29
+ private
30
+ def defaults!
31
+ super
32
+ @colors = Chart::COLORS
33
+ end
34
+ end
35
+ end
data/lib/chart/base.rb ADDED
@@ -0,0 +1,102 @@
1
+ module Chart
2
+ # set some color constants
3
+ GREEN = 0x5EC200
4
+ RED = 0xE05257
5
+
6
+ COLORS = [
7
+ 0x719CE4,
8
+ 0xFF8F00,
9
+ 0x94CB47,
10
+ 0xff3333,
11
+ 0x7412C5,
12
+ 0x78722F,
13
+ 0xFFE921,
14
+ 0x666666,
15
+ 0x7F2548,
16
+ 0xcccccc,
17
+ 0x990000,
18
+ 0x0B66AF,
19
+ 0xFF0066,
20
+ 0xBBD6E3,
21
+ 0xBEB1D2
22
+ ]
23
+
24
+ class Base
25
+ # Set the chart width
26
+ attr_accessor :width
27
+
28
+ # Set the chart height
29
+ attr_accessor :height
30
+
31
+ # Set the labels
32
+ attr_writer :labels
33
+
34
+ # Set the chart legend
35
+ attr_writer :legend
36
+
37
+ # Set the chart data
38
+ attr_accessor :data
39
+
40
+ # Set the plot area
41
+ attr_accessor :plot_area
42
+
43
+ # Set the chart left-top margins
44
+ attr_accessor :margins
45
+
46
+ # Set bar colors
47
+ attr_accessor :colors
48
+
49
+ # Set if bar is multicolor
50
+ attr_accessor :multicolor
51
+
52
+ # Set plot top margin
53
+ attr_accessor :plot_top_margin
54
+
55
+ # Set position
56
+ attr_accessor :position
57
+
58
+ # Set radius
59
+ attr_accessor :radius
60
+
61
+ # Set label format
62
+ attr_accessor :label_format
63
+
64
+ # The ChartDirector object
65
+ attr_accessor :object
66
+
67
+ # Initialize a new chart object. Specify the
68
+ # chart attributes by providing a hash.
69
+ def initialize(options={})
70
+ defaults!
71
+ options.each {|name, value| send("#{name}=", value)}
72
+ instantiate
73
+ end
74
+
75
+ # Save the chart image to the specified path
76
+ def write(path)
77
+ generate
78
+ @object.makeChart(path)
79
+ end
80
+
81
+ # Wrapper to the ActionView helpers
82
+ def helper
83
+ Helper.instance
84
+ end
85
+
86
+ # Labels attribute reader
87
+ def labels
88
+ @labels
89
+ end
90
+
91
+ # Legend attribute reader
92
+ def legend
93
+ @legend
94
+ end
95
+
96
+ private
97
+ # Set the chart defaults; should be overwritten.
98
+ def defaults!
99
+ @plot_top_margin = 50
100
+ end
101
+ end
102
+ end
@@ -0,0 +1,30 @@
1
+ module Chart
2
+ class GroupedBar < Base
3
+ def instantiate
4
+ # instantiate a new chart
5
+ @object = ChartDirector::XYChart.new(@width, @height) if @width && @height
6
+ end
7
+
8
+ def generate
9
+ # set the plot area
10
+ @object.setPlotArea(@margins[0], @margins[1], @plot_area[0], @plot_area[1], 0xe9e9e9, 0xf5f5f5)
11
+
12
+ # set the legend position
13
+ @object.addLegend(@margins[0], 0, false, "normal", 8).setBackground(ChartDirector::Transparent)
14
+
15
+ # set the top margin
16
+ @object.yAxis.setTopMargin(@plot_top_margin)
17
+
18
+ # set the labels
19
+ @object.xAxis.setLabels(@labels)
20
+
21
+ # create the layer
22
+ layer = @object.addBarLayer2(ChartDirector::Side, 0)
23
+
24
+ # add data to layer
25
+ @data.each_with_index do |data, index|
26
+ layer.addDataSet(data, @colors[index], legend[index])
27
+ end
28
+ end
29
+ end
30
+ end
data/lib/chart/pie.rb ADDED
@@ -0,0 +1,41 @@
1
+ module Chart
2
+ class Pie < Base
3
+ def instantiate
4
+ # instantiate a new chart
5
+ @object = ChartDirector::PieChart.new(@width, @height) if @width && @height
6
+ end
7
+
8
+ def generate
9
+ # set the pie size and position
10
+ @object.setPieSize(@margins[0], @margins[1], @radius)
11
+
12
+ # set data and labels
13
+ @object.setData(@data, @legend)
14
+
15
+ # set label format
16
+ @object.setLabelFormat(@label_format)
17
+
18
+ # hide label format if is nil
19
+ @object.setLabelStyle("normal", 8, ChartDirector::Transparent) if @label_format.nil?
20
+
21
+ # add legend box
22
+ @object.addLegend(@position[0], @position[1])
23
+
24
+ # set colors
25
+ @object.setColors2(ChartDirector::DataColor, @colors)
26
+
27
+ # set label position
28
+ @object.setLabelPos(15, ChartDirector::LineColor)
29
+
30
+ # set label line color
31
+ @object.setJoinLine(0x666666)
32
+ end
33
+
34
+ private
35
+ def defaults!
36
+ super
37
+ @colors = Chart::COLORS
38
+ @label_format = "{percent}%"
39
+ end
40
+ end
41
+ end
data/lib/chart.rb ADDED
@@ -0,0 +1,11 @@
1
+ require "chartdirector"
2
+
3
+ base_dir = File.dirname(__FILE__) + "/chart"
4
+ files = %w(
5
+ base
6
+ bar
7
+ grouped_bar
8
+ pie
9
+ )
10
+
11
+ files.each {|file| require "#{base_dir}/#{file}" }
metadata ADDED
@@ -0,0 +1,72 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: fnando-chart
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Nando Vieira
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-01-11 00:00:00 -08:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: rubigen
17
+ version_requirement:
18
+ version_requirements: !ruby/object:Gem::Requirement
19
+ requirements:
20
+ - - ">="
21
+ - !ruby/object:Gem::Version
22
+ version: "0"
23
+ version:
24
+ description:
25
+ email:
26
+ - fnando.vieira@gmail.com
27
+ executables: []
28
+
29
+ extensions: []
30
+
31
+ extra_rdoc_files: []
32
+
33
+ files:
34
+ - init.rb
35
+ - Rakefile
36
+ - chart.gemspec
37
+ - License.txt
38
+ - README.markdown
39
+ - lib/chart
40
+ - lib/chart/bar.rb
41
+ - lib/chart/base.rb
42
+ - lib/chart/grouped_bar.rb
43
+ - lib/chart/pie.rb
44
+ - lib/chart.rb
45
+ has_rdoc: false
46
+ homepage: http://github.com/fnando/chart
47
+ post_install_message:
48
+ rdoc_options: []
49
+
50
+ require_paths:
51
+ - lib
52
+ required_ruby_version: !ruby/object:Gem::Requirement
53
+ requirements:
54
+ - - ">="
55
+ - !ruby/object:Gem::Version
56
+ version: "0"
57
+ version:
58
+ required_rubygems_version: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - ">="
61
+ - !ruby/object:Gem::Version
62
+ version: "0"
63
+ version:
64
+ requirements: []
65
+
66
+ rubyforge_project:
67
+ rubygems_version: 1.2.0
68
+ signing_key:
69
+ specification_version: 2
70
+ summary: A simple ChartDirector wrapper
71
+ test_files: []
72
+