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