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