Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@mortenpi
Last active September 21, 2015 18:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mortenpi/0e3e031bf9d11a9ba6a6 to your computer and use it in GitHub Desktop.
Save mortenpi/0e3e031bf9d11a9ba6a6 to your computer and use it in GitHub Desktop.
A collection of helper macros and stuff for Julia's Compose and Gadfly packages.
# ComposeHacks module
#
# This module is a collection of hacks to make working with Compose and Gadfly
# more convenient (in Julia).
#
# The MIT License (MIT)
#
# Copyright (c) 2015 Morten Piibeleht
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
module ComposeHacks
import Compose
import Gadfly
import Compose.writemime
export @fixedaspect
export @noninteractive
# The @noninteractive effectively removes the javascript from Gadfly plots. The
# reason is that the the plots can make the environment (especially Juno) really
# laggy and usually the zoom feature is not needed. It works by wrapping the
# plot in a new NoninteractivePlot object and then overloading the
# Compose.writemime() function to make it use the SVG backend instead of the
# SVGJS backend.
macro noninteractive(plot)
:( NoninteractivePlot($(esc(plot))) )
end
type NoninteractivePlot
p::Gadfly.Plot
end
function writemime(io::IO, m::MIME"text/html", p::NoninteractivePlot)
buf = IOBuffer()
svg = Gadfly.SVG(buf, Compose.default_graphic_width,
Compose.default_graphic_height, false)
Gadfly.draw(svg, p.p)
writemime(io, m, svg)
end
# The @fixedaspect macro creates a backend with a fixed aspect ratio. It uses
# the Context's units to figure out the correct one. It can take either three or
# four arguments
# @fixedaspect backend context ipu
# @fixedaspect backend context filename ipu
# The arguments are the following
# - backend is the Compose backend (e.g. SVG, PNG)
# - context is the Context you want to take the aspect ratio from
# - filename is the output file name (or goes to Buffer if unspecified)
# - ipu is the "inches per unit", i.e. the size of the backend is going
# to be width (in Context units) * ipu
# Examples:
# @fixedaspect PNG c 5cm
# draw(@fixedaspect(PNG, c, "context.png", 5cm), c)
# Note: if you invoke the macro in the draw function (as in second the example)
# you need to use parens so that the second argument of draw() would not get
# used as an argument for the macro.
macro fixedaspect(args...)
if length(args) == 3
backend,context,ipu = args
:( $(esc(backend))(
$(esc(context)).units.width*$(esc(ipu)),
$(esc(context)).units.height*$(esc(ipu)))
)
elseif length(args) == 4
backend,context,filename,ipu = args
:( $(esc(backend))(
$(esc(filename)),
$(esc(context)).units.width*$(esc(ipu)),
$(esc(context)).units.height*$(esc(ipu)))
)
else
error("wrong number of arguments")
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment