Sha256: 98b085b3ea8daca08e139102036f746722f87049e414549e62bc17ba2ae7180a

Contents?: true

Size: 1.08 KB

Versions: 1

Compression:

Stored size: 1.08 KB

Contents

begin
  require 'nyaplot'
rescue LoadError => e
  puts "#{e}"
end

module Daru
  module Plotting
    module Vector

      # Plots a Vector with Nyaplot on IRuby using the given options. Yields the
      # plot object (Nyaplot::Plot) and the diagram object (Nyaplot::Diagram) 
      # to the block, which can be used for setting various options as per the 
      # Nyaplot API.
      # 
      # == Options
      #   type (:scatter, :bar, :histogram), title, x_label, y_label, color(true/false)
      # 
      # == Usage
      #   vector = Daru::Vector.new [10,20,30,40], [:one, :two, :three, :four]
      #   vector.plot(type: :bar) do |plot|
      #     plot.title "My first plot"
      #     plot.width 1200
      #   end
      def plot opts={}, &block
        options = {
          type: :scatter
        }.merge(opts)

        x_axis = options[:type] == :scatter ? Array.new(@size) { |i| i } : @index.to_a
        plot   = Nyaplot::Plot.new
        diagram = plot.add( options[:type], x_axis, @data.to_a )

        yield plot, diagram if block_given?
        
        plot.show
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
daru-0.0.5 lib/daru/plotting/vector.rb