Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tick marks to plots #792

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
96 changes: 63 additions & 33 deletions src/guide.jl
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ function render(guide::PanelBackground, theme::Gadfly.Theme,
rectangle(),
svgclass("guide background"),
stroke(theme.panel_stroke),
linewidth(theme.line_width),
fill(theme.panel_fill),
fillopacity(theme.panel_opacity),
svgattribute("pointer-events", "visible"))
Expand Down Expand Up @@ -607,29 +608,43 @@ function render(guide::XTicks, theme::Gadfly.Theme,
return PositionedGuide[]
end

# grid lines
# grid lines and tick marks
tick_length = 0.01*max(w,h)
static_grid_lines = compose!(
context(withoutjs=true),
line([[(t, 0h), (t, 1h)] for t in grids[gridvisibility]]),
stroke(theme.grid_color),
linewidth(theme.grid_line_width),
strokedash(theme.grid_strokedash),
svgclass("guide xgridlines yfixed"))
svgclass("guide xgridlines yfixed"),
(context(),
stroke(theme.tick_color),
line([[(t, 0h), (t, 0h + tick_length)] for t in grids[gridvisibility]]),
line([[(t, 1h), (t, 1h - tick_length)] for t in grids[gridvisibility]])),
(context(),
line([[(t, 0h), (t, 1h)] for t in grids[gridvisibility]]),
stroke(theme.grid_color),
strokedash(theme.grid_strokedash)))

if dynamic
dynamic_grid_lines = compose!(
context(withjs=true),
line([[(t, 0h), (t, 1h)] for t in grids]),
visible(gridvisibility),
stroke(theme.grid_color),
linewidth(theme.grid_line_width),
strokedash(theme.grid_strokedash),
svgclass("guide xgridlines yfixed"),
svgattribute("gadfly:scale", scale),
jsplotdata("focused_xgrid_color",
"\"#$(hex(theme.grid_color_focused))\""),
jsplotdata("unfocused_xgrid_color",
"\"#$(hex(theme.grid_color))\""))
visible(gridvisibility),
(context(),
(context(),
line([[(t, 0h), (t, 0h + tick_length)] for t in grids]),
line([[(t, 1h), (t, 1h - tick_length)] for t in grids])),
stroke(theme.tick_color),
svgclass("guide xgridlines yfixed"),
svgattribute("gadfly:scale", scale)),
(context(),
line([[(t, 0h), (t, 1h)] for t in grids]),
stroke(theme.grid_color),
strokedash(theme.grid_strokedash),
svgclass("guide xgridlines yfixed"),
svgattribute("gadfly:scale", scale),
jsplotdata("focused_xgrid_color",
"\"#$(hex(theme.grid_color_focused))\""),
jsplotdata("unfocused_xgrid_color",
"\"#$(hex(theme.grid_color))\"")))
grid_lines = compose!(context(), static_grid_lines, dynamic_grid_lines)
else
grid_lines = compose!(context(), static_grid_lines)
Expand Down Expand Up @@ -787,29 +802,44 @@ function render(guide::YTicks, theme::Gadfly.Theme,
return PositionedGuide[]
end

# grid lines
# grid lines and tick marks
tick_length = 0.01*max(w,h)
static_grid_lines = compose!(
context(withoutjs=true),
line([[(0w, t), (1w, t)] for t in grids[gridvisibility]]),
stroke(theme.grid_color),
linewidth(theme.grid_line_width),
strokedash(theme.grid_strokedash),
svgclass("guide ygridlines xfixed"))
svgclass("guide ygridlines xfixed"),
(context(),
stroke(theme.tick_color),
line([[(0w, t), (0w + tick_length, t)] for t in grids[gridvisibility]]),
line([[(1w, t), (1w - tick_length, t)] for t in grids[gridvisibility]])),
(context(),
line([[(0w, t), (1w, t)] for t in grids[gridvisibility]]),
stroke(theme.grid_color),
strokedash(theme.grid_strokedash)))


