Class: Plotrb::Axis

Inherits:
Object show all
Includes:
Base
Defined in:
lib/plotrb/axes.rb

Overview

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

represents a data range.

See https://github.com/trifacta/vega/wiki/Axes

Constant Summary collapse

TYPES =
%i(x y)

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Base

#add_attributes, #attributes, #classify, #collect_attributes, #define_boolean_attribute, #define_boolean_attributes, #define_multi_val_attribute, #define_multi_val_attributes, #define_single_val_attribute, #define_single_val_attributes, #defined_attributes, included, #set_attributes

Constructor Details

#initialize(type, &block) ⇒ Axis

Returns a new instance of Axis.


58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# 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

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object


123
124
125
126
127
128
129
130
131
132
133
134
135
136
# 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

Instance Attribute Details

#formatString

Returns the formatting pattern for axis labels.

Returns:

  • (String)

    the formatting pattern for axis labels


53
54
55
56
# File 'lib/plotrb/axes.rb', line 53

add_attributes :type, :scale, :orient, :format, :ticks, :values, :subdivide,
:tick_padding, :tick_size, :tick_size_major, :tick_size_minor,
:tick_size_end, :offset, :layer, :properties, :title,
:title_offset, :grid

#gridBoolean

Returns whether gridlines should be created.

Returns:

  • (Boolean)

    whether gridlines should be created


53
54
55
56
# File 'lib/plotrb/axes.rb', line 53

add_attributes :type, :scale, :orient, :format, :ticks, :values, :subdivide,
:tick_padding, :tick_size, :tick_size_major, :tick_size_minor,
:tick_size_end, :offset, :layer, :properties, :title,
:title_offset, :grid

#offsetInteger

Returns the offset by which to displace the axis from the edge of the enclosing group or data rectangle.

Returns:

  • (Integer)

    the offset by which to displace the axis from the edge of the enclosing group or data rectangle


53
54
55
56
# File 'lib/plotrb/axes.rb', line 53

add_attributes :type, :scale, :orient, :format, :ticks, :values, :subdivide,
:tick_padding, :tick_size, :tick_size_major, :tick_size_minor,
:tick_size_end, :offset, :layer, :properties, :title,
:title_offset, :grid

#orientSymbol

Returns the orientation of the axis.

Returns:

  • (Symbol)

    the orientation of the axis


53
54
55
56
# File 'lib/plotrb/axes.rb', line 53

add_attributes :type, :scale, :orient, :format, :ticks, :values, :subdivide,
:tick_padding, :tick_size, :tick_size_major, :tick_size_minor,
:tick_size_end, :offset, :layer, :properties, :title,
:title_offset, :grid

#properties(element = nil, &block) ⇒ Hash

Returns optional mark property definitions for custom styling.

Returns:

  • (Hash)

    optional mark property definitions for custom styling


53
54
55
56
# File 'lib/plotrb/axes.rb', line 53

add_attributes :type, :scale, :orient, :format, :ticks, :values, :subdivide,
:tick_padding, :tick_size, :tick_size_major, :tick_size_minor,
:tick_size_end, :offset, :layer, :properties, :title,
:title_offset, :grid

#scaleString

Returns the name of the scale backing the axis.

Returns:

  • (String)

    the name of the scale backing the axis


53
54
55
56
# File 'lib/plotrb/axes.rb', line 53

add_attributes :type, :scale, :orient, :format, :ticks, :values, :subdivide,
:tick_padding, :tick_size, :tick_size_major, :tick_size_minor,
:tick_size_end, :offset, :layer, :properties, :title,
:title_offset, :grid

#subdivideInteger

Returns the number of minor ticks between major ticks.

Returns:

  • (Integer)

    the number of minor ticks between major ticks


53
54
55
56
# File 'lib/plotrb/axes.rb', line 53

add_attributes :type, :scale, :orient, :format, :ticks, :values, :subdivide,
:tick_padding, :tick_size, :tick_size_major, :tick_size_minor,
:tick_size_end, :offset, :layer, :properties, :title,
:title_offset, :grid

#tick_paddingInteger

Returns the padding between ticks and text labels.

Returns:

  • (Integer)

    the padding between ticks and text labels


53
54
55
56
# File 'lib/plotrb/axes.rb', line 53

add_attributes :type, :scale, :orient, :format, :ticks, :values, :subdivide,
:tick_padding, :tick_size, :tick_size_major, :tick_size_minor,
:tick_size_end, :offset, :layer, :properties, :title,
:title_offset, :grid

#tick_sizeInteger

Returns the size of major, minor, and end ticks.

Returns:

  • (Integer)

    the size of major, minor, and end ticks


53
54
55
56
# File 'lib/plotrb/axes.rb', line 53

