Class SVG::Graph::Line
In: SVG/Graph/Line.rb
Parent: SVG::Graph::Graph

Create presentation quality SVG line graphs easily

Synopsis

  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();

Description

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.

Examples

www.germane-software/repositories/public/SVG/test/single.rb

Notes

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.

See also

Author

Sean E. Russell <serATgermaneHYPHENsoftwareDOTcom>

Copyright 2004 Sean E. Russell This software is available under the Ruby license

Methods

Attributes

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 of
all previous series at same point). Default is 0, set to ‘1’ to show.

Public Class methods

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.

[Source]

    # File 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

Public Instance methods

In addition to the defaults set in Graph::initialize, sets

show_data_points
true
show_data_values
true
stacked
false
area_fill
false

[Source]

     # File 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:         self.top_align = self.top_font = self.right_align = self.right_font = 1
106:       end

Protected Instance methods

[Source]

     # File SVG/Graph/Line.rb, line 174
174:       def calc_coords(field, value, width = field_width, height = field_height)
175:         coords = {:x => 0, :y => 0}
176:         coords[:x] = width * field
177:         coords[:y] = @graph_height - value * height
178:       
179:         return coords
180:       end

[Source]

     # File SVG/Graph/Line.rb, line 148
148:       def calculate_left_margin
149:         super
150:         label_left = @config[:fields][0].length / 2 * font_size * 0.6
151:         @border_left = label_left if label_left > @border_left
152:       end

[Source]

     # File SVG/Graph/Line.rb, line 182
182:       def draw_data
183:         minvalue = min_value
184:         fieldheight = (@graph_height.to_f - font_size*2*top_font) / 
185:                          (get_y_labels.max - get_y_labels.min)
186:         fieldwidth = field_width
187:         line = @data.length
188: 
189:         prev_sum = Array.new(@config[:fields].length).fill(0)
190:         cum_sum = Array.new(@config[:fields].length).fill(-minvalue)
191: 
192:         for data in @data.reverse
193:           lpath = ""
194:           apath = ""
195: 
196:           if not stacked then cum_sum.fill(-minvalue) end
197:           
198:           data[:data].each_index do |i|
199:             cum_sum[i] += data[:data][i]
200:             
201:             c = calc_coords(i, cum_sum[i], fieldwidth, fieldheight)
202:             
203:             lpath << "#{c[:x]} #{c[:y]} "
204:           end
205:         
206:           if area_fill
207:             if stacked then
208:               (prev_sum.length - 1).downto 0 do |i|
209:                 c = calc_coords(i, prev_sum[i], fieldwidth, fieldheight)
210:                 
211:                 apath << "#{c[:x]} #{c[:y]} "
212:               end
213:           
214:               c = calc_coords(0, prev_sum[0], fieldwidth, fieldheight)
215:             else
216:               apath = "V#@graph_height"
217:               c = calc_coords(0, 0, fieldwidth, fieldheight)
218:             end
219:               
220:             @graph.add_element("path", {
221:               "d" => "M#{c[:x]} #{c[:y]} L" + lpath + apath + "Z",
222:               "class" => "fill#{line}"
223:             })
224:           end
225:         
226:           @graph.add_element("path", {
227:             "d" => "M0 #@graph_height L" + lpath,
228:             "class" => "line#{line}"
229:           })
230:           
231:           if show_data_points || show_data_values
232:             cum_sum.each_index do |i|
233:               if show_data_points
234:                 @graph.add_element( "circle", {
235:                   "cx" => (fieldwidth * i).to_s,
236:                   "cy" => (@graph_height - cum_sum[i] * fieldheight).to_s,
237:                   "r" => "2.5",
238:                   "class" => "dataPoint#{line}"
239:                 })
240:               end
241:               make_datapoint_text( 
242:                 fieldwidth * i, 
243:                 @graph_height - cum_sum[i] * fieldheight - 6,
244:                 cum_sum[i] + minvalue
245:               )
246:             end
247:           end
248: 
249:           prev_sum = cum_sum.dup
250:           line -= 1
251:         end
252:       end

[Source]

     # File SVG/Graph/Line.rb, line 255
255:       def get_css
256:         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"
257:       end

[Source]

     # File SVG/Graph/Line.rb, line 144
144:       def get_x_labels
145:         @config[:fields]
146:       end

[Source]

     # File SVG/Graph/Line.rb, line 154
154:       def get_y_labels
155:         maxvalue = max_value
156:         minvalue = min_value
157:         range = maxvalue - minvalue
158:         top_pad = range == 0 ? 10 : range / 20.0
159:         scale_range = (maxvalue + top_pad) - minvalue
160: 
161:         scale_division = scale_divisions || (scale_range / 10.0)
162: 
163:         if scale_integers
164:           scale_division = scale_division < 1 ? 1 : scale_division.round
165:         end
166: 
167:         rv = []
168:         maxvalue = maxvalue%scale_division == 0 ? 
169:           maxvalue : maxvalue + scale_division
170:         minvalue.step( maxvalue, scale_division ) {|v| rv << v}
171:         return rv
172:       end

[Source]

     # File SVG/Graph/Line.rb, line 110
110:       def max_value
111:         max = 0
112:         
113:         if (stacked == true) then
114:           sums = Array.new(@config[:fields].length).fill(0)
115: 
116:           @data.each do |data|
117:             sums.each_index do |i|
118:               sums[i] += data[:data][i].to_f
119:             end
120:           end
121:           
122:           max = sums.max
123:         else
124:           max = @data.collect{|x| x[:data].max}.max
125:         end
126: 
127:         return max
128:       end

[Source]

     # File SVG/Graph/Line.rb, line 130
130:       def min_value
131:         min = 0
132:         
133:         if (min_scale_value.nil? == false) then
134:           min = min_scale_value
135:         elsif (stacked == true) then
136:           min = @data[-1][:data].min
137:         else
138:           min = @data.collect{|x| x[:data].min}.min
139:         end
140: 
141:         return min
142:       end

[Validate]