lib/SVG/Graph/Graph.rb in svg-graph-2.1.3 vs lib/SVG/Graph/Graph.rb in svg-graph-2.2.0.beta
- old
+ new
@@ -142,26 +142,33 @@
:show_graph_title => false,
:graph_title => 'Graph Title',
:show_graph_subtitle => false,
:graph_subtitle => 'Graph Sub Title',
:key => true,
+ :key_width => nil,
:key_position => :right, # bottom or right
- :font_size =>12,
- :title_font_size =>16,
- :subtitle_font_size =>14,
- :x_label_font_size =>12,
- :y_label_font_size =>12,
- :x_title_font_size =>14,
- :y_title_font_size =>14,
- :key_font_size =>10,
+ :font_size => 12,
+ :title_font_size => 16,
+ :subtitle_font_size => 14,
+ :x_label_font_size => 12,
+ :y_label_font_size => 12,
+ :x_title_font_size => 14,
+ :y_title_font_size => 14,
+ :key_font_size => 10,
+ :key_box_size => 12,
+ :key_spacing => 5,
- :no_css =>false,
- :add_popups =>false,
- :number_format => '%.2f'
+ :no_css => false,
+ :add_popups => false,
+ :popup_radius => 10,
+ :number_format => '%.2f',
+ :style_sheet => '',
+ :inline_style_sheet => ''
})
set_defaults if self.respond_to? :set_defaults
+ # override default values with user supplied values
init_with config
end
# This method allows you do add data to the graph object.
@@ -254,17 +261,27 @@
attr_accessor :height
# Set the width of the graph box, this is the total width
# of the SVG box created - not the graph it self which auto
# scales to fix the space.
attr_accessor :width
- # Set the path to an external stylesheet, set to '' if
+ # Set the path/url to an external stylesheet, set to '' if
# you want to revert back to using the defaut internal version.
#
# To create an external stylesheet create a graph using the
# default internal version and copy the stylesheet section to
# an external file and edit from there.
attr_accessor :style_sheet
+ # Define as String the stylesheet contents to be inlined, set to '' to disable.
+ # This can be used, when referring to a url via :style_sheet is not suitable.
+ # E.g. in situations where there will be no internet access or the graph must
+ # consist of only one file.
+ #
+ # If not empty, the :style_sheet parameter (url) above will be ignored and is
+ # not written to the file
+ # see also https://github.com/erullmann/svg-graph2/commit/55eb6e983f6fcc69cc5a110d0ee6e05f906f639a
+ # Default: ''
+ attr_accessor :inline_style_sheet
# (Bool) Show the value of each element of data on the graph
attr_accessor :show_data_values
# By default (nil/undefined) the x-axis is at the bottom of the graph.
# With this property a custom position for the x-axis can be defined.
# Valid values are between :min_scale_value and maximum value of the
@@ -290,14 +307,16 @@
attr_accessor :stagger_x_labels
# This puts the Y labels at alternative levels so if they
# are long field names they will not overlap so easily.
# Default is false, to turn on set to true.
attr_accessor :stagger_y_labels
- # This turns the X axis labels by 90 degrees.
+ # This turns the X axis labels by 90 degrees when true or by a custom
+ # amount when a numeric value is given.
# Default is false, to turn on set to true.
attr_accessor :rotate_x_labels
- # This turns the Y axis labels by 90 degrees.
+ # This turns the Y axis labels by 90 degrees when true or by a custom
+ # amount when a numeric value is given.
# Default is true, to turn on set to false.
attr_accessor :rotate_y_labels
# How many "steps" to use between displayed X axis labels,
# a step of one means display every label, a step of two results
# in every other label being displayed (label <gap> label <gap> label),
@@ -356,10 +375,17 @@
# false if you want to hide it.
attr_accessor :key
# Where the key should be positioned, defaults to
# :right, set to :bottom if you want to move it.
attr_accessor :key_position
+
+ attr_accessor :key_box_size
+
+ attr_accessor :key_spacing
+
+ attr_accessor :key_width
+
# Set the font size (in points) of the data point labels.
# Defaults to 12.
attr_accessor :font_size
# Set the font size of the X axis labels.
# Defaults to 12.
@@ -412,16 +438,12 @@
# by subclasses.
def init_with config
config.each { |key, value|
self.send( key.to_s+"=", value ) if self.respond_to? key
}
- @popup_radius ||= 10
end
- # size of the square box in the legend which indicates the colors
- KEY_BOX_SIZE = 12
-
# Override this (and call super) to change the margin to the left
# of the plot area. Results in @border_left being set.
#
# By default it is 7 + max label height(font size or string length, depending on rotate) + title height
def calculate_left_margin
@@ -456,12 +478,18 @@
def calculate_right_margin
@border_right = 7
if key and key_position == :right
val = keys.max { |a,b| a.length <=> b.length }
@border_right += val.length * key_font_size * 0.6
- @border_right += KEY_BOX_SIZE
+ @border_right += key_box_size
@border_right += 10 # Some padding around the box
+
+ if key_width.nil?
+ @border_right
+ else
+ @border_right = [key_width, @border_right].min
+ end
end
if (x_title_location == :end)
@border_right = [@border_right, x_title.length * x_title_font_size * 0.6].max
end
end
@@ -732,12 +760,16 @@
end
text.attributes["x"] = x.to_s
text.attributes["y"] = y.to_s
if rotate_x_labels
+ degrees = 90
+ if numeric? rotate_x_labels
+ degrees = rotate_x_labels
+ end
text.attributes["transform"] =
- "rotate( 90 #{x} #{y-x_label_font_size} )"+
+ "rotate( #{degrees} #{x} #{y-x_label_font_size} )"+
" translate( 0 -#{x_label_font_size/4} )"
text.attributes["style"] = "text-anchor: start"
else
text.attributes["style"] = "text-anchor: middle"
end
@@ -816,12 +848,16 @@
if( numeric?(label) )
textStr = @number_format % label
end
text.text = textStr
if rotate_y_labels
+ degrees = 90
+ if numeric? rotate_y_labels
+ degrees = rotate_y_labels
+ end
text.attributes["transform"] = "translate( -#{font_size} 0 ) "+
- "rotate( 90 #{x} #{y} ) "
+ "rotate( #{degrees} #{x} #{y} ) "
text.attributes["style"] = "text-anchor: middle"
else
text.attributes["y"] = (y - (y_label_font_size/2)).to_s
text.attributes["style"] = "text-anchor: end"
end
@@ -927,37 +963,37 @@
if key
group = @root.add_element( "g" )
key_count = 0
for key_name in keys
- y_offset = (KEY_BOX_SIZE * key_count) + (key_count * 5)
+ y_offset = (key_box_size * key_count) + (key_count * key_spacing)
group.add_element( "rect", {
"x" => 0.to_s,
"y" => y_offset.to_s,
- "width" => KEY_BOX_SIZE.to_s,
- "height" => KEY_BOX_SIZE.to_s,
+ "width" => key_box_size.to_s,
+ "height" => key_box_size.to_s,
"class" => "key#{key_count+1}"
})
group.add_element( "text", {
- "x" => (KEY_BOX_SIZE + 5).to_s,
- "y" => (y_offset + KEY_BOX_SIZE).to_s,
+ "x" => (key_box_size + key_spacing).to_s,
+ "y" => (y_offset + key_box_size).to_s,
"class" => "keyText"
}).text = key_name.to_s
key_count += 1
end
case key_position
when :right
- x_offset = @graph_width + @border_left + 10
- y_offset = @border_top + 20
+ x_offset = @graph_width + @border_left + (key_spacing * 2)
+ y_offset = @border_top + (key_spacing * 2)
when :bottom
- x_offset = @border_left + 20
- y_offset = @border_top + @graph_height + 5
+ x_offset = @border_left + (key_spacing * 2)
+ y_offset = @border_top + @graph_height + key_spacing
if show_x_labels
y_offset += max_x_label_height_px
end
- y_offset += x_title_font_size + 5 if show_x_title
+ y_offset += x_title_font_size + key_spacing if show_x_title
end
group.attributes["transform"] = "translate(#{x_offset} #{y_offset})"
end
end
@@ -1043,11 +1079,12 @@
# Base document
@doc = Document.new
@doc << XMLDecl.new
@doc << DocType.new( %q{svg PUBLIC "-//W3C//DTD SVG 1.0//EN" } +
%q{"http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd"} )
- if style_sheet && style_sheet != ''
+ if style_sheet && style_sheet != '' && inline_style_sheet.to_s.empty?
+ # if inline_style_sheet is defined, url style sheet is ignored
@doc << Instruction.new( "xml-stylesheet",
%Q{href="#{style_sheet}" type="text/css"} )
end
@root = @doc.add_element( "svg", {
"width" => width.to_s,
@@ -1065,13 +1102,18 @@
" Leo Lapworth & Stephan Morgan " )
@root << Comment.new( " "+"/"*66 )
defs = @root.add_element( "defs" )
add_defs defs
- if not(style_sheet && style_sheet != '') and !no_css
- @root << Comment.new(" include default stylesheet if none specified ")
- style = defs.add_element( "style", {"type"=>"text/css"} )
- style << CData.new( get_style )
+ if !no_css
+ if inline_style_sheet && inline_style_sheet != ''
+ style = defs.add_element( "style", {"type"=>"text/css"} )
+ style << CData.new( inline_style_sheet )
+ else
+ @root << Comment.new(" include default stylesheet if none specified ")
+ style = defs.add_element( "style", {"type"=>"text/css"} )
+ style << CData.new( get_style )
+ end
end
@root << Comment.new( "SVG Background" )
@root.add_element( "rect", {
"width" => width.to_s,