Class: Scruffy::Layers::Base
- Inherits:
-
Object
- Object
- Scruffy::Layers::Base
- Defined in:
- lib/scruffy/layers/base.rb
Overview
Scruffy::Layers::Base
- Author
-
Brasten Sager
- Extended By
-
A.J. Ostman
- Created
-
August 5th, 2006
- Last Modified
-
August 27, 2006
Scruffy::Layers::Base contains the basic functionality needed by the various types of graphs. The Base class is responsible holding layer information such as the title and data points.
When the graph is rendered, the graph renderer calls Base#render. Base#render sets up some standard information, and calculates the x,y coordinates of each data point. The draw() method, which should have been overridden by the current instance, is then called. The actual rendering of the graph takes place there.
Create New Graph Types
Assuming the information generated by Scruffy::Layers::Base is sufficient, you can create a new graph type simply by overriding the draw() method. See Base#draw for arguments.
Direct Known Subclasses
AllSmiles, Area, Average, Bar, Line, Pie, PieSlice, Scatter, SparklineBar, Stacked
Instance Attribute Summary collapse
-
#color ⇒ Object
readonly
Returns the value of attribute color.
-
#complexity ⇒ Object
readonly
Returns the value of attribute complexity.
-
#height ⇒ Object
readonly
The following attributes are set during the layer’s render process, and act more as a record of what just happened for later processes.
-
#max_value ⇒ Object
readonly
Returns the value of attribute max_value.
-
#min_value ⇒ Object
readonly
Returns the value of attribute min_value.
-
#opacity ⇒ Object
readonly
Returns the value of attribute opacity.
-
#options ⇒ Object
On-the-fly values for easy customization / acts as attributes.
-
#outline ⇒ Object
readonly
Returns the value of attribute outline.
-
#points ⇒ Object
Returns the value of attribute points.
-
#preferred_color ⇒ Object
Returns the value of attribute preferred_color.
-
#preferred_outline ⇒ Object
Returns the value of attribute preferred_outline.
-
#relevant_data ⇒ Object
Returns the value of attribute relevant_data.
-
#title ⇒ Object
The following attributes are user-definable at any time.
-
#width ⇒ Object
readonly
The following attributes are set during the layer’s render process, and act more as a record of what just happened for later processes.
Instance Method Summary collapse
-
#bottom_key ⇒ Object
The highest data point on this layer, or nil if relevant_data == false.
-
#bottom_value ⇒ Object
The lowest data point on this layer, or nil if relevant_data == false.
-
#draw(svg, coords, options = {}) ⇒ Object
The method called by Base#draw to render the graph.
-
#initialize(options = {}) ⇒ Base
constructor
Returns a new Base object.
-
#legend_data ⇒ Object
Returns a hash with information to be used by the legend.
-
#relevant_data? ⇒ Boolean
Returns the value of relevant_data.
-
#render(svg, options) ⇒ Object
Builds SVG code for this graph using the provided Builder object.
-
#sum_values ⇒ Object
The sum of all values.
-
#top_key ⇒ Object
The lowest data point on this layer, or nil if relevant_data == false.
-
#top_value ⇒ Object
The highest data point on this layer, or nil if relevant_data == false.
Constructor Details
#initialize(options = {}) ⇒ Base
Returns a new Base object.
Any options other that those specified below are stored in the @options variable for possible later use. This would be a good place to store options needed for a custom graph.
Options:
- title
-
Name/title of data group
- points
-
Array of data points
- preferred_color
-
Color used to render this graph, overrides theme color.
- preferred_outline
-
Color used to render this graph outline, overrides theme outline.
- relevant_data
-
Rarely used - indicates the data on this graph should not included in any graph data aggregations, such as averaging data points.
- style
-
SVG polyline style. (default: ‘fill-opacity: 0; stroke-opacity: 0.35’)
- stroke_width
-
numeric value for width of line (0.1 - 10, default: 1)
- relativestroke
-
stroke-width relative to image size? true or false (default)
- shadow
-
Display line shadow? true or false (default)
- dots
-
Display co-ord dots? true or false (default)
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/scruffy/layers/base.rb', line 60 def initialize( = {}) @title = .delete(:title) || '' @preferred_color = .delete(:color) @preferred_outline = .delete(:outline) @relevant_data = .delete(:relevant_data) || true @points = .delete(:points) || [] @points.extend Scruffy::Helpers::PointContainer unless @points.kind_of? Scruffy::Helpers::PointContainer [:stroke_width] ||= 1 [:dots] ||= false [:shadow] ||= false [:style] ||= false [:relativestroke] ||= false @options = end |
Instance Attribute Details
#color ⇒ Object (readonly)
Returns the value of attribute color.
37 38 39 |
# File 'lib/scruffy/layers/base.rb', line 37 def color @color end |
#complexity ⇒ Object (readonly)
Returns the value of attribute complexity.
40 41 42 |
# File 'lib/scruffy/layers/base.rb', line 40 def complexity @complexity end |
#height ⇒ Object (readonly)
The following attributes are set during the layer’s render process, and act more as a record of what just happened for later processes. height, width, min_value, max_value, color, opacity, complexity
35 36 37 |
# File 'lib/scruffy/layers/base.rb', line 35 def height @height end |
#max_value ⇒ Object (readonly)
Returns the value of attribute max_value.
36 37 38 |
# File 'lib/scruffy/layers/base.rb', line 36 def max_value @max_value end |
#min_value ⇒ Object (readonly)
Returns the value of attribute min_value.
36 37 38 |
# File 'lib/scruffy/layers/base.rb', line 36 def min_value @min_value end |
#opacity ⇒ Object (readonly)
Returns the value of attribute opacity.
39 40 41 |
# File 'lib/scruffy/layers/base.rb', line 39 def opacity @opacity end |
#options ⇒ Object
On-the-fly values for easy customization / acts as attributes.
30 31 32 |
# File 'lib/scruffy/layers/base.rb', line 30 def @options end |
#outline ⇒ Object (readonly)
Returns the value of attribute outline.
38 39 40 |
# File 'lib/scruffy/layers/base.rb', line 38 def outline @outline end |
#points ⇒ Object
Returns the value of attribute points.
26 27 28 |
# File 'lib/scruffy/layers/base.rb', line 26 def points @points end |
#preferred_color ⇒ Object
Returns the value of attribute preferred_color.
28 29 30 |
# File 'lib/scruffy/layers/base.rb', line 28 def preferred_color @preferred_color end |
#preferred_outline ⇒ Object
Returns the value of attribute preferred_outline.
29 30 31 |
# File 'lib/scruffy/layers/base.rb', line 29 def preferred_outline @preferred_outline end |
#relevant_data ⇒ Object
Returns the value of attribute relevant_data.
27 28 29 |
# File 'lib/scruffy/layers/base.rb', line 27 def relevant_data @relevant_data end |
#title ⇒ Object
The following attributes are user-definable at any time. title, points, relevant_data, preferred_color, options
25 26 27 |
# File 'lib/scruffy/layers/base.rb', line 25 def title @title end |
#width ⇒ Object (readonly)
The following attributes are set during the layer’s render process, and act more as a record of what just happened for later processes. height, width, min_value, max_value, color, opacity, complexity
35 36 37 |
# File 'lib/scruffy/layers/base.rb', line 35 def width @width end |
Instance Method Details
#bottom_key ⇒ Object
The highest data point on this layer, or nil if relevant_data == false
133 134 135 |
# File 'lib/scruffy/layers/base.rb', line 133 def bottom_key @relevant_data ? points.minimum_key : nil end |
#bottom_value ⇒ Object
The lowest data point on this layer, or nil if relevant_data == false
128 129 130 |
# File 'lib/scruffy/layers/base.rb', line 128 def bottom_value @relevant_data ? points.minimum_value : nil end |
#draw(svg, coords, options = {}) ⇒ Object
The method called by Base#draw to render the graph.
- svg
-
a Builder object to use for creating SVG code.
- coords
-
An array of coordinates relating to the graph’s data points. ie: [[100, 120], [200, 140], [300, 40]]
- options
-
Optional arguments.
95 96 97 |
# File 'lib/scruffy/layers/base.rb', line 95 def draw(svg, coords, ={}) raise RenderError, "You must override the Base#draw method." end |
#legend_data ⇒ Object
Returns a hash with information to be used by the legend.
Alternatively, returns nil if you don’t want this layer to be in the legend, or an array of hashes if this layer should have multiple legend entries (stacked?)
By default, #legend_data returns nil automatically if relevant_data is set to false or the @color attribute is nil. @color is set when the layer is rendered, so legends must be rendered AFTER layers.
107 108 109 110 111 112 113 114 115 |
# File 'lib/scruffy/layers/base.rb', line 107 def legend_data if relevant_data? && @color {:title => title, :color => @color, :priority => :normal} else nil end end |
#relevant_data? ⇒ Boolean
Returns the value of relevant_data
118 119 120 |
# File 'lib/scruffy/layers/base.rb', line 118 def relevant_data? @relevant_data end |
#render(svg, options) ⇒ Object
Builds SVG code for this graph using the provided Builder object. This method actually generates data needed by this graph, then passes the rendering responsibilities to Base#draw.
- svg
-
a Builder object used to create SVG code.
83 84 85 86 87 88 |
# File 'lib/scruffy/layers/base.rb', line 83 def render(svg, ) setup_variables() coords = generate_coordinates() draw(svg, coords, ) end |
#sum_values ⇒ Object
The sum of all values
143 144 145 |
# File 'lib/scruffy/layers/base.rb', line 143 def sum_values points.sum end |
#top_key ⇒ Object
The lowest data point on this layer, or nil if relevant_data == false
138 139 140 |
# File 'lib/scruffy/layers/base.rb', line 138 def top_key @relevant_data ? points.maximum_key : nil end |
#top_value ⇒ Object
The highest data point on this layer, or nil if relevant_data == false
123 124 125 |
# File 'lib/scruffy/layers/base.rb', line 123 def top_value @relevant_data ? points.maximum_value : nil end |