2016-04-29 15:41:18 +02:00
|
|
|
import Plots
|
|
|
|
|
|
|
|
"""Pre-execute hooks to set the plot size for the chunk """
|
|
|
|
function plots_set_size(chunk)
|
|
|
|
w = chunk.options[:fig_width] * chunk.options[:dpi]
|
|
|
|
h = chunk.options[:fig_height] * chunk.options[:dpi]
|
|
|
|
Plots.default(size = (w,h))
|
|
|
|
return chunk
|
|
|
|
end
|
|
|
|
|
|
|
|
push_preexecute_hook(plots_set_size)
|
2016-10-31 16:55:38 +01:00
|
|
|
|
2016-11-01 09:40:40 +01:00
|
|
|
#PNG or SVG is not working, output html
|
2016-10-31 16:55:38 +01:00
|
|
|
function Base.display(report::Report, m::MIME"image/svg+xml", data::Plots.Plot{Plots.PlotlyBackend})#
|
|
|
|
#Remove extra spaces from start of line for pandoc
|
|
|
|
s = reprmime(MIME("text/html"), data)
|
|
|
|
splitted = split(s, "\n")
|
|
|
|
start = split(splitted[1], r"(?=<div)")
|
|
|
|
#script = lstrip(start[1]) #local
|
2016-11-02 08:54:45 +01:00
|
|
|
|
2016-10-31 16:55:38 +01:00
|
|
|
div = lstrip(start[2])
|
|
|
|
plot = join(map(lstrip, splitted[2:end]), "\n")
|
|
|
|
|
|
|
|
if report.first_plot
|
2016-11-02 22:45:32 +01:00
|
|
|
report.header_script *= "<script src=\"https://cdn.plot.ly/plotly-latest.min.js\"></script>"
|
2016-10-31 16:55:38 +01:00
|
|
|
report.first_plot = false
|
|
|
|
end
|
|
|
|
|
|
|
|
report.rich_output *= "\n" * div * "\n" * plot
|
|
|
|
end
|
2016-11-01 09:40:40 +01:00
|
|
|
|
2016-11-02 08:26:10 +01:00
|
|
|
function Base.display(report::Report, m::MIME"image/png", data::Plots.Plot{Plots.PlotlyBackend})#
|
|
|
|
display(report, MIME("image/svg+xml"), data)
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2016-11-01 09:40:40 +01:00
|
|
|
#PNG or SVG is not working, output html
|
|
|
|
function Base.display(report::Report, m::MIME"image/svg+xml", plot::Plots.Plot{Plots.PlotlyJSBackend})
|
|
|
|
body = Plots.PlotlyJS.html_body(plot.o.plot)
|
|
|
|
|
|
|
|
if report.first_plot
|
2016-11-02 22:45:32 +01:00
|
|
|
report.header_script *= "<script src=\"https://cdn.plot.ly/plotly-latest.min.js\"></script>"
|
2016-11-01 09:40:40 +01:00
|
|
|
report.first_plot = false
|
|
|
|
end
|
|
|
|
|
|
|
|
report.rich_output *= "\n" * body
|
|
|
|
end
|
|
|
|
|
|
|
|
function Base.display(report::Report, m::MIME"image/png", plot::Plots.Plot{Plots.PlotlyJSBackend})
|
2016-11-02 22:45:32 +01:00
|
|
|
display(report, MIME("image/svg+xml"), data)
|
2016-11-01 09:40:40 +01:00
|
|
|
end
|
2016-11-01 14:23:14 +01:00
|
|
|
|
|
|
|
|
|
|
|
"""Add saved figure name to results and return the name"""
|
2016-12-19 13:46:00 +01:00
|
|
|
function add_plots_figure(report::Report, plot::Plots.Plot, ext)
|
2016-11-01 14:23:14 +01:00
|
|
|
chunk = report.cur_chunk
|
|
|
|
full_name, rel_name = get_figname(report, chunk, ext = ext)
|
|
|
|
|
|
|
|
Plots.savefig(plot, full_name)
|
|
|
|
push!(report.figures, rel_name)
|
|
|
|
report.fignum += 1
|
|
|
|
return full_name
|
|
|
|
end
|
|
|
|
|
2016-12-19 13:46:00 +01:00
|
|
|
function Base.display(report::Report, m::MIME"application/pdf", plot::Plots.Plot)
|
|
|
|
add_plots_figure(report, plot, ".pdf")
|
2016-11-01 14:23:14 +01:00
|
|
|
end
|
|
|
|
|
2016-12-19 13:46:00 +01:00
|
|
|
function Base.display(report::Report, m::MIME"image/png", plot::Plots.Plot)
|
|
|
|
add_plots_figure(report, plot, ".png")
|
2016-11-01 14:23:14 +01:00
|
|
|
end
|
|
|
|
|
2016-12-19 13:46:00 +01:00
|
|
|
function Base.display(report::Report, m::MIME"image/svg+xml", plot::Plots.Plot)
|
|
|
|
add_plots_figure(report, plot, ".svg")
|
2016-11-03 13:10:35 +01:00
|
|
|
end
|