Class: SimplePlot

Inherits:
SimpleOutput::SimpleOutputPlugin show all
Defined in:
lib/simpleplot.rb

Overview

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.

Instance Method Summary collapse

Methods inherited from SimpleOutput::SimpleOutputPlugin

#advance_series, #annotate, #append_hash, #append_points, #append_series_name, #append_xy, #get_data_as_points, #get_data_as_xy, #get_series_hashes, #new_data, #new_data_check, #set_hash, #set_options, #set_points, #set_x_data, #set_xy, #set_y_data, #translate_name

Constructor Details

#initialize(name_template = "_plot", size = 512) ⇒ SimplePlot

Returns a new instance of SimplePlot.



21
22
23
24
25
26
# File 'lib/simpleplot.rb', line 21

def initialize(name_template="_plot", size = 512)
   super()
   @name = name_template
   @size = size
   @series_next = 0;
end

Instance Method Details

#append_callback(x, y, name, options) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
# 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) ⇒ Object



32
33
34
35
36
37
38
39
# 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) ⇒ Object



41
42
43
44
# 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) ⇒ Object



28
29
30
# File 'lib/simpleplot.rb', line 28

def options_callback(options)
  @metadata[@current_name].merge! options
end

#saveObject



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# 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) ⇒ Object



46
47
48
49
50
51
52
53
# 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) ⇒ Object



55
56
57
58
59
60
# 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