Class: GChart::Axis
- Inherits:
-
Object
- Object
- GChart::Axis
- Defined in:
- lib/gchart/axis.rb
Direct Known Subclasses
Constant Summary collapse
- AXIS_TYPES =
[ :top, :right, :bottom, :left ]
- TEXT_ALIGNMENT =
Defaults:
:left
forRightAxis
,:center
forTopAxis
and forBottomAxis
, and:right
forLeftAxis
. { :left => -1, :center => 0, :right => 1 }
Instance Attribute Summary collapse
-
#font_size ⇒ Object
Size of font in pixels.
-
#label_positions ⇒ Object
Array of float positions for
labels
. -
#labels ⇒ Object
Array of axis labels.
-
#range ⇒ Object
Takes a
Range
of float values. -
#range_markers ⇒ Object
Array of 2-element sub-arrays such that the 1st element in each sub-array is a
Range
of float values which describe the start and end points of the range marker, and the 2nd element in each sub-array is an rrggbb color for the range marker. -
#text_alignment ⇒ Object
TEXT_ALIGNMENT
property for axis labeling. -
#text_color ⇒ Object
An rrggbb color for axis text.
Class Method Summary collapse
-
.create(axis_type) {|axis| ... } ⇒ Object
Instantiates the proper
GChart::Axis
subclass based on theaxis_type
.
Instance Method Summary collapse
-
#axis_type_label ⇒ Object
Returns a one-character label of the axis according to its type.
-
#initialize ⇒ Axis
constructor
A new instance of Axis.
-
#range_marker_type_label ⇒ Object
Returns a one-character label to indicate whether
ranger_markers
are vertical or horizontal. -
#validate! ⇒ Object
Ensures that all combinations of attributes which have been set will work with each other.
Constructor Details
#initialize ⇒ Axis
Returns a new instance of Axis.
59 60 61 62 63 |
# File 'lib/gchart/axis.rb', line 59 def initialize @labels = [] @label_positions = [] @range_markers = [] end |
Instance Attribute Details
#font_size ⇒ Object
Size of font in pixels. To set font_size
, text_color
is also required.
23 24 25 |
# File 'lib/gchart/axis.rb', line 23 def font_size @font_size end |
#label_positions ⇒ Object
Array of float positions for labels
. Without labels
, the label_positions
are self-labeling.
10 11 12 |
# File 'lib/gchart/axis.rb', line 10 def label_positions @label_positions end |
#labels ⇒ Object
Array of axis labels. Can be exactly placed along the axis with label_positions
, otherwise are evenly spaced.
6 7 8 |
# File 'lib/gchart/axis.rb', line 6 def labels @labels end |
#range ⇒ Object
Takes a Range
of float values. With labels
, defines labels
context, meaning the labels
will be spaced at their proper location within the range
. Without labels
, smart-labeling occurs for range
.
16 17 18 |
# File 'lib/gchart/axis.rb', line 16 def range @range end |
#range_markers ⇒ Object
Array of 2-element sub-arrays such that the 1st element in each sub-array is a Range
of float values which describe the start and end points of the range marker, and the 2nd element in each sub-array is an rrggbb color for the range marker. For :top
and :bottom
AXIS_TYPES
, markers are vertical. For :right
and :left
AXIS_TYPES
, markers are horizontal.
36 37 38 |
# File 'lib/gchart/axis.rb', line 36 def range_markers @range_markers end |
#text_alignment ⇒ Object
TEXT_ALIGNMENT
property for axis labeling. To set text_alignment
, both text_color
and font_size
must also be set.
28 29 30 |
# File 'lib/gchart/axis.rb', line 28 def text_alignment @text_alignment end |
#text_color ⇒ Object
An rrggbb color for axis text.
19 20 21 |
# File 'lib/gchart/axis.rb', line 19 def text_color @text_color end |
Class Method Details
.create(axis_type) {|axis| ... } ⇒ Object
Instantiates the proper GChart::Axis
subclass based on the axis_type
.
47 48 49 50 51 52 53 54 |
# File 'lib/gchart/axis.rb', line 47 def create(axis_type, &block) raise ArgumentError.new("Invalid axis type '#{axis_type}'") unless AXIS_TYPES.include?(axis_type) axis = Object.module_eval("GChart::#{axis_type.to_s.capitalize}Axis").new yield(axis) if block_given? axis end |
Instance Method Details
#axis_type_label ⇒ Object
Returns a one-character label of the axis according to its type.
66 67 68 |
# File 'lib/gchart/axis.rb', line 66 def axis_type_label raise NotImplementedError.new("Method must be overridden in a subclass of this abstract base class.") end |
#range_marker_type_label ⇒ Object
Returns a one-character label to indicate whether ranger_markers
are vertical or horizontal.
72 73 74 |
# File 'lib/gchart/axis.rb', line 72 def range_marker_type_label raise NotImplementedError.new("Method must be overridden in a subclass of this abstract base class.") end |
#validate! ⇒ Object
Ensures that all combinations of attributes which have been set will work with each other. Raises ArgumentError
otherwise.
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 |
# File 'lib/gchart/axis.rb', line 78 def validate! if labels.size > 0 and label_positions.size > 0 and labels.size != label_positions.size raise ArgumentError.new( "Both labels and label_positions have been specified, but their " + "respective counts do not match (labels.size = '#{labels.size}' " + "and label_positions.size = '#{label_positions.size}')" ) end unless label_positions.all? { |pos| pos.is_a?(Numeric) } raise ArgumentError.new( "The label_positions attribute requires numeric values for each position specified" ) end if range unless range.is_a?(Range) raise ArgumentError.new("The range attribute has been specified with a non-Range class") end unless range.first.is_a?(Numeric) raise ArgumentError.new("The range attribute has been specified with non-numeric range values") end end if font_size and not text_color raise ArgumentError.new("To specify a font_size, a text_color must also be specified") end if text_alignment and not (text_color and font_size) raise ArgumentError.new( "To specify a text_alignment, both text_color and font_size must also be specified" ) end if text_color and not GChart.valid_color?(text_color) raise ArgumentError.new("The text_color attribute has been specified with an invalid color") end if font_size and not font_size.is_a?(Numeric) raise ArgumentError.new("The font_size must have a numeric value") end if text_alignment and not TEXT_ALIGNMENT[text_alignment] raise ArgumentError.new( "The text_alignment attribute has been specified with a non-TEXT_ALIGNMENT" ) end if not range_markers.all? { |array| array.is_a?(Array) and array.size == 2 and array[0].is_a?(Range) and array[0].first.is_a?(Numeric) and GChart.valid_color?(array[1]) } raise ArgumentError.new( "The range_markers attribute must be an array of 2-element sub-arrays such that " + "the first element in each sub-array is a Range of numeric values and the second " + "element in each sub-array is a valid color" ) end end |