add_attributes :type, :scale, :orient, :format, :ticks, :values, :subdivide,
:tick_padding, :tick_size, :tick_size_major, :tick_size_minor,
:tick_size_end, :offset, :layer, :properties, :title,
:title_offset, :grid

#tick_size_endInteger

Returns the size of end ticks.

Returns:

  • (Integer)

    the size of end ticks


53
54
55
56
# File 'lib/plotrb/axes.rb', line 53

add_attributes :type, :scale, :orient, :format, :ticks, :values, :subdivide,
:tick_padding, :tick_size, :tick_size_major, :tick_size_minor,
:tick_size_end, :offset, :layer, :properties, :title,
:title_offset, :grid

#tick_size_majorInteger

Returns the size of major ticks.

Returns:

  • (Integer)

    the size of major ticks


53
54
55
56
# File 'lib/plotrb/axes.rb', line 53

add_attributes :type, :scale, :orient, :format, :ticks, :values, :subdivide,
:tick_padding, :tick_size, :tick_size_major, :tick_size_minor,
:tick_size_end, :offset, :layer, :properties, :title,
:title_offset, :grid

#tick_size_minorInteger

Returns the size of minor ticks.

Returns:

  • (Integer)

    the size of minor ticks


53
54
55
56
# File 'lib/plotrb/axes.rb', line 53

add_attributes :type, :scale, :orient, :format, :ticks, :values, :subdivide,
:tick_padding, :tick_size, :tick_size_major, :tick_size_minor,
:tick_size_end, :offset, :layer, :properties, :title,
:title_offset, :grid

#ticksInteger

Returns a desired number of ticks.

Returns:

  • (Integer)

    a desired number of ticks


53
54
55
56
# File 'lib/plotrb/axes.rb', line 53

add_attributes :type, :scale, :orient, :format, :ticks, :values, :subdivide,
:tick_padding, :tick_size, :tick_size_major, :tick_size_minor,
:tick_size_end, :offset, :layer, :properties, :title,
:title_offset, :grid

#titleString

Returns the title for the axis.

Returns:

  • (String)

    the title for the axis


53
54
55
56
# File 'lib/plotrb/axes.rb', line 53

add_attributes :type, :scale, :orient, :format, :ticks, :values, :subdivide,
:tick_padding, :tick_size, :tick_size_major, :tick_size_minor,
:tick_size_end, :offset, :layer, :properties, :title,
:title_offset, :grid

#tittle_offsetInteger

Returns the offset from the axis at which to place the title.

Returns:

  • (Integer)

    the offset from the axis at which to place the title


53
54
55
56
# File 'lib/plotrb/axes.rb', line 53

add_attributes :type, :scale, :orient, :format, :ticks, :values, :subdivide,
:tick_padding, :tick_size, :tick_size_major, :tick_size_minor,
:tick_size_end, :offset, :layer, :properties, :title,
:title_offset, :grid

#typeSymbol

Returns type of the axis, either :x or :y.

Returns:

  • (Symbol)

    type of the axis, either :x or :y


53
54
55
56
# File 'lib/plotrb/axes.rb', line 53

add_attributes :type, :scale, :orient, :format, :ticks, :values, :subdivide,
:tick_padding, :tick_size, :tick_size_major, :tick_size_minor,
:tick_size_end, :offset, :layer, :properties, :title,
:title_offset, :grid

#valuesArray

Returns explicitly set the visible axis tick values.

Returns:

  • (Array)

    explicitly set the visible axis tick values


53
54
55
56
# File 'lib/plotrb/axes.rb', line 53

add_attributes :type, :scale, :orient, :format, :ticks, :values, :subdivide,
:tick_padding, :tick_size, :tick_size_major, :tick_size_minor,
:tick_size_end, :offset, :layer, :properties, :title,
:title_offset, :grid

Instance Method Details

#above(&block) ⇒ Object


88
89
90
91
92
# File 'lib/plotrb/axes.rb', line 88

def above(&block)
  @layer = :front
  self.instance_eval(&block) if block_given?
  self
end

#above?Boolean

Returns:

  • (Boolean)

94
95
96
# File 'lib/plotrb/axes.rb', line 94

def above?
  @layer == :front
end

#below(&block) ⇒ Object


98
99
100
101
102
# File 'lib/plotrb/axes.rb', line 98

def below(&block)
  @layer = :back
  self.instance_eval(&block) if block_given?
  self
end

#below?Boolean

Returns:

  • (Boolean)

104
105
106
# File 'lib/plotrb/axes.rb', line 104

def below?
  @layer == :back
end

#no_grid(&block) ⇒ Object


108
109
110
111
112
# File 'lib/plotrb/axes.rb', line 108

def no_grid(&block)
  @grid = false
  self.instance_eval(&block) if block
  self
end