fnando-chart 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/README.markdown +96 -0
- data/Rakefile +1 -1
- data/chart.gemspec +2 -1
- data/lib/chart.rb +1 -0
- data/lib/chart/base.rb +12 -0
- data/lib/chart/line.rb +77 -0
- metadata +2 -1
data/README.markdown
CHANGED
@@ -148,6 +148,102 @@ Result:
|
|
148
148
|
|
149
149
|
![Pie chart without label](http://f.simplesideias.com.br/chart/pie_without_labels.png)
|
150
150
|
|
151
|
+
### Line chart
|
152
|
+
|
153
|
+
# set some data
|
154
|
+
ruby_data = [42, 49, 33, 38, 51, 46, 29, 41, 44, 57, 59, 52]
|
155
|
+
python_data = [50, 55, 47, 34, 42, 49, 63, 62, 73, 59, 56, 50]
|
156
|
+
erlang_data = [36, 28, 25, 33, 38, 20, 22, 30, 25, 33, 30, 24]
|
157
|
+
|
158
|
+
chart = Chart::Line.new({
|
159
|
+
:width => 400,
|
160
|
+
:height => 240,
|
161
|
+
:data => [ruby_data, python_data, erlang_data],
|
162
|
+
:legend => %w(Ruby Python Erlang),
|
163
|
+
:labels => %w(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec),
|
164
|
+
:margins => [50, 25],
|
165
|
+
:plot_area => [320, 180],
|
166
|
+
:plot_top_margin => 10,
|
167
|
+
# :style => %w(alternate dot dash),
|
168
|
+
:line_width => 3,
|
169
|
+
# :label_format => "{percent}%",
|
170
|
+
:symbols => %w(circle circle circle)
|
171
|
+
})
|
172
|
+
|
173
|
+
chart.write("line.png")
|
174
|
+
|
175
|
+
Result:
|
176
|
+
|
177
|
+
![Line chart](http://f.simplesideias.com.br/chart/line.png)
|
178
|
+
|
179
|
+
To set the line style, use the `style` attribute with the values
|
180
|
+
`dot`, `dash`, `alternate`, `solid` or `dotdash`:
|
181
|
+
|
182
|
+
# set some data
|
183
|
+
ruby_data = [42, 49, 33, 38, 51, 46, 29, 41, 44, 57, 59, 52]
|
184
|
+
python_data = [50, 55, 47, 34, 42, 49, 63, 62, 73, 59, 56, 50]
|
185
|
+
erlang_data = [36, 28, 25, 33, 38, 20, 22, 30, 25, 33, 30, 24]
|
186
|
+
|
187
|
+
chart = Chart::Line.new({
|
188
|
+
:width => 400,
|
189
|
+
:height => 240,
|
190
|
+
:data => [ruby_data, python_data, erlang_data],
|
191
|
+
:legend => %w(Ruby Python Erlang),
|
192
|
+
:labels => %w(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec),
|
193
|
+
:margins => [50, 25],
|
194
|
+
:plot_area => [320, 180],
|
195
|
+
:plot_top_margin => 10,
|
196
|
+
:line_width => 3,
|
197
|
+
:style => %w(dash dot alternate)
|
198
|
+
})
|
199
|
+
|
200
|
+
Result:
|
201
|
+
|
202
|
+
![Line chart with line style](http://f.simplesideias.com.br/chart/line_with_line_style.png)
|
203
|
+
|
204
|
+
To set the point label, use the `label_format` option or nil to hide it:
|
205
|
+
|
206
|
+
chart = Chart::Line.new({
|
207
|
+
:width => 400,
|
208
|
+
:height => 240,
|
209
|
+
:data => [ruby_data],
|
210
|
+
:legend => %w(Ruby),
|
211
|
+
:labels => %w(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec),
|
212
|
+
:margins => [50, 25],
|
213
|
+
:plot_area => [320, 180],
|
214
|
+
:plot_top_margin => 10,
|
215
|
+
:line_width => 3,
|
216
|
+
:label_format => "{value}"
|
217
|
+
})
|
218
|
+
|
219
|
+
chart.write("line_with_labels.png")
|
220
|
+
|
221
|
+
Result:
|
222
|
+
|
223
|
+
![Line chart with labels](http://f.simplesideias.com.br/chart/line_with_labels.png)
|
224
|
+
|
225
|
+
To set the point type, use the `symbols` option with the values `square`, `diamond`,
|
226
|
+
`triangle`, `circle` or an image:
|
227
|
+
|
228
|
+
chart = Chart::Line.new({
|
229
|
+
:width => 400,
|
230
|
+
:height => 240,
|
231
|
+
:data => [ruby_data],
|
232
|
+
:legend => %w(Ruby),
|
233
|
+
:labels => %w(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec),
|
234
|
+
:margins => [50, 25],
|
235
|
+
:plot_area => [320, 180],
|
236
|
+
:plot_top_margin => 10,
|
237
|
+
:line_width => 3,
|
238
|
+
:symbols => [File.dirname(__FILE__) + "/user.png"]
|
239
|
+
})
|
240
|
+
|
241
|
+
chart.write("line_with_custom_symbol.png")
|
242
|
+
|
243
|
+
Result:
|
244
|
+
|
245
|
+
![Line chart with custom symbol](http://f.simplesideias.com.br/chart/line_with_custom_symbol.png)
|
246
|
+
|
151
247
|
MAINTAINER
|
152
248
|
----------
|
153
249
|
|
data/Rakefile
CHANGED
@@ -5,7 +5,7 @@ PKG_FILES = %w(init.rb Rakefile chart.gemspec License.txt README.markdown) +
|
|
5
5
|
|
6
6
|
spec = Gem::Specification.new do |s|
|
7
7
|
s.name = "chart"
|
8
|
-
s.version = "0.0.
|
8
|
+
s.version = "0.0.2"
|
9
9
|
s.summary = "A simple ChartDirector wrapper"
|
10
10
|
s.authors = ["Nando Vieira"]
|
11
11
|
s.email = ["fnando.vieira@gmail.com"]
|
data/chart.gemspec
CHANGED
@@ -15,10 +15,11 @@ Gem::Specification.new do |s|
|
|
15
15
|
"lib/chart/bar.rb",
|
16
16
|
"lib/chart/base.rb",
|
17
17
|
"lib/chart/grouped_bar.rb",
|
18
|
+
"lib/chart/line.rb",
|
18
19
|
"lib/chart/pie.rb",
|
19
20
|
"lib/chart.rb"]
|
20
21
|
s.email = ["fnando.vieira@gmail.com"]
|
21
|
-
s.version = "0.0.
|
22
|
+
s.version = "0.0.2"
|
22
23
|
s.homepage = "http://github.com/fnando/chart"
|
23
24
|
s.name = "chart"
|
24
25
|
s.summary = "A simple ChartDirector wrapper"
|
data/lib/chart.rb
CHANGED
data/lib/chart/base.rb
CHANGED
@@ -61,6 +61,18 @@ module Chart
|
|
61
61
|
# Set label format
|
62
62
|
attr_accessor :label_format
|
63
63
|
|
64
|
+
# Set style
|
65
|
+
attr_accessor :style
|
66
|
+
|
67
|
+
# Set width
|
68
|
+
attr_accessor :width
|
69
|
+
|
70
|
+
# Set line width
|
71
|
+
attr_accessor :line_width
|
72
|
+
|
73
|
+
# Set line symbols
|
74
|
+
attr_accessor :symbols
|
75
|
+
|
64
76
|
# The ChartDirector object
|
65
77
|
attr_accessor :object
|
66
78
|
|
data/lib/chart/line.rb
ADDED
@@ -0,0 +1,77 @@
|
|
1
|
+
module Chart
|
2
|
+
class Line < Base
|
3
|
+
LINE_STYLE = {
|
4
|
+
"dash" => ChartDirector::DashLine,
|
5
|
+
"dot" => ChartDirector::DotLine,
|
6
|
+
"dotdash" => ChartDirector::DotDashLine,
|
7
|
+
"alternate" => ChartDirector::AltDashLine
|
8
|
+
}
|
9
|
+
|
10
|
+
LINE_SYMBOLS = {
|
11
|
+
"square" => ChartDirector::SquareSymbol,
|
12
|
+
"diamond" => ChartDirector::DiamondSymbol,
|
13
|
+
"triangle" => ChartDirector::TriangleSymbol,
|
14
|
+
"circle" => ChartDirector::CircleSymbol
|
15
|
+
}
|
16
|
+
|
17
|
+
def instantiate
|
18
|
+
# instantiate a new chart
|
19
|
+
@object = ChartDirector::XYChart.new(@width, @height) if @width && @height
|
20
|
+
end
|
21
|
+
|
22
|
+
def generate
|
23
|
+
# set the plot area
|
24
|
+
@object.setPlotArea(@margins[0], @margins[1], @plot_area[0], @plot_area[1], 0xe9e9e9, 0xf5f5f5)
|
25
|
+
|
26
|
+
# set the legend position
|
27
|
+
@object.addLegend(@margins[0], 0, false, "normal", 8).setBackground(ChartDirector::Transparent)
|
28
|
+
|
29
|
+
# set the top margin
|
30
|
+
@object.yAxis.setTopMargin(@plot_top_margin)
|
31
|
+
@object.xAxis.setMargin(10, 10)
|
32
|
+
|
33
|
+
# set the labels
|
34
|
+
@object.xAxis.setLabels(@labels)
|
35
|
+
|
36
|
+
# create the layer
|
37
|
+
layer = @object.addLineLayer2
|
38
|
+
|
39
|
+
# set line width
|
40
|
+
layer.setLineWidth(@line_width)
|
41
|
+
|
42
|
+
# set label format
|
43
|
+
layer.setDataLabelFormat(@label_format) unless label_format.nil?
|
44
|
+
|
45
|
+
# add data to layer
|
46
|
+
@data.each_with_index do |data, index|
|
47
|
+
# set line style or color
|
48
|
+
if @style && LINE_STYLE[@style[index].to_s]
|
49
|
+
line_style = @object.dashLineColor(@colors[index], LINE_STYLE[style[index].to_s])
|
50
|
+
else
|
51
|
+
line_style = @colors[index]
|
52
|
+
end
|
53
|
+
|
54
|
+
# add dataset
|
55
|
+
dataset = layer.addDataSet(data, line_style, legend[index])
|
56
|
+
|
57
|
+
# set symbol
|
58
|
+
symbol = LINE_SYMBOLS[@symbols[index]]
|
59
|
+
|
60
|
+
if symbol
|
61
|
+
dataset.setDataSymbol(symbol, 7, -1, @colors[index], 1)
|
62
|
+
elsif @symbols[index] && File.file?(@symbols[index])
|
63
|
+
dataset.setDataSymbol2(@symbols[index])
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
private
|
69
|
+
def defaults!
|
70
|
+
super
|
71
|
+
@colors = Chart::COLORS
|
72
|
+
@line_width = 2
|
73
|
+
@style = []
|
74
|
+
@symbols = []
|
75
|
+
end
|
76
|
+
end
|
77
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: fnando-chart
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Nando Vieira
|
@@ -40,6 +40,7 @@ files:
|
|
40
40
|
- lib/chart/bar.rb
|
41
41
|
- lib/chart/base.rb
|
42
42
|
- lib/chart/grouped_bar.rb
|
43
|
+
- lib/chart/line.rb
|
43
44
|
- lib/chart/pie.rb
|
44
45
|
- lib/chart.rb
|
45
46
|
has_rdoc: false
|