Class: Vis::Graph3d

Inherits:
Object
  • Object
show all
Includes:
Native, Utilities
Defined in:
lib/vis/graph3d.rb

Instance Method Summary collapse

Methods included from Utilities

#hash_array_to_native, included, #lower_camelize, #lower_camelize_hash, #native_to_hash_array

Constructor Details

#initialize(native_container, dataset, options = {}) ⇒ Graph3d

Returns a new instance of Graph3d.



13
14
15
16
17
18
# File 'lib/vis/graph3d.rb', line 13

def initialize(native_container, dataset, options = {})
  native_options = options_to_native(options)
  native_data = dataset.to_n
  @event_handlers = {}
  @native = `new vis.Graph3d(native_container, native_data, native_options)`
end

Instance Method Details

#get_camera_positionObject



47
48
49
50
# File 'lib/vis/graph3d.rb', line 47

def get_camera_position
  res = @native.JS.getCameraPosition()
  `Opal.Hash.$new(res)`
end

#off(event, event_handler_id) ⇒ Object



20
21
22
23
24
25
# File 'lib/vis/graph3d.rb', line 20

def off(event, event_handler_id)
  event = lower_camelize(event)
  handler = @event_handlers[event][event_handler_id]
  `self["native"].off(event, handler)`
  @event_handlers[event].delete(event_handler_id)
end

#on(event, &block) ⇒ Object



27
28
29
30
31
32
33
34
35
# File 'lib/vis/graph3d.rb', line 27

def on(event, &block)
  event = lower_camelize(event)
  @event_handlers[event] = {} unless @event_handlers[event]
  event_handler_id = `Math.random().toString(36).substring(6)`
  handler = `function(event_info) { block.$call(Opal.Hash.$new(event_info)); }`
  @event_handlers[event][event_handler_id] = handler
  `self["native"].on(event, handler)`
  event_handler_id
end

#options_to_native(options) ⇒ Object



57
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/vis/graph3d.rb', line 57

def options_to_native(options)
  return unless options
  # options must be duplicated, so callbacks dont get wrapped twice
  new_opts = {}.merge!(options)

  if new_opts.has_key?(:onclick)
    block = new_opts[:onclick]
    if `typeof block === "function"`
      unless new_opts[:onclick].JS[:hyper_wrapped]
        new_opts[:onclick] = %x{
          function(point) {
            return #{block.call(`Opal.Hash.$new(point)`)};
          }
        }
        new_opts[:onclick].JS[:hyper_wrapped] = true
      end
    end
  end

  if new_opts.has_key?(:tooltip)
    block = new_opts[:tooltip]
    if `typeof block === "function"`
      unless new_opts[:tooltip].JS[:hyper_wrapped]
        new_opts[:tooltip] = %x{
          function(item) {
            return #{block.call(`Opal.Hash.$new(item)`)};
          }
        }
        new_opts[:tooltip].JS[:hyper_wrapped] = true
      end
    end
  end

  if new_opts.has_key?(:x_value_label)
    block = new_opts[:x_value_label]
    if `typeof block === "function"`
      unless new_opts[:x_value_label].JS[:hyper_wrapped]
        new_opts[:x_value_label] = %x{
          function(value) {
            return #{block.call(value)};
          }
        }
        new_opts[:x_value_label].JS[:hyper_wrapped] = true
      end
    end
  end

  if new_opts.has_key?(:y_value_label)
    block = new_opts[:y_value_label]
    if `typeof block === "function"`
      unless new_opts[:y_value_label].JS[:hyper_wrapped]
        new_opts[:y_value_label] = %x{
          function(value) {
            return #{block.call(value)};
          }
        }
        new_opts[:y_value_label].JS[:hyper_wrapped] = true
      end
    end
  end

  if new_opts.has_key?(:z_value_label)
    block = new_opts[:z_value_label]
    if `typeof block === "function"`
      unless new_opts[:z_value_label].JS[:hyper_wrapped]
        new_opts[:z_value_label] = %x{
          function(value) {
            return #{block.call(value)};
          }
        }
        new_opts[:z_value_label].JS[:hyper_wrapped] = true
      end
    end
  end

  lower_camelize_hash(new_opts).to_n
end

#set_camera_position(dis_hor_ver) ⇒ Object



52
53
54
55
# File 'lib/vis/graph3d.rb', line 52

def set_camera_position(dis_hor_ver)
  res = @native.JS.setCameraPosition(dis_hor_ver.to_n)
  `Opal.Hash.$new(res)`
end

#set_data(dataset) ⇒ Object



37
38
39
40
# File 'lib/vis/graph3d.rb', line 37

def set_data(dataset)
  native_data = dataset.to_n
  @native.JS.setData(native_data)
end

#set_options(options) ⇒ Object



42
43
44
45
# File 'lib/vis/graph3d.rb', line 42

def set_options(options)
  native_options = options_to_native(options)
  @native.JS.setOptions(native_options)
end