Class: Flammarion::Plot
- Inherits:
-
Object
- Object
- Flammarion::Plot
- Defined in:
- lib/flammarion/plot.rb
Overview
A representation of a plot in an engraving
Instance Attribute Summary collapse
-
#engraving ⇒ Object
readonly
Returns the value of attribute engraving.
Instance Method Summary collapse
-
#initialize(*args) ⇒ Plot
constructor
Creates a plot with it's own engraving.
-
#layout(options) ⇒ Object
Changes the layout of an already existing plot.
-
#plot(data, options = {}) ⇒ Plot
Plots data to this current plot.
-
#save(options = {}, &block) ⇒ Object
Saves the plot as a static image.
-
#to_png(options = {}) ⇒ Object
Converts the plot to a png image.
-
#to_svg(options = {}) ⇒ Object
Converts the plot to an svg image.
Constructor Details
#initialize ⇒ Plot #initialize(i, t, e) ⇒ Plot
Creates a plot with it's own engraving.
15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/flammarion/plot.rb', line 15 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 |
Instance Attribute Details
#engraving ⇒ Object (readonly)
Returns the value of attribute engraving
5 6 7 |
# File 'lib/flammarion/plot.rb', line 5 def engraving @engraving end |
Instance Method Details
#layout(options) ⇒ Object
Changes the layout of an already existing plot.
69 70 71 |
# File 'lib/flammarion/plot.rb', line 69 def layout() @engraving.send_json({action:'plot', id:@id, target: @target, layout: }) end |
#plot(array, options = {}) ⇒ Plot #plot(dataset, options = {}) ⇒ Plot #plot(datasets, options = {}) ⇒ Plot
Plots data to this current plot. If it has previously been plotted, this
plot will be overwritten. If you want to add a new plot to an engraving,
then use Flammarion::Writeable.plot
.
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/flammarion/plot.rb', line 51 def plot(data, = {}) if data.respond_to?(:keys) = .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()] end @engraving.send_json({action:'plot', id:@id, target:@target, data:data}.merge()) end |
#save(options = {}, &block) ⇒ Object
Saves the plot as a static image. block
will be called with a
hash argurment when the plot is finished being converted to an image
75 76 77 78 79 |
# File 'lib/flammarion/plot.rb', line 75 def save( = {}, &block) id = @engraving.make_id @engraving.callbacks[id] = block @engraving.send_json({action:'savePlot', id:@id, target:@target, callback_id: id, format: }) end |
#to_png(options = {}) ⇒ Object
Converts the plot to a png image. If a block is given, it will be called with the png data. Otherwise this function will wait until the image has been created, and then return a string containing the png data
84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/flammarion/plot.rb', line 84 def to_png( = {}) png = nil save(.merge({format: 'png'})) do |data| d = data['data'] png = Base64.decode64(d[d.index(',') + 1..-1]) if block_given? yield png end end unless block_given? sleep 0.1 while png.nil? return png end end |
#to_svg(options = {}) ⇒ Object
Converts the plot to an svg image. If a block is given, it will be called with the svg xml string. Otherwise this function will wait until the image has been created, and then return a string containing the svg xml string
102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/flammarion/plot.rb', line 102 def to_svg( = {}) svg = nil save(.merge({format: 'svg'})) do |data| d = data['data'] svg = URI.unescape(d[d.index(',') + 1 .. -1]) if block_given? yield svg end end unless block_given? sleep 0.1 while svg.nil? return svg end end |