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

For creating SVG plots of scalar data

Synopsis

  require 'SVG/Graph/Plot'

  # Data sets are x,y pairs
  # Note that multiple data sets can differ in length, and that the
  # data in the datasets needn't be in order; they will be ordered
  # by the plot along the X-axis.
  projection = [
    6, 11,    0, 5,   18, 7,   1, 11,   13, 9,   1, 2,   19, 0,   3, 13,
    7, 9
  ]
  actual = [
    0, 18,    8, 15,    9, 4,   18, 14,   10, 2,   11, 6,  14, 12,
    15, 6,   4, 17,   2, 12
  ]

  graph = SVG::Graph::Plot.new({
          :height => 500,
          :width => 300,
    :key => true,
    :scale_x_integers => true,
    :scale_y_integerrs => true,
  })

  graph.add_data({
          :data => projection
    :title => 'Projected',
  })

  graph.add_data({
          :data => actual,
    :title => 'Actual',
  })

  print graph.burn()

Description

Produces a graph of scalar data.

This object aims to allow you to easily create high quality SVG scalar plots. 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/plot.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.

Unlike the other types of charts, data sets must contain x,y pairs:

  [ 1, 2 ]    # A data set with 1 point: (1,2)
  [ 1,2, 5,6] # A data set with 2 points: (1,2) and (5,6)

See also

Author

Sean E. Russell <serATgermaneHYPHENsoftwareDOTcom>

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

Methods

Constants

X = 0
Y = 1

Attributes

area_fill  [RW]  Fill the area under the line
min_x_value  [RW]  Set the minimum value of the X axis
min_y_value  [RW]  Set the minimum value of the Y axis
scale_x_divisions  [RW]  Determines the scaling for the X axis divisions.
  graph.scale_x_divisions = 2

would cause the graph to attempt to generate labels stepped by 2; EG: 0,2,4,6,8…

scale_x_integers  [RW]  Make the X axis labels integers
scale_y_divisions  [RW]  Determines the scaling for the Y axis divisions.
  graph.scale_y_divisions = 0.5

would cause the graph to attempt to generate labels stepped by 0.5; EG: 0, 0.5, 1, 1.5, 2, …

scale_y_integers  [RW]  Make the Y axis labels integers
show_data_points  [RW]  Show a small circle on the graph where the line goes from one point to the next.

Public Instance methods

Adds data to the plot. The data must be in X,Y pairs; EG

  [ 1, 2 ]    # A data set with 1 point: (1,2)
  [ 1,2, 5,6] # A data set with 2 points: (1,2) and (5,6)

[Source]

     # File SVG/Graph/Plot.rb, line 137
137:       def add_data data
138:         @data = [] unless @data
139: 
140:         raise "No data provided by #{conf.inspect}" unless data[:data] and
141:         data[:data].kind_of? Array
142:         raise "Data supplied must be x,y pairs!  "+
143:           "The data provided contained an odd set of "+
144:           "data points" unless data[:data].length % 2 == 0
145:         return if data[:data].length == 0
146: 
147:         x = []
148:         y = []
149:         data[:data].each_index {|i|
150:           (i%2 == 0 ? x : y) << data[:data][i]
151:         }
152:         sort( x, y )
153:         data[:data] = [x,y]
154:         @data << data
155:       end

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

show_data_values
true
show_data_points
true
area_fill
false
stacked
false

[Source]

     # File SVG/Graph/Plot.rb, line 95
 95:       def set_defaults
 96:         init_with(
 97:                   :show_data_values  => true,
 98:                   :show_data_points  => true,
 99:                   :area_fill         => false,
100:                   :stacked           => false
101:                  )
102:                  self.top_align = self.right_align = self.top_font = self.right_font = 1
103:       end

Protected Instance methods

[Source]

     # File SVG/Graph/Plot.rb, line 164
164:       def calculate_left_margin
165:         super
166:         label_left = get_x_labels[0].to_s.length / 2 * font_size * 0.6
167:         @border_left = label_left if label_left > @border_left
168:       end

[Source]

     # File SVG/Graph/Plot.rb, line 170
170:       def calculate_right_margin
171:         super
172:         label_right = get_x_labels[-1].to_s.length / 2 * font_size * 0.6
173:         @border_right = label_right if label_right > @border_right
174:       end

[Source]

     # File SVG/Graph/Plot.rb, line 252
