From 737edc11663491a1347218ab3a576a961611f8cc Mon Sep 17 00:00:00 2001 From: Leif Berg Date: Fri, 8 Nov 2013 21:26:49 -0600 Subject: [PATCH 1/9] edited main library to support interaction with files in Windows --- gnuplot.lua | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/gnuplot.lua b/gnuplot.lua index 68cd682..8c13ff7 100644 --- a/gnuplot.lua +++ b/gnuplot.lua @@ -27,18 +27,18 @@ --]] local gnuplot = {} - +local tmpdir = os.getenv("TMP") -- ** auxiliary functions ** local temp_files = {} -local function remove_temp_files() +function remove_temp_files() for _, fname in ipairs(temp_files) do - os.remove(fname) + os.remove(fname) end end local function write_temp_file(content) - local name = os.tmpname() + local name = tmpdir..os.tmpname() local file = io.open(name, 'w') file:write(content) file:close() @@ -172,8 +172,10 @@ function gnuplot.do_plot(g, cmd, path) local name = write_temp_file( code ) local opt = "" if g._type == "wxt" then opt = '--persist' end + -- print(string.format("%s %s %s", gnuplot.bin, opt, name)) os.execute( string.format("%s %s %s", gnuplot.bin, opt, name) ) - + --added following function call to ensure garbage collection on Windows + remove_temp_files() return g end From 2e2204b6e33dab20ad806b3b4a97e645ab616d2f Mon Sep 17 00:00:00 2001 From: Leif Berg Date: Fri, 8 Nov 2013 21:27:38 -0600 Subject: [PATCH 2/9] updated example script for Windows and added lua function to create a full path for an output file --- example.lua | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/example.lua b/example.lua index 9538405..96ffafb 100644 --- a/example.lua +++ b/example.lua @@ -1,7 +1,17 @@ -#! /usr/bin/env lua +require "AddAppDirectory" +require "getScriptFilename" +AddAppDirectory() -local gp = require('gnuplot') --- gp.bin = '/usr/local/bin/mygnuplot' +local gp = runfile[[gnuplot.lua]] +gp.bin = [["C:\Program Files (x86)\gnuplot\bin\gnuplot.exe"]] + +local function createFullFilePathForWindows(filename) + local filenamefullpath = string.match(getScriptFilename(), "(.-)([^\\]-([^%.]+))$")..filename + filenamefullpath = string.gsub(filenamefullpath,"\\","/") + return filenamefullpath +end + +local outfile = createFullFilePathForWindows("sample.png") local g = gp{ -- all optional, with sane defaults @@ -46,7 +56,8 @@ local g = gp{ width = 2, }, } -}:plot('output.png') +}:plot(outfile) + -- plot with other terminals --g:plot('output.svg') From 68e530316b214b454ba7f5fc020dbe76523f657b Mon Sep 17 00:00:00 2001 From: Leif Berg Date: Fri, 8 Nov 2013 21:36:06 -0600 Subject: [PATCH 3/9] adding gitignore file to ignore output images --- .gitignore | 1 + 1 file changed, 1 insertion(+) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..aab52d9 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +*.png \ No newline at end of file From 221d2ee1e4427d16fdd4ce48ba7826f2b8ea5f3f Mon Sep 17 00:00:00 2001 From: Leif Berg Date: Sat, 9 Nov 2013 11:00:52 -0600 Subject: [PATCH 4/9] played with example to explore adding parameters --- example.lua | 44 ++++++++++++++++++++++++-------------------- 1 file changed, 24 insertions(+), 20 deletions(-) diff --git a/example.lua b/example.lua index 96ffafb..98e461c 100644 --- a/example.lua +++ b/example.lua @@ -5,6 +5,7 @@ AddAppDirectory() local gp = runfile[[gnuplot.lua]] gp.bin = [["C:\Program Files (x86)\gnuplot\bin\gnuplot.exe"]] + local function createFullFilePathForWindows(filename) local filenamefullpath = string.match(getScriptFilename(), "(.-)([^\\]-([^%.]+))$")..filename filenamefullpath = string.gsub(filenamefullpath,"\\","/") @@ -19,42 +20,45 @@ local g = gp{ height = 480, xlabel = "X axis", ylabel = "Y axis", + xrange = "[0:10]", + yrange = "[0:100]", key = "top left", consts = { gamma = 2.5 }, data = { - gp.file { -- plot from a file - "myfile", -- file path - title = "Title 1", -- optional - }, + -- gp.file { -- plot from a file + -- createFullFilePathForWindows("my_data_file"), -- file path + -- title = "Title 1", -- optional + -- }, - gp.array { -- plot from an 'array-like' thing in memory. Could be a - -- numlua matrix, for example. + gp.array { -- plot from an 'array-like' thing in memory. Could be a numlua matrix, for example. + { {0,1,2,3,4,5,6,7,8,9}, -- x - {3,4,5,6,7,8,9,8,7,6} -- y + {30,40,50,60,70,80,90,80,70,60}, -- y }, - title = "Title 2", -- optional + title = "My Bar Graph", -- optional using = {1,2}, -- optional - with = 'linespoints' -- optional + with = 'lines', -- optional + }, - gp.func { -- plot from a Lua function - function(x) -- function to plot - return 3* math.sin(2*x) + 4 - end, + -- gp.func { -- plot from a Lua function + -- function(x) -- function to plot + -- return 3* math.sin(2*x) + 4 + -- end, - range = {-2, 10, 0.01}, -- optional - width = 3, -- optional - }, + -- range = {-2, 10, 0.01}, -- optional + -- width = 3, -- optional + -- }, - gp.gpfunc { -- plot from a native gnuplot function - "gamma*sin(1.8*x) + 3", - width = 2, - }, + -- gp.gpfunc { -- plot from a native gnuplot function + -- "gamma*sin(1.8*x) + 3", + -- width = 2, + -- }, } }:plot(outfile) From 32e56dd38ece811d77ac113a0dbc4c6d346985b1 Mon Sep 17 00:00:00 2001 From: Leif Berg Date: Sat, 9 Nov 2013 11:01:05 -0600 Subject: [PATCH 5/9] changed name of sample data file (for example) --- myfile => my_data_file | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename myfile => my_data_file (100%) diff --git a/myfile b/my_data_file similarity index 100% rename from myfile rename to my_data_file From c4249ced8db3ff1bc026ce86eaa13cf892e170e1 Mon Sep 17 00:00:00 2001 From: Leif Berg Date: Sat, 9 Nov 2013 11:03:01 -0600 Subject: [PATCH 6/9] added better tmpdir setting for windows (should ignore under linux) changed tmp file string with string.gsub to use correct slashes for paths in windows --- gnuplot.lua | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/gnuplot.lua b/gnuplot.lua index 8c13ff7..6db4e95 100644 --- a/gnuplot.lua +++ b/gnuplot.lua @@ -27,7 +27,11 @@ --]] local gnuplot = {} -local tmpdir = os.getenv("TMP") +local tmpdir = "" +--set tmpdir for Windows, leaves it blank for Linux +if os.getenv("TMP") then + tmpdir = os.getenv("TMP") +end -- ** auxiliary functions ** local temp_files = {} @@ -39,12 +43,11 @@ end local function write_temp_file(content) local name = tmpdir..os.tmpname() + name = string.gsub(name,"\\","/") local file = io.open(name, 'w') file:write(content) file:close() - table.insert(temp_files, name) - return name end From a5804cea43e02433aec06527f59c76c119d4bde7 Mon Sep 17 00:00:00 2001 From: Leif Berg Date: Sat, 9 Nov 2013 15:22:06 -0600 Subject: [PATCH 7/9] recommented sections of original example --- example.lua | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/example.lua b/example.lua index 98e461c..99e7c45 100644 --- a/example.lua +++ b/example.lua @@ -46,19 +46,19 @@ local g = gp{ }, - -- gp.func { -- plot from a Lua function - -- function(x) -- function to plot - -- return 3* math.sin(2*x) + 4 - -- end, + gp.func { -- plot from a Lua function + function(x) -- function to plot + return 3* math.sin(2*x) + 4 + end, - -- range = {-2, 10, 0.01}, -- optional - -- width = 3, -- optional - -- }, + range = {-2, 10, 0.01}, -- optional + width = 3, -- optional + }, - -- gp.gpfunc { -- plot from a native gnuplot function - -- "gamma*sin(1.8*x) + 3", - -- width = 2, - -- }, + gp.gpfunc { -- plot from a native gnuplot function + "gamma*sin(1.8*x) + 3", + width = 2, + }, } }:plot(outfile) From fef76c63099524c2009bb1812b5f9b2fc6966568 Mon Sep 17 00:00:00 2001 From: Leif Berg Date: Sat, 9 Nov 2013 15:26:12 -0600 Subject: [PATCH 8/9] removed rockspec --- lua-gnuplot-0.3-1.rockspec | 32 -------------------------------- 1 file changed, 32 deletions(-) delete mode 100644 lua-gnuplot-0.3-1.rockspec diff --git a/lua-gnuplot-0.3-1.rockspec b/lua-gnuplot-0.3-1.rockspec deleted file mode 100644 index d7ee011..0000000 --- a/lua-gnuplot-0.3-1.rockspec +++ /dev/null @@ -1,32 +0,0 @@ -package = "lua-gnuplot" -version = "0.3-1" - -source = { - url = "https://bitbucket.org/lucashnegri/lua-gnuplot/get/v0.3.tar.bz2", - md5 = "15e010691ab518b9b1b55e1d257f378a", --update - dir = "lucashnegri-lua-gnuplot-6119390145a8", --update -} - -description = { - summary = "Lua interface for gnuplot", - detailed = [[ - lua-gnuplot is a small library for using gnuplot in a Lua program. It works by - generating a gnuplot script from a Lua plot definition and calling the - gnuplot program for you. - ]], - homepage = "https://bitbucket.org/lucashnegri/lua-gnuplot", - license = "MIT", - maintainer = "Lucas Hermann Negri ", -} - -dependencies = { - "lua >= 5.1" -} - -build = { - type = "builtin", - modules = { - ["gnuplot"] = "gnuplot.lua", - }, - copy_directories = {}, -} From fec01f8ef7ba0870ec1a82866871e131f6277d87 Mon Sep 17 00:00:00 2001 From: Leif Berg Date: Sat, 9 Nov 2013 15:26:28 -0600 Subject: [PATCH 9/9] created VRJuggLua interface and example --- vrj_interface.lua | 60 +++++++++++++++++++++++++++++++++++++++ vrj_interface_example.lua | 14 +++++++++ 2 files changed, 74 insertions(+) create mode 100644 vrj_interface.lua create mode 100644 vrj_interface_example.lua diff --git a/vrj_interface.lua b/vrj_interface.lua new file mode 100644 index 0000000..f1a1802 --- /dev/null +++ b/vrj_interface.lua @@ -0,0 +1,60 @@ +require "AddAppDirectory" +require "getScriptFilename" +AddAppDirectory() + +local gp = runfile[[gnuplot.lua]] +gp.bin = [["C:\Program Files (x86)\gnuplot\bin\gnuplot.exe"]] + +local function createFullFilePathForWindows(filename) + local filenamefullpath = string.match(getScriptFilename(), "(.-)([^\\]-([^%.]+))$")..filename + return string.gsub(filenamefullpath,"\\","/") +end + +function gnuplotImage(a) + local outfile = createFullFilePathForWindows(a.outfile or "sample.png") + local xvalues = {} + local yvalues = {} + if a.values and not (a.xvalues and a.yvalues) then + yvalues = a.values + for i = 1, #yvalues, 1 do table.insert(xvalues,i) end + else + xvalues = a.xvalues or {1,2,3,4,5,6,7,8,9,10} + yvalues = a.yvalues or {1,2,3,4,5,6,7,8,9,10} + end + local my_title = a.title or "My Data Graph" + local graph_type = a.m_type or 'boxes' + if a.color then mycolor = string.format('rgb "%s"',a.color) else mycolor = 'rgb "#0000FF"' end + local g = gp{ + width = a.width or 640, + height = a.height or 480, + xlabel = a.xlabel or "X axis", + ylabel = a.ylabel or "Y axis", + xrange = "["..math.min(unpack(xvalues))..":"..math.max(unpack(xvalues)).."]", + yrange = "["..math.min(unpack(yvalues))..":"..math.max(unpack(yvalues)).."]", + key = a.key or "top", + style = a.style or "fill solid .25", + consts = { + gamma = 2.5 + }, + data = { + gp.array { + { + xvalues, -- x + yvalues, -- y + }, + title = my_title, -- optional + using = {1,2}, -- optional + with = graph_type, -- optional + color = mycolor, + }, + } + } + g:plot(outfile) + print("gnuplotImage: "..outfile.." created") + return outfile +end + +local my_data = {220,16,805,11,305,30,45,76,91,50} + + + diff --git a/vrj_interface_example.lua b/vrj_interface_example.lua new file mode 100644 index 0000000..9cab79d --- /dev/null +++ b/vrj_interface_example.lua @@ -0,0 +1,14 @@ +require "AddAppDirectory" +AddAppDirectory() + +runfile[[vrj_interface.lua]] + +myImage = gnuplotImage{ + title = "My Sample Data", -- optional, but probably a good idea + values = my_data, -- must pass table of values (or xvalues and yvalues separately) + m_type = "lines", -- "lines" for line-graph, "boxes" or bar-graph + color = "#FF0000", -- optional + outfile = "mySampleData.png", -- optional +} + +print(myImage) \ No newline at end of file