if dynamic
dynamic_grid_lines = compose!(
context(withjs=true),
line([[(0w, t), (1w, t)] for t in grids]),
visible(gridvisibility),
stroke(theme.grid_color),
linewidth(theme.grid_line_width),
strokedash(theme.grid_strokedash),
svgclass("guide ygridlines xfixed"),
svgattribute("gadfly:scale", scale),
jsplotdata("focused_ygrid_color",
"\"#$( hex(theme.grid_color_focused))\""),
jsplotdata("unfocused_ygrid_color",
"\"#$(hex(theme.grid_color))\""))
visible(gridvisibility),
(context(),
(context(),
line([[(0w, t), (0w + tick_length, t)] for t in grids]),
line([[(1w, t), (1w - tick_length, t)] for t in grids])),
stroke(theme.tick_color),
svgclass("guide ygridlines xfixed"),
svgattribute("gadfly:scale", scale)),
(context(),
line([[(0w, t), (1w, t)] for t in grids]),
stroke(theme.grid_color),
strokedash(theme.grid_strokedash),
svgclass("guide ygridlines xfixed"),
svgattribute("gadfly:scale", scale),
jsplotdata("focused_ygrid_color",
"\"#$(hex(theme.grid_color_focused))\""),
jsplotdata("unfocused_ygrid_color",
"\"#$(hex(theme.grid_color))\"")))
grid_lines = compose!(context(), static_grid_lines, dynamic_grid_lines)
else
grid_lines = compose!(context(), static_grid_lines)
Expand Down Expand Up @@ -856,7 +886,7 @@ function render(guide::YTicks, theme::Gadfly.Theme,
penalty=hpenalty), hlayout)

vlayout = ctxpromise() do draw_context
static_grid_lines = compose!(
static_labels = compose!(
context(),
text([1.0w - padding], ticks[tickvisibility], labels[tickvisibility],
[hcenter], [vbottom],
Expand All @@ -867,7 +897,7 @@ function render(guide::YTicks, theme::Gadfly.Theme,
fontsize(theme.minor_label_font_size),
svgclass("guide ylabels"))

dynamic_grid_lines = compose!(
dynamic_labels = compose!(
context(),
text([1.0w - padding], ticks, labels,
[hcenter], [vbottom],
Expand All @@ -880,7 +910,7 @@ function render(guide::YTicks, theme::Gadfly.Theme,
svgattribute("gadfly:scale", scale),
svgclass("guide ylabels"))

return compose!(context(), static_grid_lines, dynamic_grid_lines)
return compose!(context(), static_labels, dynamic_labels)
end
vlayout_context =
compose!(context(minwidth=maximum(label_heights[tickvisibility]),
Expand Down
8 changes: 5 additions & 3 deletions src/theme.jl
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ function default_middle_color(fill_color::TransparentColor)
fill_color.alpha)
end


get_stroke_vector(::@compat(Void)) = []
get_stroke_vector(vec::AbstractVector) = vec
function get_stroke_vector(linestyle::Symbol)
Expand All @@ -89,7 +89,7 @@ end
# Width of lines in the line geometry.
line_width, Measure, 0.3mm

# type of dash style (a Compose.StrokeDash object which takes a vector of sold/missing/solid/missing/...
# type of dash style (a Compose.StrokeDash object which takes a vector of sold/missing/solid/missing/...
# lengths which are applied cyclically)
line_style, Maybe(Vector), nothing

Expand Down Expand Up @@ -202,6 +202,9 @@ end

rug_size, Measure, 2.0mm

# Management of axis-margin ticks.
tick_color, ColorOrNothing, nothing

# TODO: This stuff is too incomprehensible to be in theme, I think. Put it
# somewhere else.

Expand All @@ -224,4 +227,3 @@ end


const default_theme = Theme()

3 changes: 3 additions & 0 deletions test/tick_color.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
using Gadfly

p = plot(x=rand(10), y=rand(10),Theme(panel_stroke=colorant"black", tick_color=colorant"black"))