forked from clbustos/svg-graph
-
Notifications
You must be signed in to change notification settings - Fork 20
/
bar.rb
48 lines (42 loc) · 1.43 KB
/
bar.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# require 'SVG/Graph/Bar'
x_axis = ['1-10', '10-30', '30-50', '50-70', 'older']
options = {
:width => 640,
:height => 500,
:stack => :side, # the stack option is valid for Bar graphs only
:fields => x_axis,
:graph_title => "kg per head and year chocolate consumption",
:show_graph_title => true,
:scale_integers => true,
:show_x_title => true,
:x_title => 'Age in years',
:stagger_x_labels => true,
:rotate_x_labels => true,
:x_title_location => :end,
:show_y_title => true,
:y_title => 'kg/year',
:y_title_location => :end,
:add_popups => true,
:no_css => true,
# :x_axis_position => 0,
# :y_axis_position => '30-50',
}
data1 = [15, 4, 6.7, 4, 2.8]
data2 = [1, 5, 4, 5, 12.7]
g = SVG::Graph::Bar.new(options)
g.add_data( {
:data => data1,
:title => "Dataset1"
})
g.add_data( {
:data => data2,
:title => "Dataset2"
})
# graph.burn # this returns a full valid xml document containing the graph
# graph.burn_svg_only # this only returns the <svg>...</svg> node
output_filename = File.basename(__FILE__, ".rb")
if defined?(USE_FOR_TESTING)
File.open(File.join(OUTPUT_FOLDER, "#{output_filename}.html"), "w") {|f| f.write(g.burn)}
else
File.open(File.expand_path("#{output_filename}.svg",__dir__), 'w') {|f| f.write(g.burn_svg_only)} # for inclusion into readme.md
end