Class: Charty::Backends::Plotly
- Inherits:
-
Object
- Object
- Charty::Backends::Plotly
- Defined in:
- lib/charty/backends/plotly.rb
Defined Under Namespace
Modules: IRubyOutput Classes: HTML
Class Attribute Summary collapse
Instance Attribute Summary collapse
-
#context ⇒ Object
readonly
Returns the value of attribute context.
Instance Method Summary collapse
- #bar(bar_pos, group_names, values, colors, orient, label: nil, width:, align: :center, conf_int: nil, error_colors: nil, error_width: nil, cap_size: nil) ⇒ Object
- #begin_figure ⇒ Object
- #box_plot(plot_data, group_names, orient:, colors:, gray:, dodge:, width:, flier_size: 5, whisker: 1.5, notch: false) ⇒ Object
- #disable_xaxis_grid ⇒ Object
- #disable_yaxis_grid ⇒ Object
- #grouped_box_plot(plot_data, group_names, color_names, orient:, colors:, gray:, dodge:, width:, flier_size: 5, whisker: 1.5, notch: false) ⇒ Object
- #initilize ⇒ Object
- #invert_yaxis ⇒ Object
- #label(x, y) ⇒ Object
- #legend(loc:, title:) ⇒ Object
- #plot(plot, context) ⇒ Object
- #render(context, filename) ⇒ Object
- #save(filename, title: nil) ⇒ Object
- #scatter(x, y, variables, legend:, color:, color_mapper:, style:, style_mapper:, size:, size_mapper:) ⇒ Object
- #series=(series) ⇒ Object
- #set_xlabel(label) ⇒ Object
- #set_xlim(min, max) ⇒ Object
- #set_xtick_labels(labels) ⇒ Object
- #set_xticks(values) ⇒ Object
- #set_ylabel(label) ⇒ Object
- #set_ylim(min, max) ⇒ Object
- #set_ytick_labels(labels) ⇒ Object
- #set_yticks(values) ⇒ Object
- #show ⇒ Object
Class Attribute Details
.chart_id ⇒ Object
14 15 16 |
# File 'lib/charty/backends/plotly.rb', line 14 def chart_id @chart_id ||= 0 end |
.plotly_src ⇒ Object
24 25 26 |
# File 'lib/charty/backends/plotly.rb', line 24 def plotly_src @plotly_src ||= 'https://cdn.plot.ly/plotly-latest.min.js' end |
.with_api_load_tag ⇒ Object
18 19 20 21 22 |
# File 'lib/charty/backends/plotly.rb', line 18 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
#context ⇒ Object (readonly)
Returns the value of attribute context.
9 10 11 |
# File 'lib/charty/backends/plotly.rb', line 9 def context @context end |
Instance Method Details
#bar(bar_pos, group_names, values, colors, orient, label: nil, width:, align: :center, conf_int: nil, error_colors: nil, error_width: nil, cap_size: nil) ⇒ Object
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 |
# File 'lib/charty/backends/plotly.rb', line 124 def (, group_names, values, colors, orient, label: nil, width: 0.8r, align: :center, conf_int: nil, error_colors: nil, error_width: nil, cap_size: nil) = Array() values = Array(values) colors = Array(colors).map(&:to_hex_string) if orient == :v x, y = , values x = group_names unless group_names.nil? else x, y = values, y = group_names unless group_names.nil? end trace = { type: :bar, orientation: orient, x: x, y: y, width: width, marker: {color: colors} } trace[:name] = label unless label.nil? unless conf_int.nil? errors_low = conf_int.map.with_index {|(low, _), i| values[i] - low } errors_high = conf_int.map.with_index {|(_, high), i| high - values[i] } = { type: :data, visible: true, symmetric: false, array: errors_high, arrayminus: errors_low, color: error_colors[0].to_hex_string } [:thickness] = error_width unless error_width.nil? [:width] = cap_size unless cap_size.nil? = orient == :v ? :error_y : :error_x trace[] = end @traces << trace if group_names @layout[:barmode] = :group end end |
#begin_figure ⇒ Object
119 120 121 122 |
# File 'lib/charty/backends/plotly.rb', line 119 def begin_figure @traces = [] @layout = {showlegend: false} end |
#box_plot(plot_data, group_names, orient:, colors:, gray:, dodge:, width:, flier_size: 5, whisker: 1.5, notch: false) ⇒ Object
174 175 176 177 178 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 174 def box_plot(plot_data, group_names, orient:, colors:, gray:, dodge:, width: 0.8r, flier_size: 5, whisker: 1.5, notch: false) colors = Array(colors).map(&:to_hex_string) gray = gray.to_hex_string width = Float(width) flier_size = Float(width) whisker = Float(whisker) traces = plot_data.map.with_index do |group_data, i| group_data = Array(group_data) trace = { type: :box, orientation: orient, name: group_names[i], marker: {color: colors[i]} } if orient == :v trace.update(y: group_data) else trace.update(x: group_data) end trace end traces.reverse! if orient == :h @traces.concat(traces) end |
#disable_xaxis_grid ⇒ Object
415 416 417 |
# File 'lib/charty/backends/plotly.rb', line 415 def disable_xaxis_grid # do nothing end |
#disable_yaxis_grid ⇒ Object
419 420 421 |
# File 'lib/charty/backends/plotly.rb', line 419 def disable_yaxis_grid # do nothing end |
#grouped_box_plot(plot_data, group_names, color_names, orient:, colors:, gray:, dodge:, width:, flier_size: 5, whisker: 1.5, notch: false) ⇒ Object
205 206 207 208 209 210 211 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 247 248 |
# File 'lib/charty/backends/plotly.rb', line 205 def grouped_box_plot(plot_data, group_names, color_names, orient:, colors:, gray:, dodge:, width: 0.8r, flier_size: 5, whisker: 1.5, notch: false) colors = Array(colors).map(&:to_hex_string) gray = gray.to_hex_string width = Float(width) flier_size = Float(width) whisker = Float(whisker) @layout[:boxmode] = :group if orient == :h @layout[:xaxis] ||= {} @layout[:xaxis][:zeroline] = false plot_data = plot_data.map {|d| d.reverse } group_names = group_names.reverse end traces = color_names.map.with_index do |color_name, i| group_keys = group_names.flat_map.with_index { |name, j| Array.new(plot_data[i][j].length, name) }.flatten values = plot_data[i].flat_map {|d| Array(d) } trace = { type: :box, orientation: orient, name: color_name, marker: {color: colors[i]} } if orient == :v trace.update(y: values, x: group_keys) else trace.update(x: values, y: group_keys) end trace end @traces.concat(traces) end |
#initilize ⇒ Object
29 30 |
# File 'lib/charty/backends/plotly.rb', line 29 def initilize end |
#invert_yaxis ⇒ Object
423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 |
# File 'lib/charty/backends/plotly.rb', line 423 def invert_yaxis @traces.each do |trace| case trace[:type] when :bar trace[:y].reverse! end end if @layout[:boxmode] == :group @traces.reverse! end if @layout[:yaxis] && @layout[:yaxis][:ticktext] @layout[:yaxis][:ticktext].reverse! end end |
#label(x, y) ⇒ Object
32 33 |
# File 'lib/charty/backends/plotly.rb', line 32 def label(x, y) end |
#legend(loc:, title:) ⇒ Object
440 441 442 443 444 445 446 447 448 |
# File 'lib/charty/backends/plotly.rb', line 440 def legend(loc:, title:) @layout[:showlegend] = true @layout[:legend] = { title: { text: title } } # TODO: Handle loc end |
#plot(plot, context) ⇒ Object
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/charty/backends/plotly.rb', line 43 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
39 40 41 |
# File 'lib/charty/backends/plotly.rb', line 39 def render(context, filename) plot(nil, context) end |
#save(filename, title: nil) ⇒ Object
450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 |
# File 'lib/charty/backends/plotly.rb', line 450 def save(filename, title: nil) html = <<~HTML <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>%{title}</title> <script src="https://cdn.plot.ly/plotly-latest.min.js"></script> </head> <body> <div id="%{id}" style="width: 100%%; height:100%%;"></div> <script type="text/javascript"> Plotly.newPlot("%{id}", %{data}, %{layout}); </script> </body> </html> HTML html %= { title: title || default_html_title, id: SecureRandom.uuid, data: JSON.dump(@traces), layout: JSON.dump(@layout) } File.write(filename, html) nil end |
#scatter(x, y, variables, legend:, color:, color_mapper:, style:, style_mapper:, size:, size_mapper:) ⇒ Object
250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 |
# File 'lib/charty/backends/plotly.rb', line 250 def scatter(x, y, variables, legend:, color:, color_mapper:, style:, style_mapper:, size:, size_mapper:) if legend == :full warn("Plotly backend does not support full verbosity legend") end orig_x, orig_y = x, y x = case x when Charty::Vector x.to_a else Array.try_convert(x) end if x.nil? raise ArgumentError, "Invalid value for x: %p" % orig_x end y = case y when Charty::Vector y.to_a else Array.try_convert(y) end if y.nil? raise ArgumentError, "Invalid value for y: %p" % orig_y end unless color.nil? && style.nil? grouped_scatter(x, y, variables, legend: legend, color: color, color_mapper: color_mapper, style: style, style_mapper: style_mapper, size: size, size_mapper: size_mapper) return end trace = { type: :scatter, mode: :markers, x: x, y: y, marker: { line: { width: 1, color: "#fff" }, size: 10 } } unless size.nil? trace[:marker][:size] = size_mapper[size].map {|x| 6.0 + x * 6.0 } end @traces << trace end |
#series=(series) ⇒ Object
35 36 37 |
# File 'lib/charty/backends/plotly.rb', line 35 def series=(series) @series = series end |
#set_xlabel(label) ⇒ Object
371 372 373 374 |
# File 'lib/charty/backends/plotly.rb', line 371 def set_xlabel(label) @layout[:xaxis] ||= {} @layout[:xaxis][:title] = label end |
#set_xlim(min, max) ⇒ Object
405 406 407 408 |
# File 'lib/charty/backends/plotly.rb', line 405 def set_xlim(min, max) @layout[:xaxis] ||= {} @layout[:xaxis][:range] = [min, max] end |
#set_xtick_labels(labels) ⇒ Object
393 394 395 396 397 |
# File 'lib/charty/backends/plotly.rb', line 393 def set_xtick_labels(labels) @layout[:xaxis] ||= {} @layout[:xaxis][:tickmode] = "array" @layout[:xaxis][:ticktext] = labels end |
#set_xticks(values) ⇒ Object
381 382 383 384 385 |
# File 'lib/charty/backends/plotly.rb', line 381 def set_xticks(values) @layout[:xaxis] ||= {} @layout[:xaxis][:tickmode] = "array" @layout[:xaxis][:tickvals] = values end |
#set_ylabel(label) ⇒ Object
376 377 378 379 |
# File 'lib/charty/backends/plotly.rb', line 376 def set_ylabel(label) @layout[:yaxis] ||= {} @layout[:yaxis][:title] = label end |
#set_ylim(min, max) ⇒ Object
410 411 412 413 |
# File 'lib/charty/backends/plotly.rb', line 410 def set_ylim(min, max) @layout[:yaxis] ||= {} @layout[:yaxis][:range] = [min, max] end |
#set_ytick_labels(labels) ⇒ Object
399 400 401 402 403 |
# File 'lib/charty/backends/plotly.rb', line 399 def set_ytick_labels(labels) @layout[:yaxis] ||= {} @layout[:yaxis][:tickmode] = "array" @layout[:yaxis][:ticktext] = labels end |
#set_yticks(values) ⇒ Object
387 388 389 390 391 |
# File 'lib/charty/backends/plotly.rb', line 387 def set_yticks(values) @layout[:yaxis] ||= {} @layout[:yaxis][:tickmode] = "array" @layout[:yaxis][:tickvals] = values end |
#show ⇒ Object
481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 |
# File 'lib/charty/backends/plotly.rb', line 481 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 |