Skip to content

Commit

Permalink
Add support for Plots and Makie (#116)
Browse files Browse the repository at this point in the history
  • Loading branch information
rikhuijzer authored Jun 18, 2021
1 parent fc49409 commit 5a42853
Show file tree
Hide file tree
Showing 9 changed files with 107 additions and 4 deletions.
4 changes: 4 additions & 0 deletions .github/workflows/CI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ jobs:
run: |
cd docs
julia --project -e 'using BooksDocs; BooksDocs.build()'
env:
# Fixes 'GKS: can't connect to GKS socket application' errors
# and quality of output plots in GR back end.
GKSwstype: nul

- name: Deploy to secondary branch
# Always updates documentation when ubuntu passes, which is fine.
Expand Down
2 changes: 2 additions & 0 deletions docs/Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ DataFrames = "a93c6f00-e57d-5684-b7b6-d8193f3e46c0"
Dates = "ade2ca70-3891-5945-98fb-dc099432e06a"
Latexify = "23fbe1c1-3f47-55db-b15f-69d7ec21a316"
MCMCChains = "c7f686f2-ff18-58e9-bc7b-31028e88f75d"
Makie = "ee78f7c6-11fb-53f2-987a-cfe4a2b5a57a"
Plots = "91a5bcdd-55d7-5caf-9e0b-520d859cae80"
Statistics = "10745b16-79ce-11e8-11f9-7d13ad32a3b2"
TOML = "fa267f1f-6049-4f14-aa54-33bafae1ed76"

Expand Down
18 changes: 15 additions & 3 deletions docs/contents/demo.md
Original file line number Diff line number Diff line change
Expand Up @@ -274,10 +274,10 @@ _gen/my_data_mean-sco.md

## Plots {#sec:plots}

Conversions for AlgebraOfGraphics are also included, see @fig:example_plot.
An AlgebraOfGraphics plot is shown below in @fig:example_plot.
For Plots.jl and Makie.jl see, respectively section @sec:plotsjl and @sec:makie.
This is actually a bit tricky, because we want to show vector graphics (SVG) on the web, but these are not supported (well) by LaTeX.
Therefore, portable network graphics (PNG) images are passed to LaTeX via cairosvg;
I found that this tool does the best conversions without relying on Cairo and/or Fontconfig, which are not so stable in combination with Compose in my experience.
Therefore, portable network graphics (PNG) images are also created and passed to LaTeX when building a PDF.

```{.include}
_gen/example_plot-sco.md
Expand Down Expand Up @@ -310,6 +310,18 @@ And, for adjusting the caption, use `Options`:
_gen/combined_options_plot-sco.md
```

### Plots {#sec:plotsjl}

```{.include}
_gen/plotsjl-sco.md
```

### Makie {#sec:makie}

```{.include}
_gen/makiejl-sco.md
```

## Other notes

### Multilingual books
Expand Down
4 changes: 4 additions & 0 deletions docs/src/BooksDocs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ using CairoMakie
using CodeTracking
using DataFrames
using Dates
using Plots

# Defaulting plot to Plots; Makie can use Makie.plot.
plot = Plots.plot

include("includes.jl")

Expand Down
12 changes: 12 additions & 0 deletions docs/src/includes.jl
Original file line number Diff line number Diff line change
Expand Up @@ -164,4 +164,16 @@ function combined_options_plot()
Options(fg; caption="Sine function")
end

function plotsjl()
p = plot(1:10, 1:2:20)
Options(p; caption="An example plot with Plots.jl")
end

function makiejl()
x = range(0, 10, length=100)
y = sin.(x)
p = lines(x, y)
Options(p; caption="An example plot with Makie.jl")
end

chain() = MCMCChains.Chains([1])
2 changes: 2 additions & 0 deletions src/Books.jl
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ export serve

function __init__()
@require AlgebraOfGraphics="cbdf2221-f076-402e-a563-3d30da359d67" include("outputs/aog.jl")
@require Makie="ee78f7c6-11fb-53f2-987a-cfe4a2b5a57a" include("outputs/makie.jl")
@require Plots="91a5bcdd-55d7-5caf-9e0b-520d859cae80" include("outputs/plots.jl")
end

end # module
2 changes: 1 addition & 1 deletion src/outputs/aog.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
@debug "Loading AlgebraOfGraphics support into Books via Requires"
@debug "Loading AlgebraOfGraphics.jl support into Books via Requires"

using AlgebraOfGraphics
using CairoMakie
Expand Down
36 changes: 36 additions & 0 deletions src/outputs/makie.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
@debug "Loading Makie.jl support into Books via Requires"

using CairoMakie
import Makie

function convert_output(path, p::Makie.FigureAxisPlot; caption=nothing, label=nothing)
im_dir = joinpath(BUILD_DIR, "im")
mkpath(im_dir)

if isnothing(path)
# Not determining some random name here, because it would require cleanups too.
msg = """
It is not possible to write an image without specifying a path.
Use `Options(p; filename=filename)` where `p` is a Makie.jl plot.
"""
throw(ErrorException(msg))
end
file, _ = method_name(path)

println("Writing plot images for $file")
svg_filename = "$file.svg"
svg_path = joinpath(im_dir, svg_filename)
# Explicit rm due to https://github.com/JuliaIO/FileIO.jl/issues/338.
rm(svg_path; force=true)
Makie.FileIO.save(svg_path, p)

png_filename = "$file.png"
png_path = joinpath(im_dir, png_filename)
rm(png_path; force=true)
px_per_unit = 3 # Ensure high resolution.
Makie.FileIO.save(png_path, p; px_per_unit)

im_link = joinpath("im", svg_filename)
caption, label = caption_label(path, caption, label)
pandoc_image(file, png_path; caption, label)
end
31 changes: 31 additions & 0 deletions src/outputs/plots.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
@debug "Loading Plots.jl support into Books via Requires"

import Plots

function convert_output(path, p::Plots.Plot; caption=nothing, label=nothing)
im_dir = joinpath(BUILD_DIR, "im")
mkpath(im_dir)

if isnothing(path)
# Not determining some random name here, because it would require cleanups too.
msg = """
It is not possible to write an image without specifying a path or filename.
Use `Options(p; filename=filename)` where `p` is a Plots.jl plot.
"""
throw(ErrorException(msg))
end
file, _ = method_name(path)

println("Writing plot images for $file")
svg_filename = "$file.svg"
svg_path = joinpath(im_dir, svg_filename)
Plots.savefig(p, svg_path)

png_filename = "$file.png"
png_path = joinpath(im_dir, png_filename)
Plots.savefig(p, png_path)

im_link = joinpath("im", svg_filename)
caption, label = caption_label(path, caption, label)
pandoc_image(file, png_path; caption, label)
end

0 comments on commit 5a42853

Please sign in to comment.