252:       def draw_data
253:         line = 1
254: 
255:         x_min, x_max, x_div = x_range
256:         y_min, y_max, y_div = y_range
257:         x_step = (@graph_width.to_f - font_size*2) / (x_max-x_min)
258:         y_step = (@graph_height.to_f -  font_size*2) / (y_max-y_min)
259: 
260:         for data in @data
261:           x_points = data[:data][X]
262:           y_points = data[:data][Y]
263: 
264:           lpath = "L"
265:           x_start = 0
266:           y_start = 0
267:           x_points.each_index { |idx|
268:             x = (x_points[idx] -  x_min) * x_step
269:             y = @graph_height - (y_points[idx] -  y_min) * y_step
270:             x_start, y_start = x,y if idx == 0
271:             lpath << "#{x} #{y} "
272:           }
273: 
274:           if area_fill
275:             @graph.add_element( "path", {
276:               "d" => "M#{x_start} #@graph_height #{lpath} V#@graph_height Z",
277:               "class" => "fill#{line}"
278:             })
279:           end
280: 
281:           @graph.add_element( "path", {
282:             "d" => "M#{x_start} #{y_start} #{lpath}",
283:             "class" => "line#{line}"
284:           })
285: 
286:           if show_data_points || show_data_values
287:             x_points.each_index { |idx|
288:               x = (x_points[idx] -  x_min) * x_step
289:               y = @graph_height - (y_points[idx] -  y_min) * y_step
290:               if show_data_points
291:                 @graph.add_element( "circle", {
292:                   "cx" => x.to_s,
293:                   "cy" => y.to_s,
294:                   "r" => "2.5",
295:                   "class" => "dataPoint#{line}"
296:                 })
297:                 add_popup(x, y, format( x_points[idx], y_points[idx] )) if add_popups
298:               end
299:               make_datapoint_text( x, y-6, y_points[idx] ) if show_data_values
300:             }
301:           end
302:           line += 1
303:         end
304:       end

[Source]

     # File SVG/Graph/Plot.rb, line 240
240:       def field_height
241:         values = get_y_values
242:         max = @data.collect{|x| x[:data][Y].max }.max
243:         if values.length == 1
244:           dx = values[-1]
245:         else
246:           dx = (max - values[-1]).to_f / (values[-1] - values[-2])
247:         end
248:         (@graph_height.to_f - font_size*2*top_font) /
249:           (values.length + dx - top_align)
250:       end

[Source]

     # File SVG/Graph/Plot.rb, line 205
205:       def field_width
206:         values = get_x_values
207:         max = @data.collect{|x| x[:data][X][-1]}.max
208:         dx = (max - values[-1]).to_f / (values[-1] - values[-2])
209:         (@graph_width.to_f - font_size*2*right_font) /
210:           (values.length + dx - right_align)
211:       end

[Source]

     # File SVG/Graph/Plot.rb, line 306
306:       def format x, y
307:         "(#{(x * 100).to_i / 100}, #{(y * 100).to_i / 100})"
308:       end

[Source]

     # File SVG/Graph/Plot.rb, line 310
310:       def get_css
311:         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"
312:       end
get_x_labels()

Alias for get_x_values

[Source]

     # File SVG/Graph/Plot.rb, line 197
197:       def get_x_values
198:         min_value, max_value, scale_division = x_range
199:         rv = []
200:         min_value.step( max_value, scale_division ) {|v| rv << v}
201:         return rv
202:       end
get_y_labels()

Alias for get_y_values

[Source]

     # File SVG/Graph/Plot.rb, line 232
232:       def get_y_values
233:         min_value, max_value, scale_division = y_range
234:         rv = []
235:         min_value.step( max_value, scale_division ) {|v| rv << v}
236:         return rv
237:       end

[Source]

     # File SVG/Graph/Plot.rb, line 160
160:       def keys
161:         @data.collect{ |x| x[:title] }
162:       end

[Source]

     # File SVG/Graph/Plot.rb, line 179
179:       def x_range
180:         max_value = @data.collect{|x| x[:data][X][-1] }.max
181:         min_value = @data.collect{|x| x[:data][X][0] }.min
182:         min_value = min_value<min_x_value ? min_value : min_x_value if min_x_value
183: 
184:         range = max_value - min_value
185:         right_pad = range == 0 ? 10 : range / 20.0
186:         scale_range = (max_value + right_pad) - min_value
187: 
188:         scale_division = scale_x_divisions || (scale_range / 10.0)
189: 
190:         if scale_x_integers
191:           scale_division = scale_division < 1 ? 1 : scale_division.round
192:         end
193: 
194:         [min_value, max_value, scale_division]
195:       end

[Source]

     # File SVG/Graph/Plot.rb, line 214
214:       def y_range
215:         max_value = @data.collect{|x| x[:data][Y].max }.max
216:         min_value = @data.collect{|x| x[:data][Y].min }.min
217:         min_value = min_value<min_y_value ? min_value : min_y_value if min_y_value
218: 
219:         range = max_value - min_value
220:         top_pad = range == 0 ? 10 : range / 20.0
221:         scale_range = (max_value + top_pad) - min_value
222: 
223:         scale_division = scale_y_divisions || (scale_range / 10.0)
224: 
225:         if scale_y_integers
226:           scale_division = scale_division < 1 ? 1 : scale_division.round
227:         end
228: 
229:         return [min_value, max_value, scale_division]
230:       end

[Validate]