Chris@909: require 'SVG/Graph/Graph' Chris@909: Chris@909: module SVG Chris@909: module Graph Chris@909: # === For creating SVG plots of scalar data Chris@909: # Chris@909: # = Synopsis Chris@909: # Chris@909: # require 'SVG/Graph/Plot' Chris@909: # Chris@909: # # Data sets are x,y pairs Chris@909: # # Note that multiple data sets can differ in length, and that the Chris@909: # # data in the datasets needn't be in order; they will be ordered Chris@909: # # by the plot along the X-axis. Chris@909: # projection = [ Chris@909: # 6, 11, 0, 5, 18, 7, 1, 11, 13, 9, 1, 2, 19, 0, 3, 13, Chris@909: # 7, 9 Chris@909: # ] Chris@909: # actual = [ Chris@909: # 0, 18, 8, 15, 9, 4, 18, 14, 10, 2, 11, 6, 14, 12, Chris@909: # 15, 6, 4, 17, 2, 12 Chris@909: # ] Chris@909: # Chris@909: # graph = SVG::Graph::Plot.new({ Chris@909: # :height => 500, Chris@909: # :width => 300, Chris@909: # :key => true, Chris@909: # :scale_x_integers => true, Chris@909: # :scale_y_integerrs => true, Chris@909: # }) Chris@909: # Chris@909: # graph.add_data({ Chris@909: # :data => projection Chris@909: # :title => 'Projected', Chris@909: # }) Chris@909: # Chris@909: # graph.add_data({ Chris@909: # :data => actual, Chris@909: # :title => 'Actual', Chris@909: # }) Chris@909: # Chris@909: # print graph.burn() Chris@909: # Chris@909: # = Description Chris@909: # Chris@909: # Produces a graph of scalar data. Chris@909: # Chris@909: # This object aims to allow you to easily create high quality Chris@909: # SVG[http://www.w3c.org/tr/svg] scalar plots. You can either use the Chris@909: # default style sheet or supply your own. Either way there are many options Chris@909: # which can be configured to give you control over how the graph is Chris@909: # generated - with or without a key, data elements at each point, title, Chris@909: # subtitle etc. Chris@909: # Chris@909: # = Examples Chris@909: # Chris@909: # http://www.germane-software/repositories/public/SVG/test/plot.rb Chris@909: # Chris@909: # = Notes Chris@909: # Chris@909: # The default stylesheet handles upto 10 data sets, if you Chris@909: # use more you must create your own stylesheet and add the Chris@909: # additional settings for the extra data sets. You will know Chris@909: # if you go over 10 data sets as they will have no style and Chris@909: # be in black. Chris@909: # Chris@909: # Unlike the other types of charts, data sets must contain x,y pairs: Chris@909: # Chris@909: # [ 1, 2 ] # A data set with 1 point: (1,2) Chris@909: # [ 1,2, 5,6] # A data set with 2 points: (1,2) and (5,6) Chris@909: # Chris@909: # = See also Chris@909: # Chris@909: # * SVG::Graph::Graph Chris@909: # * SVG::Graph::BarHorizontal Chris@909: # * SVG::Graph::Bar Chris@909: # * SVG::Graph::Line Chris@909: # * SVG::Graph::Pie Chris@909: # * SVG::Graph::TimeSeries Chris@909: # Chris@909: # == Author Chris@909: # Chris@909: # Sean E. Russell Chris@909: # Chris@909: # Copyright 2004 Sean E. Russell Chris@909: # This software is available under the Ruby license[LICENSE.txt] Chris@909: # Chris@909: class Plot < Graph Chris@909: Chris@909: # In addition to the defaults set by Graph::initialize, sets Chris@909: # [show_data_values] true Chris@909: # [show_data_points] true Chris@909: # [area_fill] false Chris@909: # [stacked] false Chris@909: def set_defaults Chris@909: init_with( Chris@909: :show_data_values => true, Chris@909: :show_data_points => true, Chris@909: :area_fill => false, Chris@909: :stacked => false Chris@909: ) Chris@909: self.top_align = self.right_align = self.top_font = self.right_font = 1 Chris@909: end Chris@909: Chris@909: # Determines the scaling for the X axis divisions. Chris@909: # Chris@909: # graph.scale_x_divisions = 2 Chris@909: # Chris@909: # would cause the graph to attempt to generate labels stepped by 2; EG: Chris@909: # 0,2,4,6,8... Chris@909: attr_accessor :scale_x_divisions Chris@909: # Determines the scaling for the Y axis divisions. Chris@909: # Chris@909: # graph.scale_y_divisions = 0.5 Chris@909: # Chris@909: # would cause the graph to attempt to generate labels stepped by 0.5; EG: Chris@909: # 0, 0.5, 1, 1.5, 2, ... Chris@909: attr_accessor :scale_y_divisions Chris@909: # Make the X axis labels integers Chris@909: attr_accessor :scale_x_integers Chris@909: # Make the Y axis labels integers Chris@909: attr_accessor :scale_y_integers Chris@909: # Fill the area under the line Chris@909: attr_accessor :area_fill Chris@909: # Show a small circle on the graph where the line Chris@909: # goes from one point to the next. Chris@909: attr_accessor :show_data_points Chris@909: # Set the minimum value of the X axis Chris@909: attr_accessor :min_x_value Chris@909: # Set the minimum value of the Y axis Chris@909: attr_accessor :min_y_value Chris@909: Chris@909: Chris@909: # Adds data to the plot. The data must be in X,Y pairs; EG Chris@909: # [ 1, 2 ] # A data set with 1 point: (1,2) Chris@909: # [ 1,2, 5,6] # A data set with 2 points: (1,2) and (5,6) Chris@909: def add_data data Chris@909: @data = [] unless @data Chris@909: Chris@909: raise "No data provided by #{conf.inspect}" unless data[:data] and Chris@909: data[:data].kind_of? Array Chris@909: raise "Data supplied must be x,y pairs! "+ Chris@909: "The data provided contained an odd set of "+ Chris@909: "data points" unless data[:data].length % 2 == 0 Chris@909: return if data[:data].length == 0 Chris@909: Chris@909: x = [] Chris@909: y = [] Chris@909: data[:data].each_index {|i| Chris@909: (i%2 == 0 ? x : y) << data[:data][i] Chris@909: } Chris@909: sort( x, y ) Chris@909: data[:data] = [x,y] Chris@909: @data << data Chris@909: end Chris@909: Chris@909: Chris@909: protected Chris@909: Chris@909: def keys Chris@909: @data.collect{ |x| x[:title] } Chris@909: end Chris@909: Chris@909: def calculate_left_margin Chris@909: super Chris@909: label_left = get_x_labels[0].to_s.length / 2 * font_size * 0.6 Chris@909: @border_left = label_left if label_left > @border_left Chris@909: end Chris@909: Chris@909: def calculate_right_margin Chris@909: super Chris@909: label_right = get_x_labels[-1].to_s.length / 2 * font_size * 0.6 Chris@909: @border_right = label_right if label_right > @border_right Chris@909: end Chris@909: Chris@909: Chris@909: X = 0 Chris@909: Y = 1 Chris@909: def x_range Chris@909: max_value = @data.collect{|x| x[:data][X][-1] }.max Chris@909: min_value = @data.collect{|x| x[:data][X][0] }.min Chris@909: min_value = min_value "M#{x_start} #@graph_height #{lpath} V#@graph_height Z", Chris@909: "class" => "fill#{line}" Chris@909: }) Chris@909: end Chris@909: Chris@909: @graph.add_element( "path", { Chris@909: "d" => "M#{x_start} #{y_start} #{lpath}", Chris@909: "class" => "line#{line}" Chris@909: }) Chris@909: Chris@909: if show_data_points || show_data_values Chris@909: x_points.each_index { |idx| Chris@909: x = (x_points[idx] - x_min) * x_step Chris@909: y = @graph_height - (y_points[idx] - y_min) * y_step Chris@909: if show_data_points Chris@909: @graph.add_element( "circle", { Chris@909: "cx" => x.to_s, Chris@909: "cy" => y.to_s, Chris@909: "r" => "2.5", Chris@909: "class" => "dataPoint#{line}" Chris@909: }) Chris@909: add_popup(x, y, format( x_points[idx], y_points[idx] )) if add_popups Chris@909: end Chris@909: make_datapoint_text( x, y-6, y_points[idx] ) if show_data_values Chris@909: } Chris@909: end Chris@909: line += 1 Chris@909: end Chris@909: end Chris@909: Chris@909: def format x, y Chris@909: "(#{(x * 100).to_i / 100}, #{(y * 100).to_i / 100})" Chris@909: end Chris@909: Chris@909: def get_css Chris@909: return <