diff --git a/examples/winston_sample.mdw b/examples/winston_sample.mdw index 0ad44f6..13cb2cb 100644 --- a/examples/winston_sample.mdw +++ b/examples/winston_sample.mdw @@ -27,7 +27,6 @@ The figures and code can be included in the output. <<>>= using Winston -figure() t = linspace(0, 2*pi, 100) plot(t, sinc(t)) xlabel("x") @@ -37,6 +36,21 @@ ylabel("sinc(x)") You can also include a plot with caption and hide the code: <>= -figure() plot(cumsum(randn(1000, 1))) @ + +<<>>= +x = linspace(0, 3pi, 100) +c = cos(x) +s = sin(x) + +figure() +p = FramedPlot( + title="title!", + xlabel="\\Sigma x^2_i", + ylabel="\\Theta_i") + +add(p, FillBetween(x, c, x, s)) +add(p, Curve(x, c, color="red")) +add(p, Curve(x, s, color="blue")) +@ diff --git a/src/JuliaReport.jl b/src/JuliaReport.jl index e4b430a..6429e0b 100644 --- a/src/JuliaReport.jl +++ b/src/JuliaReport.jl @@ -1,6 +1,6 @@ module JuliaReport using Compat -import Base: display +import Base: display, writemime #Contains report global properties type Report <: Display @@ -68,7 +68,7 @@ function weave(source ; doctype = "pandoc", plotlib="PyPlot", informat="noweb", else l_plotlib = lowercase(plotlib) if l_plotlib == "winston" - eval(Expr(:using, :Winston)) + eval(parse("""include(Pkg.dir("JuliaReport","src","winston.jl"))""")) rcParams[:plotlib] = "Winston" elseif l_plotlib == "pyplot" eval(Expr(:using, :PyPlot)) @@ -169,10 +169,10 @@ function run(parsed) else chunk[:result] = run_block(chunk[:content]) end - if rcParams[:plotlib] == "Gadfly" - chunk[:fig] && (chunk[:figure] = copy(report.figures)) - else + if rcParams[:plotlib] == "PyPlot" chunk[:fig] && (chunk[:figure] = savefigs(chunk)) + else + chunk[:fig] && (chunk[:figure] = copy(report.figures)) end end parsed[i] = copy(chunk) @@ -185,10 +185,6 @@ function savefigs(chunk) l_plotlib = lowercase(rcParams[:plotlib]) if l_plotlib == "pyplot" return savefigs_pyplot(chunk) - elseif l_plotlib == "winston" - return savefigs_winston(chunk) - elseif l_plotlib == "gadfly" - return String[] end end @@ -210,34 +206,6 @@ function savefigs_pyplot(chunk) return fignames end -#This currently only works if there is only one figure/chunk -#Doesn"t work with FramedPlots -#Doesn"t work with Julia 0.4 -function savefigs_winston(chunk) - fignames = String[] - ext = report.formatdict[:figfmt] - figpath = joinpath(report.cwd, report.figdir) - isdir(figpath) || mkdir(figpath) - - chunkid = chunk[:name] == nothing ? chunk[:number] : chunk[:name] - - #println(Winston._display.figs) - #println(Winston._display.fig_order) - - #Iterate over all open figures, save them and store names - for fig = copy(Winston._display.fig_order) - full_name = joinpath(report.cwd, report.figdir, "$(report.basename)_$(chunkid)_$fig$ext") - rel_name = "$(report.figdir)/$(report.basename)_$(chunkid)_$fig$ext" #Relative path is used in output - #@show rel_name - #figure(fig) #Calling figure clears the canvas! - #savefig(Winston._display.figs[gcf()].plot, full_name) #Produces empty figures - savefig(full_name) - closefig() - push!(fignames, rel_name) - end - return fignames -end - function display(report::Report, m::MIME"text/plain", data) @@ -246,7 +214,18 @@ function display(report::Report, m::MIME"text/plain", data) push!(report.executed, s) end +function get_figname(report::Report, chunk) + figpath = joinpath(report.cwd, report.figdir) + isdir(figpath) || mkdir(figpath) + ext = report.formatdict[:figfmt] + fig = report.fignum + chunk = report.cur_chunk + chunkid = (chunk[:name] == nothing) ? chunk[:number] : chunk[:name] + full_name = joinpath(report.cwd, report.figdir, "$(report.basename)_$(chunkid)_$fig$ext") + rel_name = "$(report.figdir)/$(report.basename)_$(chunkid)_$fig$ext" #Relative path is used in output + return full_name, rel_name +end export weave diff --git a/src/gadfly.jl b/src/gadfly.jl index 420ce11..4fefc06 100644 --- a/src/gadfly.jl +++ b/src/gadfly.jl @@ -1,29 +1,15 @@ using Gadfly -import Base: start, next, done, display, writemime - - Gadfly.set_default_plot_format(:png) - - -function display(doc::Report, m::MIME"image/png", data) - - figpath = joinpath(report.cwd, report.figdir) - isdir(figpath) || mkdir(figpath) - - ext = report.formatdict[:figfmt] - - - - fig = doc.fignum +#Captures figures +function display(report::Report, m::MIME"image/png", data) chunk = report.cur_chunk - chunkid = (chunk[:name] == nothing) ? chunk[:number] : chunk[:name] - full_name = joinpath(report.cwd, report.figdir, "$(report.basename)_$(chunkid)_$fig$ext") - rel_name = "$(report.figdir)/$(report.basename)_$(chunkid)_$fig$ext" #Relative path is used in output + full_name, rel_name = get_figname(report, chunk) docformat = formats[report.formatdict[:doctype]] + #Add to results for term chunks and store otherwise if chunk[:term] chunk[:figure] = [rel_name] report.cur_result *= "\n" * report.formatdict[:codeend] @@ -31,14 +17,11 @@ function display(doc::Report, m::MIME"image/png", data) report.cur_result *= "\n\n" * report.formatdict[:codestart] chunk[:figure] = String[] else - push!(doc.figures, rel_name) + push!(report.figures, rel_name) end - doc.fignum += 1 + report.fignum += 1 out = open(full_name, "w") writemime(out, m, data) close(out) - - - #push!(doc.executed, filename) end diff --git a/src/winston.jl b/src/winston.jl new file mode 100644 index 0000000..8e4e91f --- /dev/null +++ b/src/winston.jl @@ -0,0 +1,25 @@ +using Winston + +function display(report::Report, m::MIME"image/png", data) + + chunk = report.cur_chunk + full_name, rel_name = get_figname(report, chunk) + + docformat = formats[report.formatdict[:doctype]] + + #Add to results for term chunks and store otherwise + if chunk[:term] + chunk[:figure] = [rel_name] + report.cur_result *= "\n" * report.formatdict[:codeend] + report.cur_result *= formatfigures(chunk, docformat) + report.cur_result *= "\n\n" * report.formatdict[:codestart] + chunk[:figure] = String[] + else + push!(report.figures, rel_name) + end + + report.fignum += 1 + out = open(full_name, "w") + writemime(out, m, data) + close(out) +end