Class | SVG::Graph::Line |
In: |
lib/SVG/Graph/Line.rb
|
Parent: | SVG::Graph::Graph |
require 'SVG/Graph/Line' fields = %w(Jan Feb Mar); data_sales_02 = [12, 45, 21] data_sales_03 = [15, 30, 40] graph = SVG::Graph::Line.new({ :height => 500, :width => 300, :fields => fields, }) graph.add_data({ :data => data_sales_02, :title => 'Sales 2002', }) graph.add_data({ :data => data_sales_03, :title => 'Sales 2003', }) print "Content-type: image/svg+xml\r\n\r\n"; print graph.burn();
This object aims to allow you to easily create high quality SVG line 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.
www.germane-software/repositories/public/SVG/test/single.rb
The default stylesheet handles upto 10 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 10 data sets as they will have no style and be in black.
Sean E. Russell <serATgermaneHYPHENsoftwareDOTcom>
Copyright 2004 Sean E. Russell This software is available under the Ruby license
area_fill | [RW] | Fill in the area under the plot if true |
show_data_points | [RW] | Show a small circle on the graph where the line goes from one point to the next. |
stacked | [RW] | Accumulates each data set. (i.e. Each point increased by sum ofall previous series at same point). Default is 0, set to ‘1’ to show. |
The constructor takes a hash reference, fields (the names for each field on the X axis) MUST be set, all other values are defaulted to those shown above - with the exception of style_sheet which defaults to using the internal style sheet.
# File lib/SVG/Graph/Line.rb, line 85 85: def initialize config 86: raise "fields was not supplied or is empty" unless config[:fields] && 87: config[:fields].kind_of?(Array) && 88: config[:fields].length > 0 89: super 90: end
In addition to the defaults set in Graph::initialize, sets
# File lib/SVG/Graph/Line.rb, line 97 97: def set_defaults 98: init_with( 99: :show_data_points => true, 100: :show_data_values => true, 101: :stacked => false, 102: :area_fill => false 103: ) 104: 105: 106: self.top_align = self.top_font = self.right_align = self.right_font = 1 107: end
# File lib/SVG/Graph/Line.rb, line 175 175: def calc_coords(field, value, width = field_width, height = field_height) 176: coords = {:x => 0, :y => 0} 177: coords[:x] = width * field 178: coords[:y] = @graph_height - value * height 179: 180: return coords 181: end
# File lib/SVG/Graph/Line.rb, line 149 149: def calculate_left_margin 150: super 151: label_left = @config[:fields][0].length / 2 * font_size * 0.6 152: @border_left = label_left if label_left > @border_left 153: end
# File lib/SVG/Graph/Line.rb, line 183 183: def draw_data 184: minvalue = min_value 185: fieldheight = (@graph_height.to_f - font_size*2*top_font) / 186: (get_y_labels.max - get_y_labels.min) 187: fieldwidth = field_width 188: line = @data.length 189: 190: prev_sum = Array.new(@config[:fields].length).fill(0) 191: cum_sum = Array.new(@config[:fields].length).fill(-minvalue) 192: 193: for data in @data.reverse 194: lpath = "" 195: apath = "" 196: 197: if not stacked then cum_sum.fill(-minvalue) end 198: 199: data[:data].each_index do |i| 200: cum_sum[i] += data[:data][i] 201: 202: c = calc_coords(i, cum_sum[i], fieldwidth, fieldheight) 203: 204: lpath << "#{c[:x]} #{c[:y]} " 205: end 206: 207: if area_fill 208: if stacked then 209: (prev_sum.length - 1).downto 0 do |i| 210: c = calc_coords(i, prev_sum[i], fieldwidth, fieldheight) 211: 212: apath << "#{c[:x]} #{c[:y]} " 213: end 214: 215: c = calc_coords(0, prev_sum[0], fieldwidth, fieldheight) 216: else 217: apath = "V#@graph_height" 218: c = calc_coords(0, 0, fieldwidth, fieldheight) 219: end 220: 221: @graph.add_element("path", { 222: "d" => "M#{c[:x]} #{c[:y]} L" + lpath + apath + "Z", 223: "class" => "fill#{line}" 224: }) 225: end 226: 227: @graph.add_element("path", { 228: "d" => "M0 #@graph_height L" + lpath, 229: "class" => "line#{line}" 230: }) 231: 232: if show_data_points || show_data_values 233: cum_sum.each_index do |i| 234: if show_data_points 235: @graph.add_element( "circle", { 236: "cx" => (fieldwidth * i).to_s, 237: "cy" => (@graph_height - cum_sum[i] * fieldheight).to_s, 238: "r" => "2.5", 239: "class" => "dataPoint#{line}" 240: }) 241: end 242: make_datapoint_text( 243: fieldwidth * i, 244: @graph_height - cum_sum[i] * fieldheight - 6, 245: cum_sum[i] + minvalue 246: ) 247: end 248: end 249: 250: prev_sum = cum_sum.dup 251: line -= 1 252: end 253: end
# File lib/SVG/Graph/Line.rb, line 256 256: def get_css 257: return "/* default line styles */\n.line1{\n fill: none;\n stroke: #ff0000;\n stroke-width: 1px; \n}\n.line2{\n fill: none;\n stroke: #0000ff;\n stroke-width: 1px; \n}\n.line3{\n fill: none;\n stroke: #00ff00;\n stroke-width: 1px; \n}\n.line4{\n fill: none;\n stroke: #ffcc00;\n stroke-width: 1px; \n}\n.line5{\n fill: none;\n stroke: #00ccff;\n stroke-width: 1px; \n}\n.line6{\n fill: none;\n stroke: #ff00ff;\n stroke-width: 1px; \n}\n.line7{\n fill: none;\n stroke: #00ffff;\n stroke-width: 1px; \n}\n.line8{\n fill: none;\n stroke: #ffff00;\n stroke-width: 1px; \n}\n.line9{\n fill: none;\n stroke: #ccc6666;\n stroke-width: 1px; \n}\n.line10{\n fill: none;\n stroke: #663399;\n stroke-width: 1px; \n}\n.line11{\n fill: none;\n stroke: #339900;\n stroke-width: 1px; \n}\n.line12{\n fill: none;\n stroke: #9966FF;\n stroke-width: 1px; \n}\n/* default fill styles */\n.fill1{\n fill: #cc0000;\n fill-opacity: 0.2;\n stroke: none;\n}\n.fill2{\n fill: #0000cc;\n fill-opacity: 0.2;\n stroke: none;\n}\n.fill3{\n fill: #00cc00;\n fill-opacity: 0.2;\n stroke: none;\n}\n.fill4{\n fill: #ffcc00;\n fill-opacity: 0.2;\n stroke: none;\n}\n.fill5{\n fill: #00ccff;\n fill-opacity: 0.2;\n stroke: none;\n}\n.fill6{\n fill: #ff00ff;\n fill-opacity: 0.2;\n stroke: none;\n}\n.fill7{\n fill: #00ffff;\n fill-opacity: 0.2;\n stroke: none;\n}\n.fill8{\n fill: #ffff00;\n fill-opacity: 0.2;\n stroke: none;\n}\n.fill9{\n fill: #cc6666;\n fill-opacity: 0.2;\n stroke: none;\n}\n.fill10{\n fill: #663399;\n fill-opacity: 0.2;\n stroke: none;\n}\n.fill11{\n fill: #339900;\n fill-opacity: 0.2;\n stroke: none;\n}\n.fill12{\n fill: #9966FF;\n fill-opacity: 0.2;\n stroke: none;\n}\n/* default line styles */\n.key1,.dataPoint1{\n fill: #ff0000;\n stroke: none;\n stroke-width: 1px; \n}\n.key2,.dataPoint2{\n fill: #0000ff;\n stroke: none;\n stroke-width: 1px; \n}\n.key3,.dataPoint3{\n fill: #00ff00;\n stroke: none;\n stroke-width: 1px; \n}\n.key4,.dataPoint4{\n fill: #ffcc00;\n stroke: none;\n stroke-width: 1px; \n}\n.key5,.dataPoint5{\n fill: #00ccff;\n stroke: none;\n stroke-width: 1px; \n}\n.key6,.dataPoint6{\n fill: #ff00ff;\n stroke: none;\n stroke-width: 1px; \n}\n.key7,.dataPoint7{\n fill: #00ffff;\n stroke: none;\n stroke-width: 1px; \n}\n.key8,.dataPoint8{\n fill: #ffff00;\n stroke: none;\n stroke-width: 1px; \n}\n.key9,.dataPoint9{\n fill: #cc6666;\n stroke: none;\n stroke-width: 1px; \n}\n.key10,.dataPoint10{\n fill: #663399;\n stroke: none;\n stroke-width: 1px; \n}\n.key11,.dataPoint11{\n fill: #339900;\n stroke: none;\n stroke-width: 1px; \n}\n.key12,.dataPoint12{\n fill: #9966FF;\n stroke: none;\n stroke-width: 1px; \n}\n" 258: end
# File lib/SVG/Graph/Line.rb, line 155 155: def get_y_labels 156: maxvalue = max_value 157: minvalue = min_value 158: range = maxvalue - minvalue 159: top_pad = range == 0 ? 10 : range / 20.0 160: scale_range = (maxvalue + top_pad) - minvalue 161: 162: scale_division = scale_divisions || (scale_range / 10.0) 163: 164: if scale_integers 165: scale_division = scale_division < 1 ? 1 : scale_division.round 166: end 167: 168: rv = [] 169: maxvalue = maxvalue%scale_division == 0 ? 170: maxvalue : maxvalue + scale_division 171: minvalue.step( maxvalue, scale_division ) {|v| rv << v} 172: return rv 173: end
# File lib/SVG/Graph/Line.rb, line 111 111: def max_value 112: max = 0 113: 114: if (stacked == true) then 115: sums = Array.new(@config[:fields].length).fill(0) 116: 117: @data.each do |data| 118: sums.each_index do |i| 119: sums[i] += data[:data][i].to_f 120: end 121: end 122: 123: max = sums.max 124: else 125: max = @data.collect{|x| x[:data].max}.max 126: end 127: 128: return max 129: end
# File lib/SVG/Graph/Line.rb, line 131 131: def min_value 132: min = 0 133: 134: if (min_scale_value.nil? == false) then 135: min = min_scale_value 136: elsif (stacked == true) then 137: min = @data[-1][:data].min 138: else 139: min = @data.collect{|x| x[:data].min}.min 140: end 141: 142: return min 143: end