Chris@1294: begin Chris@1294: require 'zlib' Chris@1294: rescue Chris@1464: # Zlib not available Chris@1294: end Chris@1294: Chris@1294: require 'rexml/document' Chris@1294: Chris@1294: module SVG Chris@1294: module Graph Chris@1294: VERSION = '@ANT_VERSION@' Chris@1294: Chris@1294: # === Base object for generating SVG Graphs Chris@1294: # Chris@1294: # == Synopsis Chris@1294: # Chris@1294: # This class is only used as a superclass of specialized charts. Do not Chris@1294: # attempt to use this class directly, unless creating a new chart type. Chris@1294: # Chris@1294: # For examples of how to subclass this class, see the existing specific Chris@1294: # subclasses, such as SVG::Graph::Pie. Chris@1294: # Chris@1294: # == Examples Chris@1294: # Chris@1294: # For examples of how to use this package, see either the test files, or Chris@1294: # the documentation for the specific class you want to use. Chris@1294: # Chris@1294: # * file:test/plot.rb Chris@1294: # * file:test/single.rb Chris@1294: # * file:test/test.rb Chris@1294: # * file:test/timeseries.rb Chris@1294: # Chris@1294: # == Description Chris@1294: # Chris@1294: # This package should be used as a base for creating SVG graphs. Chris@1294: # Chris@1294: # == Acknowledgements Chris@1294: # Chris@1294: # Leo Lapworth for creating the SVG::TT::Graph package which this Ruby Chris@1294: # port is based on. Chris@1294: # Chris@1294: # Stephen Morgan for creating the TT template and SVG. Chris@1294: # Chris@1294: # == See Chris@1294: # Chris@1294: # * SVG::Graph::BarHorizontal Chris@1294: # * SVG::Graph::Bar Chris@1294: # * SVG::Graph::Line Chris@1294: # * SVG::Graph::Pie Chris@1294: # * SVG::Graph::Plot 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 Graph Chris@1294: include REXML Chris@1294: Chris@1294: # Initialize the graph object with the graph settings. You won't Chris@1294: # instantiate this class directly; see the subclass for options. Chris@1294: # [width] 500 Chris@1294: # [height] 300 Chris@1294: # [show_x_guidelines] false Chris@1294: # [show_y_guidelines] true Chris@1294: # [show_data_values] true Chris@1294: # [min_scale_value] 0 Chris@1294: # [show_x_labels] true Chris@1294: # [stagger_x_labels] false Chris@1294: # [rotate_x_labels] false Chris@1294: # [step_x_labels] 1 Chris@1294: # [step_include_first_x_label] true Chris@1294: # [show_y_labels] true Chris@1294: # [rotate_y_labels] false Chris@1294: # [scale_integers] false Chris@1294: # [show_x_title] false Chris@1294: # [x_title] 'X Field names' Chris@1294: # [show_y_title] false Chris@1294: # [y_title_text_direction] :bt Chris@1294: # [y_title] 'Y Scale' Chris@1294: # [show_graph_title] false Chris@1294: # [graph_title] 'Graph Title' Chris@1294: # [show_graph_subtitle] false Chris@1294: # [graph_subtitle] 'Graph Sub Title' Chris@1294: # [key] true, Chris@1294: # [key_position] :right, # bottom or righ Chris@1294: # [font_size] 12 Chris@1294: # [title_font_size] 16 Chris@1294: # [subtitle_font_size] 14 Chris@1294: # [x_label_font_size] 12 Chris@1294: # [x_title_font_size] 14 Chris@1294: # [y_label_font_size] 12 Chris@1294: # [y_title_font_size] 14 Chris@1294: # [key_font_size] 10 Chris@1294: # [no_css] false Chris@1294: # [add_popups] false Chris@1294: def initialize( config ) Chris@1294: @config = config Chris@1294: Chris@1294: self.top_align = self.top_font = self.right_align = self.right_font = 0 Chris@1294: Chris@1294: init_with({ Chris@1294: :width => 500, Chris@1294: :height => 300, Chris@1294: :show_x_guidelines => false, Chris@1294: :show_y_guidelines => true, Chris@1294: :show_data_values => true, Chris@1294: Chris@1294: # :min_scale_value => 0, Chris@1294: Chris@1294: :show_x_labels => true, Chris@1294: :stagger_x_labels => false, Chris@1294: :rotate_x_labels => false, Chris@1294: :step_x_labels => 1, Chris@1294: :step_include_first_x_label => true, Chris@1294: Chris@1294: :show_y_labels => true, Chris@1294: :rotate_y_labels => false, Chris@1294: :stagger_y_labels => false, Chris@1294: :scale_integers => false, Chris@1294: Chris@1294: :show_x_title => false, Chris@1294: :x_title => 'X Field names', Chris@1294: Chris@1294: :show_y_title => false, Chris@1294: :y_title_text_direction => :bt, Chris@1294: :y_title => 'Y Scale', Chris@1294: Chris@1294: :show_graph_title => false, Chris@1294: :graph_title => 'Graph Title', Chris@1294: :show_graph_subtitle => false, Chris@1294: :graph_subtitle => 'Graph Sub Title', Chris@1294: :key => true, Chris@1294: :key_position => :right, # bottom or right Chris@1294: Chris@1294: :font_size =>12, Chris@1294: :title_font_size =>16, Chris@1294: :subtitle_font_size =>14, Chris@1294: :x_label_font_size =>12, Chris@1294: :x_title_font_size =>14, Chris@1294: :y_label_font_size =>12, Chris@1294: :y_title_font_size =>14, Chris@1294: :key_font_size =>10, Chris@1294: Chris@1294: :no_css =>false, Chris@1294: :add_popups =>false, Chris@1294: }) Chris@1294: Chris@1294: set_defaults if respond_to? :set_defaults Chris@1294: Chris@1294: init_with config Chris@1294: end Chris@1294: Chris@1294: Chris@1294: # This method allows you do add data to the graph object. Chris@1294: # It can be called several times to add more data sets in. Chris@1294: # Chris@1294: # data_sales_02 = [12, 45, 21]; Chris@1294: # Chris@1294: # graph.add_data({ Chris@1294: # :data => data_sales_02, Chris@1294: # :title => 'Sales 2002' Chris@1294: # }) Chris@1294: def add_data conf Chris@1294: @data = [] unless defined? @data Chris@1294: Chris@1294: if conf[:data] and conf[:data].kind_of? Array Chris@1294: @data << conf Chris@1294: else Chris@1294: raise "No data provided by #{conf.inspect}" Chris@1294: end Chris@1294: end Chris@1294: Chris@1294: Chris@1294: # This method removes all data from the object so that you can Chris@1294: # reuse it to create a new graph but with the same config options. Chris@1294: # Chris@1294: # graph.clear_data Chris@1294: def clear_data Chris@1294: @data = [] Chris@1294: end Chris@1294: Chris@1294: Chris@1294: # This method processes the template with the data and Chris@1294: # config which has been set and returns the resulting SVG. Chris@1294: # Chris@1294: # This method will croak unless at least one data set has Chris@1294: # been added to the graph object. Chris@1294: # Chris@1294: # print graph.burn Chris@1294: def burn Chris@1294: raise "No data available" unless @data.size > 0 Chris@1294: Chris@1294: calculations if respond_to? :calculations Chris@1294: Chris@1294: start_svg Chris@1294: calculate_graph_dimensions Chris@1294: @foreground = Element.new( "g" ) Chris@1294: draw_graph Chris@1294: draw_titles Chris@1294: draw_legend Chris@1294: draw_data Chris@1294: @graph.add_element( @foreground ) Chris@1294: style Chris@1294: Chris@1294: data = "" Chris@1294: @doc.write( data, 0 ) Chris@1294: Chris@1294: if @config[:compress] Chris@1464: if Object.const_defined?(:Zlib) Chris@1294: inp, out = IO.pipe Chris@1294: gz = Zlib::GzipWriter.new( out ) Chris@1294: gz.write data Chris@1294: gz.close Chris@1294: data = inp.read Chris@1294: else Chris@1294: data << ""; Chris@1294: end Chris@1294: end Chris@1294: Chris@1294: return data Chris@1294: end Chris@1294: Chris@1294: Chris@1294: # Set the height of the graph box, this is the total height Chris@1294: # of the SVG box created - not the graph it self which auto Chris@1294: # scales to fix the space. Chris@1294: attr_accessor :height Chris@1294: # Set the width of the graph box, this is the total width Chris@1294: # of the SVG box created - not the graph it self which auto Chris@1294: # scales to fix the space. Chris@1294: attr_accessor :width Chris@1294: # Set the path to an external stylesheet, set to '' if Chris@1294: # you want to revert back to using the defaut internal version. Chris@1294: # Chris@1294: # To create an external stylesheet create a graph using the Chris@1294: # default internal version and copy the stylesheet section to Chris@1294: # an external file and edit from there. Chris@1294: attr_accessor :style_sheet Chris@1294: # (Bool) Show the value of each element of data on the graph Chris@1294: attr_accessor :show_data_values Chris@1294: # The point at which the Y axis starts, defaults to '0', Chris@1294: # if set to nil it will default to the minimum data value. Chris@1294: attr_accessor :min_scale_value Chris@1294: # Whether to show labels on the X axis or not, defaults Chris@1294: # to true, set to false if you want to turn them off. Chris@1294: attr_accessor :show_x_labels Chris@1294: # This puts the X labels at alternative levels so if they Chris@1294: # are long field names they will not overlap so easily. Chris@1294: # Default it false, to turn on set to true. Chris@1294: attr_accessor :stagger_x_labels Chris@1294: # This puts the Y labels at alternative levels so if they Chris@1294: # are long field names they will not overlap so easily. Chris@1294: # Default it false, to turn on set to true. Chris@1294: attr_accessor :stagger_y_labels Chris@1294: # This turns the X axis labels by 90 degrees. Chris@1294: # Default it false, to turn on set to true. Chris@1294: attr_accessor :rotate_x_labels Chris@1294: # This turns the Y axis labels by 90 degrees. Chris@1294: # Default it false, to turn on set to true. Chris@1294: attr_accessor :rotate_y_labels Chris@1294: # How many "steps" to use between displayed X axis labels, Chris@1294: # a step of one means display every label, a step of two results Chris@1294: # in every other label being displayed (label label label), Chris@1294: # a step of three results in every third label being displayed Chris@1294: # (label label label) and so on. Chris@1294: attr_accessor :step_x_labels Chris@1294: # Whether to (when taking "steps" between X axis labels) step from Chris@1294: # the first label (i.e. always include the first label) or step from Chris@1294: # the X axis origin (i.e. start with a gap if step_x_labels is greater Chris@1294: # than one). Chris@1294: attr_accessor :step_include_first_x_label Chris@1294: # Whether to show labels on the Y axis or not, defaults Chris@1294: # to true, set to false if you want to turn them off. Chris@1294: attr_accessor :show_y_labels Chris@1294: # Ensures only whole numbers are used as the scale divisions. Chris@1294: # Default it false, to turn on set to true. This has no effect if Chris@1294: # scale divisions are less than 1. Chris@1294: attr_accessor :scale_integers Chris@1294: # This defines the gap between markers on the Y axis, Chris@1294: # default is a 10th of the max_value, e.g. you will have Chris@1294: # 10 markers on the Y axis. NOTE: do not set this too Chris@1294: # low - you are limited to 999 markers, after that the Chris@1294: # graph won't generate. Chris@1294: attr_accessor :scale_divisions Chris@1294: # Whether to show the title under the X axis labels, Chris@1294: # default is false, set to true to show. Chris@1294: attr_accessor :show_x_title Chris@1294: # What the title under X axis should be, e.g. 'Months'. Chris@1294: attr_accessor :x_title Chris@1294: # Whether to show the title under the Y axis labels, Chris@1294: # default is false, set to true to show. Chris@1294: attr_accessor :show_y_title Chris@1294: # Aligns writing mode for Y axis label. Chris@1294: # Defaults to :bt (Bottom to Top). Chris@1294: # Change to :tb (Top to Bottom) to reverse. Chris@1294: attr_accessor :y_title_text_direction Chris@1294: # What the title under Y axis should be, e.g. 'Sales in thousands'. Chris@1294: attr_accessor :y_title Chris@1294: # Whether to show a title on the graph, defaults Chris@1294: # to false, set to true to show. Chris@1294: attr_accessor :show_graph_title Chris@1294: # What the title on the graph should be. Chris@1294: attr_accessor :graph_title Chris@1294: # Whether to show a subtitle on the graph, defaults Chris@1294: # to false, set to true to show. Chris@1294: attr_accessor :show_graph_subtitle Chris@1294: # What the subtitle on the graph should be. Chris@1294: attr_accessor :graph_subtitle Chris@1294: # Whether to show a key, defaults to false, set to Chris@1294: # true if you want to show it. Chris@1294: attr_accessor :key Chris@1294: # Where the key should be positioned, defaults to Chris@1294: # :right, set to :bottom if you want to move it. Chris@1294: attr_accessor :key_position Chris@1294: # Set the font size (in points) of the data point labels Chris@1294: attr_accessor :font_size Chris@1294: # Set the font size of the X axis labels Chris@1294: attr_accessor :x_label_font_size Chris@1294: # Set the font size of the X axis title Chris@1294: attr_accessor :x_title_font_size Chris@1294: # Set the font size of the Y axis labels Chris@1294: attr_accessor :y_label_font_size Chris@1294: # Set the font size of the Y axis title Chris@1294: attr_accessor :y_title_font_size Chris@1294: # Set the title font size Chris@1294: attr_accessor :title_font_size Chris@1294: # Set the subtitle font size Chris@1294: attr_accessor :subtitle_font_size Chris@1294: # Set the key font size Chris@1294: attr_accessor :key_font_size Chris@1294: # Show guidelines for the X axis Chris@1294: attr_accessor :show_x_guidelines Chris@1294: # Show guidelines for the Y axis Chris@1294: attr_accessor :show_y_guidelines Chris@1294: # Do not use CSS if set to true. Many SVG viewers do not support CSS, but Chris@1294: # not using CSS can result in larger SVGs as well as making it impossible to Chris@1294: # change colors after the chart is generated. Defaults to false. Chris@1294: attr_accessor :no_css Chris@1294: # Add popups for the data points on some graphs Chris@1294: attr_accessor :add_popups Chris@1294: Chris@1294: Chris@1294: protected Chris@1294: Chris@1294: def sort( *arrys ) Chris@1294: sort_multiple( arrys ) Chris@1294: end Chris@1294: Chris@1294: # Overwrite configuration options with supplied options. Used Chris@1294: # by subclasses. Chris@1294: def init_with config Chris@1294: config.each { |key, value| Chris@1294: self.send((key.to_s+"=").to_sym, value ) if respond_to? key.to_sym Chris@1294: } Chris@1294: end Chris@1294: Chris@1294: attr_accessor :top_align, :top_font, :right_align, :right_font Chris@1294: Chris@1294: KEY_BOX_SIZE = 12 Chris@1294: Chris@1294: # Override this (and call super) to change the margin to the left Chris@1294: # of the plot area. Results in @border_left being set. Chris@1294: def calculate_left_margin Chris@1294: @border_left = 7 Chris@1294: # Check for Y labels Chris@1294: max_y_label_height_px = rotate_y_labels ? Chris@1294: y_label_font_size : Chris@1294: get_y_labels.max{|a,b| Chris@1294: a.to_s.length<=>b.to_s.length Chris@1294: }.to_s.length * y_label_font_size * 0.6 Chris@1294: @border_left += max_y_label_height_px if show_y_labels Chris@1294: @border_left += max_y_label_height_px + 10 if stagger_y_labels Chris@1294: @border_left += y_title_font_size + 5 if show_y_title Chris@1294: end Chris@1294: Chris@1294: Chris@1294: # Calculates the width of the widest Y label. This will be the Chris@1294: # character height if the Y labels are rotated Chris@1294: def max_y_label_width_px Chris@1294: return font_size if rotate_y_labels Chris@1294: end Chris@1294: Chris@1294: Chris@1294: # Override this (and call super) to change the margin to the right Chris@1294: # of the plot area. Results in @border_right being set. Chris@1294: def calculate_right_margin Chris@1294: @border_right = 7 Chris@1294: if key and key_position == :right Chris@1294: val = keys.max { |a,b| a.length <=> b.length } Chris@1294: @border_right += val.length * key_font_size * 0.6 Chris@1294: @border_right += KEY_BOX_SIZE Chris@1294: @border_right += 10 # Some padding around the box Chris@1294: end Chris@1294: end Chris@1294: Chris@1294: Chris@1294: # Override this (and call super) to change the margin to the top Chris@1294: # of the plot area. Results in @border_top being set. Chris@1294: def calculate_top_margin Chris@1294: @border_top = 5 Chris@1294: @border_top += title_font_size if show_graph_title Chris@1294: @border_top += 5 Chris@1294: @border_top += subtitle_font_size if show_graph_subtitle Chris@1294: end Chris@1294: Chris@1294: Chris@1294: # Adds pop-up point information to a graph. Chris@1294: def add_popup( x, y, label ) Chris@1294: txt_width = label.length * font_size * 0.6 + 10 Chris@1294: tx = (x+txt_width > width ? x-5 : x+5) Chris@1294: t = @foreground.add_element( "text", { Chris@1294: "x" => tx.to_s, Chris@1294: "y" => (y - font_size).to_s, Chris@1294: "visibility" => "hidden", Chris@1294: }) Chris@1294: t.attributes["style"] = "fill: #000; "+ Chris@1294: (x+txt_width > width ? "text-anchor: end;" : "text-anchor: start;") Chris@1294: t.text = label.to_s Chris@1294: t.attributes["id"] = t.object_id.to_s Chris@1294: Chris@1294: @foreground.add_element( "circle", { Chris@1294: "cx" => x.to_s, Chris@1294: "cy" => y.to_s, Chris@1294: "r" => "10", Chris@1294: "style" => "opacity: 0", Chris@1294: "onmouseover" => Chris@1294: "document.getElementById(#{t.object_id}).setAttribute('visibility', 'visible' )", Chris@1294: "onmouseout" => Chris@1294: "document.getElementById(#{t.object_id}).setAttribute('visibility', 'hidden' )", Chris@1294: }) Chris@1294: Chris@1294: end Chris@1294: Chris@1294: Chris@1294: # Override this (and call super) to change the margin to the bottom Chris@1294: # of the plot area. Results in @border_bottom being set. Chris@1294: def calculate_bottom_margin Chris@1294: @border_bottom = 7 Chris@1294: if key and key_position == :bottom Chris@1294: @border_bottom += @data.size * (font_size + 5) Chris@1294: @border_bottom += 10 Chris@1294: end Chris@1294: if show_x_labels Chris@1294: max_x_label_height_px = (not rotate_x_labels) ? Chris@1294: x_label_font_size : Chris@1294: get_x_labels.max{|a,b| Chris@1294: a.to_s.length<=>b.to_s.length Chris@1294: }.to_s.length * x_label_font_size * 0.6 Chris@1294: @border_bottom += max_x_label_height_px Chris@1294: @border_bottom += max_x_label_height_px + 10 if stagger_x_labels Chris@1294: end Chris@1294: @border_bottom += x_title_font_size + 5 if show_x_title Chris@1294: end Chris@1294: Chris@1294: Chris@1294: # Draws the background, axis, and labels. Chris@1294: def draw_graph Chris@1294: @graph = @root.add_element( "g", { Chris@1294: "transform" => "translate( #@border_left #@border_top )" Chris@1294: }) Chris@1294: Chris@1294: # Background Chris@1294: @graph.add_element( "rect", { Chris@1294: "x" => "0", Chris@1294: "y" => "0", Chris@1294: "width" => @graph_width.to_s, Chris@1294: "height" => @graph_height.to_s, Chris@1294: "class" => "graphBackground" Chris@1294: }) Chris@1294: Chris@1294: # Axis Chris@1294: @graph.add_element( "path", { Chris@1294: "d" => "M 0 0 v#@graph_height", Chris@1294: "class" => "axis", Chris@1294: "id" => "xAxis" Chris@1294: }) Chris@1294: @graph.add_element( "path", { Chris@1294: "d" => "M 0 #@graph_height h#@graph_width", Chris@1294: "class" => "axis", Chris@1294: "id" => "yAxis" Chris@1294: }) Chris@1294: Chris@1294: draw_x_labels Chris@1294: draw_y_labels Chris@1294: end Chris@1294: Chris@1294: Chris@1294: # Where in the X area the label is drawn Chris@1294: # Centered in the field, should be width/2. Start, 0. Chris@1294: def x_label_offset( width ) Chris@1294: 0 Chris@1294: end Chris@1294: Chris@1294: def make_datapoint_text( x, y, value, style="" ) Chris@1294: if show_data_values Chris@1294: @foreground.add_element( "text", { Chris@1294: "x" => x.to_s, Chris@1294: "y" => y.to_s, Chris@1294: "class" => "dataPointLabel", Chris@1294: "style" => "#{style} stroke: #fff; stroke-width: 2;" Chris@1294: }).text = value.to_s Chris@1294: text = @foreground.add_element( "text", { Chris@1294: "x" => x.to_s, Chris@1294: "y" => y.to_s, Chris@1294: "class" => "dataPointLabel" Chris@1294: }) Chris@1294: text.text = value.to_s Chris@1294: text.attributes["style"] = style if style.length > 0 Chris@1294: end Chris@1294: end Chris@1294: Chris@1294: Chris@1294: # Draws the X axis labels Chris@1294: def draw_x_labels Chris@1294: stagger = x_label_font_size + 5 Chris@1294: if show_x_labels Chris@1294: label_width = field_width Chris@1294: Chris@1294: count = 0 Chris@1294: for label in get_x_labels Chris@1294: if step_include_first_x_label == true then Chris@1294: step = count % step_x_labels Chris@1294: else Chris@1294: step = (count + 1) % step_x_labels Chris@1294: end Chris@1294: Chris@1294: if step == 0 then Chris@1294: text = @graph.add_element( "text" ) Chris@1294: text.attributes["class"] = "xAxisLabels" Chris@1294: text.text = label.to_s Chris@1294: Chris@1294: x = count * label_width + x_label_offset( label_width ) Chris@1294: y = @graph_height + x_label_font_size + 3 Chris@1294: t = 0 - (font_size / 2) Chris@1294: Chris@1294: if stagger_x_labels and count % 2 == 1 Chris@1294: y += stagger Chris@1294: @graph.add_element( "path", { Chris@1294: "d" => "M#{x} #@graph_height v#{stagger}", Chris@1294: "class" => "staggerGuideLine" Chris@1294: }) Chris@1294: end Chris@1294: Chris@1294: text.attributes["x"] = x.to_s Chris@1294: text.attributes["y"] = y.to_s Chris@1294: if rotate_x_labels Chris@1294: text.attributes["transform"] = Chris@1294: "rotate( 90 #{x} #{y-x_label_font_size} )"+ Chris@1294: " translate( 0 -#{x_label_font_size/4} )" Chris@1294: text.attributes["style"] = "text-anchor: start" Chris@1294: else Chris@1294: text.attributes["style"] = "text-anchor: middle" Chris@1294: end Chris@1294: end Chris@1294: Chris@1294: draw_x_guidelines( label_width, count ) if show_x_guidelines Chris@1294: count += 1 Chris@1294: end Chris@1294: end Chris@1294: end Chris@1294: Chris@1294: Chris@1294: # Where in the Y area the label is drawn Chris@1294: # Centered in the field, should be width/2. Start, 0. Chris@1294: def y_label_offset( height ) Chris@1294: 0 Chris@1294: end Chris@1294: Chris@1294: Chris@1294: def field_width Chris@1294: (@graph_width.to_f - font_size*2*right_font) / Chris@1294: (get_x_labels.length - right_align) Chris@1294: end Chris@1294: Chris@1294: Chris@1294: def field_height Chris@1294: (@graph_height.to_f - font_size*2*top_font) / Chris@1294: (get_y_labels.length - top_align) Chris@1294: end Chris@1294: Chris@1294: Chris@1294: # Draws the Y axis labels Chris@1294: def draw_y_labels Chris@1294: stagger = y_label_font_size + 5 Chris@1294: if show_y_labels Chris@1294: label_height = field_height Chris@1294: Chris@1294: count = 0 Chris@1294: y_offset = @graph_height + y_label_offset( label_height ) Chris@1294: y_offset += font_size/1.2 unless rotate_y_labels Chris@1294: for label in get_y_labels Chris@1294: y = y_offset - (label_height * count) Chris@1294: x = rotate_y_labels ? 0 : -3 Chris@1294: Chris@1294: if stagger_y_labels and count % 2 == 1 Chris@1294: x -= stagger Chris@1294: @graph.add_element( "path", { Chris@1294: "d" => "M#{x} #{y} h#{stagger}", Chris@1294: "class" => "staggerGuideLine" Chris@1294: }) Chris@1294: end Chris@1294: Chris@1294: text = @graph.add_element( "text", { Chris@1294: "x" => x.to_s, Chris@1294: "y" => y.to_s, Chris@1294: "class" => "yAxisLabels" Chris@1294: }) Chris@1294: text.text = label.to_s Chris@1294: if rotate_y_labels Chris@1294: text.attributes["transform"] = "translate( -#{font_size} 0 ) "+ Chris@1294: "rotate( 90 #{x} #{y} ) " Chris@1294: text.attributes["style"] = "text-anchor: middle" Chris@1294: else Chris@1294: text.attributes["y"] = (y - (y_label_font_size/2)).to_s Chris@1294: text.attributes["style"] = "text-anchor: end" Chris@1294: end Chris@1294: draw_y_guidelines( label_height, count ) if show_y_guidelines Chris@1294: count += 1 Chris@1294: end Chris@1294: end Chris@1294: end Chris@1294: Chris@1294: Chris@1294: # Draws the X axis guidelines Chris@1294: def draw_x_guidelines( label_height, count ) Chris@1294: if count != 0 Chris@1294: @graph.add_element( "path", { Chris@1294: "d" => "M#{label_height*count} 0 v#@graph_height", Chris@1294: "class" => "guideLines" Chris@1294: }) Chris@1294: end Chris@1294: end Chris@1294: Chris@1294: Chris@1294: # Draws the Y axis guidelines Chris@1294: def draw_y_guidelines( label_height, count ) Chris@1294: if count != 0 Chris@1294: @graph.add_element( "path", { Chris@1294: "d" => "M0 #{@graph_height-(label_height*count)} h#@graph_width", Chris@1294: "class" => "guideLines" Chris@1294: }) Chris@1294: end Chris@1294: end Chris@1294: Chris@1294: Chris@1294: # Draws the graph title and subtitle Chris@1294: def draw_titles Chris@1294: if show_graph_title Chris@1294: @root.add_element( "text", { Chris@1294: "x" => (width / 2).to_s, Chris@1294: "y" => (title_font_size).to_s, Chris@1294: "class" => "mainTitle" Chris@1294: }).text = graph_title.to_s Chris@1294: end Chris@1294: Chris@1294: if show_graph_subtitle Chris@1294: y_subtitle = show_graph_title ? Chris@1294: title_font_size + 10 : Chris@1294: subtitle_font_size Chris@1294: @root.add_element("text", { Chris@1294: "x" => (width / 2).to_s, Chris@1294: "y" => (y_subtitle).to_s, Chris@1294: "class" => "subTitle" Chris@1294: }).text = graph_subtitle.to_s Chris@1294: end Chris@1294: Chris@1294: if show_x_title Chris@1294: y = @graph_height + @border_top + x_title_font_size Chris@1294: if show_x_labels Chris@1294: y += x_label_font_size + 5 if stagger_x_labels Chris@1294: y += x_label_font_size + 5 Chris@1294: end Chris@1294: x = width / 2 Chris@1294: Chris@1294: @root.add_element("text", { Chris@1294: "x" => x.to_s, Chris@1294: "y" => y.to_s, Chris@1294: "class" => "xAxisTitle", Chris@1294: }).text = x_title.to_s Chris@1294: end Chris@1294: Chris@1294: if show_y_title Chris@1294: x = y_title_font_size + (y_title_text_direction==:bt ? 3 : -3) Chris@1294: y = height / 2 Chris@1294: Chris@1294: text = @root.add_element("text", { Chris@1294: "x" => x.to_s, Chris@1294: "y" => y.to_s, Chris@1294: "class" => "yAxisTitle", Chris@1294: }) Chris@1294: text.text = y_title.to_s Chris@1294: if y_title_text_direction == :bt Chris@1294: text.attributes["transform"] = "rotate( -90, #{x}, #{y} )" Chris@1294: else Chris@1294: text.attributes["transform"] = "rotate( 90, #{x}, #{y} )" Chris@1294: end Chris@1294: end Chris@1294: end Chris@1294: Chris@1294: def keys Chris@1294: return @data.collect{ |d| d[:title] } Chris@1294: end Chris@1294: Chris@1294: # Draws the legend on the graph Chris@1294: def draw_legend Chris@1294: if key Chris@1294: group = @root.add_element( "g" ) Chris@1294: Chris@1294: key_count = 0 Chris@1294: for key_name in keys Chris@1294: y_offset = (KEY_BOX_SIZE * key_count) + (key_count * 5) Chris@1294: group.add_element( "rect", { Chris@1294: "x" => 0.to_s, Chris@1294: "y" => y_offset.to_s, Chris@1294: "width" => KEY_BOX_SIZE.to_s, Chris@1294: "height" => KEY_BOX_SIZE.to_s, Chris@1294: "class" => "key#{key_count+1}" Chris@1294: }) Chris@1294: group.add_element( "text", { Chris@1294: "x" => (KEY_BOX_SIZE + 5).to_s, Chris@1294: "y" => (y_offset + KEY_BOX_SIZE).to_s, Chris@1294: "class" => "keyText" Chris@1294: }).text = key_name.to_s Chris@1294: key_count += 1 Chris@1294: end Chris@1294: Chris@1294: case key_position Chris@1294: when :right Chris@1294: x_offset = @graph_width + @border_left + 10 Chris@1294: y_offset = @border_top + 20 Chris@1294: when :bottom Chris@1294: x_offset = @border_left + 20 Chris@1294: y_offset = @border_top + @graph_height + 5 Chris@1294: if show_x_labels Chris@1294: max_x_label_height_px = (not rotate_x_labels) ? Chris@1294: x_label_font_size : Chris@1294: get_x_labels.max{|a,b| Chris@1294: a.to_s.length<=>b.to_s.length Chris@1294: }.to_s.length * x_label_font_size * 0.6 Chris@1294: x_label_font_size Chris@1294: y_offset += max_x_label_height_px Chris@1294: y_offset += max_x_label_height_px + 5 if stagger_x_labels Chris@1294: end Chris@1294: y_offset += x_title_font_size + 5 if show_x_title Chris@1294: end Chris@1294: group.attributes["transform"] = "translate(#{x_offset} #{y_offset})" Chris@1294: end Chris@1294: end Chris@1294: Chris@1294: Chris@1294: private Chris@1294: Chris@1294: def sort_multiple( arrys, lo=0, hi=arrys[0].length-1 ) Chris@1294: if lo < hi Chris@1294: p = partition(arrys,lo,hi) Chris@1294: sort_multiple(arrys, lo, p-1) Chris@1294: sort_multiple(arrys, p+1, hi) Chris@1294: end Chris@1294: arrys Chris@1294: end Chris@1294: Chris@1294: def partition( arrys, lo, hi ) Chris@1294: p = arrys[0][lo] Chris@1294: l = lo Chris@1294: z = lo+1 Chris@1294: while z <= hi Chris@1294: if arrys[0][z] < p Chris@1294: l += 1 Chris@1294: arrys.each { |arry| arry[z], arry[l] = arry[l], arry[z] } Chris@1294: end Chris@1294: z += 1 Chris@1294: end Chris@1294: arrys.each { |arry| arry[lo], arry[l] = arry[l], arry[lo] } Chris@1294: l Chris@1294: end Chris@1294: Chris@1294: def style Chris@1294: if no_css Chris@1294: styles = parse_css Chris@1294: @root.elements.each("//*[@class]") { |el| Chris@1294: cl = el.attributes["class"] Chris@1294: style = styles[cl] Chris@1294: style += el.attributes["style"] if el.attributes["style"] Chris@1294: el.attributes["style"] = style Chris@1294: } Chris@1294: end Chris@1294: end Chris@1294: Chris@1294: def parse_css Chris@1294: css = get_style Chris@1294: rv = {} Chris@1294: while css =~ /^(\.(\w+)(?:\s*,\s*\.\w+)*)\s*\{/m Chris@1294: names_orig = names = $1 Chris@1294: css = $' Chris@1294: css =~ /([^}]+)\}/m Chris@1294: content = $1 Chris@1294: css = $' Chris@1294: Chris@1294: nms = [] Chris@1294: while names =~ /^\s*,?\s*\.(\w+)/ Chris@1294: nms << $1 Chris@1294: names = $' Chris@1294: end Chris@1294: Chris@1294: content = content.tr( "\n\t", " ") Chris@1294: for name in nms Chris@1294: current = rv[name] Chris@1294: current = current ? current+"; "+content : content Chris@1294: rv[name] = current.strip.squeeze(" ") Chris@1294: end Chris@1294: end Chris@1294: return rv Chris@1294: end Chris@1294: Chris@1294: Chris@1294: # Override and place code to add defs here Chris@1294: def add_defs defs Chris@1294: end Chris@1294: Chris@1294: Chris@1294: def start_svg Chris@1294: # Base document Chris@1294: @doc = Document.new Chris@1294: @doc << XMLDecl.new Chris@1294: @doc << DocType.new( %q{svg PUBLIC "-//W3C//DTD SVG 1.0//EN" } + Chris@1294: %q{"http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd"} ) Chris@1294: if style_sheet && style_sheet != '' Chris@1294: @doc << Instruction.new( "xml-stylesheet", Chris@1294: %Q{href="#{style_sheet}" type="text/css"} ) Chris@1294: end Chris@1294: @root = @doc.add_element( "svg", { Chris@1294: "width" => width.to_s, Chris@1294: "height" => height.to_s, Chris@1294: "viewBox" => "0 0 #{width} #{height}", Chris@1294: "xmlns" => "http://www.w3.org/2000/svg", Chris@1294: "xmlns:xlink" => "http://www.w3.org/1999/xlink", Chris@1294: "xmlns:a3" => "http://ns.adobe.com/AdobeSVGViewerExtensions/3.0/", Chris@1294: "a3:scriptImplementation" => "Adobe" Chris@1294: }) Chris@1294: @root << Comment.new( " "+"\\"*66 ) Chris@1294: @root << Comment.new( " Created with SVG::Graph " ) Chris@1294: @root << Comment.new( " SVG::Graph by Sean E. Russell " ) Chris@1294: @root << Comment.new( " Losely based on SVG::TT::Graph for Perl by"+ Chris@1294: " Leo Lapworth & Stephan Morgan " ) Chris@1294: @root << Comment.new( " "+"/"*66 ) Chris@1294: Chris@1294: defs = @root.add_element( "defs" ) Chris@1294: add_defs defs Chris@1294: if not(style_sheet && style_sheet != '') and !no_css Chris@1294: @root << Comment.new(" include default stylesheet if none specified ") Chris@1294: style = defs.add_element( "style", {"type"=>"text/css"} ) Chris@1294: style << CData.new( get_style ) Chris@1294: end Chris@1294: Chris@1294: @root << Comment.new( "SVG Background" ) Chris@1294: @root.add_element( "rect", { Chris@1294: "width" => width.to_s, Chris@1294: "height" => height.to_s, Chris@1294: "x" => "0", Chris@1294: "y" => "0", Chris@1294: "class" => "svgBackground" Chris@1294: }) Chris@1294: end Chris@1294: Chris@1294: Chris@1294: def calculate_graph_dimensions Chris@1294: calculate_left_margin Chris@1294: calculate_right_margin Chris@1294: calculate_bottom_margin Chris@1294: calculate_top_margin Chris@1294: @graph_width = width - @border_left - @border_right Chris@1294: @graph_height = height - @border_top - @border_bottom Chris@1294: end Chris@1294: Chris@1294: def get_style Chris@1294: return <