module Flammarion # A representation of a plot in a writeable area. See {Writeable#plot} class Plot attr_reader :engraving def initialize(*args) if args.size == 0 then @engraving = Engraving.new @id = @engraving.make_id @target = "default" elsif args.size == 3 then id, target, engraving = args @id = id @target = target @engraving = engraving else raise ArgumentError.new("ArgumentError: wrong number of arguments (#{args.size} for 0 or 3)") end end def plot(data, options = {}) if data.respond_to?(:keys) options = options.merge(data) if data.include?(:xy) then data = data.clone data[:x] = data[:xy].map(&:first) data[:y] = data[:xy].map(&:last) data.delete(:xy) end data = [data] elsif not data.first.respond_to?(:keys) data = [{y:data, x:(1..data.size).to_a}.merge(options)] end @engraving.send_json({action:'plot', id:@id, target:@target, data:data}.merge(options)) end def to_svg svg = nil @engraving.script(%|$('#plot-i9').find('svg')[0].outerHTML|, coffee:false) {|r| svg = r['result'] || "Error"} sleep(0.1) while svg.nil? return svg end def layout(options) @engraving.send_json({action:'plot', id:@id, target: @target, layout: options}) end end end