Class: GChart::Axis

Inherits:
Object
  • Object
show all
Defined in:
lib/gchart/axis.rb

Direct Known Subclasses

HorizontalAxis, VerticalAxis

Constant Summary collapse

AXIS_TYPES =
[ :top, :right, :bottom, :left ]
TEXT_ALIGNMENT =

Defaults: :left for RightAxis, :center for TopAxis and for BottomAxis, and :right for LeftAxis.

{ :left => -1, :center => 0, :right => 1 }

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeAxis

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_sizeObject

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_positionsObject

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

#labelsObject

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

#rangeObject

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_markersObject

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_alignmentObject

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_colorObject

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.

Yields:

  • (axis)

Raises:

  • (ArgumentError)


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_labelObject

Returns a one-character label of the axis according to its type.

Raises:

  • (NotImplementedError)


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_labelObject

Returns a one-character label to indicate whether ranger_markers are vertical or horizontal.

Raises:

  • (NotImplementedError)


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