class Charty::Backends::PlotlyHelpers::PlotlyRenderer
Public Instance Methods
render(figure)
click to toggle source
# File lib/charty/backends/plotly_helpers/plotly_renderer.rb, line 9 def render(figure) json = JSON.generate(figure, allow_nan: true) case json when /\b(?:Infinity|NaN)\b/ visit(figure) else JSON.load(json) end end
Private Instance Methods
type_error(obj)
click to toggle source
# File lib/charty/backends/plotly_helpers/plotly_renderer.rb, line 115 def type_error(obj) raise TypeError, "Unable to convert to JSON: %p" % obj end
visit(obj)
click to toggle source
# File lib/charty/backends/plotly_helpers/plotly_renderer.rb, line 19 def visit(obj) case obj when Integer, String, Symbol, true, false, nil obj when Numeric visit_float(obj) when Time visit_time(obj) when Date visit_date(obj) when DateTime visit_datetime(obj) when Array visit_array(obj) when Hash visit_hash(obj) when ->(x) { defined?(Numo::NArray) && obj.is_a?(Numo::NArray) } visit_array(obj.to_a) when ->(x) { defined?(NMatrix) && obj.is_a?(NMatrix) } visit_array(obj.to_a) when ->(x) { defined?(Numpy::NDArray) && obj.is_a?(Numpy::NDArray) } visit_array(obj.to_a) when ->(x) { defined?(PyCall::List) && obj.is_a?(PyCall::List) } visit_array(obj.to_a) when ->(x) { defined?(PyCall::Tuple) && obj.is_a?(PyCall::Tuple) } visit_array(obj.to_a) when ->(x) { defined?(PyCall::Dict) && obj.is_a?(PyCall::Dict) } visit_hash(obj.to_h) when ->(x) { defined?(Pandas::Series) && obj.is_a?(Pandas::Series) } visit_array(obj.to_a) else str = String.try_convert(obj) return str unless str.nil? ary = Array.try_convert(obj) return visit_array(ary) unless ary.nil? hsh = Hash.try_convert(obj) return visit_hash(hsh) unless hsh.nil? type_error(obj) end end
visit_array(obj)
click to toggle source
# File lib/charty/backends/plotly_helpers/plotly_renderer.rb, line 102 def visit_array(obj) obj.map {|x| visit(x) } end
visit_date(obj)
click to toggle source
# File lib/charty/backends/plotly_helpers/plotly_renderer.rb, line 94 def visit_date(obj) obj.iso8601(6) end
visit_datetime(obj)
click to toggle source
# File lib/charty/backends/plotly_helpers/plotly_renderer.rb, line 98 def visit_datetime(obj) obj.iso8601(6) end
visit_float(obj)
click to toggle source
# File lib/charty/backends/plotly_helpers/plotly_renderer.rb, line 77 def visit_float(obj) obj = obj.to_f rescue RangeError type_error(obj) else case when obj.finite? obj else nil end end
visit_hash(obj)
click to toggle source
# File lib/charty/backends/plotly_helpers/plotly_renderer.rb, line 106 def visit_hash(obj) obj.map { |key, value| [ key, visit(value) ] }.to_h end
visit_time(obj)
click to toggle source
# File lib/charty/backends/plotly_helpers/plotly_renderer.rb, line 90 def visit_time(obj) obj.iso8601(6) end