Class: Flotr::Plot

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

Overview

The Plot object. Serves as a container for the data series (Data objects)

and for the plot options. Call the plot/show methods to generate the 
plot file Flotr::OUTPUT_FILE.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(title = "Flotr Plot") ⇒ Plot

Creates a new plot. You only have to provide a title for the plot, that

will (probably) be used as a page title by the active template.


88
89
90
91
92
93
94
95
96
# File 'lib/flotr.rb', line 88

def initialize(title = "Flotr Plot")
  @title = title
  @template = "#{BASENAME}/#{STD_TEMPLATE}"
  @series = []
  @options = {}
  @width, @height = 600, 300
  @label = @xlim = @ylim = {}
  @output_file = OUTPUT_FILE
end

Instance Attribute Details

#commentObject

Returns the value of attribute comment.



80
81
82
# File 'lib/flotr.rb', line 80

def comment
  @comment
end

#heightObject

Returns the value of attribute height.



81
82
83
# File 'lib/flotr.rb', line 81

def height
  @height
end

#labelObject

Returns the value of attribute label.



82
83
84
# File 'lib/flotr.rb', line 82

def label
  @label
end

#optionsObject

Returns the value of attribute options.



80
81
82
# File 'lib/flotr.rb', line 80

def options
  @options
end

#output_fileObject

Returns the value of attribute output_file.



80
81
82
# File 'lib/flotr.rb', line 80

def output_file
  @output_file
end

#seriesObject

Returns the value of attribute series.



80
81
82
# File 'lib/flotr.rb', line 80

def series
  @series
end

#templateObject

Returns the value of attribute template.



80
81
82
# File 'lib/flotr.rb', line 80

def template
  @template
end

#titleObject

Returns the value of attribute title.



80
81
82
# File 'lib/flotr.rb', line 80

def title
  @title
end

#widthObject

Returns the value of attribute width.



81
82
83
# File 'lib/flotr.rb', line 81

def width
  @width
end

#xlimObject

Returns the value of attribute xlim.



82
83
84
# File 'lib/flotr.rb', line 82

def xlim
  @xlim
end

#ylimObject

Returns the value of attribute ylim.



82
83
84
# File 'lib/flotr.rb', line 82

def ylim
  @ylim
end

Instance Method Details

#<<(serie) ⇒ Object

Adds a Data serie to the Plot. It also returns self, so multiple calls

can be concatenated as in plot << series1 << series2 << series3.


157
158
159
160
161
162
163
# File 'lib/flotr.rb', line 157

def <<(serie)
  if serie.instance_of? Data
    @series << serie
  else
    raise ArgumentError
  end
end

#inspectObject

Provides a JavaScript-formatted description of the all the series.



168
169
170
171
# File 'lib/flotr.rb', line 168

def inspect
  data = @series.map {|s| s.inspect}
  "[ #{data*', '} ]"
end

#saveObject Also known as: plot

Generates the html file containing the plot and fires up a preferred

browser window with the plot loaded.


132
133
134
135
136
137
# File 'lib/flotr.rb', line 132

def save
  eruby = Erubis::Eruby.new(File.read(@template))
  File.open(@output_file, 'w') do |f|
    f.print eruby.result(binding())
  end
end

#showObject



141
142
143
144
145
146
147
148
149
150
151
# File 'lib/flotr.rb', line 141

def show
  self.save
  case RUBY_PLATFORM
  when /darwin/
    system "open \"#{@output_file}\""
  when /mswin/
    system "start #{@output_file}"
  else
    puts "open #{@output_file} in your preferred browser"
  end
end

#std_templateObject

Returns the currently selected standard template, or nil if the active

template is a custom one. Use Plot#template=(full/path/to/template.rhtml)
to set a custom template.


120
121
122
123
124
125
126
# File 'lib/flotr.rb', line 120

def std_template
  if File.dirname(@template) == BASENAME
    File.basename(@template, ".rhtml")
  else
    nil
  end
end

#std_template=(template) ⇒ Object

Selects the active template. It has to be one of those included in the

result of the std_templates method.


110
111
112
113
# File 'lib/flotr.rb', line 110

def std_template=(template)
  raise "Template #{template} does not exist!" unless self.std_templates.include?(template) 
  @template = "#{BASENAME}/#{template}.rhtml"
end

#std_templatesObject

Lists the available standard templates. These are .rhtml files located in

the lib floder inside the gem. The extension .rhtml is dropped.


102
103
104
# File 'lib/flotr.rb', line 102

def std_templates
  Dir.glob("#{BASENAME}/*.rhtml").map {|f| File.basename(f, ".rhtml")}
end