Class: Flammarion::Plot

Inherits:
Object
  • Object
show all
Defined in:
lib/flammarion/plot.rb

Overview

A representation of a plot in an engraving

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializePlot #initialize(i, t, e) ⇒ Plot

Creates a plot with it’s own engraving.

Overloads:

  • #initializePlot

    Examples:

    p = Flammarion::Plot.new
    p.plot([1,2,3,4])
    p.plot([5,6,7])


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

#engravingObject (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(options)
  @engraving.send_json({action:'plot', id:@id, target: @target, layout: options})
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.

Overloads:

  • #plot(array, options = {}) ⇒ Plot

    Examples:

    f.plot([1,3,4,2])
    f.plot(100.times.map{rand}, mode: 'markers')

    Parameters:

    • values (Array<Number>)

      A list of numbers to plot

  • #plot(dataset, options = {}) ⇒ Plot

    Examples:

    f.plot(x: (1..314).to_a.map{|x| Math.sin(x.to_f / 20.0)}, y:(1..314).to_a.map{|x| Math.sin(x.to_f / 10)}, replace:true)
    f.plot(x: [Time.now, Time.now + 24*60*60].map(&:to_s), y: [55, 38], type:'bar', replace:true)

    Parameters:

    • A (Hash)

      hash representing a Plotly trace

  • #plot(datasets, options = {}) ⇒ Plot

    Examples:

    f.plot(5.times.map{|t| {x: 100.times.to_a, y: 100.times.map{rand * t}, name: "Trace #{t}"}}, xaxis: {title: "A non-random number"}, yaxis: {title: "A random number"})

    Parameters:

    • An (Array<Hash>)

      array of Plotly traces

Returns:

  • (Plot)

    A Plot object for manipulation after creation.

See Also:



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, 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

#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(options = {}, &block)
  id = @engraving.make_id
  @engraving.callbacks[id] = block
  @engraving.send_json({action:'savePlot', id:@id, target:@target, callback_id: id, format: options})
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(options = {})
  png = nil
  save(options.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(options = {})
  svg = nil
  save(options.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