Class: Charty::Plotlyjs

Inherits:
PlotterAdapter
  • Object
show all
Defined in:
lib/charty/backends/plotlyjs.rb

Constant Summary collapse

Name =
"plotlyjs"

Instance Method Summary collapse

Constructor Details

#initializePlotlyjs

Returns a new instance of Plotlyjs.



15
16
17
18
# File 'lib/charty/backends/plotlyjs.rb', line 15

def initialize
  # do nothing
  @plot = nil
end

Instance Method Details

#data(context) ⇒ Object



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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/charty/backends/plotlyjs.rb', line 32

def data(context)
  # minus_color = '#cccccc'
  # plus_color = '#66ff66'
  minus_color = '#999999'
  plus_color = '#00d42f'

  # scatter
  # bar
  # heatmap
  # bubble
  # area (scatter, tonexty)
  # barh (bar, orientation: 'h')
  # waterfall
  # pie
  # sunburst
  case context.method
  when :bar
    context.series.map do |data|
      y_minus = data.ys.select{|i| i < 0}
      x_minus = data.xs[0..(y_minus.size - 1)]
      y_plus = data.ys.select{|i| i >= 0}
      x_plus = data.xs[y_minus.size..-1]

      minus = {
        x: x_minus,
        y: y_minus,
        type: "#{context.method}",
        marker: {
          color: "#{minus_color}",
        }
      }
      plus = {
        x: x_plus,
        y: y_plus,
        type: "#{context.method}",
        marker: {
          color: "#{plus_color}",
        }
      }
      [minus, plus]
    end
  when :barh
    context.series.map do |data|
      x_plus = data.xs.select{|i| i >= 0}
      y_plus = data.ys[0..(x_plus.size - 1)]
      x_minus = data.xs.select{|i| i < 0}
      y_minus = data.ys[x_plus.size..-1]

      minus = {
        x: x_minus,
        y: y_minus,
        type: 'bar',
        orientation: 'h',
        marker: {
          color: "#{minus_color}",
        }
      }
      plus = {
        x: x_plus,
        y: y_plus,
        type: 'bar',
        orientation: 'h',
        marker: {
          color: "#{plus_color}",
        }
      }
      [plus, minus]
    end
  end
end

#label(x, y) ⇒ Object



20
21
# File 'lib/charty/backends/plotlyjs.rb', line 20

def label(x, y)
end

#series=(series) ⇒ Object



23
24
25
# File 'lib/charty/backends/plotlyjs.rb', line 23

def series=(series)
  @series = series
end

#to_json(context) ⇒ Object



27
28
29
30
# File 'lib/charty/backends/plotlyjs.rb', line 27

def to_json(context)
  require 'json'
  data(context)[0].to_json
end