Class: MyChart::XY

Inherits:
Object
  • Object
show all
Defined in:
lib/my_chart/xy.rb

Instance Method Summary collapse

Constructor Details

#initialize(objs_hash) ⇒ XY

Returns a new instance of XY.



5
6
7
# File 'lib/my_chart/xy.rb', line 5

def initialize objs_hash
  @objs_hash = objs_hash
end

Instance Method Details

#==(obj) ⇒ Object



29
30
31
# File 'lib/my_chart/xy.rb', line 29

def == obj
  obj.kind_of? XY and value == obj.value
end

#complete_keys(range = nil) ⇒ Object



33
34
35
36
37
38
39
40
# File 'lib/my_chart/xy.rb', line 33

def complete_keys range=nil
  return self unless range
  new_hash = value.dup
  range.each do |key|
    new_hash[key] = [] unless new_hash[key]
  end
  XY.new new_hash
end

#datasetsObject



25
26
27
# File 'lib/my_chart/xy.rb', line 25

def datasets
  [{label: 'xy', data: value.values.map{|objs| objs.count}}]
end

#group_by(&blk) ⇒ Object



13
14
15
# File 'lib/my_chart/xy.rb', line 13

def group_by &blk
  XYZ.new Hash[value.map{ |group_name, objs| [group_name, objs.group_by(&blk)] }]
end

#labelsObject



21
22
23
# File 'lib/my_chart/xy.rb', line 21

def labels
  value.keys
end

#limit(config) ⇒ Object



57
58
59
60
61
62
# File 'lib/my_chart/xy.rb', line 57

def limit config
  limit_method, num = config.first ? [:first, config.first] : [:last, config.last]
  limited = value.to_a.send limit_method, num
  new_hash = Hash[limited]
  XY.new new_hash
end

#select(&blk) ⇒ Object



9
10
11
# File 'lib/my_chart/xy.rb', line 9

def select &blk
  XY.new value.select(&blk)
end

#sort(order) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/my_chart/xy.rb', line 42

def sort order
  compare = if :key === order.asc
              -> a, b { a[0] <=> b[0] }
       elsif :key === order.desc
              -> a, b { b[0] <=> a[0] }
       elsif :count === order.asc
              -> a, b { a[1].size <=> b[1].size }
       else
              -> a, b { b[1].size <=> a[1].size }
       end
  sorted = value.to_a.sort &compare
  new_hash = Hash[sorted]
  XY.new new_hash
end

#valueObject



17
18
19
# File 'lib/my_chart/xy.rb', line 17

def value
  @objs_hash
end