Skip to content

Commit

Permalink
Switch to AlgebraOfGraphics (#113)
Browse files Browse the repository at this point in the history
  • Loading branch information
rikhuijzer authored Jun 9, 2021
1 parent a4d8780 commit 1fe67d0
Show file tree
Hide file tree
Showing 12 changed files with 68 additions and 84 deletions.
2 changes: 0 additions & 2 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ version = "0.4.4"

[deps]
CodeTracking = "da1fd8a2-8d9e-5ec2-8556-3022fb5608a2"
Compose = "a81c6b42-2e10-5240-aca2-a61377ecd94b"
DataFrames = "a93c6f00-e57d-5684-b7b6-d8193f3e46c0"
Latexify = "23fbe1c1-3f47-55db-b15f-69d7ec21a316"
LiveServer = "16fef848-5104-11e9-1b77-fb7a48bbb589"
Expand All @@ -21,7 +20,6 @@ pandoc_jll = "c5432543-76ad-5c9d-82bf-db097047a5e2"

[compat]
CodeTracking = "1.0"
Compose = "0.9"
DataFrames = "0.22, 1.0, 1.1"
Latexify = "0.15"
LiveServer = "0.6"
Expand Down
5 changes: 2 additions & 3 deletions docs/Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,17 @@ authors = ["Rik Huijzer <[email protected]>"]
version = "0.1.0"

[deps]
AlgebraOfGraphics = "cbdf2221-f076-402e-a563-3d30da359d67"
Books = "939d5c6b-51ae-42e7-97ca-7564d0d4ad91"
CairoMakie = "13f3f980-e62b-5c42-98c6-ff1f3baf88f0"
CodeTracking = "da1fd8a2-8d9e-5ec2-8556-3022fb5608a2"
DataFrames = "a93c6f00-e57d-5684-b7b6-d8193f3e46c0"
Dates = "ade2ca70-3891-5945-98fb-dc099432e06a"
Gadfly = "c91e804a-d5a3-530f-b6f0-dfbca275c004"
Latexify = "23fbe1c1-3f47-55db-b15f-69d7ec21a316"
MCMCChains = "c7f686f2-ff18-58e9-bc7b-31028e88f75d"
Statistics = "10745b16-79ce-11e8-11f9-7d13ad32a3b2"
TOML = "fa267f1f-6049-4f14-aa54-33bafae1ed76"

# Only setting compat entries when there is not already one in the main Project.toml.
[compat]
Gadfly = "1.3"
Latexify = "0.15"
MCMCChains = "4.9"
8 changes: 4 additions & 4 deletions docs/contents/demo.md
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ _gen/my_data_mean-sco.md

## Plots {#sec:plots}

Conversions for Gadfly are also included, see @fig:example_plot.
Conversions for AlgebraOfGraphics are also included, see @fig:example_plot.
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.
Expand All @@ -284,7 +284,7 @@ _gen/example_plot-sco.md
```

If the output is a string instead of the output you expected, then check whether you load the related packages in time.
For example, for this Gadfly plot, you need to load Gadfly.jl together with Books.jl for Requires.jl to work.
For example, for this plot, you need to load AlgebraOfGraphics.jl together with Books.jl so that Requires.jl will load the code for handling AlgebraOfGraphics objects.

For multiple images, use `Options.(objects, paths)`:

Expand All @@ -298,13 +298,13 @@ Resulting in @fig:example_plot_2 and @fig:example_plot_3:
_gen/multiple_example_plots.md
```

For changing the size, use `ImageOptions`:
For changing the size, use `axis` from AlgebraOfGraphics:

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

Or, use the combination for setting captions and size
And, for adjusting the caption, use `Options`:

```{.include}
_gen/combined_options_plot-sco.md
Expand Down
3 changes: 2 additions & 1 deletion docs/src/BooksDocs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@ import MCMCChains
import Statistics
import TOML

using AlgebraOfGraphics
using Books
using CairoMakie
using CodeTracking
using DataFrames
using Dates
using Gadfly

include("includes.jl")

Expand Down
19 changes: 12 additions & 7 deletions docs/src/includes.jl
Original file line number Diff line number Diff line change
Expand Up @@ -135,28 +135,33 @@ module_example_definition() = code_block("""

function example_plot()
I = 1:30
Gadfly.plot(x=I, y=I.^2)
df = (x=I, y=I.^2)
xy = data(df) * mapping(:x, :y)
fg = draw(xy)
end

function multiple_example_plots()
paths = ["example_plot_$i" for i in 2:3]
I = 1:30
df = (x=I, y=I.*2, z=I.^3)
objects = [
plot(x=I, y=I),
plot(x=I, y=I.^3)
draw(data(df) * mapping(:x, :y))
draw(data(df) * mapping(:x, :z))
]
Options.(objects, paths)
end

function image_options_plot()
I = 1:0.1:30
p = plot(x=I, y=sin.(I), Geom.line)
ImageOptions(p; width=6inch, height=2inch)
df = (x=I, y=sin.(I))
xy = data(df) * visual(Lines) * mapping(:x, :y)
axis = (width = 600, height = 140)
draw(xy; axis)
end

function combined_options_plot()
imageoptions = image_options_plot()
Options(imageoptions; caption="Sine function")
p = image_options_plot()
Options(p; caption="Sine function")
end

chain() = MCMCChains.Chains([1])
3 changes: 1 addition & 2 deletions src/Books.jl
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ include("ci.jl")
include("build.jl")
include("serve.jl")
include("output.jl")
include("outputs/compose.jl")
include("outputs/dataframes.jl")
include("showcode.jl")
include("generate.jl")
Expand All @@ -37,7 +36,7 @@ export gen
export serve

function __init__()
@require Gadfly="c91e804a-d5a3-530f-b6f0-dfbca275c004" include("outputs/gadfly.jl")
@require AlgebraOfGraphics="cbdf2221-f076-402e-a563-3d30da359d67" include("outputs/aog.jl")
end

end # module
2 changes: 1 addition & 1 deletion src/output.jl
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ This passes the objects through show to use the overrides that package creators
```jldoctest
julia> using MCMCChains
julia> chn = Chains([1]);
julia> chn = Chains([1]; info=(start_time=[1.0], stop_time=[1.0]));
julia> string(chn)
"MCMC chain (1×1×1 Array{Int64, 3})"
Expand Down
36 changes: 36 additions & 0 deletions src/outputs/aog.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
@debug "Loading AlgebraOfGraphics support into Books via Requires"

using AlgebraOfGraphics
using CairoMakie

function convert_output(path, fg::AlgebraOfGraphics.FigureGrid; 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 AlgebraOfGraphics 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)
AlgebraOfGraphics.save(svg_path, fg)

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

im_link = joinpath("im", svg_filename)
caption, label = caption_label(path, caption, label)
pandoc_image(file, png_path; caption, label)
end
54 changes: 0 additions & 54 deletions src/outputs/compose.jl

This file was deleted.

5 changes: 0 additions & 5 deletions src/outputs/gadfly.jl

This file was deleted.

5 changes: 2 additions & 3 deletions test/Project.toml
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
[deps]
AlgebraOfGraphics = "cbdf2221-f076-402e-a563-3d30da359d67"
CairoMakie = "13f3f980-e62b-5c42-98c6-ff1f3baf88f0"
DataFrames = "a93c6f00-e57d-5684-b7b6-d8193f3e46c0"
Documenter = "e30172f5-a6a5-5a46-863b-614d45cd2de4"
Gadfly = "c91e804a-d5a3-530f-b6f0-dfbca275c004"
MCMCChains = "c7f686f2-ff18-58e9-bc7b-31028e88f75d"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"

# Only setting compat entries when there is not already one in the main Project.toml.
[compat]
Documenter = "0.26"
Gadfly = "1.3"
MCMCChains = "4.9"
10 changes: 8 additions & 2 deletions test/generate.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import Gadfly
using AlgebraOfGraphics
using CairoMakie
using DataFrames

@testset "generate" begin
Expand Down Expand Up @@ -28,8 +29,13 @@ using DataFrames

@test contains(B.convert_output(nothing, DataFrame(A = [1])), "---")

X = 1:30
df = (x=X, y=X.*2)
xy = data(df) * mapping(:x, :y)
fg = draw(xy)

mktemp() do path, io
@test contains(B.convert_output(path, Gadfly.plot()), ".png")
@test contains(B.convert_output(path, fg), ".png")
end
im_dir = joinpath(B.BUILD_DIR, "im")
rm(im_dir; force=true, recursive=true)
Expand Down

0 comments on commit 1fe67d0

Please sign in to comment.