Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

initial import, thank you to the original author!

  • Loading branch information...
commit 239c570632147dd517874cb193c5282536ee162a 1 parent 0d13710
@rdp authored
View
3  AUTHORS.txt
@@ -0,0 +1,3 @@
+Gordon James Miller
+Ara T. Howard
+
View
25 ChangeLog
@@ -0,0 +1,25 @@
+Version 2.2 14-Nov-2005
+
+* Formally added the LICENSE.txt file. It is the new BSD license as defined
+ by opensource.org. See that file for details.
+
+* Added Gnuplot.which to try and fix the recurring problem of windows users
+ having to hack code to get things working.
+
+* Added the Gnuplot.gnuplot function so that I can unit test the finding
+ gnuplot executable routine.
+
+* In the Array.to_gplot method the terminating e is added to the output. This
+ is in response to Bug #2209.
+
+Version 2.1 17-Nov-2004
+
+* Array.to_gplot and Array.to_gsplot now support passing in arrays of
+ arbitrary objects. This is in response to a request by Yoshiki Tsunesada
+ (Tracker ID 1063)
+
+Version 2.0 10-Nov-2004
+
+* The 2.0 version of the gnuplot interface is a cross between the original,
+ object oriented version, and the version 1.0 which was simply a string
+ manipulation library.
View
27 LICENSE.txt
@@ -0,0 +1,27 @@
+Copyright (c) 2004-2005, Gordon James Miller (gmiller@bittwiddlers.com)
+All rights reserved.
+
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions are met:
+
+ * Redistributions of source code must retain the above copyright notice,
+ this list of conditions and the following disclaimer.
+
+ * Redistributions in binary form must reproduce the above copyright
+ notice, this list of conditions and the following disclaimer in the
+ documentation and/or other materials provided with the distribution.
+
+ * Neither the name of the BitTwiddlers, Inc. nor the names of its
+ contributors may be used to endorse or promote products derived from
+ this software without specific prior written permission.
+
+THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
+FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
View
12 README
@@ -0,0 +1,12 @@
+To install as a rubygem you must have a recent version of rubygems (> 0.8)
+
+ruby gnuplot.gemspec
+gem install gnuplot-<version>.gem
+
+
+To install ruby in the traditional way
+
+ruby extconf.rb
+make install
+
+See the howto.html for details on how to setup the module and generate plots.
View
2  extconf.rb
@@ -0,0 +1,2 @@
+require 'mkmf'
+create_makefile 'gnuplot'
View
24 gnuplot.gemspec
@@ -0,0 +1,24 @@
+# -*- ruby -*-
+
+require 'rubygems'
+
+spec = Gem::Specification.new do |s|
+ s.name = 'gnuplot'
+ s.summary = "Utility library to aid in interacting with gnuplot"
+ s.version = "2.2"
+ s.platform = Gem::Platform::RUBY
+ s.date = Time.new
+
+ s.files = [ "lib/gnuplot.rb" ]
+
+ s.autorequire = 'gnuplot.rb'
+ s.author = "Gordon James Miller, Roger Pack"
+ s.email = "rogerpack2005@gmail.com"
+ s.homepage = "http://github.com/rogerdpack/ruby_gnuplot/tree/master"
+end
+
+if $0==__FILE__
+ Gem.manage_gems
+ Gem::Builder.new(spec).build
+end
+spec
View
295 howto.html
@@ -0,0 +1,295 @@
+<html>
+ <head>
+ <title> Ruby Gnuplot - How To </title>
+ <style TYPE="text/css">
+ BODY { background: lightyellow; foreground: black; link: #000099;
+ vlink: #000066 }
+
+ H1 { color: green; text-align: center }
+ H2 { color: green; text-align: left }
+ H3 { color: green; text-align: left }
+ </style>
+ </head>
+
+ <body>
+ <h1> Ruby Gnuplot - How To </h1>
+
+ <p align="center">
+ [<a href="http://rubyforge.org/projects/rgplot">Rubyforge Project
+ page</a>]
+ [<a href="ChangeLog">ChangeLog</a>]
+ [<a href="AUTHORS.txt">Authors</a>]
+ [<a href="LICENSE.txt">License</a>]
+ </p>
+
+ <h2> History and Background </h2>
+
+ <p>
+ Gnuplot is a program that has a rich language for the generation of
+ plots. It has a unique place in academia as it was one of the first
+ freely available programs for plot generation. I started using gnuplot
+ over 10 years ago while pursuing my Master's degree in Physics and have
+ been using it actively ever since.
+ </p>
+
+ <h3>Version 0.9 </h3>
+ <blockquote>
+ My first attempt at a Ruby interface to gnuplot was an object interface
+ encapsulating gnuplot language. This was taken directly from the Python
+ gnuplot interface. In spite of my being very familiar with Gnuplot and
+ Ruby and being the author of the RGnuplot package, I found it
+ non-intuitive to use the RGnuplot package. I found myself constantly
+ looking at the code to figure out what I needed to do.
+
+ This was not sufficient and did not sit well.
+ </blockquote>
+
+ <h3>Version 1.0</h3>
+ <blockquote>
+ The second attempt at a Ruby interface was to do absolutely nothing but
+ use Ruby's built in string manipulation methods. This meant that I
+ could simply use my knowledge of Gnuplot without having to worry about
+ objects.
+
+ While in some ways an improvement over Version 0.9, it still did not sit
+ well with me.
+ </blockquote>
+
+ <h3> Version 2.0 </h3>
+ <blockquote>
+ After attending RubyConf 2004 I was inspired by Rich Kilmer's use of
+ Ruby to implement domain specific languages. That is the current
+ implementation of Gnuplot and quite probably the one that I'll stick
+ with for some time. This version combines the direct mapping of the
+ gnuplot language without wrapping with the ruby syntax and mechanism of
+ adding methods to existing classes to interface Ruby objects with
+ gnuplot.
+ </blockquote>
+
+ <h2>Setup </h2>
+
+ <h3>Version 2.2</h3>
+
+ <p> If the 'gnuplot' command is in your path then there is no required
+ setup. If the gnuplot executable for your system is called something other
+ than simply 'gnuplot' then set the RB_GNUPLOT environment variable to the
+ name of the executable. This must either be a full path to the gnuplot
+ command or an executable filename that exists in your PATH environment
+ variable.
+ </p>
+
+
+ <h2> Ruby Gnuplot Concepts </h2>
+
+ <p>
+ Gnuplot has a very simple conceptual model. Calls to <em>Set</em> are
+ made to set parameters and either <em>Plot</em> or <em>Splot</em> is
+ called to generate the actual plot. The <em>dataset</em> to be
+ plotted can be specified in a number of ways, contained in a seperate
+ file, generated from a function, read from standard input, or read
+ immediately after the plot command.
+ </p>
+
+ <p>
+ The object model for the Ruby gnuplot wrapper directly mimics this
+ layout and flow. The following are the standard steps for generating a
+ plot:
+ </p>
+
+ <ol>
+ <li> <p> Instantiate a Plot or Splot object and set parameters by gnuplot
+ variable name.</p> </li>
+
+ <li> <p>Instantiate DataSet objects and attach Ruby objects containing
+ the data to be plotted to the DataSet. Attach properties that modify
+ the plot command using the modifier name.</p>
+ </li>
+
+ <li> <p>Send the Plot/Splot object to a Gnuplot instance for
+ plotting.</p> </li>
+ </ol>
+
+ <p>
+ The Version 2.0 interface makes very heavy use of blocks leading to very
+ readable code.
+ </p>
+
+ <p>Gnuplot.open </p>
+ <blockquote>
+ Instantiates a new Gnuplot process. The path to the executable is
+ determined on a Unix or MacOSX system using the which command. Windows
+ users, I have no idea what to do.
+
+ If a block is given to the function the opened process is passed into
+ the block. This mimics the most common usage of the File.open method.
+ </blockquote>
+
+ <p>Plot.new </p>
+ <p>SPlot.new </p>
+ <blockquote>
+ Create a new Plot or Splot object. DataSets are attached to the object
+ to specify the data and its properties.
+
+ If a block is given to the function, the plot object is passed into the
+ block.
+ </blockquote>
+
+ <p>DataSet.new</p>
+ <blockquote>
+ Associates a Ruby object containing the data to plot with the properties
+ that will be passed to the plot command for that dataset. Any Ruby
+ object can be associated with a DataSet as long as it understands the
+ to_gplot method.
+ </blockquote>
+
+ <p> to_gplot </p>
+ <blockquote>
+ Within Gnuplot, plot data is read in very simple formats. The
+ to_gplot method is expected to write the data of the object in a format
+ that is understandable by Gnuplot. One of the many great things about
+ Ruby is that methods can be added after the original declaration. The
+ gnuplot module defines the to_gplot method on the following classes:
+
+ Array, String, and Matrix.
+
+ Simply define a to_gplot method on your own class to tie the class into
+ gnuplot.
+ </blockquote>
+
+ <h2> Examples </h2>
+
+ <h3> Simple sin wave </h3>
+
+ <p> The following example simply plots the value of sin(x) between the
+ ranges of -10 and 10. A few points to notice:
+ </p>
+
+ <ul>
+ <li><p> The code uses nested blocks to construct the plot. The newly
+ created object is passed to the block so it can be modified in
+ place.
+ </p>
+ </li>
+
+ <li><p> Each of the gnuplot plot variables are modified using the
+ variable name as a method name on the plot object or on the dataset
+ object. The wrapper also takes care of the single quoting that is
+ required on some of the variables like title, ylabel, and xlabel.
+ </p>
+ </li>
+
+ <li><p> The plot object simply has an array of DataSets. The
+ constructor initializes this empty array before yielding to the
+ block. This method uses the &lt;&lt; operator to add the DataSet to
+ the plot.
+ </p>
+ </li>
+
+ <li><p> When the plot block ends, if an IO object is given to the Plot
+ constructor, the plot commands will be written to the IO object.
+ Any object can be passed to the constructor as long as it
+ understands the &lt;&lt; operator.
+ </p>
+ </li>
+ </ul>
+
+ <pre class='code'>
+Gnuplot.open do |gp|
+ Gnuplot::Plot.new( gp ) do |plot|
+
+ plot.xrange "[-10:10]"
+ plot.title "Sin Wave Example"
+ plot.ylabel "x"
+ plot.xlabel "sin(x)"
+
+ plot.data << Gnuplot::DataSet.new( "sin(x)" ) do |ds|
+ ds.with = "lines"
+ ds.linewidth = 4
+ end
+
+ end
+
+end
+ </pre>
+
+ <h3> Plotting discrete points </h3>
+
+ <p>
+ Array data can be plotted quite easily since Arrays have a defined
+ to_gplot method.
+ </p>
+
+ <ul>
+ <li><p>Simply pass an array of data to the constructor of the DataSet
+ object or set the data property of the DataSet. In this example,
+ because there are two arrays, each array will be a single column of
+ data to the gnuplot process.
+ </p>
+ </li>
+ </ul>
+
+ <pre class='code'>
+Gnuplot.open do |gp|
+ Gnuplot::Plot.new( gp ) do |plot|
+
+ plot.title "Array Plot Example"
+ plot.ylabel "x"
+ plot.xlabel "x^2"
+
+ x = (0..50).collect { |v| v.to_f }
+ y = x.collect { |v| v ** 2 }
+
+ plot.data << Gnuplot::DataSet.new( [x, y] ) do |ds|
+ ds.with = "linespoints"
+ ds.notitle
+ end
+ end
+end
+ </pre>
+
+ <h3> Multiple Data Sets </h3>
+
+ <p> As many data sets as are desired can be attached to a plot. Each of
+ these can have their own plot modifiers. Notice in this example how the
+ data array is explicitly set instead of using the &lt;&lt; operator.
+ </p>
+
+ <p> Also in this example, the commands are not written to the Gnuplot
+ process but are instead written to a File called gnuplot.dat. This file
+ can later be run directly by a gnuplot process as it contains only the
+ gnuplot commands.
+ </p>
+
+ <pre class='code'>
+File.open( "gnuplot.dat", "w") do |gp|
+ Gnuplot::Plot.new( gp ) do |plot|
+
+ plot.xrange "[-10:10]"
+ plot.title "Sin Wave Example"
+ plot.ylabel "x"
+ plot.xlabel "sin(x)"
+
+ x = (0..50).collect { |v| v.to_f }
+ y = x.collect { |v| v ** 2 }
+
+ plot.data = [
+ Gnuplot::DataSet.new( "sin(x)" ) { |ds|
+ ds.with = "lines"
+ ds.title = "String function"
+ ds.linewidth = 4
+ },
+
+ Gnuplot::DataSet.new( [x, y] ) { |ds|
+ ds.with = "linespoints"
+ ds.title = "Array data"
+ }
+ ]
+
+ end
+
+end
+
+ </pre>
+
+ </body>
+</html>
View
283 lib/gnuplot.rb
@@ -0,0 +1,283 @@
+# Methods and variables for interacting with the gnuplot process. Most of
+# these methods are for sending data to a gnuplot process, not for reading from
+# it. Most of the methods are implemented as added methods to the built in
+# classes.
+
+require 'matrix'
+
+module Gnuplot
+
+ # Trivial implementation of the which command that uses the PATH environment
+ # variable to attempt to find the given application. The application must
+ # be executable and reside in one of the directories in the PATH environment
+ # to be found. The first match that is found will be returned.
+ #
+ # bin [String] The name of the executable to search for.
+ #
+ # Return the full path to the first match or nil if no match is found.
+ #
+ def Gnuplot.which ( bin )
+ return bin if File::executable? bin
+
+ path = ENV['PATH'] # || ENV['WHAT_EVER_WINDOWS_PATH_VAR_IS']
+ path.split(File::PATH_SEPARATOR).each do |dir|
+ candidate = File::join dir, bin.strip
+ return candidate if File::executable? candidate
+ end
+
+ # This is an implementation that works when the which command is
+ # available.
+ #
+ # IO.popen("which #{bin}") { |io| return io.readline.chomp }
+
+ return nil
+ end
+
+ # Find the path to the gnuplot executable. The name of the executable can
+ # be specified using the RB_GNUPLOT environment variable but will default to
+ # the command 'gnuplot'.
+ #
+ # persist [bool] Add the persist flag to the gnuplot executable
+ #
+ # Return the path to the gnuplot executable or nil if one cannot be found.
+ def Gnuplot.gnuplot( persist = true )
+ cmd = which( ENV['RB_GNUPLOT'] || 'gnuplot' )
+ cmd += " -persist" if persist
+ cmd
+ end
+
+ # Open a gnuplot process that exists in the current PATH. If the persist
+ # flag is true then the -persist flag is added to the command line. The
+ # path to the gnuplot executable is determined using the 'which' command.
+ #
+ # See the gnuplot documentation for information on the persist flag.
+ #
+ # <b>todo</b> Add a method to pass the gnuplot path to the function.
+
+ def Gnuplot.open( persist=true )
+ cmd = Gnuplot.gnuplot( persist ) or raise 'gnuplot not found'
+ IO::popen( cmd, "w") { |io| yield io }
+ end
+
+
+ # Holds command information and performs the formatting of that command
+ # information to a Gnuplot process. When constructing a new plot for
+ # gnuplot, this is the first object that must be instantiated. On this
+ # object set the various properties and add data sets.
+
+ class Plot
+ attr_accessor :cmd, :data, :sets
+
+ QUOTED = [ "title", "output", "xlabel", "ylabel" ]
+
+ def initialize (io = nil, cmd = "plot")
+ @cmd = cmd
+ @sets = []
+ @data = []
+ yield self if block_given?
+
+ io << to_gplot if io
+ end
+
+ # Invoke the set method on the plot using the name of the invoked method
+ # as the set variable and any arguments that have been passed as the
+ # value. See the +set+ method for more details.
+
+ def method_missing( methId, *args )
+ set methId.id2name, *args
+ end
+
+
+ # Set a variable to the given value. +Var+ must be a gnuplot variable and
+ # +value+ must be the value to set it to. Automatic quoting will be
+ # performed if the variable requires it.
+ #
+ # This is overloaded by the +method_missing+ method so see that for more
+ # readable code.
+
+ def set ( var, value = "" )
+ value = "'#{value}'" if QUOTED.include? var unless value =~ /^'.*'$/
+ @sets << [ var, value ]
+ end
+
+
+ # Return the current value of the variable. This will return the setting
+ # that is currently in the instance, not one that's been given to a
+ # gnuplot process.
+
+ def [] ( var )
+ v = @sets.assoc( var )
+ v[1] || nil
+ end
+
+
+ def add_data ( ds )
+ @data << ds
+ end
+
+
+ def to_gplot (io = "")
+ @sets.each { |var, val| io << "set #{var} #{val}\n" }
+
+ if @data.size > 0 then
+ io << @cmd << " " << @data.collect { |e| e.plot_args }.join(", ")
+ io << "\n"
+
+ v = @data.collect { |ds| ds.to_gplot }
+ io << v.compact.join("e\n")
+ end
+
+ io
+ end
+ end
+
+
+ class SPlot < Plot
+
+ def initialize (io = nil, cmd = "splot")
+ super
+ end
+
+ def to_gplot (io = "")
+ @sets.each { |var, val| io << "set #{var} #{val}\n" }
+
+ if @data.size > 0 then
+ io << @cmd << " "
+ io << @data.collect { |e| e.plot_args }.join(", ")
+ io << "\n"
+
+ @data.each do |ds|
+ io << ds.to_gsplot << "e\n"
+ end
+ end
+
+ io
+ end
+
+ end
+
+
+
+ # Container for a single dataset being displayed by gnuplot. Each object
+ # has a reference to the actual data being plotted as well as settings that
+ # control the "plot" command. The data object must support the to_gplot
+ # command.
+ #
+ # +data+ The data that will be plotted. The only requirement is that the
+ # object understands the to_gplot method.
+ #
+ # The following attributes correspond to their related string in the gnuplot
+ # command. See the gnuplot documentation for more information on this.
+ #
+ # title, with
+ #
+ # @todo Use the delegator to delegate to the data property.
+
+ class DataSet
+ attr_accessor :title, :with, :using, :data, :linewidth, :matrix
+
+ def initialize (data = nil)
+ @data = data
+ yield self if block_given?
+ end
+
+ def notitle
+ @title = "notitle"
+ end
+
+ def plot_args (io = "")
+
+ # Order of these is important or gnuplot barfs on 'em
+
+ io << ( (@data.instance_of? String) ? @data : "'-'" )
+
+ io << " using #{@using}" if @using
+
+ io << case @title
+ when /notitle/ then " notitle"
+ when nil then ""
+ else " title '#{@title}'"
+ end
+
+ io << " matrix" if @matrix
+ io << " with #{@with}" if @with
+ io << " linewidth #{@linewidth}" if @linewidth
+ io
+ end
+
+ def to_gplot
+ case @data
+ when nil then nil
+ when String then nil
+ else @data.to_gplot
+ end
+ end
+
+ def to_gsplot
+ case @data
+ when nil then nil
+ when String then nil
+ else @data.to_gsplot
+ end
+ end
+
+ end
+end
+
+class Array
+ def to_gplot
+ if ( self[0].kind_of? Array ) then
+ tmp = self[0].zip( *self[1..-1] )
+ tmp.collect { |a| a.join(" ") }.join("\n") + "\ne"
+ elsif ( self[0].kind_of? Numeric ) then
+ s = ""
+ self.length.times { |i| s << "#{self[i]}\n" }
+ s
+ else
+ self[0].zip( *self[1..-1] ).to_gplot
+ end
+ end
+
+ def to_gsplot
+ f = ""
+
+ if ( self[0].kind_of? Array ) then
+ x = self[0]
+ y = self[1]
+ d = self[2]
+
+ x.each_with_index do |xv, i|
+ y.each_with_index do |yv, j|
+ f << [ xv, yv, d[i][j] ].join(" ") << "\n"
+ end
+ # f << "\n"
+ end
+ elsif ( self[0].kind_of? Numeric ) then
+ self.length.times do |i| f << "#{self[i]}\n" end
+ else
+ self[0].zip( *self[1..-1] ).to_gsplot
+ end
+
+ f
+ end
+end
+
+class Matrix
+ def to_gplot (x = nil, y = nil)
+ xgrid = x || (0...self.column_size).to_a
+ ygrid = y || (0...self.row_size).to_a
+
+ f = ""
+ ygrid.length.times do |j|
+ y = ygrid[j]
+ xgrid.length.times do |i|
+ if ( self[j,i] ) then
+ f << "#{xgrid[i]} #{y} #{self[j,i]}\n"
+ end
+ end
+ end
+
+ f
+ end
+
+end
View
21 test/arrtest.rb
@@ -0,0 +1,21 @@
+require '../lib/gnuplot'
+
+Gnuplot.open do |gp|
+ Gnuplot::Plot.new( gp ) do |plot|
+
+ plot.title "Array Plot Example"
+ plot.ylabel "x"
+ plot.xlabel "x^2"
+
+ x = (0..50).collect { |v| v.to_f }
+ y = x.collect { |v| v ** 2 }
+
+ plot.data << Gnuplot::DataSet.new( [x, y] ) do |ds|
+ ds.with = "linespoints"
+ ds.notitle
+ end
+
+ end
+
+end
+
View
31 test/multtest.rb
@@ -0,0 +1,31 @@
+require '../lib/gnuplot'
+
+# File.open( "gnuplot.dat", "w") do |gp|
+Gnuplot.open do |gp|
+ Gnuplot::Plot.new( gp ) do |plot|
+
+ plot.xrange "[-10:10]"
+ plot.title "Sin Wave Example"
+ plot.ylabel "x"
+ plot.xlabel "sin(x)"
+
+ x = (0..50).collect { |v| v.to_f }
+ y = x.collect { |v| v ** 2 }
+
+ plot.data = [
+ Gnuplot::DataSet.new( "sin(x)" ) { |ds|
+ ds.with = "lines"
+ ds.title = "String function"
+ ds.linewidth = 4
+ },
+
+ Gnuplot::DataSet.new( [x, y] ) { |ds|
+ ds.with = "linespoints"
+ ds.title = "Array data"
+ }
+ ]
+
+ end
+
+end
+
View
19 test/sinwave.rb
@@ -0,0 +1,19 @@
+require '../lib/gnuplot'
+
+Gnuplot.open do |gp|
+ Gnuplot::Plot.new( gp ) do |plot|
+
+ plot.xrange "[-10:10]"
+ plot.title "Sin Wave Example"
+ plot.ylabel "x"
+ plot.xlabel "sin(x)"
+
+ plot.data << Gnuplot::DataSet.new( "sin(x)" ) do |ds|
+ ds.with = "lines"
+ ds.linewidth = 4
+ end
+
+ end
+
+end
+
View
120 test/test_gnuplot.rb
@@ -0,0 +1,120 @@
+# -*- ruby -*-
+
+require '../lib/gnuplot'
+require 'test/unit'
+
+class StdDataTest < Test::Unit::TestCase
+
+ def test_array_1d
+ data = (0..5).to_a
+ ds = Gnuplot::DataSet.new( data )
+
+ assert data, ds.data
+ assert data.join("\n") + "\n", ds.to_gplot
+ end
+
+
+ # Test a multidimensional array.
+
+ def test_array_nd
+ d1 = (0..3).to_a
+ d2 = d1.collect { |v| 3 * v }
+ d3 = d2.collect { |v| 4 * v }
+
+ data = [ d1, d2, d3 ]
+ ds = Gnuplot::DataSet.new( data )
+
+ assert data, ds.data
+ assert "0 0 0\n1 3 12\n2 6 24\n3 9 36\n", ds.to_gplot
+ end
+end
+
+
+class DataSetTest < Test::Unit::TestCase
+
+ def test_yield_ctor
+ ds = Gnuplot::DataSet.new do |ds|
+ ds.with = "lines"
+ ds.using = "1:2"
+ ds.data = [ [0, 1, 2], [1, 2, 5] ]
+ end
+
+ assert "lines", ds.with
+ assert "1:2", ds.using
+ assert nil == ds.title
+ assert [ [0, 1, 2], [1, 2, 5] ], ds.data
+ assert "'-' using 1:2 with lines", ds.plot_args
+ assert "0 1\n1 2\n2 5\n", ds.to_gplot
+ end
+
+end
+
+
+class PlotTest < Test::Unit::TestCase
+
+ def test_no_data
+ plot = Gnuplot::Plot.new do |p|
+ p.set "output", "'foo'"
+ p.set "terminal", "postscript enhanced"
+ end
+
+ assert( plot.sets,
+ [ ["output", "'foo'"],
+ ["terminal", "postscript enhanced"] ] )
+
+
+ assert( plot.to_gplot, \
+ "set output 'foo'\nset terminal postscript enhanced\n" )
+
+ end
+
+ def test_set
+ plot = Gnuplot::Plot.new do |p|
+ p.set "title", "foo"
+ end
+ assert "'foo'", plot["title"]
+
+ plot.set "title", "'foo'"
+ assert "'foo'", plot["title"]
+ end
+
+end
+
+
+require 'rbconfig'
+CONFIG = Config::MAKEFILE_CONFIG
+
+# This attempts to test the functions that comprise the gnuplot package. Most
+# of the bug reports that I get for this package have to do with finding the
+# gnuplot executable under different environments so that makes it difficult
+# to test on a single environment. To try to get around this I'm using the
+# rbconfig library and its path to the sh environment variable.
+
+class GnuplotModuleTest
+
+ def test_which
+ # Put the spaces around the command to make sure that it gets stripped
+ # properly.
+ assert( CONFIG["SHELL"], Gnuplot::which(" sh " ) )
+ assert( CONFIG["SHELL"], Gnuplot::which( CONFIG["SHELL"] ) )
+ end
+
+
+ def test_gnuplot
+ cmd = Gnuplot.gnuplot
+ assert( Gnuplot::which("gnuplot") + " -persist", cmd )
+
+ cmd = Gnuplot.gnuplot(false)
+ assert( Gnuplot::which("gnuplot"), cmd )
+
+ # If I set the name of the gnuplot environment variable to a different
+ # name (one that is in the path) then I should get the shell name as the
+ # result of the gnuplot call.
+
+ ENV["RB_GNUPLOT"] = "sh"
+ assert( CONFIG["SHELL"], Gnuplot.gnuplot(false) )
+ end
+
+end
+
+
Please sign in to comment.
Something went wrong with that request. Please try again.