Class: Charty::Backends::Plotly

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

Defined Under Namespace

Modules: IRubyOutput Classes: HTML

Class Attribute Summary collapse

Instance Attribute Summary collapse

Instance Method Summary collapse

Class Attribute Details

.chart_idObject



13
14
15
# File 'lib/charty/backends/plotly.rb', line 13

def chart_id
  @chart_id ||= 0
end

.plotly_srcObject



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

def plotly_src
  @plotly_src ||= 'https://cdn.plot.ly/plotly-latest.min.js'
end

.with_api_load_tagObject



17
18
19
20
21
# File 'lib/charty/backends/plotly.rb', line 17

def with_api_load_tag
  return @with_api_load_tag unless @with_api_load_tag.nil?

  @with_api_load_tag = true
end

Instance Attribute Details

#contextObject (readonly)

Returns the value of attribute context.



8
9
10
# File 'lib/charty/backends/plotly.rb', line 8

def context
  @context
end

Instance Method Details

#bar(bar_pos, values, color: nil, width:, align: :center, orient: :v) ⇒ Object



123
124
125
126
127
128
129
130
131
132
# File 'lib/charty/backends/plotly.rb', line 123

def bar(bar_pos, values, color: nil, width: 0.8r, align: :center, orient: :v)
  color = Array(color).map(&:to_hex_string)
  @traces << {
    type: :bar,
    x: bar_pos,
    y: values,
    marker: {color: color}
  }
  @layout[:showlegend] = false
end

#begin_figureObject



118
119
120
121
# File 'lib/charty/backends/plotly.rb', line 118

def begin_figure
  @traces = []
  @layout = {}
end

#box_plot(plot_data, positions, color:, gray:, width:, flier_size: 5, whisker: 1.5, notch: false) ⇒ Object



134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/charty/backends/plotly.rb', line 134

def box_plot(plot_data, positions, color:, gray:,
             width: 0.8r, flier_size: 5, whisker: 1.5, notch: false)
  color = Array(color).map(&:to_hex_string)
  plot_data.each_with_index do |group_data, i|
    data = if group_data.empty?
             {type: :box, y: [] }
           else
             {type: :box, y: group_data, marker: {color: color[i]}}
           end
    @traces << data
  end
  @layout[:showlegend] = false
end

#disable_xaxis_gridObject



175
176
177
# File 'lib/charty/backends/plotly.rb', line 175

def disable_xaxis_grid
  # do nothing
end

#initilizeObject



28
29
# File 'lib/charty/backends/plotly.rb', line 28

def initilize
end

#label(x, y) ⇒ Object



31
32
# File 'lib/charty/backends/plotly.rb', line 31

def label(x, y)
end

#plot(plot, context) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/charty/backends/plotly.rb', line 42

def plot(plot, context)
  context = context
  self.class.chart_id += 1

  case context.method
  when :bar
    render_graph(context, :bar)
  when :curve
    render_graph(context, :scatter)
  when :scatter
    render_graph(context, nil, options: {data: {mode: "markers"}})
  else
    raise NotImplementedError
  end
end

#render(context, filename) ⇒ Object



38
39
40
# File 'lib/charty/backends/plotly.rb', line 38

def render(context, filename)
  plot(nil, context)
end

#series=(series) ⇒ Object



34
35
36
# File 'lib/charty/backends/plotly.rb', line 34

def series=(series)
  @series = series
end

#set_xlabel(label) ⇒ Object



148
149
150
151
# File 'lib/charty/backends/plotly.rb', line 148

def set_xlabel(label)
  @layout[:xaxis] ||= {}
  @layout[:xaxis][:title] = label
end

#set_xlim(min, max) ⇒ Object



170
171
172
173
# File 'lib/charty/backends/plotly.rb', line 170

def set_xlim(min, max)
  @layout[:xaxis] ||= {}
  @layout[:xaxis][:range] = [min, max]
end

#set_xtick_labels(labels) ⇒ Object



164
165
166
167
168
# File 'lib/charty/backends/plotly.rb', line 164

def set_xtick_labels(labels)
  @layout[:xaxis] ||= {}
  @layout[:xaxis][:tickmode] = "array"
  @layout[:xaxis][:ticktext] = labels
end

#set_xticks(values) ⇒ Object



158
159
160
161
162
# File 'lib/charty/backends/plotly.rb', line 158

def set_xticks(values)
  @layout[:xaxis] ||= {}
  @layout[:xaxis][:tickmode] = "array"
  @layout[:xaxis][:tickvals] = values
end

#set_ylabel(label) ⇒ Object



153
154
155
156
# File 'lib/charty/backends/plotly.rb', line 153

def set_ylabel(label)
  @layout[:yaxis] ||= {}
  @layout[:yaxis][:title] = label
end

#showObject



179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
# File 'lib/charty/backends/plotly.rb', line 179

def show
  unless defined?(IRuby)
    raise NotImplementedError,
          "Plotly backend outside of IRuby is not supported"
  end

  IRubyOutput.prepare

  html = <<~HTML
    <div id="%{id}" style="width: 100%%; height:100%%;"></div>
    <script type="text/javascript">
      requirejs(["plotly"], function (Plotly) {
        Plotly.newPlot("%{id}", %{data}, %{layout});
      });
    </script>
  HTML

  html %= {
    id: SecureRandom.uuid,
    data: JSON.dump(@traces),
    layout: JSON.dump(@layout)
  }
  IRuby.display(html, mime: "text/html")
  nil
end