class Plotrb::Axis

Axes provide axis lines, ticks, and labels to convey how a spatial range

represents a data range.

See {github.com/trifacta/vega/wiki/Axes}

Constants

TYPES

Public Class Methods

new(type, &block) click to toggle source
# File lib/plotrb/axes.rb, line 58
def initialize(type, &block)
  @type = type
  define_single_val_attributes(:scale, :orient, :title, :title_offset,
                               :format, :ticks, :subdivide, :tick_padding,
                               :tick_size, :tick_size_major, :tick_size_end,
                               :tick_size_minor, :offset, :layer)
  define_boolean_attribute(:grid)
  define_multi_val_attributes(:values)
  self.singleton_class.class_eval {
    alias_method :from, :scale
    alias_method :offset_title_by, :title_offset
    alias_method :subdivide_by, :subdivide
    alias_method :major_tick_size, :tick_size_major
    alias_method :minor_tick_size, :tick_size_minor
    alias_method :end_tick_size, :tick_size_end
    alias_method :offset_by, :offset
    alias_method :show_grid, :grid
    alias_method :with_grid, :grid
    alias_method :show_grid?, :grid?
    alias_method :with_grid?, :grid?
  }
  self.instance_eval(&block) if block_given?
  ::Plotrb::Kernel.axes << self
  self
end

Public Instance Methods

above(&block) click to toggle source
# File lib/plotrb/axes.rb, line 88
def above(&block)
  @layer = :front
  self.instance_eval(&block) if block_given?
  self
end
above?() click to toggle source
# File lib/plotrb/axes.rb, line 94
def above?
  @layer == :front
end
below(&block) click to toggle source
# File lib/plotrb/axes.rb, line 98
def below(&block)
  @layer = :back
  self.instance_eval(&block) if block_given?
  self
end
below?() click to toggle source
# File lib/plotrb/axes.rb, line 104
def below?
  @layer == :back
end
method_missing(method, *args, &block) click to toggle source
Calls superclass method Plotrb::Kernel#method_missing
# File lib/plotrb/axes.rb, line 123
def method_missing(method, *args, &block)
  case method.to_s
    when /^with_(\d+)_ticks$/  # set number of ticks. eg. in_20_ticks
      self.ticks($1.to_i, &block)
    when /^subdivide_by_(\d+)$/ # set subdivide of ticks
      self.subdivide($1.to_i, &block)
    when /^at_(top|bottom|left|right)$/ # set orient of the axis
      self.orient($1.to_sym, &block)
    when /^in_(front|back)$/ # set layer of the axis
      self.layer($1.to_sym, &block)
    else
      super
  end
end
no_grid(&block) click to toggle source
# File lib/plotrb/axes.rb, line 108
def no_grid(&block)
  @grid = false
  self.instance_eval(&block) if block
  self
end
properties(element=nil, &block) click to toggle source
# File lib/plotrb/axes.rb, line 114
def properties(element=nil, &block)
  @properties ||= {}
  return @properties unless element
  @properties.merge!(
      element.to_sym => ::Plotrb::Mark::MarkProperty.new(:text, &block)
  )
  self
end
type() click to toggle source
# File lib/plotrb/axes.rb, line 84
def type
  @type
end

Private Instance Methods

attribute_post_processing() click to toggle source
# File lib/plotrb/axes.rb, line 140
def attribute_post_processing
  process_type
  process_scale
  process_orient
  process_format
  process_layer
  process_properties
end
process_format() click to toggle source
# File lib/plotrb/axes.rb, line 176
def process_format
  return unless @format
  # D3's format specifier has general form:
  # [​[fill]align][sign][symbol][0][width][,][.precision][type]
  # the regex is taken from d3/src/format/format.js
  re =
    /(?:([^{])?([<>=^]))?([+\- ])?([$#])?(0)?(\d+)?(,)?(\.-?\d+)?([a-z%])?/i
  @format = @format.to_s
  if @format =~ re
    if "#{$1}#{$2}#{$3}#{$4}#{$5}#{$6}#{$7}#{$8}#{$9}" != @format
      raise ArgumentError, 'Invalid format specifier'
    end
  end
end
process_layer() click to toggle source
# File lib/plotrb/axes.rb, line 191
def process_layer
  return unless @layer
  unless %i(front back).include?(@layer.to_sym)
    raise ArgumentError, 'Invalid Axis layer'
  end
end
process_orient() click to toggle source
# File lib/plotrb/axes.rb, line 169
def process_orient
  return unless @orient
  unless %i(top bottom left right).include?(@orient.to_sym)
    raise ArgumentError, 'Invalid Axis orient'
  end
end
process_properties() click to toggle source
# File lib/plotrb/axes.rb, line 198
def process_properties
  return unless @properties
  valid_elements = %i(ticks major_ticks minor_ticks grid labels axis)
  unless (@properties.keys - valid_elements).empty?
    raise ArgumentError, 'Invalid property element'
  end
end
process_scale() click to toggle source
# File lib/plotrb/axes.rb, line 155
def process_scale
  return unless @scale
  case @scale
    when String
      unless ::Plotrb::Kernel.find_scale(@scale)
        raise ArgumentError, 'Scale not found'
      end
    when ::Plotrb::Scale
      @scale = @scale.name
    else
      raise ArgumentError, 'Unknown Scale'
  end
end
process_type() click to toggle source
# File lib/plotrb/axes.rb, line 149
def process_type
  unless TYPES.include?(@type)
    raise ArgumentError, 'Invalid Axis type'
  end
end