Class: Plotrb::Legend

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

Overview

Legends visualize scales. Legends aid interpretation of scales with ranges such as colors, shapes and sizes. See https://github.com/trifacta/vega/wiki/Legends

Constant Summary collapse

LEGEND_PROPERTIES =

@return [Symbol] the name of the scale that determines an item's size

@!attribute values
 @return [Array] explicitly set the visible legend values
[:size, :shape, :fill, :stroke, :orient, :title, 
:format, :offset, :values, :properties]

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(&block) ⇒ Legend

Returns a new instance of Legend.


35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/plotrb/legends.rb', line 35

def initialize(&block)
	define_single_val_attributes(:size, :shape, :fill, :stroke, :orient, 
	:title, :format, :offset)			
	define_multi_val_attributes(:values)
	self.singleton_class.class_eval {
		alias_method :name, :title
		alias_method :offset_by, :offset
	}
	self.instance_eval(&block) if block_given?
	::Plotrb::Kernel.legends << self
	self
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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


57
58
59
60
61
62
63
64
65
66
# File 'lib/plotrb/legends.rb', line 57

def method_missing(method, *args, &block)
	case method.to_s
		when /^at_(left|right)$/ # set orient of the legend
			self.orient($1.to_sym, &block)
		when /^with_(\d+)_name/ # set the title of the legend
			self.title($1.to_s, &block)
		else
			super
	end
end

Instance Attribute Details

#fillSymbol

Returns the name of the scale that determines an item's fill color.

Returns:

  • (Symbol)

    the name of the scale that determines an item's fill color


30
31
# File 'lib/plotrb/legends.rb', line 30

LEGEND_PROPERTIES = [:size, :shape, :fill, :stroke, :orient, :title, 
:format, :offset, :values, :properties]

#formatString

Returns an optional formatting pattern for legend labels.

Returns:

  • (String)

    an optional formatting pattern for legend labels


30
31
# File 'lib/plotrb/legends.rb', line 30

LEGEND_PROPERTIES = [:size, :shape, :fill, :stroke, :orient, :title, 
:format, :offset, :values, :properties]

#offsetInteger

Returns the offset of the legend.

Returns:

  • (Integer)

    the offset of the legend


30
31
# File 'lib/plotrb/legends.rb', line 30

LEGEND_PROPERTIES = [:size, :shape, :fill, :stroke, :orient, :title, 
:format, :offset, :values, :properties]

#orientSymbol

Returns the orientation of the legend.

Returns:

  • (Symbol)

    the orientation of the legend


30
31
# File 'lib/plotrb/legends.rb', line 30

LEGEND_PROPERTIES = [:size, :shape, :fill, :stroke, :orient, :title, 
:format, :offset, :values, :properties]

#shapeSymbol

Returns the name of the scale that determines an item's shape.

Returns:

  • (Symbol)

    the name of the scale that determines an item's shape


30
31
# File 'lib/plotrb/legends.rb', line 30

LEGEND_PROPERTIES = [:size, :shape, :fill, :stroke, :orient, :title, 
:format, :offset, :values, :properties]

#sizeObject


# File 'lib/plotrb/legends.rb', line 10

#strokeSymbol

Returns the name of the scale that determines an item's stroke color.

Returns:

  • (Symbol)

    the name of the scale that determines an item's stroke color


30
31
# File 'lib/plotrb/legends.rb', line 30

LEGEND_PROPERTIES = [:size, :shape, :fill, :stroke, :orient, :title, 
:format, :offset, :values, :properties]

#titleSymbol

Returns the title for the legend.

Returns:

  • (Symbol)

    the title for the legend


30
31
# File 'lib/plotrb/legends.rb', line 30

LEGEND_PROPERTIES = [:size, :shape, :fill, :stroke, :orient, :title, 
:format, :offset, :values, :properties]

Instance Method Details

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


48
49
50
51
52
53
54
55
# File 'lib/plotrb/legends.rb', line 48

def properties(element=nil, &block)
	@properties ||= {}
	return @properties unless element
	@properties.merge!(
		element.to_sym => ::Plotrb::Mark::MarkProperty.new(:text, &block)
	)
	self
end