class Axlsx::Axes

The Axes class creates and manages axis information and serialization for charts.

Public Class Methods

new(options={}) click to toggle source

@param [Hash] options options used to generate axis each key should be an axis name like :val_axis and its value should be the class of the axis type to construct. The :cat_axis, if there is one, must come first (we assume a Ruby 1.9+ Hash or an OrderedHash).

# File lib/axlsx/drawing/axes.rb, line 11
def initialize(options={})
  raise(ArgumentError, "CatAxis must come first") if options.keys.include?(:cat_axis) && options.keys.first != :cat_axis
  options.each do |name, axis_class|
    add_axis(name, axis_class)
  end
end

Public Instance Methods

[](name) click to toggle source

provides assiciative access to a specic axis store in an axes

instance. @return [Axis]

# File lib/axlsx/drawing/axes.rb, line 21
def [](name)
  axes.assoc(name)[1]
end
add_axis(name, axis_class) click to toggle source

Adds an axis to the collection @param [Symbol] name The name of the axis @param [Axis] axis_class The axis class to generate

# File lib/axlsx/drawing/axes.rb, line 44
def add_axis(name, axis_class)
  axis = axis_class.new
  set_cross_axis(axis)
  axes << [name, axis]
end
to_xml_string(str = '', options = {}) click to toggle source

Serializes the object @param [String] str @param [Hash] options @option options ids If the ids option is specified only the axis identifier is serialized. Otherwise, each axis is serialized in full.

# File lib/axlsx/drawing/axes.rb, line 31
def to_xml_string(str = '', options = {})
  if options[:ids]
    # CatAxis must come first in the XML (for Microsoft Excel at least)
    sorted = axes.sort_by { |name, axis| axis.kind_of?(CatAxis) ? 0 : 1 }
    sorted.each { |axis| str << ('<c:axId val="' << axis[1].id.to_s << '"/>') }        
  else
    axes.each { |axis| axis[1].to_xml_string(str) }
  end
end

Private Instance Methods

axes() click to toggle source
# File lib/axlsx/drawing/axes.rb, line 52
def axes
  @axes ||= []
end
set_cross_axis(axis) click to toggle source
# File lib/axlsx/drawing/axes.rb, line 56
def set_cross_axis(axis)
  axes.first[1].cross_axis = axis if axes.size == 1
  axis.cross_axis = axes.first[1] unless axes.empty?
end