Class: DataPoint

Inherits:
Object
  • Object
show all
Defined in:
lib/SVG/Graph/DataPoint.rb

Overview

Allows to customize datapoint shapes

Constant Summary collapse

OVERLAY =

magic string that defines if a shape is intented to be overlayed to a default. this allowes to have strike through of a circle etc.

"OVERLAY"
DEFAULT_SHAPE =
lambda{|x,y,line| ["circle", {
    "cx" => x,
    "cy" => y,
    "r" => "2.5",
    "class" => "dataPoint#{line}"
  }]
}
CRITERIA =
[]

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(x, y, line) ⇒ DataPoint

creates a new DataPoint



49
50
51
52
53
# File 'lib/SVG/Graph/DataPoint.rb', line 49

def initialize(x, y, line)
  @x = x
  @y = y
  @line = line
end

Class Method Details

.configure_shape_criteria(*matchers) ⇒ Object

matchers are class scope. Once configured, each DataPoint instance will have access to the same matchers

Examples:

DataPoint.configure_shape_criteria(
  [/.*/, lambda{|x,y,line|
    [ 'polygon',
      {
      "points" => "#{x-1.5},#{y+2.5} #{x+1.5},#{y+2.5} #{x+1.5},#{y-2.5} #{x-1.5},#{y-2.5}",
      "class" => "dataPoint#{line}"
      }
    ]
  }]
)


36
37
38
# File 'lib/SVG/Graph/DataPoint.rb', line 36

def DataPoint.configure_shape_criteria(*matchers)
  CRITERIA.push(*matchers)
end

.reset_shape_criteriaObject



41
42
43
# File 'lib/SVG/Graph/DataPoint.rb', line 41

def DataPoint.reset_shape_criteria
  CRITERIA.clear
end

Instance Method Details

#shape(datapoint_description = nil) ⇒ Array<Array>

Returns different shapes depending on datapoint descriptions, if shape criteria have been configured. The definded criteria are evaluated in two stages, first the ones, which are note defined as overlay. then the "OVERLAY"

Examples:

Return value

# two dimensional array, the splatted (*) inner array can be used as argument to REXML::add_element
[["svgtag",  {"points" => "", "class"=> "dataPoint#{line}" } ], ["svgtag", {"points"=>"", "class"=> ""}], ...]


68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/SVG/Graph/DataPoint.rb', line 68

def shape(datapoint_description=nil)
  # select all criteria with size 2, and collect rendered lambdas in an array
  shapes = CRITERIA.select {|criteria|
    criteria.size == 2
  }.collect {|regexp, proc|
    proc.call(@x, @y, @line) if datapoint_description =~ regexp
  }.compact
  # if above did not render anything use the defalt shape
  shapes = [DEFAULT_SHAPE.call(@x, @y, @line)] if shapes.empty?

  overlays = CRITERIA.select { |criteria|
    criteria.last == OVERLAY
  }.collect { |regexp, proc|
    proc.call(@x, @y, @line) if datapoint_description =~ regexp
  }.compact

  return shapes + overlays
end