class SVG::Graph::Bar
Create presentation quality SVG
bar graphs easily¶ ↑
Synopsis¶ ↑
require 'SVG/Graph/Bar' fields = %w(Jan Feb Mar); data_sales_02 = [12, 45, 21] graph = SVG::Graph::Bar.new( :height => 500, :width => 300, :fields => fields ) graph.add_data( :data => data_sales_02, :title => 'Sales 2002' ) print "Content-type: image/svg+xml\r\n\r\n" print graph.burn
Description¶ ↑
This object aims to allow you to easily create high quality SVG[www.w3c.org/tr/svg bar graphs. You can either use the default style sheet or supply your own. Either way there are many options which can be configured to give you control over how the graph is generated - with or without a key, data elements at each point, title, subtitle etc.
Notes¶ ↑
The default stylesheet handles upto 12 data sets, if you use more you must create your own stylesheet and add the additional settings for the extra data sets. You will know if you go over 12 data sets as they will have no style and be in black.
Examples¶ ↑
See also¶ ↑
Public Instance Methods
set_defaults()
click to toggle source
See Graph::initialize and BarBase::set_defaults
Calls superclass method
# File lib/SVG/Graph/Bar.rb 62 def set_defaults 63 super 64 self.top_align = self.top_font = 1 65 end
Protected Instance Methods
draw_data()
click to toggle source
# File lib/SVG/Graph/Bar.rb 98 def draw_data 99 minvalue = min_value 100 fieldwidth = field_width 101 102 unit_size = (@graph_height.to_f - font_size*2*top_font) / 103 (get_y_labels.max - get_y_labels.min) 104 bargap = bar_gap ? (fieldwidth < 10 ? fieldwidth / 2 : 10) : 0 105 106 bar_width = fieldwidth - bargap 107 bar_width /= @data.length if stack == :side 108 #x_mod = (@graph_width-bargap)/2 - (stack==:side ? bar_width/2 : 0) 109 110 bottom = @graph_height 111 112 field_count = 0 113 @config[:fields].each_index { |i| 114 dataset_count = 0 115 for dataset in @data 116 117 # cases (assume 0 = +ve): 118 # value min length 119 # +ve +ve value - min 120 # +ve -ve value - 0 121 # -ve -ve value.abs - 0 122 123 value = dataset[:data][i] 124 125 left = (fieldwidth * field_count) 126 127 length = (value.abs - (minvalue > 0 ? minvalue : 0)) * unit_size 128 # top is 0 if value is negative 129 top = bottom - (((value < 0 ? 0 : value) - minvalue) * unit_size) 130 left += bar_width * dataset_count if stack == :side 131 132 @graph.add_element( "rect", { 133 "x" => left.to_s, 134 "y" => top.to_s, 135 "width" => bar_width.to_s, 136 "height" => length.to_s, 137 "class" => "fill#{dataset_count+1}" 138 }) 139 140 make_datapoint_text(left + bar_width/2.0, top - 6, value.to_s) 141 dataset_count += 1 142 end 143 field_count += 1 144 } 145 end
get_x_labels()
click to toggle source
# File lib/SVG/Graph/Bar.rb 69 def get_x_labels 70 @config[:fields] 71 end
get_y_labels()
click to toggle source
# File lib/SVG/Graph/Bar.rb 73 def get_y_labels 74 maxvalue = max_value 75 minvalue = min_value 76 range = maxvalue - minvalue 77 78 top_pad = range == 0 ? 10 : range / 20.0 79 scale_range = (maxvalue + top_pad) - minvalue 80 81 scale_division = scale_divisions || (scale_range / 10.0) 82 83 if scale_integers 84 scale_division = scale_division < 1 ? 1 : scale_division.round 85 end 86 87 rv = [] 88 maxvalue = maxvalue%scale_division == 0 ? 89 maxvalue : maxvalue + scale_division 90 minvalue.step( maxvalue, scale_division ) {|v| rv << v} 91 return rv 92 end
x_label_offset( width )
click to toggle source
# File lib/SVG/Graph/Bar.rb 94 def x_label_offset( width ) 95 width / 2.0 96 end