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 diff --git a/example.lua b/example.lua index 9538405..99e7c45 100644 --- a/example.lua +++ b/example.lua @@ -1,7 +1,18 @@ -#! /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 @@ -9,27 +20,30 @@ 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 @@ -46,7 +60,8 @@ local g = gp{ width = 2, }, } -}:plot('output.png') +}:plot(outfile) + -- plot with other terminals --g:plot('output.svg') diff --git a/gnuplot.lua b/gnuplot.lua index 68cd682..6db4e95 100644 --- a/gnuplot.lua +++ b/gnuplot.lua @@ -27,24 +27,27 @@ --]] local gnuplot = {} - +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 = {} -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() + name = string.gsub(name,"\\","/") local file = io.open(name, 'w') file:write(content) file:close() - table.insert(temp_files, name) - return name end @@ -172,8 +175,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 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 = {}, -} diff --git a/myfile b/my_data_file similarity index 100% rename from myfile rename to my_data_file 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