Class: Nyaplot::Plot

Inherits:
Object
  • Object
show all
Includes:
ChartMethods, Exportable
Defined in:
lib/nyaplot/plot.rb

Constant Summary

Constants included from Exportable

Exportable::PATH_NYAPLOT

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ChartMethods

#add_annotation, #add_arrow, #add_circle, #add_rect, #add_text

Methods included from Exportable

#export_html, #generate_html, #raise_display_failed, #show, #to_iruby, #to_png

Constructor Details

#initialize(df = nil, **opts) ⇒ Plot

Returns a new instance of Plot.



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
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
# File 'lib/nyaplot/plot.rb', line 22

def initialize(df=nil, **opts)
  @df = df
  @opts = {
    width: 400,
    height: 400
  }.merge(opts)

  wh = {
    width: @opts[:width],
    height: @opts[:height]
  }
  
  @xdomain = nil
  @ydomain = nil
  
  ## internal use
  @glyphs = []
  @charts = []
  @deps = []
  @temp_deps = []

  ## properties
  @x_axis_h = 35
  @y_axis_w = 70
  @with_layer
  @is_interactive = true
  
  ## layers
  @stage = ad ::Layers::Stage.new({})
  @context = ad ::Layers::Context.new(wh)
  @background = ad ::Layers::Background.new(
    {
      width: wh[:width]+4,
      height: wh[:height]+4,
      dx: -2,
      dy: -2
    })
  
  @xscale = ad ::Layers::Scale.new({type: nil, range: [0, @opts[:width]]})
  @yscale = ad ::Layers::Scale.new({type: nil, range: [@opts[:height], 0]})

  @xaxis = ad ::Layers::Axis.new({scale: @xscale, height: @x_axis_h})
  @yaxis = ad ::Layers::Axis.new({scale: @yscale, orient: :left, width: @y_axis_w})

  @grid = ad ::Layers::Grid.new({xscale: @xscale, yscale: @yscale})
  @position = ad ::Layers::Position2d.new({x: @xscale, y: @yscale})
  @wheel_zoom = ad ::Layers::Wheelzoom.new({xscale: @xscale, yscale: @yscale, updates: [@grid, @xaxis, @yaxis]}.merge(wh))

  arg = {
    dx: (@y_axis_w/2),
    text: "",
    font_size: 26,
    text_anchor: :start,
    yalign: :center,
    xalign: :center,
    margin: {top: 0, bottom: 20, left: 5, right: 5}
  }
  @title = ad ::Layers::Label.new(arg)
  @xlabel = nil
  @ylabel = nil
end

Instance Attribute Details

#backgroundObject

Returns the value of attribute background.



14
15
16
# File 'lib/nyaplot/plot.rb', line 14

def background
  @background
end

#gridObject

Returns the value of attribute grid.



14
15
16
# File 'lib/nyaplot/plot.rb', line 14

def grid
  @grid
end

#xaxisObject

Returns the value of attribute xaxis.



14
15
16
# File 'lib/nyaplot/plot.rb', line 14

def xaxis
  @xaxis
end

#yaxisObject

Returns the value of attribute yaxis.



14
15
16
# File 'lib/nyaplot/plot.rb', line 14

def yaxis
  @yaxis
end

Class Method Details

.from(df, **opts) ⇒ Object



17
18
19
# File 'lib/nyaplot/plot.rb', line 17

def from(df, **opts)
  Plot.new(df, **opts)
end

Instance Method Details

#add(chart_type, xarr = nil, yarr = nil, **opts) ⇒ Object



103
104
105
106
# File 'lib/nyaplot/plot.rb', line 103

def add(chart_type, xarr=nil, yarr=nil, **opts)
  self.send(chart_type, @opts.merge(opts))
  self
end

#add_dependency(layer) ⇒ Object Also known as: ad



84
85
86
87
88
# File 'lib/nyaplot/plot.rb', line 84

def add_dependency(layer)
  raise "Layer should be an instance of LayerBase" unless layer.is_a? ::Layers::LayerBase
  @deps.push(layer)
  layer
end

#add_temp_dependency(layer) ⇒ Object Also known as: atd



92
93
94
95
# File 'lib/nyaplot/plot.rb', line 92

def add_temp_dependency(layer)
  @temp_deps.push(layer)
  layer
end

#add_with_df(df, chart_type, xlabel, ylabel) ⇒ Object

deprecated.



109
110
111
112
# File 'lib/nyaplot/plot.rb', line 109

def add_with_df(df, chart_type, xlabel, ylabel)
  # TODO
  self
end

#clear_temp_dependencyObject



99
100
101
# File 'lib/nyaplot/plot.rb', line 99

def clear_temp_dependency
  @temp_deps = []
end

#column(r, l, opts = {}) ⇒ Object



170
171
172
173
174
175
# File 'lib/nyaplot/plot.rb', line 170

def column(r, l, opts={})
  r = r.to_node if r.is_a? ::Layers::LayerBase
  l = l.to_node if l.is_a? ::Layers::LayerBase
  cl = atd ::Layers::Column.new(opts)
  cl.to_node([r, l])
end

#decide_domain(arrs, scale_type) ⇒ Object



