class SimplePlot

SimplePlot

Copyright 2014 Austen Higgins-Cassidy
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
     http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

end

Public Class Methods

new(name_template="_plot", size = 512) click to toggle source
Calls superclass method SimpleOutput::SimpleOutputPlugin::new
# File lib/simpleplot.rb, line 21
def initialize(name_template="_plot", size = 512)
   super()
   @name = name_template
   @size = size
   @series_next = 0;
end

Public Instance Methods

append_callback(x,y,name,options) click to toggle source
# File lib/simpleplot.rb, line 62
def append_callback(x,y,name,options)
  xmin = x.min
  xmax = x.max
  ymin = y.min
  ymax = y.max
  @metadata[name]['length'] = @metadata[name]['length'] < x.size ? x.size : @metadata[name]['length']
  @metadata[name]['xmin'] = xmin < @metadata[name]['xmin'] ? xmin : @metadata[name]['xmin']
  @metadata[name]['xmax'] = xmax > @metadata[name]['xmax'] ? xmax : @metadata[name]['xmax']
  @metadata[name]['ymin'] = ymin < @metadata[name]['ymin'] ? ymin : @metadata[name]['ymin']
  @metadata[name]['ymax'] = ymax > @metadata[name]['ymax'] ? ymax : @metadata[name]['ymax']
  check_title(name, options) 
end
check_title(name, options) click to toggle source
# File lib/simpleplot.rb, line 32
def check_title(name, options)
  if options.has_key?('series')
    @metadata[name]['series_titles'] << options['series']
  else
    @metadata[name]['series_titles'] << "set-#{@series_next}"
    @series_next += 1
  end
end
new_data_callback(name) click to toggle source
# File lib/simpleplot.rb, line 41
def new_data_callback(name)
  name = translate_name(name)
  @metadata[name] = {'length' => 0, 'xlabel' => 'x', 'ylabel' => 'y', 'xmin' => 0 , 'xmax' => 10, 'ymin' => 0, 'ymax' => 10, 'series_titles' => [], 'histogram' => false, 'bincount' => 10, 'normalized' => false, 'xsize' => 640, 'ysize' => 480}
end
options_callback(options) click to toggle source
# File lib/simpleplot.rb, line 28
def options_callback(options)
  @metadata[@current_name].merge! options
end
save() click to toggle source
# File lib/simpleplot.rb, line 75
def save()
  data = self.get_data_as_xy()
  data.each do |set_name, series|
    Gnuplot.open do |gp|
      Gnuplot::Plot.new(gp) do |plot|
        plot.terminal "png size #{@metadata[set_name]['xsize']},#{@metadata[set_name]['ysize']}"
       
        plot.output "#{set_name+@name}.png"
        #plot.set('size', '{1,1}')

        plot.title set_name

        plot.xlabel @metadata[set_name]['xlabel']
        plot.ylabel @metadata[set_name]['ylabel']
        plot.data = []
        max = @metadata[set_name]['ymax']
        min = @metadata[set_name]['ymin']
        if min == max
          max = min + 1
        end
        if @metadata[set_name]['histogram']
          size = @metadata[set_name]['length']
          bins = @metadata[set_name]['bincount'] 
          width = (max.to_f-min.to_f).to_f/bins.to_f
          #bins = size.to_f/width.to_f
          plot.yrange '[0:]'
          plot.xrange "[#{min}:#{max}]"
          plot.set('boxwidth',width*0.9)
          plot.set('offset', 'graph 0.05,0.05,0.05,0.0')
          plot.set('xtics' " #{min}, #{width.to_f}, #{max}")
          plot.set('tics', 'out nomirror')
          plot.set('style', 'fill solid 0.5')
        else
          plot.xrange "[#{@metadata[set_name]['xmin']}:#{@metadata[set_name]['xmax']}]"
          plot.yrange "[#{min}:#{max}]"
        end
        series.each_with_index do |line, index|
          
          if @metadata[set_name]['histogram']
            data_pts = line[1]
          else
            data_pts = line
          end
           
          d = Gnuplot::DataSet.new(data_pts) 
          d.title = @metadata[set_name]['series_titles'][index]
         
          if @metadata[set_name]['histogram']
            d.using = "(#{width.to_f}*floor($1/#{width.to_f})+#{width.to_f}/2.0):(1.0) smooth freq w boxes lc rgb\"blue\""
          else
             d.with = "linespoints"
             d.linewidth = 2
          end
          plot.data << d
          
        end
      end
    end
  end
end
set_x_callback(data, name, options) click to toggle source
# File lib/simpleplot.rb, line 46
def set_x_callback(data, name, options)
  xmin = data.min
  xmax = data.max
  @metadata[name]['xmin'] = xmin
  @metadata[name]['xmax'] = xmax
  @metadata[name]['length'] = (@metadata[name]['length'] < data.size) ? data.size : @metadata[name]['length']
  check_title(name, options)
end
set_y_callback(data, name, options) click to toggle source
# File lib/simpleplot.rb, line 55
def set_y_callback(data, name, options)
  ymin = data.min
  ymax = data.max
  @metadata[name]['ymin'] = ymin
  @metadata[name]['ymax'] = ymax
end