Set proper EOL on SVG source files (#12971).
git-svn-id: svn+ssh://rubyforge.org/var/svn/redmine/trunk@11269 e93f8b46-1217-0410-a6f0-8f06a7374b81
This commit is contained in:
parent
a2bf01cf89
commit
08826ed0b6
|
@ -1,148 +1,148 @@
|
|||
require 'rexml/document'
|
||||
require 'SVG/Graph/Graph'
|
||||
require 'SVG/Graph/BarBase'
|
||||
|
||||
module SVG
|
||||
module Graph
|
||||
# === Create presentation quality SVG bar graphs easily
|
||||
#
|
||||
# = Synopsis
|
||||
#
|
||||
# require 'SVG/Graph/Bar'
|
||||
#
|
||||
# fields = %w(Jan Feb Mar);
|
||||
# data_sales_02 = [12, 45, 21]
|
||||
#
|
||||
# graph = SVG::Graph::Bar.new(
|
||||
# :height => 500,
|
||||
# :width => 300,
|
||||
# :fields => fields
|
||||
# )
|
||||
#
|
||||
# graph.add_data(
|
||||
# :data => data_sales_02,
|
||||
# :title => 'Sales 2002'
|
||||
# )
|
||||
#
|
||||
# print "Content-type: image/svg+xml\r\n\r\n"
|
||||
# print graph.burn
|
||||
#
|
||||
# = Description
|
||||
#
|
||||
# This object aims to allow you to easily create high quality
|
||||
# SVG[http://www.w3c.org/tr/svg bar graphs. You can either use the default
|
||||
# style sheet or supply your own. Either way there are many options which
|
||||
# can be configured to give you control over how the graph is generated -
|
||||
# with or without a key, data elements at each point, title, subtitle etc.
|
||||
#
|
||||
# = Notes
|
||||
#
|
||||
# The default stylesheet handles upto 12 data sets, if you
|
||||
# use more you must create your own stylesheet and add the
|
||||
# additional settings for the extra data sets. You will know
|
||||
# if you go over 12 data sets as they will have no style and
|
||||
# be in black.
|
||||
#
|
||||
# = Examples
|
||||
#
|
||||
# * http://germane-software.com/repositories/public/SVG/test/test.rb
|
||||
#
|
||||
# = See also
|
||||
#
|
||||
# * SVG::Graph::Graph
|
||||
# * SVG::Graph::BarHorizontal
|
||||
# * SVG::Graph::Line
|
||||
# * SVG::Graph::Pie
|
||||
# * SVG::Graph::Plot
|
||||
# * SVG::Graph::TimeSeries
|
||||
class Bar < BarBase
|
||||
include REXML
|
||||
|
||||
# See Graph::initialize and BarBase::set_defaults
|
||||
def set_defaults
|
||||
super
|
||||
self.top_align = self.top_font = 1
|
||||
end
|
||||
|
||||
protected
|
||||
|
||||
def get_x_labels
|
||||
@config[:fields]
|
||||
end
|
||||
|
||||
def get_y_labels
|
||||
maxvalue = max_value
|
||||
minvalue = min_value
|
||||
range = maxvalue - minvalue
|
||||
|
||||
top_pad = range == 0 ? 10 : range / 20.0
|
||||
scale_range = (maxvalue + top_pad) - minvalue
|
||||
|
||||
scale_division = scale_divisions || (scale_range / 10.0)
|
||||
|
||||
if scale_integers
|
||||
scale_division = scale_division < 1 ? 1 : scale_division.round
|
||||
end
|
||||
|
||||
rv = []
|
||||
maxvalue = maxvalue%scale_division == 0 ?
|
||||
maxvalue : maxvalue + scale_division
|
||||
minvalue.step( maxvalue, scale_division ) {|v| rv << v}
|
||||
return rv
|
||||
end
|
||||
|
||||
def x_label_offset( width )
|
||||
width / 2.0
|
||||
end
|
||||
|
||||
def draw_data
|
||||
minvalue = min_value
|
||||
fieldwidth = field_width
|
||||
|
||||
unit_size = (@graph_height.to_f - font_size*2*top_font) /
|
||||
(get_y_labels.max - get_y_labels.min)
|
||||
bargap = bar_gap ? (fieldwidth < 10 ? fieldwidth / 2 : 10) : 0
|
||||
|
||||
bar_width = fieldwidth - bargap
|
||||
bar_width /= @data.length if stack == :side
|
||||
x_mod = (@graph_width-bargap)/2 - (stack==:side ? bar_width/2 : 0)
|
||||
|
||||
bottom = @graph_height
|
||||
|
||||
field_count = 0
|
||||
@config[:fields].each_index { |i|
|
||||
dataset_count = 0
|
||||
for dataset in @data
|
||||
|
||||
# cases (assume 0 = +ve):
|
||||
# value min length
|
||||
# +ve +ve value - min
|
||||
# +ve -ve value - 0
|
||||
# -ve -ve value.abs - 0
|
||||
|
||||
value = dataset[:data][i]
|
||||
|
||||
left = (fieldwidth * field_count)
|
||||
|
||||
length = (value.abs - (minvalue > 0 ? minvalue : 0)) * unit_size
|
||||
# top is 0 if value is negative
|
||||
top = bottom - (((value < 0 ? 0 : value) - minvalue) * unit_size)
|
||||
left += bar_width * dataset_count if stack == :side
|
||||
|
||||
@graph.add_element( "rect", {
|
||||
"x" => left.to_s,
|
||||
"y" => top.to_s,
|
||||
"width" => bar_width.to_s,
|
||||
"height" => length.to_s,
|
||||
"class" => "fill#{dataset_count+1}"
|
||||
})
|
||||
|
||||
make_datapoint_text(left + bar_width/2.0, top - 6, value.to_s)
|
||||
dataset_count += 1
|
||||
end
|
||||
field_count += 1
|
||||
}
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
require 'rexml/document'
|
||||
require 'SVG/Graph/Graph'
|
||||
require 'SVG/Graph/BarBase'
|
||||
|
||||
module SVG
|
||||
module Graph
|
||||
# === Create presentation quality SVG bar graphs easily
|
||||
#
|
||||
# = Synopsis
|
||||
#
|
||||
# require 'SVG/Graph/Bar'
|
||||
#
|
||||
# fields = %w(Jan Feb Mar);
|
||||
# data_sales_02 = [12, 45, 21]
|
||||
#
|
||||
# graph = SVG::Graph::Bar.new(
|
||||
# :height => 500,
|
||||
# :width => 300,
|
||||
# :fields => fields
|
||||
# )
|
||||
#
|
||||
# graph.add_data(
|
||||
# :data => data_sales_02,
|
||||
# :title => 'Sales 2002'
|
||||
# )
|
||||
#
|
||||
# print "Content-type: image/svg+xml\r\n\r\n"
|
||||
# print graph.burn
|
||||
#
|
||||
# = Description
|
||||
#
|
||||
# This object aims to allow you to easily create high quality
|
||||
# SVG[http://www.w3c.org/tr/svg bar graphs. You can either use the default
|
||||
# style sheet or supply your own. Either way there are many options which
|
||||
# can be configured to give you control over how the graph is generated -
|
||||
# with or without a key, data elements at each point, title, subtitle etc.
|
||||
#
|
||||
# = Notes
|
||||
#
|
||||
# The default stylesheet handles upto 12 data sets, if you
|
||||
# use more you must create your own stylesheet and add the
|
||||
# additional settings for the extra data sets. You will know
|
||||
# if you go over 12 data sets as they will have no style and
|
||||
# be in black.
|
||||
#
|
||||
# = Examples
|
||||
#
|
||||
# * http://germane-software.com/repositories/public/SVG/test/test.rb
|
||||
#
|
||||
# = See also
|
||||
#
|
||||
# * SVG::Graph::Graph
|
||||
# * SVG::Graph::BarHorizontal
|
||||
# * SVG::Graph::Line
|
||||
# * SVG::Graph::Pie
|
||||
# * SVG::Graph::Plot
|
||||
# * SVG::Graph::TimeSeries
|
||||
class Bar < BarBase
|
||||
include REXML
|
||||
|
||||
# See Graph::initialize and BarBase::set_defaults
|
||||
def set_defaults
|
||||
super
|
||||
self.top_align = self.top_font = 1
|
||||
end
|
||||
|
||||
protected
|
||||
|
||||
def get_x_labels
|
||||
@config[:fields]
|
||||
end
|
||||
|
||||
def get_y_labels
|
||||
maxvalue = max_value
|
||||
minvalue = min_value
|
||||
range = maxvalue - minvalue
|
||||
|
||||
top_pad = range == 0 ? 10 : range / 20.0
|
||||
scale_range = (maxvalue + top_pad) - minvalue
|
||||
|
||||
scale_division = scale_divisions || (scale_range / 10.0)
|
||||
|
||||
if scale_integers
|
||||
scale_division = scale_division < 1 ? 1 : scale_division.round
|
||||
end
|
||||
|
||||
rv = []
|
||||
maxvalue = maxvalue%scale_division == 0 ?
|
||||
maxvalue : maxvalue + scale_division
|
||||
minvalue.step( maxvalue, scale_division ) {|v| rv << v}
|
||||
return rv
|
||||
end
|
||||
|
||||
def x_label_offset( width )
|
||||
width / 2.0
|
||||
end
|
||||
|
||||
def draw_data
|
||||
minvalue = min_value
|
||||
fieldwidth = field_width
|
||||
|
||||
unit_size = (@graph_height.to_f - font_size*2*top_font) /
|
||||
(get_y_labels.max - get_y_labels.min)
|
||||
bargap = bar_gap ? (fieldwidth < 10 ? fieldwidth / 2 : 10) : 0
|
||||
|
||||
bar_width = fieldwidth - bargap
|
||||
bar_width /= @data.length if stack == :side
|
||||
x_mod = (@graph_width-bargap)/2 - (stack==:side ? bar_width/2 : 0)
|
||||
|
||||
bottom = @graph_height
|
||||
|
||||
field_count = 0
|
||||
@config[:fields].each_index { |i|
|
||||
dataset_count = 0
|
||||
for dataset in @data
|
||||
|
||||
# cases (assume 0 = +ve):
|
||||
# value min length
|
||||
# +ve +ve value - min
|
||||
# +ve -ve value - 0
|
||||
# -ve -ve value.abs - 0
|
||||
|
||||
value = dataset[:data][i]
|
||||
|
||||
left = (fieldwidth * field_count)
|
||||
|
||||
length = (value.abs - (minvalue > 0 ? minvalue : 0)) * unit_size
|
||||
# top is 0 if value is negative
|
||||
top = bottom - (((value < 0 ? 0 : value) - minvalue) * unit_size)
|
||||
left += bar_width * dataset_count if stack == :side
|
||||
|
||||
@graph.add_element( "rect", {
|
||||
"x" => left.to_s,
|
||||
"y" => top.to_s,
|
||||
"width" => bar_width.to_s,
|
||||
"height" => length.to_s,
|
||||
"class" => "fill#{dataset_count+1}"
|
||||
})
|
||||
|
||||
make_datapoint_text(left + bar_width/2.0, top - 6, value.to_s)
|
||||
dataset_count += 1
|
||||
end
|
||||
field_count += 1
|
||||
}
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -1,139 +1,139 @@
|
|||
require 'rexml/document'
|
||||
require 'SVG/Graph/Graph'
|
||||
|
||||
module SVG
|
||||
module Graph
|
||||
# = Synopsis
|
||||
#
|
||||
# A superclass for bar-style graphs. Do not attempt to instantiate
|
||||
# directly; use one of the subclasses instead.
|
||||
#
|
||||
# = Author
|
||||
#
|
||||
# Sean E. Russell <serATgermaneHYPHENsoftwareDOTcom>
|
||||
#
|
||||
# Copyright 2004 Sean E. Russell
|
||||
# This software is available under the Ruby license[LICENSE.txt]
|
||||
#
|
||||
class BarBase < SVG::Graph::Graph
|
||||
# Ensures that :fields are provided in the configuration.
|
||||
def initialize config
|
||||
raise "fields was not supplied or is empty" unless config[:fields] &&
|
||||
config[:fields].kind_of?(Array) &&
|
||||
config[:fields].length > 0
|
||||
super
|
||||
end
|
||||
|
||||
# In addition to the defaults set in Graph::initialize, sets
|
||||
# [bar_gap] true
|
||||
# [stack] :overlap
|
||||
def set_defaults
|
||||
init_with( :bar_gap => true, :stack => :overlap )
|
||||
end
|
||||
|
||||
# Whether to have a gap between the bars or not, default
|
||||
# is true, set to false if you don't want gaps.
|
||||
attr_accessor :bar_gap
|
||||
# How to stack data sets. :overlap overlaps bars with
|
||||
# transparent colors, :top stacks bars on top of one another,
|
||||
# :side stacks the bars side-by-side. Defaults to :overlap.
|
||||
attr_accessor :stack
|
||||
|
||||
|
||||
protected
|
||||
|
||||
def max_value
|
||||
@data.collect{|x| x[:data].max}.max
|
||||
end
|
||||
|
||||
def min_value
|
||||
min = 0
|
||||
if min_scale_value.nil?
|
||||
min = @data.collect{|x| x[:data].min}.min
|
||||
min = min > 0 ? 0 : min
|
||||
else
|
||||
min = min_scale_value
|
||||
end
|
||||
return min
|
||||
end
|
||||
|
||||
def get_css
|
||||
return <<EOL
|
||||
/* default fill styles for multiple datasets (probably only use a single dataset on this graph though) */
|
||||
.key1,.fill1{
|
||||
fill: #ff0000;
|
||||
fill-opacity: 0.5;
|
||||
stroke: none;
|
||||
stroke-width: 0.5px;
|
||||
}
|
||||
.key2,.fill2{
|
||||
fill: #0000ff;
|
||||
fill-opacity: 0.5;
|
||||
stroke: none;
|
||||
stroke-width: 1px;
|
||||
}
|
||||
.key3,.fill3{
|
||||
fill: #00ff00;
|
||||
fill-opacity: 0.5;
|
||||
stroke: none;
|
||||
stroke-width: 1px;
|
||||
}
|
||||
.key4,.fill4{
|
||||
fill: #ffcc00;
|
||||
fill-opacity: 0.5;
|
||||
stroke: none;
|
||||
stroke-width: 1px;
|
||||
}
|
||||
.key5,.fill5{
|
||||
fill: #00ccff;
|
||||
fill-opacity: 0.5;
|
||||
stroke: none;
|
||||
stroke-width: 1px;
|
||||
}
|
||||
.key6,.fill6{
|
||||
fill: #ff00ff;
|
||||
fill-opacity: 0.5;
|
||||
stroke: none;
|
||||
stroke-width: 1px;
|
||||
}
|
||||
.key7,.fill7{
|
||||
fill: #00ffff;
|
||||
fill-opacity: 0.5;
|
||||
stroke: none;
|
||||
stroke-width: 1px;
|
||||
}
|
||||
.key8,.fill8{
|
||||
fill: #ffff00;
|
||||
fill-opacity: 0.5;
|
||||
stroke: none;
|
||||
stroke-width: 1px;
|
||||
}
|
||||
.key9,.fill9{
|
||||
fill: #cc6666;
|
||||
fill-opacity: 0.5;
|
||||
stroke: none;
|
||||
stroke-width: 1px;
|
||||
}
|
||||
.key10,.fill10{
|
||||
fill: #663399;
|
||||
fill-opacity: 0.5;
|
||||
stroke: none;
|
||||
stroke-width: 1px;
|
||||
}
|
||||
.key11,.fill11{
|
||||
fill: #339900;
|
||||
fill-opacity: 0.5;
|
||||
stroke: none;
|
||||
stroke-width: 1px;
|
||||
}
|
||||
.key12,.fill12{
|
||||
fill: #9966FF;
|
||||
fill-opacity: 0.5;
|
||||
stroke: none;
|
||||
stroke-width: 1px;
|
||||
}
|
||||
EOL
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
require 'rexml/document'
|
||||
require 'SVG/Graph/Graph'
|
||||
|
||||
module SVG
|
||||
module Graph
|
||||
# = Synopsis
|
||||
#
|
||||
# A superclass for bar-style graphs. Do not attempt to instantiate
|
||||
# directly; use one of the subclasses instead.
|
||||
#
|
||||
# = Author
|
||||
#
|
||||
# Sean E. Russell <serATgermaneHYPHENsoftwareDOTcom>
|
||||
#
|
||||
# Copyright 2004 Sean E. Russell
|
||||
# This software is available under the Ruby license[LICENSE.txt]
|
||||
#
|
||||
class BarBase < SVG::Graph::Graph
|
||||
# Ensures that :fields are provided in the configuration.
|
||||
def initialize config
|
||||
raise "fields was not supplied or is empty" unless config[:fields] &&
|
||||
config[:fields].kind_of?(Array) &&
|
||||
config[:fields].length > 0
|
||||
super
|
||||
end
|
||||
|
||||
# In addition to the defaults set in Graph::initialize, sets
|
||||
# [bar_gap] true
|
||||
# [stack] :overlap
|
||||
def set_defaults
|
||||
init_with( :bar_gap => true, :stack => :overlap )
|
||||
end
|
||||
|
||||
# Whether to have a gap between the bars or not, default
|
||||
# is true, set to false if you don't want gaps.
|
||||
attr_accessor :bar_gap
|
||||
# How to stack data sets. :overlap overlaps bars with
|
||||
# transparent colors, :top stacks bars on top of one another,
|
||||
# :side stacks the bars side-by-side. Defaults to :overlap.
|
||||
attr_accessor :stack
|
||||
|
||||
|
||||
protected
|
||||
|
||||
def max_value
|
||||
@data.collect{|x| x[:data].max}.max
|
||||
end
|
||||
|
||||
def min_value
|
||||
min = 0
|
||||
if min_scale_value.nil?
|
||||
min = @data.collect{|x| x[:data].min}.min
|
||||
min = min > 0 ? 0 : min
|
||||
else
|
||||
min = min_scale_value
|
||||
end
|
||||
return min
|
||||
end
|
||||
|
||||
def get_css
|
||||
return <<EOL
|
||||
/* default fill styles for multiple datasets (probably only use a single dataset on this graph though) */
|
||||
.key1,.fill1{
|
||||
fill: #ff0000;
|
||||
fill-opacity: 0.5;
|
||||
stroke: none;
|
||||
stroke-width: 0.5px;
|
||||
}
|
||||
.key2,.fill2{
|
||||
fill: #0000ff;
|
||||
fill-opacity: 0.5;
|
||||
stroke: none;
|
||||
stroke-width: 1px;
|
||||
}
|
||||
.key3,.fill3{
|
||||
fill: #00ff00;
|
||||
fill-opacity: 0.5;
|
||||
stroke: none;
|
||||
stroke-width: 1px;
|
||||
}
|
||||
.key4,.fill4{
|
||||
fill: #ffcc00;
|
||||
fill-opacity: 0.5;
|
||||
stroke: none;
|
||||
stroke-width: 1px;
|
||||
}
|
||||
.key5,.fill5{
|
||||
fill: #00ccff;
|
||||
fill-opacity: 0.5;
|
||||
stroke: none;
|
||||
stroke-width: 1px;
|
||||
}
|
||||
.key6,.fill6{
|
||||
fill: #ff00ff;
|
||||
fill-opacity: 0.5;
|
||||
stroke: none;
|
||||
stroke-width: 1px;
|
||||
}
|
||||
.key7,.fill7{
|
||||
fill: #00ffff;
|
||||
fill-opacity: 0.5;
|
||||
stroke: none;
|
||||
stroke-width: 1px;
|
||||
}
|
||||
.key8,.fill8{
|
||||
fill: #ffff00;
|
||||
fill-opacity: 0.5;
|
||||
stroke: none;
|
||||
stroke-width: 1px;
|
||||
}
|
||||
.key9,.fill9{
|
||||
fill: #cc6666;
|
||||
fill-opacity: 0.5;
|
||||
stroke: none;
|
||||
stroke-width: 1px;
|
||||
}
|
||||
.key10,.fill10{
|
||||
fill: #663399;
|
||||
fill-opacity: 0.5;
|
||||
stroke: none;
|
||||
stroke-width: 1px;
|
||||
}
|
||||
.key11,.fill11{
|
||||
fill: #339900;
|
||||
fill-opacity: 0.5;
|
||||
stroke: none;
|
||||
stroke-width: 1px;
|
||||
}
|
||||
.key12,.fill12{
|
||||
fill: #9966FF;
|
||||
fill-opacity: 0.5;
|
||||
stroke: none;
|
||||
stroke-width: 1px;
|
||||
}
|
||||
EOL
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -1,149 +1,149 @@
|
|||
require 'rexml/document'
|
||||
require 'SVG/Graph/BarBase'
|
||||
|
||||
module SVG
|
||||
module Graph
|
||||
# === Create presentation quality SVG horitonzal bar graphs easily
|
||||
#
|
||||
# = Synopsis
|
||||
#
|
||||
# require 'SVG/Graph/BarHorizontal'
|
||||
#
|
||||
# fields = %w(Jan Feb Mar)
|
||||
# data_sales_02 = [12, 45, 21]
|
||||
#
|
||||
# graph = SVG::Graph::BarHorizontal.new({
|
||||
# :height => 500,
|
||||
# :width => 300,
|
||||
# :fields => fields,
|
||||
# })
|
||||
#
|
||||
# graph.add_data({
|
||||
# :data => data_sales_02,
|
||||
# :title => 'Sales 2002',
|
||||
# })
|
||||
#
|
||||
# print "Content-type: image/svg+xml\r\n\r\n"
|
||||
# print graph.burn
|
||||
#
|
||||
# = Description
|
||||
#
|
||||
# This object aims to allow you to easily create high quality
|
||||
# SVG horitonzal bar graphs. You can either use the default style sheet
|
||||
# or supply your own. Either way there are many options which can
|
||||
# be configured to give you control over how the graph is
|
||||
# generated - with or without a key, data elements at each point,
|
||||
# title, subtitle etc.
|
||||
#
|
||||
# = Examples
|
||||
#
|
||||
# * http://germane-software.com/repositories/public/SVG/test/test.rb
|
||||
#
|
||||
# = See also
|
||||
#
|
||||
# * SVG::Graph::Graph
|
||||
# * SVG::Graph::Bar
|
||||
# * SVG::Graph::Line
|
||||
# * SVG::Graph::Pie
|
||||
# * SVG::Graph::Plot
|
||||
# * SVG::Graph::TimeSeries
|
||||
#
|
||||
# == Author
|
||||
#
|
||||
# Sean E. Russell <serATgermaneHYPHENsoftwareDOTcom>
|
||||
#
|
||||
# Copyright 2004 Sean E. Russell
|
||||
# This software is available under the Ruby license[LICENSE.txt]
|
||||
#
|
||||
class BarHorizontal < BarBase
|
||||
# In addition to the defaults set in BarBase::set_defaults, sets
|
||||
# [rotate_y_labels] true
|
||||
# [show_x_guidelines] true
|
||||
# [show_y_guidelines] false
|
||||
def set_defaults
|
||||
super
|
||||
init_with(
|
||||
:rotate_y_labels => true,
|
||||
:show_x_guidelines => true,
|
||||
:show_y_guidelines => false
|
||||
)
|
||||
self.right_align = self.right_font = 1
|
||||
end
|
||||
|
||||
protected
|
||||
|
||||
def get_x_labels
|
||||
maxvalue = max_value
|
||||
minvalue = min_value
|
||||
range = maxvalue - minvalue
|
||||
top_pad = range == 0 ? 10 : range / 20.0
|
||||
scale_range = (maxvalue + top_pad) - minvalue
|
||||
|
||||
scale_division = scale_divisions || (scale_range / 10.0)
|
||||
|
||||
if scale_integers
|
||||
scale_division = scale_division < 1 ? 1 : scale_division.round
|
||||
end
|
||||
|
||||
rv = []
|
||||
maxvalue = maxvalue%scale_division == 0 ?
|
||||
maxvalue : maxvalue + scale_division
|
||||
minvalue.step( maxvalue, scale_division ) {|v| rv << v}
|
||||
return rv
|
||||
end
|
||||
|
||||
def get_y_labels
|
||||
@config[:fields]
|
||||
end
|
||||
|
||||
def y_label_offset( height )
|
||||
height / -2.0
|
||||
end
|
||||
|
||||
def draw_data
|
||||
minvalue = min_value
|
||||
fieldheight = field_height
|
||||
|
||||
unit_size = (@graph_width.to_f - font_size*2*right_font ) /
|
||||
(get_x_labels.max - get_x_labels.min )
|
||||
bargap = bar_gap ? (fieldheight < 10 ? fieldheight / 2 : 10) : 0
|
||||
|
||||
bar_height = fieldheight - bargap
|
||||
bar_height /= @data.length if stack == :side
|
||||
y_mod = (bar_height / 2) + (font_size / 2)
|
||||
|
||||
field_count = 1
|
||||
@config[:fields].each_index { |i|
|
||||
dataset_count = 0
|
||||
for dataset in @data
|
||||
value = dataset[:data][i]
|
||||
|
||||
top = @graph_height - (fieldheight * field_count)
|
||||
top += (bar_height * dataset_count) if stack == :side
|
||||
# cases (assume 0 = +ve):
|
||||
# value min length left
|
||||
# +ve +ve value.abs - min minvalue.abs
|
||||
# +ve -ve value.abs - 0 minvalue.abs
|
||||
# -ve -ve value.abs - 0 minvalue.abs + value
|
||||
length = (value.abs - (minvalue > 0 ? minvalue : 0)) * unit_size
|
||||
left = (minvalue.abs + (value < 0 ? value : 0)) * unit_size
|
||||
|
||||
@graph.add_element( "rect", {
|
||||
"x" => left.to_s,
|
||||
"y" => top.to_s,
|
||||
"width" => length.to_s,
|
||||
"height" => bar_height.to_s,
|
||||
"class" => "fill#{dataset_count+1}"
|
||||
})
|
||||
|
||||
make_datapoint_text(
|
||||
left+length+5, top+y_mod, value, "text-anchor: start; "
|
||||
)
|
||||
dataset_count += 1
|
||||
end
|
||||
field_count += 1
|
||||
}
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
require 'rexml/document'
|
||||
require 'SVG/Graph/BarBase'
|
||||
|
||||
module SVG
|
||||
module Graph
|
||||
# === Create presentation quality SVG horitonzal bar graphs easily
|
||||
#
|
||||
# = Synopsis
|
||||
#
|
||||
# require 'SVG/Graph/BarHorizontal'
|
||||
#
|
||||
# fields = %w(Jan Feb Mar)
|
||||
# data_sales_02 = [12, 45, 21]
|
||||
#
|
||||
# graph = SVG::Graph::BarHorizontal.new({
|
||||
# :height => 500,
|
||||
# :width => 300,
|
||||
# :fields => fields,
|
||||
# })
|
||||
#
|
||||
# graph.add_data({
|
||||
# :data => data_sales_02,
|
||||
# :title => 'Sales 2002',
|
||||
# })
|
||||
#
|
||||
# print "Content-type: image/svg+xml\r\n\r\n"
|
||||
# print graph.burn
|
||||
#
|
||||
# = Description
|
||||
#
|
||||
# This object aims to allow you to easily create high quality
|
||||
# SVG horitonzal bar graphs. You can either use the default style sheet
|
||||
# or supply your own. Either way there are many options which can
|
||||
# be configured to give you control over how the graph is
|
||||
# generated - with or without a key, data elements at each point,
|
||||
# title, subtitle etc.
|
||||
#
|
||||
# = Examples
|
||||
#
|
||||
# * http://germane-software.com/repositories/public/SVG/test/test.rb
|
||||
#
|
||||
# = See also
|
||||
#
|
||||
# * SVG::Graph::Graph
|
||||
# * SVG::Graph::Bar
|
||||
# * SVG::Graph::Line
|
||||
# * SVG::Graph::Pie
|
||||
# * SVG::Graph::Plot
|
||||
# * SVG::Graph::TimeSeries
|
||||
#
|
||||
# == Author
|
||||
#
|
||||
# Sean E. Russell <serATgermaneHYPHENsoftwareDOTcom>
|
||||
#
|
||||
# Copyright 2004 Sean E. Russell
|
||||
# This software is available under the Ruby license[LICENSE.txt]
|
||||
#
|
||||
class BarHorizontal < BarBase
|
||||
# In addition to the defaults set in BarBase::set_defaults, sets
|
||||
# [rotate_y_labels] true
|
||||
# [show_x_guidelines] true
|
||||
# [show_y_guidelines] false
|
||||
def set_defaults
|
||||
super
|
||||
init_with(
|
||||
:rotate_y_labels => true,
|
||||
:show_x_guidelines => true,
|
||||
:show_y_guidelines => false
|
||||
)
|
||||
self.right_align = self.right_font = 1
|
||||
end
|
||||
|
||||
protected
|
||||
|
||||
def get_x_labels
|
||||
maxvalue = max_value
|
||||
minvalue = min_value
|
||||
range = maxvalue - minvalue
|
||||
top_pad = range == 0 ? 10 : range / 20.0
|
||||
scale_range = (maxvalue + top_pad) - minvalue
|
||||
|
||||
scale_division = scale_divisions || (scale_range / 10.0)
|
||||
|
||||
if scale_integers
|
||||
scale_division = scale_division < 1 ? 1 : scale_division.round
|
||||
end
|
||||
|
||||
rv = []
|
||||
maxvalue = maxvalue%scale_division == 0 ?
|
||||
maxvalue : maxvalue + scale_division
|
||||
minvalue.step( maxvalue, scale_division ) {|v| rv << v}
|
||||
return rv
|
||||
end
|
||||
|
||||
def get_y_labels
|
||||
@config[:fields]
|
||||
end
|
||||
|
||||
def y_label_offset( height )
|
||||
height / -2.0
|
||||
end
|
||||
|
||||
def draw_data
|
||||
minvalue = min_value
|
||||
fieldheight = field_height
|
||||
|
||||
unit_size = (@graph_width.to_f - font_size*2*right_font ) /
|
||||
(get_x_labels.max - get_x_labels.min )
|
||||
bargap = bar_gap ? (fieldheight < 10 ? fieldheight / 2 : 10) : 0
|
||||
|
||||
bar_height = fieldheight - bargap
|
||||
bar_height /= @data.length if stack == :side
|
||||
y_mod = (bar_height / 2) + (font_size / 2)
|
||||
|
||||
field_count = 1
|
||||
@config[:fields].each_index { |i|
|
||||
dataset_count = 0
|
||||
for dataset in @data
|
||||
value = dataset[:data][i]
|
||||
|
||||
top = @graph_height - (fieldheight * field_count)
|
||||
top += (bar_height * dataset_count) if stack == :side
|
||||
# cases (assume 0 = +ve):
|
||||
# value min length left
|
||||
# +ve +ve value.abs - min minvalue.abs
|
||||
# +ve -ve value.abs - 0 minvalue.abs
|
||||
# -ve -ve value.abs - 0 minvalue.abs + value
|
||||
length = (value.abs - (minvalue > 0 ? minvalue : 0)) * unit_size
|
||||
left = (minvalue.abs + (value < 0 ? value : 0)) * unit_size
|
||||
|
||||
@graph.add_element( "rect", {
|
||||
"x" => left.to_s,
|
||||
"y" => top.to_s,
|
||||
"width" => length.to_s,
|
||||
"height" => bar_height.to_s,
|
||||
"class" => "fill#{dataset_count+1}"
|
||||
})
|
||||
|
||||
make_datapoint_text(
|
||||
left+length+5, top+y_mod, value, "text-anchor: start; "
|
||||
)
|
||||
dataset_count += 1
|
||||
end
|
||||
field_count += 1
|
||||
}
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
File diff suppressed because it is too large
Load Diff
|
@ -1,395 +1,395 @@
|
|||
require 'SVG/Graph/Graph'
|
||||
|
||||
module SVG
|
||||
module Graph
|
||||
# === Create presentation quality SVG pie graphs easily
|
||||
#
|
||||
# == Synopsis
|
||||
#
|
||||
# require 'SVG/Graph/Pie'
|
||||
#
|
||||
# fields = %w(Jan Feb Mar)
|
||||
# data_sales_02 = [12, 45, 21]
|
||||
#
|
||||
# graph = SVG::Graph::Pie.new({
|
||||
# :height => 500,
|
||||
# :width => 300,
|
||||
# :fields => fields,
|
||||
# })
|
||||
#
|
||||
# graph.add_data({
|
||||
# :data => data_sales_02,
|
||||
# :title => 'Sales 2002',
|
||||
# })
|
||||
#
|
||||
# print "Content-type: image/svg+xml\r\n\r\n"
|
||||
# print graph.burn();
|
||||
#
|
||||
# == Description
|
||||
#
|
||||
# This object aims to allow you to easily create high quality
|
||||
# SVG pie graphs. You can either use the default style sheet
|
||||
# or supply your own. Either way there are many options which can
|
||||
# be configured to give you control over how the graph is
|
||||
# generated - with or without a key, display percent on pie chart,
|
||||
# title, subtitle etc.
|
||||
#
|
||||
# = Examples
|
||||
#
|
||||
# http://www.germane-software/repositories/public/SVG/test/single.rb
|
||||
#
|
||||
# == See also
|
||||
#
|
||||
# * SVG::Graph::Graph
|
||||
# * SVG::Graph::BarHorizontal
|
||||
# * SVG::Graph::Bar
|
||||
# * SVG::Graph::Line
|
||||
# * SVG::Graph::Plot
|
||||
# * SVG::Graph::TimeSeries
|
||||
#
|
||||
# == Author
|
||||
#
|
||||
# Sean E. Russell <serATgermaneHYPHENsoftwareDOTcom>
|
||||
#
|
||||
# Copyright 2004 Sean E. Russell
|
||||
# This software is available under the Ruby license[LICENSE.txt]
|
||||
#
|
||||
class Pie < Graph
|
||||
# Defaults are those set by Graph::initialize, and
|
||||
# [show_shadow] true
|
||||
# [shadow_offset] 10
|
||||
# [show_data_labels] false
|
||||
# [show_actual_values] false
|
||||
# [show_percent] true
|
||||
# [show_key_data_labels] true
|
||||
# [show_key_actual_values] true
|
||||
# [show_key_percent] false
|
||||
# [expanded] false
|
||||
# [expand_greatest] false
|
||||
# [expand_gap] 10
|
||||
# [show_x_labels] false
|
||||
# [show_y_labels] false
|
||||
# [datapoint_font_size] 12
|
||||
def set_defaults
|
||||
init_with(
|
||||
:show_shadow => true,
|
||||
:shadow_offset => 10,
|
||||
|
||||
:show_data_labels => false,
|
||||
:show_actual_values => false,
|
||||
:show_percent => true,
|
||||
|
||||
:show_key_data_labels => true,
|
||||
:show_key_actual_values => true,
|
||||
:show_key_percent => false,
|
||||
|
||||
:expanded => false,
|
||||
:expand_greatest => false,
|
||||
:expand_gap => 10,
|
||||
|
||||
:show_x_labels => false,
|
||||
:show_y_labels => false,
|
||||
:datapoint_font_size => 12
|
||||
)
|
||||
@data = []
|
||||
end
|
||||
|
||||
# Adds a data set to the graph.
|
||||
#
|
||||
# graph.add_data( { :data => [1,2,3,4] } )
|
||||
#
|
||||
# Note that the :title is not necessary. If multiple
|
||||
# data sets are added to the graph, the pie chart will
|
||||
# display the +sums+ of the data. EG:
|
||||
#
|
||||
# graph.add_data( { :data => [1,2,3,4] } )
|
||||
# graph.add_data( { :data => [2,3,5,9] } )
|
||||
#
|
||||
# is the same as:
|
||||
#
|
||||
# graph.add_data( { :data => [3,5,8,13] } )
|
||||
def add_data arg
|
||||
arg[:data].each_index {|idx|
|
||||
@data[idx] = 0 unless @data[idx]
|
||||
@data[idx] += arg[:data][idx]
|
||||
}
|
||||
end
|
||||
|
||||
# If true, displays a drop shadow for the chart
|
||||
attr_accessor :show_shadow
|
||||
# Sets the offset of the shadow from the pie chart
|
||||
attr_accessor :shadow_offset
|
||||
# If true, display the data labels on the chart
|
||||
attr_accessor :show_data_labels
|
||||
# If true, display the actual field values in the data labels
|
||||
attr_accessor :show_actual_values
|
||||
# If true, display the percentage value of each pie wedge in the data
|
||||
# labels
|
||||
attr_accessor :show_percent
|
||||
# If true, display the labels in the key
|
||||
attr_accessor :show_key_data_labels
|
||||
# If true, display the actual value of the field in the key
|
||||
attr_accessor :show_key_actual_values
|
||||
# If true, display the percentage value of the wedges in the key
|
||||
attr_accessor :show_key_percent
|
||||
# If true, "explode" the pie (put space between the wedges)
|
||||
attr_accessor :expanded
|
||||
# If true, expand the largest pie wedge
|
||||
attr_accessor :expand_greatest
|
||||
# The amount of space between expanded wedges
|
||||
attr_accessor :expand_gap
|
||||
# The font size of the data point labels
|
||||
attr_accessor :datapoint_font_size
|
||||
|
||||
|
||||
protected
|
||||
|
||||
def add_defs defs
|
||||
gradient = defs.add_element( "filter", {
|
||||
"id"=>"dropshadow",
|
||||
"width" => "1.2",
|
||||
"height" => "1.2",
|
||||
} )
|
||||
gradient.add_element( "feGaussianBlur", {
|
||||
"stdDeviation" => "4",
|
||||
"result" => "blur"
|
||||
})
|
||||
end
|
||||
|
||||
# We don't need the graph
|
||||
def draw_graph
|
||||
end
|
||||
|
||||
def get_y_labels
|
||||
[""]
|
||||
end
|
||||
|
||||
def get_x_labels
|
||||
[""]
|
||||
end
|
||||
|
||||
def keys
|
||||
total = 0
|
||||
max_value = 0
|
||||
@data.each {|x| total += x }
|
||||
percent_scale = 100.0 / total
|
||||
count = -1
|
||||
a = @config[:fields].collect{ |x|
|
||||
count += 1
|
||||
v = @data[count]
|
||||
perc = show_key_percent ? " "+(v * percent_scale).round.to_s+"%" : ""
|
||||
x + " [" + v.to_s + "]" + perc
|
||||
}
|
||||
end
|
||||
|
||||
RADIANS = Math::PI/180
|
||||
|
||||
def draw_data
|
||||
@graph = @root.add_element( "g" )
|
||||
background = @graph.add_element("g")
|
||||
midground = @graph.add_element("g")
|
||||
|
||||
diameter = @graph_height > @graph_width ? @graph_width : @graph_height
|
||||
diameter -= expand_gap if expanded or expand_greatest
|
||||
diameter -= datapoint_font_size if show_data_labels
|
||||
diameter -= 10 if show_shadow
|
||||
radius = diameter / 2.0
|
||||
|
||||
xoff = (width - diameter) / 2
|
||||
yoff = (height - @border_bottom - diameter)
|
||||
yoff -= 10 if show_shadow
|
||||
@graph.attributes['transform'] = "translate( #{xoff} #{yoff} )"
|
||||
|
||||
wedge_text_pad = 5
|
||||
wedge_text_pad = 20 if show_percent and show_data_labels
|
||||
|
||||
total = 0
|
||||
max_value = 0
|
||||
@data.each {|x|
|
||||
max_value = max_value < x ? x : max_value
|
||||
total += x
|
||||
}
|
||||
percent_scale = 100.0 / total
|
||||
|
||||
prev_percent = 0
|
||||
rad_mult = 3.6 * RADIANS
|
||||
@config[:fields].each_index { |count|
|
||||
value = @data[count]
|
||||
percent = percent_scale * value
|
||||
|
||||
radians = prev_percent * rad_mult
|
||||
x_start = radius+(Math.sin(radians) * radius)
|
||||
y_start = radius-(Math.cos(radians) * radius)
|
||||
radians = (prev_percent+percent) * rad_mult
|
||||
x_end = radius+(Math.sin(radians) * radius)
|
||||
x_end -= 0.00001 if @data.length == 1
|
||||
y_end = radius-(Math.cos(radians) * radius)
|
||||
path = "M#{radius},#{radius} L#{x_start},#{y_start} "+
|
||||
"A#{radius},#{radius} "+
|
||||
"0, #{percent >= 50 ? '1' : '0'},1, "+
|
||||
"#{x_end} #{y_end} Z"
|
||||
|
||||
|
||||
wedge = @foreground.add_element( "path", {
|
||||
"d" => path,
|
||||
"class" => "fill#{count+1}"
|
||||
})
|
||||
|
||||
translate = nil
|
||||
tx = 0
|
||||
ty = 0
|
||||
half_percent = prev_percent + percent / 2
|
||||
radians = half_percent * rad_mult
|
||||
|
||||
if show_shadow
|
||||
shadow = background.add_element( "path", {
|
||||
"d" => path,
|
||||
"filter" => "url(#dropshadow)",
|
||||
"style" => "fill: #ccc; stroke: none;"
|
||||
})
|
||||
clear = midground.add_element( "path", {
|
||||
"d" => path,
|
||||
"style" => "fill: #fff; stroke: none;"
|
||||
})
|
||||
end
|
||||
|
||||
if expanded or (expand_greatest && value == max_value)
|
||||
tx = (Math.sin(radians) * expand_gap)
|
||||
ty = -(Math.cos(radians) * expand_gap)
|
||||
translate = "translate( #{tx} #{ty} )"
|
||||
wedge.attributes["transform"] = translate
|
||||
clear.attributes["transform"] = translate if clear
|
||||
end
|
||||
|
||||
if show_shadow
|
||||
shadow.attributes["transform"] =
|
||||
"translate( #{tx+shadow_offset} #{ty+shadow_offset} )"
|
||||
end
|
||||
|
||||
if show_data_labels and value != 0
|
||||
label = ""
|
||||
label += @config[:fields][count] if show_key_data_labels
|
||||
label += " ["+value.to_s+"]" if show_actual_values
|
||||
label += " "+percent.round.to_s+"%" if show_percent
|
||||
|
||||
msr = Math.sin(radians)
|
||||
mcr = Math.cos(radians)
|
||||
tx = radius + (msr * radius)
|
||||
ty = radius -(mcr * radius)
|
||||
|
||||
if expanded or (expand_greatest && value == max_value)
|
||||
tx += (msr * expand_gap)
|
||||
ty -= (mcr * expand_gap)
|
||||
end
|
||||
@foreground.add_element( "text", {
|
||||
"x" => tx.to_s,
|
||||
"y" => ty.to_s,
|
||||
"class" => "dataPointLabel",
|
||||
"style" => "stroke: #fff; stroke-width: 2;"
|
||||
}).text = label.to_s
|
||||
@foreground.add_element( "text", {
|
||||
"x" => tx.to_s,
|
||||
"y" => ty.to_s,
|
||||
"class" => "dataPointLabel",
|
||||
}).text = label.to_s
|
||||
end
|
||||
|
||||
prev_percent += percent
|
||||
}
|
||||
end
|
||||
|
||||
|
||||
def round val, to
|
||||
up = 10**to.to_f
|
||||
(val * up).to_i / up
|
||||
end
|
||||
|
||||
|
||||
def get_css
|
||||
return <<EOL
|
||||
.dataPointLabel{
|
||||
fill: #000000;
|
||||
text-anchor:middle;
|
||||
font-size: #{datapoint_font_size}px;
|
||||
font-family: "Arial", sans-serif;
|
||||
font-weight: normal;
|
||||
}
|
||||
|
||||
/* key - MUST match fill styles */
|
||||
.key1,.fill1{
|
||||
fill: #ff0000;
|
||||
fill-opacity: 0.7;
|
||||
stroke: none;
|
||||
stroke-width: 1px;
|
||||
}
|
||||
.key2,.fill2{
|
||||
fill: #0000ff;
|
||||
fill-opacity: 0.7;
|
||||
stroke: none;
|
||||
stroke-width: 1px;
|
||||
}
|
||||
.key3,.fill3{
|
||||
fill-opacity: 0.7;
|
||||
fill: #00ff00;
|
||||
stroke: none;
|
||||
stroke-width: 1px;
|
||||
}
|
||||
.key4,.fill4{
|
||||
fill-opacity: 0.7;
|
||||
fill: #ffcc00;
|
||||
stroke: none;
|
||||
stroke-width: 1px;
|
||||
}
|
||||
.key5,.fill5{
|
||||
fill-opacity: 0.7;
|
||||
fill: #00ccff;
|
||||
stroke: none;
|
||||
stroke-width: 1px;
|
||||
}
|
||||
.key6,.fill6{
|
||||
fill-opacity: 0.7;
|
||||
fill: #ff00ff;
|
||||
stroke: none;
|
||||
stroke-width: 1px;
|
||||
}
|
||||
.key7,.fill7{
|
||||
fill-opacity: 0.7;
|
||||
fill: #00ff99;
|
||||
stroke: none;
|
||||
stroke-width: 1px;
|
||||
}
|
||||
.key8,.fill8{
|
||||
fill-opacity: 0.7;
|
||||
fill: #ffff00;
|
||||
stroke: none;
|
||||
stroke-width: 1px;
|
||||
}
|
||||
.key9,.fill9{
|
||||
fill-opacity: 0.7;
|
||||
fill: #cc6666;
|
||||
stroke: none;
|
||||
stroke-width: 1px;
|
||||
}
|
||||
.key10,.fill10{
|
||||
fill-opacity: 0.7;
|
||||
fill: #663399;
|
||||
stroke: none;
|
||||
stroke-width: 1px;
|
||||
}
|
||||
.key11,.fill11{
|
||||
fill-opacity: 0.7;
|
||||
fill: #339900;
|
||||
stroke: none;
|
||||
stroke-width: 1px;
|
||||
}
|
||||
.key12,.fill12{
|
||||
fill-opacity: 0.7;
|
||||
fill: #9966FF;
|
||||
stroke: none;
|
||||
stroke-width: 1px;
|
||||
}
|
||||
EOL
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
require 'SVG/Graph/Graph'
|
||||
|
||||
module SVG
|
||||
module Graph
|
||||
# === Create presentation quality SVG pie graphs easily
|
||||
#
|
||||
# == Synopsis
|
||||
#
|
||||
# require 'SVG/Graph/Pie'
|
||||
#
|
||||
# fields = %w(Jan Feb Mar)
|
||||
# data_sales_02 = [12, 45, 21]
|
||||
#
|
||||
# graph = SVG::Graph::Pie.new({
|
||||
# :height => 500,
|
||||
# :width => 300,
|
||||
# :fields => fields,
|
||||
# })
|
||||
#
|
||||
# graph.add_data({
|
||||
# :data => data_sales_02,
|
||||
# :title => 'Sales 2002',
|
||||
# })
|
||||
#
|
||||
# print "Content-type: image/svg+xml\r\n\r\n"
|
||||
# print graph.burn();
|
||||
#
|
||||
# == Description
|
||||
#
|
||||
# This object aims to allow you to easily create high quality
|
||||
# SVG pie graphs. You can either use the default style sheet
|
||||
# or supply your own. Either way there are many options which can
|
||||
# be configured to give you control over how the graph is
|
||||
# generated - with or without a key, display percent on pie chart,
|
||||
# title, subtitle etc.
|
||||
#
|
||||
# = Examples
|
||||
#
|
||||
# http://www.germane-software/repositories/public/SVG/test/single.rb
|
||||
#
|
||||
# == See also
|
||||
#
|
||||
# * SVG::Graph::Graph
|
||||
# * SVG::Graph::BarHorizontal
|
||||
# * SVG::Graph::Bar
|
||||
# * SVG::Graph::Line
|
||||
# * SVG::Graph::Plot
|
||||
# * SVG::Graph::TimeSeries
|
||||
#
|
||||
# == Author
|
||||
#
|
||||
# Sean E. Russell <serATgermaneHYPHENsoftwareDOTcom>
|
||||
#
|
||||
# Copyright 2004 Sean E. Russell
|
||||
# This software is available under the Ruby license[LICENSE.txt]
|
||||
#
|
||||
class Pie < Graph
|
||||
# Defaults are those set by Graph::initialize, and
|
||||
# [show_shadow] true
|
||||
# [shadow_offset] 10
|
||||
# [show_data_labels] false
|
||||
# [show_actual_values] false
|
||||
# [show_percent] true
|
||||
# [show_key_data_labels] true
|
||||
# [show_key_actual_values] true
|
||||
# [show_key_percent] false
|
||||
# [expanded] false
|
||||
# [expand_greatest] false
|
||||
# [expand_gap] 10
|
||||
# [show_x_labels] false
|
||||
# [show_y_labels] false
|
||||
# [datapoint_font_size] 12
|
||||
def set_defaults
|
||||
init_with(
|
||||
:show_shadow => true,
|
||||
:shadow_offset => 10,
|
||||
|
||||
:show_data_labels => false,
|
||||
:show_actual_values => false,
|
||||
:show_percent => true,
|
||||
|
||||
:show_key_data_labels => true,
|
||||
:show_key_actual_values => true,
|
||||
:show_key_percent => false,
|
||||
|
||||
:expanded => false,
|
||||
:expand_greatest => false,
|
||||
:expand_gap => 10,
|
||||
|
||||
:show_x_labels => false,
|
||||
:show_y_labels => false,
|
||||
:datapoint_font_size => 12
|
||||
)
|
||||
@data = []
|
||||
end
|
||||
|
||||
# Adds a data set to the graph.
|
||||
#
|
||||
# graph.add_data( { :data => [1,2,3,4] } )
|
||||
#
|
||||
# Note that the :title is not necessary. If multiple
|
||||
# data sets are added to the graph, the pie chart will
|
||||
# display the +sums+ of the data. EG:
|
||||
#
|
||||
# graph.add_data( { :data => [1,2,3,4] } )
|
||||
# graph.add_data( { :data => [2,3,5,9] } )
|
||||
#
|
||||
# is the same as:
|
||||
#
|
||||
# graph.add_data( { :data => [3,5,8,13] } )
|
||||
def add_data arg
|
||||
arg[:data].each_index {|idx|
|
||||
@data[idx] = 0 unless @data[idx]
|
||||
@data[idx] += arg[:data][idx]
|
||||
}
|
||||
end
|
||||
|
||||
# If true, displays a drop shadow for the chart
|
||||
attr_accessor :show_shadow
|
||||
# Sets the offset of the shadow from the pie chart
|
||||
attr_accessor :shadow_offset
|
||||
# If true, display the data labels on the chart
|
||||
attr_accessor :show_data_labels
|
||||
# If true, display the actual field values in the data labels
|
||||
attr_accessor :show_actual_values
|
||||
# If true, display the percentage value of each pie wedge in the data
|
||||
# labels
|
||||
attr_accessor :show_percent
|
||||
# If true, display the labels in the key
|
||||
attr_accessor :show_key_data_labels
|
||||
# If true, display the actual value of the field in the key
|
||||
attr_accessor :show_key_actual_values
|
||||
# If true, display the percentage value of the wedges in the key
|
||||
attr_accessor :show_key_percent
|
||||
# If true, "explode" the pie (put space between the wedges)
|
||||
attr_accessor :expanded
|
||||
# If true, expand the largest pie wedge
|
||||
attr_accessor :expand_greatest
|
||||
# The amount of space between expanded wedges
|
||||
attr_accessor :expand_gap
|
||||
# The font size of the data point labels
|
||||
attr_accessor :datapoint_font_size
|
||||
|
||||
|
||||
protected
|
||||
|
||||
def add_defs defs
|
||||
gradient = defs.add_element( "filter", {
|
||||
"id"=>"dropshadow",
|
||||
"width" => "1.2",
|
||||
"height" => "1.2",
|
||||
} )
|
||||
gradient.add_element( "feGaussianBlur", {
|
||||
"stdDeviation" => "4",
|
||||
"result" => "blur"
|
||||
})
|
||||
end
|
||||
|
||||
# We don't need the graph
|
||||
def draw_graph
|
||||
end
|
||||
|
||||
def get_y_labels
|
||||
[""]
|
||||
end
|
||||
|
||||
def get_x_labels
|
||||
[""]
|
||||
end
|
||||
|
||||
def keys
|
||||
total = 0
|
||||
max_value = 0
|
||||
@data.each {|x| total += x }
|
||||
percent_scale = 100.0 / total
|
||||
count = -1
|
||||
a = @config[:fields].collect{ |x|
|
||||
count += 1
|
||||
v = @data[count]
|
||||
perc = show_key_percent ? " "+(v * percent_scale).round.to_s+"%" : ""
|
||||
x + " [" + v.to_s + "]" + perc
|
||||
}
|
||||
end
|
||||
|
||||
RADIANS = Math::PI/180
|
||||
|
||||
def draw_data
|
||||
@graph = @root.add_element( "g" )
|
||||
background = @graph.add_element("g")
|
||||
midground = @graph.add_element("g")
|
||||
|
||||
diameter = @graph_height > @graph_width ? @graph_width : @graph_height
|
||||
diameter -= expand_gap if expanded or expand_greatest
|
||||
diameter -= datapoint_font_size if show_data_labels
|
||||
diameter -= 10 if show_shadow
|
||||
radius = diameter / 2.0
|
||||
|
||||
xoff = (width - diameter) / 2
|
||||
yoff = (height - @border_bottom - diameter)
|
||||
yoff -= 10 if show_shadow
|
||||
@graph.attributes['transform'] = "translate( #{xoff} #{yoff} )"
|
||||
|
||||
wedge_text_pad = 5
|
||||
wedge_text_pad = 20 if show_percent and show_data_labels
|
||||
|
||||
total = 0
|
||||
max_value = 0
|
||||
@data.each {|x|
|
||||
max_value = max_value < x ? x : max_value
|
||||
total += x
|
||||
}
|
||||
percent_scale = 100.0 / total
|
||||
|
||||
prev_percent = 0
|
||||
rad_mult = 3.6 * RADIANS
|
||||
@config[:fields].each_index { |count|
|
||||
value = @data[count]
|
||||
percent = percent_scale * value
|
||||
|
||||
radians = prev_percent * rad_mult
|
||||
x_start = radius+(Math.sin(radians) * radius)
|
||||
y_start = radius-(Math.cos(radians) * radius)
|
||||
radians = (prev_percent+percent) * rad_mult
|
||||
x_end = radius+(Math.sin(radians) * radius)
|
||||
x_end -= 0.00001 if @data.length == 1
|
||||
y_end = radius-(Math.cos(radians) * radius)
|
||||
path = "M#{radius},#{radius} L#{x_start},#{y_start} "+
|
||||
"A#{radius},#{radius} "+
|
||||
"0, #{percent >= 50 ? '1' : '0'},1, "+
|
||||
"#{x_end} #{y_end} Z"
|
||||
|
||||
|
||||
wedge = @foreground.add_element( "path", {
|
||||
"d" => path,
|
||||
"class" => "fill#{count+1}"
|
||||
})
|
||||
|
||||
translate = nil
|
||||
tx = 0
|
||||
ty = 0
|
||||
half_percent = prev_percent + percent / 2
|
||||
radians = half_percent * rad_mult
|
||||
|
||||
if show_shadow
|
||||
shadow = background.add_element( "path", {
|
||||
"d" => path,
|
||||
"filter" => "url(#dropshadow)",
|
||||
"style" => "fill: #ccc; stroke: none;"
|
||||
})
|
||||
clear = midground.add_element( "path", {
|
||||
"d" => path,
|
||||
"style" => "fill: #fff; stroke: none;"
|
||||
})
|
||||
end
|
||||
|
||||
if expanded or (expand_greatest && value == max_value)
|
||||
tx = (Math.sin(radians) * expand_gap)
|
||||
ty = -(Math.cos(radians) * expand_gap)
|
||||
translate = "translate( #{tx} #{ty} )"
|
||||
wedge.attributes["transform"] = translate
|
||||
clear.attributes["transform"] = translate if clear
|
||||
end
|
||||
|
||||
if show_shadow
|
||||
shadow.attributes["transform"] =
|
||||
"translate( #{tx+shadow_offset} #{ty+shadow_offset} )"
|
||||
end
|
||||
|
||||
if show_data_labels and value != 0
|
||||
label = ""
|
||||
label += @config[:fields][count] if show_key_data_labels
|
||||
label += " ["+value.to_s+"]" if show_actual_values
|
||||
label += " "+percent.round.to_s+"%" if show_percent
|
||||
|
||||
msr = Math.sin(radians)
|
||||
mcr = Math.cos(radians)
|
||||
tx = radius + (msr * radius)
|
||||
ty = radius -(mcr * radius)
|
||||
|
||||
if expanded or (expand_greatest && value == max_value)
|
||||
tx += (msr * expand_gap)
|
||||
ty -= (mcr * expand_gap)
|
||||
end
|
||||
@foreground.add_element( "text", {
|
||||
"x" => tx.to_s,
|
||||
"y" => ty.to_s,
|
||||
"class" => "dataPointLabel",
|
||||
"style" => "stroke: #fff; stroke-width: 2;"
|
||||
}).text = label.to_s
|
||||
@foreground.add_element( "text", {
|
||||
"x" => tx.to_s,
|
||||
"y" => ty.to_s,
|
||||
"class" => "dataPointLabel",
|
||||
}).text = label.to_s
|
||||
end
|
||||
|
||||
prev_percent += percent
|
||||
}
|
||||
end
|
||||
|
||||
|
||||
def round val, to
|
||||
up = 10**to.to_f
|
||||
(val * up).to_i / up
|
||||
end
|
||||
|
||||
|
||||
def get_css
|
||||
return <<EOL
|
||||
.dataPointLabel{
|
||||
fill: #000000;
|
||||
text-anchor:middle;
|
||||
font-size: #{datapoint_font_size}px;
|
||||
font-family: "Arial", sans-serif;
|
||||
font-weight: normal;
|
||||
}
|
||||
|
||||
/* key - MUST match fill styles */
|
||||
.key1,.fill1{
|
||||
fill: #ff0000;
|
||||
fill-opacity: 0.7;
|
||||
stroke: none;
|
||||
stroke-width: 1px;
|
||||
}
|
||||
.key2,.fill2{
|
||||
fill: #0000ff;
|
||||
fill-opacity: 0.7;
|
||||
stroke: none;
|
||||
stroke-width: 1px;
|
||||
}
|
||||
.key3,.fill3{
|
||||
fill-opacity: 0.7;
|
||||
fill: #00ff00;
|
||||
stroke: none;
|
||||
stroke-width: 1px;
|
||||
}
|
||||
.key4,.fill4{
|
||||
fill-opacity: 0.7;
|
||||
fill: #ffcc00;
|
||||
stroke: none;
|
||||
stroke-width: 1px;
|
||||
}
|
||||
.key5,.fill5{
|
||||
fill-opacity: 0.7;
|
||||
fill: #00ccff;
|
||||
stroke: none;
|
||||
stroke-width: 1px;
|
||||
}
|
||||
.key6,.fill6{
|
||||
fill-opacity: 0.7;
|
||||
fill: #ff00ff;
|
||||
stroke: none;
|
||||
stroke-width: 1px;
|
||||
}
|
||||
.key7,.fill7{
|
||||
fill-opacity: 0.7;
|
||||
fill: #00ff99;
|
||||
stroke: none;
|
||||
stroke-width: 1px;
|
||||
}
|
||||
.key8,.fill8{
|
||||
fill-opacity: 0.7;
|
||||
fill: #ffff00;
|
||||
stroke: none;
|
||||
stroke-width: 1px;
|
||||
}
|
||||
.key9,.fill9{
|
||||
fill-opacity: 0.7;
|
||||
fill: #cc6666;
|
||||
stroke: none;
|
||||
stroke-width: 1px;
|
||||
}
|
||||
.key10,.fill10{
|
||||
fill-opacity: 0.7;
|
||||
fill: #663399;
|
||||
stroke: none;
|
||||
stroke-width: 1px;
|
||||
}
|
||||
.key11,.fill11{
|
||||
fill-opacity: 0.7;
|
||||
fill: #339900;
|
||||
stroke: none;
|
||||
stroke-width: 1px;
|
||||
}
|
||||
.key12,.fill12{
|
||||
fill-opacity: 0.7;
|
||||
fill: #9966FF;
|
||||
stroke: none;
|
||||
stroke-width: 1px;
|
||||
}
|
||||
EOL
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
File diff suppressed because it is too large
Load Diff
|
@ -1,238 +1,238 @@
|
|||
require 'SVG/Graph/Plot'
|
||||
|
||||
module SVG
|
||||
module Graph
|
||||
# === For creating SVG plots of scalar temporal data
|
||||
#
|
||||
# = Synopsis
|
||||
#
|
||||
# require 'SVG/Graph/TimeSeriess'
|
||||
#
|
||||
# # Data sets are x,y pairs
|
||||
# data1 = ["6/17/72", 11, "1/11/72", 7, "4/13/04 17:31", 11,
|
||||
# "9/11/01", 9, "9/1/85", 2, "9/1/88", 1, "1/15/95", 13]
|
||||
# data2 = ["8/1/73", 18, "3/1/77", 15, "10/1/98", 4,
|
||||
# "5/1/02", 14, "3/1/95", 6, "8/1/91", 12, "12/1/87", 6,
|
||||
# "5/1/84", 17, "10/1/80", 12]
|
||||
#
|
||||
# graph = SVG::Graph::TimeSeries.new( {
|
||||
# :width => 640,
|
||||
# :height => 480,
|
||||
# :graph_title => title,
|
||||
# :show_graph_title => true,
|
||||
# :no_css => true,
|
||||
# :key => true,
|
||||
# :scale_x_integers => true,
|
||||
# :scale_y_integers => true,
|
||||
# :min_x_value => 0,
|
||||
# :min_y_value => 0,
|
||||
# :show_data_labels => true,
|
||||
# :show_x_guidelines => true,
|
||||
# :show_x_title => true,
|
||||
# :x_title => "Time",
|
||||
# :show_y_title => true,
|
||||
# :y_title => "Ice Cream Cones",
|
||||
# :y_title_text_direction => :bt,
|
||||
# :stagger_x_labels => true,
|
||||
# :x_label_format => "%m/%d/%y",
|
||||
# })
|
||||
#
|
||||
# graph.add_data({
|
||||
# :data => projection
|
||||
# :title => 'Projected',
|
||||
# })
|
||||
#
|
||||
# graph.add_data({
|
||||
# :data => actual,
|
||||
# :title => 'Actual',
|
||||
# })
|
||||
#
|
||||
# print graph.burn()
|
||||
#
|
||||
# = Description
|
||||
#
|
||||
# Produces a graph of temporal scalar data.
|
||||
#
|
||||
# = Examples
|
||||
#
|
||||
# http://www.germane-software/repositories/public/SVG/test/timeseries.rb
|
||||
#
|
||||
# = Notes
|
||||
#
|
||||
# The default stylesheet handles upto 10 data sets, if you
|
||||
# use more you must create your own stylesheet and add the
|
||||
# additional settings for the extra data sets. You will know
|
||||
# if you go over 10 data sets as they will have no style and
|
||||
# be in black.
|
||||
#
|
||||
# Unlike the other types of charts, data sets must contain x,y pairs:
|
||||
#
|
||||
# [ "12:30", 2 ] # A data set with 1 point: ("12:30",2)
|
||||
# [ "01:00",2, "14:20",6] # A data set with 2 points: ("01:00",2) and
|
||||
# # ("14:20",6)
|
||||
#
|
||||
# Note that multiple data sets within the same chart can differ in length,
|
||||
# and that the data in the datasets needn't be in order; they will be ordered
|
||||
# by the plot along the X-axis.
|
||||
#
|
||||
# The dates must be parseable by ParseDate, but otherwise can be
|
||||
# any order of magnitude (seconds within the hour, or years)
|
||||
#
|
||||
# = See also
|
||||
#
|
||||
# * SVG::Graph::Graph
|
||||
# * SVG::Graph::BarHorizontal
|
||||
# * SVG::Graph::Bar
|
||||
# * SVG::Graph::Line
|
||||
# * SVG::Graph::Pie
|
||||
# * SVG::Graph::Plot
|
||||
#
|
||||
# == Author
|
||||
#
|
||||
# Sean E. Russell <serATgermaneHYPHENsoftwareDOTcom>
|
||||
#
|
||||
# Copyright 2004 Sean E. Russell
|
||||
# This software is available under the Ruby license[LICENSE.txt]
|
||||
#
|
||||
class TimeSeries < Plot
|
||||
# In addition to the defaults set by Graph::initialize and
|
||||
# Plot::set_defaults, sets:
|
||||
# [x_label_format] '%Y-%m-%d %H:%M:%S'
|
||||
# [popup_format] '%Y-%m-%d %H:%M:%S'
|
||||
def set_defaults
|
||||
super
|
||||
init_with(
|
||||
#:max_time_span => '',
|
||||
:x_label_format => '%Y-%m-%d %H:%M:%S',
|
||||
:popup_format => '%Y-%m-%d %H:%M:%S'
|
||||
)
|
||||
end
|
||||
|
||||
# The format string use do format the X axis labels.
|
||||
# See Time::strformat
|
||||
attr_accessor :x_label_format
|
||||
# Use this to set the spacing between dates on the axis. The value
|
||||
# must be of the form
|
||||
# "\d+ ?(days|weeks|months|years|hours|minutes|seconds)?"
|
||||
#
|
||||
# EG:
|
||||
#
|
||||
# graph.timescale_divisions = "2 weeks"
|
||||
#
|
||||
# will cause the chart to try to divide the X axis up into segments of
|
||||
# two week periods.
|
||||
attr_accessor :timescale_divisions
|
||||
# The formatting used for the popups. See x_label_format
|
||||
attr_accessor :popup_format
|
||||
|
||||
# Add data to the plot.
|
||||
#
|
||||
# d1 = [ "12:30", 2 ] # A data set with 1 point: ("12:30",2)
|
||||
# d2 = [ "01:00",2, "14:20",6] # A data set with 2 points: ("01:00",2) and
|
||||
# # ("14:20",6)
|
||||
# graph.add_data(
|
||||
# :data => d1,
|
||||
# :title => 'One'
|
||||
# )
|
||||
# graph.add_data(
|
||||
# :data => d2,
|
||||
# :title => 'Two'
|
||||
# )
|
||||
#
|
||||
# Note that the data must be in time,value pairs, and that the date format
|
||||
# may be any date that is parseable by ParseDate.
|
||||
def add_data data
|
||||
@data = [] unless @data
|
||||
|
||||
raise "No data provided by #{@data.inspect}" unless data[:data] and
|
||||
data[:data].kind_of? Array
|
||||
raise "Data supplied must be x,y pairs! "+
|
||||
"The data provided contained an odd set of "+
|
||||
"data points" unless data[:data].length % 2 == 0
|
||||
return if data[:data].length == 0
|
||||
|
||||
|
||||
x = []
|
||||
y = []
|
||||
data[:data].each_index {|i|
|
||||
if i%2 == 0
|
||||
t = DateTime.parse( data[:data][i] ).to_time
|
||||
x << t.to_i
|
||||
else
|
||||
y << data[:data][i]
|
||||
end
|
||||
}
|
||||
sort( x, y )
|
||||
data[:data] = [x,y]
|
||||
@data << data
|
||||
end
|
||||
|
||||
|
||||
protected
|
||||
|
||||
def min_x_value=(value)
|
||||
@min_x_value = DateTime.parse( value ).to_time
|
||||
end
|
||||
|
||||
|
||||
def format x, y
|
||||
Time.at( x ).strftime( popup_format )
|
||||
end
|
||||
|
||||
def get_x_labels
|
||||
get_x_values.collect { |v| Time.at(v).strftime( x_label_format ) }
|
||||
end
|
||||
|
||||
private
|
||||
def get_x_values
|
||||
rv = []
|
||||
min, max, scale_division = x_range
|
||||
if timescale_divisions
|
||||
timescale_divisions =~ /(\d+) ?(day|week|month|year|hour|minute|second)?/
|
||||
division_units = $2 ? $2 : "day"
|
||||
amount = $1.to_i
|
||||
if amount
|
||||
step = nil
|
||||
case division_units
|
||||
when "month"
|
||||
cur = min
|
||||
while cur < max
|
||||
rv << cur
|
||||
arr = Time.at( cur ).to_a
|
||||
arr[4] += amount
|
||||
if arr[4] > 12
|
||||
arr[5] += (arr[4] / 12).to_i
|
||||
arr[4] = (arr[4] % 12)
|
||||
end
|
||||
cur = Time.local(*arr).to_i
|
||||
end
|
||||
when "year"
|
||||
cur = min
|
||||
while cur < max
|
||||
rv << cur
|
||||
arr = Time.at( cur ).to_a
|
||||
arr[5] += amount
|
||||
cur = Time.local(*arr).to_i
|
||||
end
|
||||
when "week"
|
||||
step = 7 * 24 * 60 * 60 * amount
|
||||
when "day"
|
||||
step = 24 * 60 * 60 * amount
|
||||
when "hour"
|
||||
step = 60 * 60 * amount
|
||||
when "minute"
|
||||
step = 60 * amount
|
||||
when "second"
|
||||
step = amount
|
||||
end
|
||||
min.step( max, step ) {|v| rv << v} if step
|
||||
|
||||
return rv
|
||||
end
|
||||
end
|
||||
min.step( max, scale_division ) {|v| rv << v}
|
||||
return rv
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
require 'SVG/Graph/Plot'
|
||||
|
||||
module SVG
|
||||
module Graph
|
||||
# === For creating SVG plots of scalar temporal data
|
||||
#
|
||||
# = Synopsis
|
||||
#
|
||||
# require 'SVG/Graph/TimeSeriess'
|
||||
#
|
||||
# # Data sets are x,y pairs
|
||||
# data1 = ["6/17/72", 11, "1/11/72", 7, "4/13/04 17:31", 11,
|
||||
# "9/11/01", 9, "9/1/85", 2, "9/1/88", 1, "1/15/95", 13]
|
||||
# data2 = ["8/1/73", 18, "3/1/77", 15, "10/1/98", 4,
|
||||
# "5/1/02", 14, "3/1/95", 6, "8/1/91", 12, "12/1/87", 6,
|
||||
# "5/1/84", 17, "10/1/80", 12]
|
||||
#
|
||||
# graph = SVG::Graph::TimeSeries.new( {
|
||||
# :width => 640,
|
||||
# :height => 480,
|
||||
# :graph_title => title,
|
||||
# :show_graph_title => true,
|
||||
# :no_css => true,
|
||||
# :key => true,
|
||||
# :scale_x_integers => true,
|
||||
# :scale_y_integers => true,
|
||||
# :min_x_value => 0,
|
||||
# :min_y_value => 0,
|
||||
# :show_data_labels => true,
|
||||
# :show_x_guidelines => true,
|
||||
# :show_x_title => true,
|
||||
# :x_title => "Time",
|
||||
# :show_y_title => true,
|
||||
# :y_title => "Ice Cream Cones",
|
||||
# :y_title_text_direction => :bt,
|
||||
# :stagger_x_labels => true,
|
||||
# :x_label_format => "%m/%d/%y",
|
||||
# })
|
||||
#
|
||||
# graph.add_data({
|
||||
# :data => projection
|
||||
# :title => 'Projected',
|
||||
# })
|
||||
#
|
||||
# graph.add_data({
|
||||
# :data => actual,
|
||||
# :title => 'Actual',
|
||||
# })
|
||||
#
|
||||
# print graph.burn()
|
||||
#
|
||||
# = Description
|
||||
#
|
||||
# Produces a graph of temporal scalar data.
|
||||
#
|
||||
# = Examples
|
||||
#
|
||||
# http://www.germane-software/repositories/public/SVG/test/timeseries.rb
|
||||
#
|
||||
# = Notes
|
||||
#
|
||||
# The default stylesheet handles upto 10 data sets, if you
|
||||
# use more you must create your own stylesheet and add the
|
||||
# additional settings for the extra data sets. You will know
|
||||
# if you go over 10 data sets as they will have no style and
|
||||
# be in black.
|
||||
#
|
||||
# Unlike the other types of charts, data sets must contain x,y pairs:
|
||||
#
|
||||
# [ "12:30", 2 ] # A data set with 1 point: ("12:30",2)
|
||||
# [ "01:00",2, "14:20",6] # A data set with 2 points: ("01:00",2) and
|
||||
# # ("14:20",6)
|
||||
#
|
||||
# Note that multiple data sets within the same chart can differ in length,
|
||||
# and that the data in the datasets needn't be in order; they will be ordered
|
||||
# by the plot along the X-axis.
|
||||
#
|
||||
# The dates must be parseable by ParseDate, but otherwise can be
|
||||
# any order of magnitude (seconds within the hour, or years)
|
||||
#
|
||||
# = See also
|
||||
#
|
||||
# * SVG::Graph::Graph
|
||||
# * SVG::Graph::BarHorizontal
|
||||
# * SVG::Graph::Bar
|
||||
# * SVG::Graph::Line
|
||||
# * SVG::Graph::Pie
|
||||
# * SVG::Graph::Plot
|
||||
#
|
||||
# == Author
|
||||
#
|
||||
# Sean E. Russell <serATgermaneHYPHENsoftwareDOTcom>
|
||||
#
|
||||
# Copyright 2004 Sean E. Russell
|
||||
# This software is available under the Ruby license[LICENSE.txt]
|
||||
#
|
||||
class TimeSeries < Plot
|
||||
# In addition to the defaults set by Graph::initialize and
|
||||
# Plot::set_defaults, sets:
|
||||
# [x_label_format] '%Y-%m-%d %H:%M:%S'
|
||||
# [popup_format] '%Y-%m-%d %H:%M:%S'
|
||||
def set_defaults
|
||||
super
|
||||
init_with(
|
||||
#:max_time_span => '',
|
||||
:x_label_format => '%Y-%m-%d %H:%M:%S',
|
||||
:popup_format => '%Y-%m-%d %H:%M:%S'
|
||||
)
|
||||
end
|
||||
|
||||
# The format string use do format the X axis labels.
|
||||
# See Time::strformat
|
||||
attr_accessor :x_label_format
|
||||
# Use this to set the spacing between dates on the axis. The value
|
||||
# must be of the form
|
||||
# "\d+ ?(days|weeks|months|years|hours|minutes|seconds)?"
|
||||
#
|
||||
# EG:
|
||||
#
|
||||
# graph.timescale_divisions = "2 weeks"
|
||||
#
|
||||
# will cause the chart to try to divide the X axis up into segments of
|
||||
# two week periods.
|
||||
attr_accessor :timescale_divisions
|
||||
# The formatting used for the popups. See x_label_format
|
||||
attr_accessor :popup_format
|
||||
|
||||
# Add data to the plot.
|
||||
#
|
||||
# d1 = [ "12:30", 2 ] # A data set with 1 point: ("12:30",2)
|
||||
# d2 = [ "01:00",2, "14:20",6] # A data set with 2 points: ("01:00",2) and
|
||||
# # ("14:20",6)
|
||||
# graph.add_data(
|
||||
# :data => d1,
|
||||
# :title => 'One'
|
||||
# )
|
||||
# graph.add_data(
|
||||
# :data => d2,
|
||||
# :title => 'Two'
|
||||
# )
|
||||
#
|
||||
# Note that the data must be in time,value pairs, and that the date format
|
||||
# may be any date that is parseable by ParseDate.
|
||||
def add_data data
|
||||
@data = [] unless @data
|
||||
|
||||
raise "No data provided by #{@data.inspect}" unless data[:data] and
|
||||
data[:data].kind_of? Array
|
||||
raise "Data supplied must be x,y pairs! "+
|
||||
"The data provided contained an odd set of "+
|
||||
"data points" unless data[:data].length % 2 == 0
|
||||
return if data[:data].length == 0
|
||||
|
||||
|
||||
x = []
|
||||
y = []
|
||||
data[:data].each_index {|i|
|
||||
if i%2 == 0
|
||||
t = DateTime.parse( data[:data][i] ).to_time
|
||||
x << t.to_i
|
||||
else
|
||||
y << data[:data][i]
|
||||
end
|
||||
}
|
||||
sort( x, y )
|
||||
data[:data] = [x,y]
|
||||
@data << data
|
||||
end
|
||||
|
||||
|
||||
protected
|
||||
|
||||
def min_x_value=(value)
|
||||
@min_x_value = DateTime.parse( value ).to_time
|
||||
end
|
||||
|
||||
|
||||
def format x, y
|
||||
Time.at( x ).strftime( popup_format )
|
||||
end
|
||||
|
||||
def get_x_labels
|
||||
get_x_values.collect { |v| Time.at(v).strftime( x_label_format ) }
|
||||
end
|
||||
|
||||
private
|
||||
def get_x_values
|
||||
rv = []
|
||||
min, max, scale_division = x_range
|
||||
if timescale_divisions
|
||||
timescale_divisions =~ /(\d+) ?(day|week|month|year|hour|minute|second)?/
|
||||
division_units = $2 ? $2 : "day"
|
||||
amount = $1.to_i
|
||||
if amount
|
||||
step = nil
|
||||
case division_units
|
||||
when "month"
|
||||
cur = min
|
||||
while cur < max
|
||||
rv << cur
|
||||
arr = Time.at( cur ).to_a
|
||||
arr[4] += amount
|
||||
if arr[4] > 12
|
||||
arr[5] += (arr[4] / 12).to_i
|
||||
arr[4] = (arr[4] % 12)
|
||||
end
|
||||
cur = Time.local(*arr).to_i
|
||||
end
|
||||
when "year"
|
||||
cur = min
|
||||
while cur < max
|
||||
rv << cur
|
||||
arr = Time.at( cur ).to_a
|
||||
arr[5] += amount
|
||||
cur = Time.local(*arr).to_i
|
||||
end
|
||||
when "week"
|
||||
step = 7 * 24 * 60 * 60 * amount
|
||||
when "day"
|
||||
step = 24 * 60 * 60 * amount
|
||||
when "hour"
|
||||
step = 60 * 60 * amount
|
||||
when "minute"
|
||||
step = 60 * amount
|
||||
when "second"
|
||||
step = amount
|
||||
end
|
||||
min.step( max, step ) {|v| rv << v} if step
|
||||
|
||||
return rv
|
||||
end
|
||||
end
|
||||
min.step( max, scale_division ) {|v| rv << v}
|
||||
return rv
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in New Issue