194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
# File 'lib/nyaplot/plot.rb', line 194

def decide_domain(arrs, scale_type)
  if scale_type.nil?
    scale_type = (arrs.all?{|arr| arr.length==2 && arr.all?{|v| v.is_a?(Numeric)}}) ? "linear" : "ordinal"
  end

  [scale_type, (case scale_type.to_s
   when "time" then
     [] # TODO
   when "ordinal"then
     arrs.flatten.uniq
   when "linear", "pow", "log" then
     [
       arrs.map{|arr| arr[0]}.min,
       arrs.map{|arr| arr[1]}.max
     ]
   end)]
end

#decide_xdomain(scale_type) ⇒ Object



184
185
186
187
# File 'lib/nyaplot/plot.rb', line 184

def decide_xdomain(scale_type)
  arrs = @charts.map{|c| c.xdomain}
  decide_domain(arrs, scale_type)
end

#decide_ydomain(scale_type) ⇒ Object



189
190
191
192
# File 'lib/nyaplot/plot.rb', line 189

def decide_ydomain(scale_type)
  arrs = @charts.map{|c| c.ydomain}
  decide_domain(arrs, scale_type)
end

#row(t, b, opts = {}) ⇒ Object



177
178
179
180
181
182
# File 'lib/nyaplot/plot.rb', line 177

def row(t, b, opts={})
  t = t.to_node if t.is_a? ::Layers::LayerBase
  b = b.to_node if b.is_a? ::Layers::LayerBase
  rw = atd ::Layers::Row.new(opts)
  rw.to_node([t, b])
end

#stack(me, children) ⇒ Object

s methods for #to_json ##



165
166
167
168
# File 'lib/nyaplot/plot.rb', line 165

def stack(me, children)
  children = children.map{|c| c.is_a?(::Layers::LayerBase) ? c.to_node([]) : c}
  me.to_node(children)
end

#title(txt) ⇒ Object



114
115
116
117
# File 'lib/nyaplot/plot.rb', line 114

def title(txt)
  @title.text(txt)
  self
end

#to_json(*args) ⇒ Object



212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
# File 'lib/nyaplot/plot.rb', line 212

def to_json(*args)
  ## decide domain
  xscale_, xdomain = @xdomain.nil? ? decide_xdomain(@xscale.type) : [@xscale.type, @xdomain]
  yscale_, ydomain = @ydomain.nil? ? decide_ydomain(@yscale.type) : [@yscale.type, @ydomain]
  @xscale.type xscale_
  @xscale.domain xdomain
  @yscale.type yscale_
  @yscale.domain ydomain
  
  ## create layout tree
  c = stack(@context, @glyphs)
  c = stack(@grid, [c])
  c = stack(@background, [c])
  # c = stack(@tooltip, [c])
  if xscale_ != "ordinal" && yscale_ != "ordinal"
    @wheel_zoom.updates(@wheel_zoom.updates + @glyphs)
    c = stack(@wheel_zoom, [c]) 
  end
  c = column(@yaxis, row(c, @xaxis), {margin: {top: 10, bottom: 15, left: 15, right: 15}})
  c = row(c, @xlabel) unless @xlabel.nil?
  c = column(@ylabel, c) unless @ylabel.nil?
  #c = column(c, @legend)
  c = row(@title, c) unless @title.nil?
  c = stack(@stage, [c])
  c[:parser_type] = "svg"

  defs = @deps + @temp_deps + @glyphs
  clear_temp_dependency
  
  {
    uuid: SecureRandom.uuid,
    defs: defs,
    layout: c
  }.to_json
end

#xdomain(arr) ⇒ Object



153
154
155
156
# File 'lib/nyaplot/plot.rb', line 153

def xdomain(arr)
  @xdomain = arr
  self
end

#xlabel(txt) ⇒ Object



119
120
121
122
123
124
125
126
127
128
# File 'lib/nyaplot/plot.rb', line 119

def xlabel(txt)
  arg = {
    dx: @y_axis_w/2,
    text: txt,
    xalign: :center,
    dominant_baseline: "text-before-edge"
  }
  @xlabel = ad ::Layers::Label.new(arg)
  self
end

#xscale(type) ⇒ Object



141
142
143
144
145
# File 'lib/nyaplot/plot.rb', line 141

def xscale(type)
  raise "Not supported." unless [:time, :linear, :log, :pow, :ordinal].index type
  @xscale.type type
  self
end

#ydomain(arr) ⇒ Object



158
159
160
161
# File 'lib/nyaplot/plot.rb', line 158

def ydomain(arr)
  @ydomain = arr
  self
end

#ylabel(txt) ⇒ Object



130
131
132
133
134
135
136
137
138
139
# File 'lib/nyaplot/plot.rb', line 130

def ylabel(txt)
  arg = {
    text: txt,
    rotate: -90,
    xalign: :center,
    yalign: :center
  }
  @ylabel = ad ::Layers::Label.new(arg)
  self
end

#yscale(type) ⇒ Object



147
148
149
150
151
# File 'lib/nyaplot/plot.rb', line 147

def yscale(type)
  raise "Not supported." unless [:time, :linear, :log, :pow, :ordinal].index type
  @yscale.type type
  self
end