diff --git a/README.md b/README.md index 33225b3..f107bc7 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,7 @@ # Gnuplot +Work in progress... + [![Build Status](https://travis-ci.org/gcalderone/Gnuplot.jl.svg?branch=master)](https://travis-ci.org/gcalderone/Gnuplot.jl) [![Coverage Status](https://coveralls.io/repos/gcalderone/Gnuplot.jl/badge.svg?branch=master&service=github)](https://coveralls.io/github/gcalderone/Gnuplot.jl?branch=master) diff --git a/REQUIRE b/REQUIRE index b54093c..bf62ecc 100644 --- a/REQUIRE +++ b/REQUIRE @@ -1,2 +1,2 @@ julia 0.6 -AbbrvKW 0.2.0 +AbbrvKW 0.3.0 diff --git a/src/Gnuplot.jl b/src/Gnuplot.jl index 6162df7..66188b6 100644 --- a/src/Gnuplot.jl +++ b/src/Gnuplot.jl @@ -196,8 +196,8 @@ end """ Return a unique data block name """ -function gp_mkBlockName(prefix="") - if prefix == "" +@AbbrvKW function gp_mkBlockName(;prefix::Union{Void,String}=nothing) + if prefix == nothing prefix = string("d", gp_current()) end @@ -251,24 +251,21 @@ Example: gp_setOption(cmd="/path/to/gnuplot", verb=2, startup="set term wxt") ``` """ -function gp_setOption(;kw...) - @AbbrvKW_check(kw, - cmd::Nullable{String}=nothing, - startup::Nullable{String}=nothing, - verbose::Nullable{Int}=nothing) - - if !isnull(startup) - main.startup = get(startup) +@AbbrvKW function gp_setOption(;cmd::Union{Void,String}=nothing, + startup::Union{Void,String}=nothing, + verbose::Union{Void,Int}=nothing) + if startup != nothing + main.startup = startup end - if !isnull(cmd) - main.gnuplotCmd = get(cmd) + if cmd != nothing + main.gnuplotCmd = cmd checkGnuplotVersion() end - if !isnull(verbose) - @assert (0 <= get(verbose) <= 4) - main.verboseLev = get(verbose) + if verbose != nothing + @assert (0 <= verbose <= 4) + main.verboseLev = verbose end return nothing @@ -517,24 +514,24 @@ gp_cmd("set key left", xrange=(1,3)) gp_cmd(title="My title", xlab="X label", xla="Y label") ``` """ -function gp_cmd(v::String=""; kw...) - #@show kw - @AbbrvKW_check(kw, - multiID::Nullable{Int}=nothing, - xrange::Nullable{NTuple{2, Float64}}=nothing, - yrange::Nullable{NTuple{2, Float64}}=nothing, - zrange::Nullable{NTuple{2, Float64}}=nothing, - title::Nullable{String}=nothing, - xlabel::Nullable{String}=nothing, - ylabel::Nullable{String}=nothing, - zlabel::Nullable{String}=nothing, - xlog::Nullable{Bool}=nothing, - ylog::Nullable{Bool}=nothing, - zlog::Nullable{Bool}=nothing) +@AbbrvKW function gp_cmd(v::String=""; + splot::Union{Void,Bool}=nothing, + multiID::Union{Void,Int}=nothing, + xrange::Union{Void,NTuple{2, Number}}=nothing, + yrange::Union{Void,NTuple{2, Number}}=nothing, + zrange::Union{Void,NTuple{2, Number}}=nothing, + title::Union{Void,String}=nothing, + xlabel::Union{Void,String}=nothing, + ylabel::Union{Void,String}=nothing, + zlabel::Union{Void,String}=nothing, + xlog::Union{Void,Bool}=nothing, + ylog::Union{Void,Bool}=nothing, + zlog::Union{Void,Bool}=nothing) gp_getProcOrStartIt() cur = main.states[main.curPos] - mID = isnull(multiID) ? cur.multiID : get(multiID) + splot == nothing || (cur.splot = splot) + mID = multiID == nothing ? cur.multiID : multiID if v != "" push!(cur.cmds, MultiCmd(v, mID)) @@ -543,18 +540,18 @@ function gp_cmd(v::String=""; kw...) end end - isnull(xrange) || gp_cmd(multiID=mID, "set xrange [" * join(get(xrange), ":") * "]") - isnull(yrange) || gp_cmd(multiID=mID, "set yrange [" * join(get(yrange), ":") * "]") - isnull(zrange) || gp_cmd(multiID=mID, "set zrange [" * join(get(zrange), ":") * "]") + xrange == nothing || gp_cmd(multiID=mID, "set xrange [" * join(xrange, ":") * "]") + yrange == nothing || gp_cmd(multiID=mID, "set yrange [" * join(yrange, ":") * "]") + zrange == nothing || gp_cmd(multiID=mID, "set zrange [" * join(zrange, ":") * "]") - isnull(title) || gp_cmd(multiID=mID, "set title '" * get(title ) * "'") - isnull(xlabel) || gp_cmd(multiID=mID, "set xlabel '" * get(xlabel) * "'") - isnull(ylabel) || gp_cmd(multiID=mID, "set ylabel '" * get(ylabel) * "'") - isnull(zlabel) || gp_cmd(multiID=mID, "set zlabel '" * get(zlabel) * "'") + title == nothing || gp_cmd(multiID=mID, "set title '" * title * "'") + xlabel == nothing || gp_cmd(multiID=mID, "set xlabel '" * xlabel * "'") + ylabel == nothing || gp_cmd(multiID=mID, "set ylabel '" * ylabel * "'") + zlabel == nothing || gp_cmd(multiID=mID, "set zlabel '" * zlabel * "'") - isnull(xlog) || gp_cmd(multiID=mID, (get(xlog) ? "" : "un") * "set logscale x") - isnull(ylog) || gp_cmd(multiID=mID, (get(ylog) ? "" : "un") * "set logscale y") - isnull(zlog) || gp_cmd(multiID=mID, (get(zlog) ? "" : "un") * "set logscale z") + xlog == nothing || gp_cmd(multiID=mID, (xlog ? "" : "un") * "set logscale x") + ylog == nothing || gp_cmd(multiID=mID, (ylog ? "" : "un") * "set logscale y") + zlog == nothing || gp_cmd(multiID=mID, (zlog ? "" : "un") * "set logscale z") end @@ -593,14 +590,14 @@ name = gp_data(x, y, name="MyChosenName") The returned name can be used as input to `gp_plot`. """ -function gp_data(data::Vararg{AbstractArray{T,1},N}; kw...) where {T,N} - @AbbrvKW_check(kw, name::String="", prefix::String="") - +@AbbrvKW function gp_data(data::Vararg{AbstractArray{T,1},N}; + name::Union{Void,String}=nothing, + prefix::Union{Void,String}=nothing) where {T,N} gp_getProcOrStartIt() cur = main.states[main.curPos] - if name == "" - name = gp_mkBlockName(prefix) + if name == nothing + name = gp_mkBlockName(pre=prefix) end name = "\$$name" @@ -674,23 +671,20 @@ gp_plot("\$src u 1:(\\\$2+10) w l tit 'Pow 2.2, offset=10'") gp_dump() # Do the plot ``` """ -function gp_plot(spec::String; kw...) - @AbbrvKW_check(kw, - lastData::Bool=false, - file::Nullable{String}=nothing, - multiID::Nullable{Int}=nothing, - splot::Nullable{Bool}=nothing) +@AbbrvKW function gp_plot(spec::String; + lastData::Bool=false, + file::Union{Void,String}=nothing, + multiID::Union{Void,Int}=nothing) gp_getProcOrStartIt() cur = main.states[main.curPos] - mID = isnull(multiID) ? cur.multiID : get(multiID) - isnull(splot) || (cur.splot = splot) + mID = multiID == nothing ? cur.multiID : multiID src = "" if lastData src = cur.lastDataName - elseif !isnull(file) - src = "'" * get(file) * "'" + elseif file != nothing + src = "'" * file * "'" end push!(cur.plot, MultiCmd("$src $spec", mID)) end @@ -698,7 +692,7 @@ end #--------------------------------------------------------------------- """ -Similar to `@gp`, but do not adds the calls to `gp_reset()` at the +Similar to `@gp`, but do not add calls to `gp_reset()` at the beginning and `gp_dump()` at the end. """ macro gp_(args...) @@ -848,18 +842,16 @@ end Print all data and commands stored in the current session on STDOUT or on a file. """ -function gp_dump(;kw...) - @AbbrvKW_check(kw, - all::Bool=false, - dry::Bool=false, - data::Bool=false, - file::Nullable{String}=nothing) - +@AbbrvKW function gp_dump(; all::Bool=false, + dry::Bool=false, + data::Bool=false, + file::Union{Void,String}=nothing) + if main.curPos == 0 return "" end - if !isnull(file) + if file != nothing all = true dry = true end @@ -901,8 +893,8 @@ function gp_dump(;kw...) push!(out, "unset multiplot") end - if !isnull(file) - sOut = open(get(file), "w") + if file != nothing + sOut = open(file, "w") for s in out; println(sOut, s); end close(sOut) end diff --git a/test/runtests.jl b/test/runtests.jl index e030eb1..a7ea0dc 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -77,8 +77,7 @@ function gp_test(terminal="unknown") terminal == "unknown" || pressEnter() #----------------------------------------------------------------- - @gp( - "set format y \"%.1f\"", + @gp("set format y \"%.1f\"", "set key box opaque", xr=(-2pi,2pi), :multi, "layout 2,2 columnsfirst title \"Multiplot title\"", @@ -123,6 +122,70 @@ function gp_test(terminal="unknown") ) terminal == "unknown" || pressEnter() + #----------------------------------------------------------------- + @gp(""" + approx_1(x) = x - x**3/6 + approx_2(x) = x - x**3/6 + x**5/120 + approx_3(x) = x - x**3/6 + x**5/120 - x**7/5040 + label1 = "x - {x^3}/3!" + label2 = "x - {x^3}/3! + {x^5}/5!" + label3 = "x - {x^3}/3! + {x^5}/5! - {x^7}/7!" + # + set termoption enhanced + save_encoding = GPVAL_ENCODING + set encoding utf8 + # + set title "Polynomial approximation of sin(x)" + set key Left center top reverse + set xrange [ -3.2 : 3.2 ] + set xtics ("-π" -pi, "-π/2" -pi/2, 0, "π/2" pi/2, "π" pi) + set format y "%.1f" + set samples 500 + set style fill solid 0.4 noborder""", + :plot, "'+' using 1:(sin(\$1)):(approx_1(\$1)) with filledcurve title label1 lt 3", + :plot, "'+' using 1:(sin(\$1)):(approx_2(\$1)) with filledcurve title label2 lt 2", + :plot, "'+' using 1:(sin(\$1)):(approx_3(\$1)) with filledcurve title label3 lt 1", + :plot, "sin(x) with lines lw 1 lc rgb 'black'") + + #----------------------------------------------------------------- + @gp(""" + set zrange [-1:1] + unset label + unset arrow + sinc(u,v) = sin(sqrt(u**2+v**2)) / sqrt(u**2+v**2) + set xrange [-5:5]; set yrange [-5:5] + set arrow from 5,-5,-1.2 to 5,5,-1.2 lt -1 + set label 1 "increasing v" at 6,0,-1 + set arrow from 5,6,-1 to 5,5,-1 lt -1 + set label 2 "u=0" at 5,6.5,-1 + set arrow from 5,6,sinc(5,5) to 5,5,sinc(5,5) lt -1 + set label 3 "u=1" at 5,6.5,sinc(5,5) + set parametric + set hidden3d offset 0 # front/back coloring makes no sense for fenceplot # + set isosamples 2,33 + xx=-5; dx=(4.99-(-4.99))/9 + x0=xx; xx=xx+dx + x1=xx; xx=xx+dx + x2=xx; xx=xx+dx + x3=xx; xx=xx+dx + x4=xx; xx=xx+dx + x5=xx; xx=xx+dx + x6=xx; xx=xx+dx + x7=xx; xx=xx+dx + x8=xx; xx=xx+dx + x9=xx; xx=xx+dx""", + splot=true, + :plot, "[u=0:1][v=-4.99:4.99]x0, v, (u<0.5) ? -1 : sinc(x0,v) notitle", + :plot, "x1, v, (u<0.5) ? -1 : sinc(x1,v) notitle", + :plot, "x2, v, (u<0.5) ? -1 : sinc(x2,v) notitle", + :plot, "x3, v, (u<0.5) ? -1 : sinc(x3,v) notitle", + :plot, "x4, v, (u<0.5) ? -1 : sinc(x4,v) notitle", + :plot, "x5, v, (u<0.5) ? -1 : sinc(x5,v) notitle", + :plot, "x6, v, (u<0.5) ? -1 : sinc(x6,v) notitle", + :plot, "x7, v, (u<0.5) ? -1 : sinc(x7,v) notitle", + :plot, "x8, v, (u<0.5) ? -1 : sinc(x8,v) notitle", + :plot, "x9, v, (u<0.5) ? -1 : sinc(x9,v) notitle") + gp_exitAll() return true end