forked from GiovineItalia/Gadfly.jl
/
guide.jl
1122 lines (928 loc) · 36.8 KB
/
guide.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
module Guide
using Color
using Compose
using DataStructures
using Gadfly
using Iterators
using JSON
import Gadfly: render, escape_id, default_statistic, jsdata, jsplotdata,
svg_color_class_from_label
# Where the guide should be placed in relation to the plot.
abstract GuidePosition
immutable TopGuidePosition <: GuidePosition end
immutable RightGuidePosition <: GuidePosition end
immutable BottomGuidePosition <: GuidePosition end
immutable LeftGuidePosition <: GuidePosition end
immutable UnderGuidePosition <: GuidePosition end
immutable OverGuidePosition <: GuidePosition end
const top_guide_position = TopGuidePosition()
const right_guide_position = RightGuidePosition()
const bottom_guide_position = BottomGuidePosition()
const left_guide_position = LeftGuidePosition()
const under_guide_position = UnderGuidePosition()
const over_guide_position = OverGuidePosition()
# A guide graphic is a position associated with one or more contexts.
# Multiple contexts represent multiple layout possibilites that will be
# optimized over.
immutable PositionedGuide
ctxs::Vector{Context}
order::Int
position::GuidePosition
end
immutable PanelBackground <: Gadfly.GuideElement
end
const background = PanelBackground
function render(guide::PanelBackground, theme::Gadfly.Theme,
aes::Gadfly.Aesthetics)
back = compose!(context(order=-1),
rectangle(),
svgclass("guide background"),
stroke(theme.panel_stroke),
fill(theme.panel_fill),
fillopacity(theme.panel_opacity),
svgattribute("pointer-events", "visible"))
return [PositionedGuide([back], 0, under_guide_position)]
end
immutable ZoomSlider <: Gadfly.GuideElement
end
const zoomslider = ZoomSlider
function render(guide::ZoomSlider, theme::Gadfly.Theme,
aes::Gadfly.Aesthetics)
edge_pad = 3mm
slide_pad = 0.5mm
button_size = 4mm
slider_size = 20mm
background_color = "#eaeaea"
foreground_color = "#6a6a6a"
highlight_color = "#cd5c5c";
minus_button = compose!(
context(1w - edge_pad - 2*button_size - slider_size,
edge_pad, button_size, button_size),
rectangle(),
stroke(foreground_color),
strokeopacity(0.0),
linewidth(0.3mm),
{context(),
polygon([(0.2, 0.4), (0.8, 0.4),
(0.8, 0.6), (0.2, 0.6)]),
fill(foreground_color),
svgclass("button_logo")},
fill(background_color),
jscall(
"""
click(Gadfly.zoomslider_zoomout_click)
.mouseenter(Gadfly.zoomslider_button_mouseover)
.mouseleave(Gadfly.zoomslider_button_mouseout)
"""),
jsdata("mouseout_color", "\"$(foreground_color)\""),
jsdata("mouseover_color", "\"$(highlight_color)\""))
slider_width = 2mm
slider_xpos = 1w - edge_pad - button_size - slider_size + slide_pad
slider_min_pos = slider_xpos + slider_width/2
slider_max_pos = slider_xpos + slider_size - 2*slide_pad - slider_width/2
slider = compose!(
context(slider_xpos, edge_pad, slider_size - 2 * slide_pad, button_size),
{context(),
rectangle(),
fill(background_color),
jscall("click(Gadfly.zoomslider_track_click)"),
jsdata("min_pos", "%x", Measure[slider_min_pos]),
jsdata("max_pos", "%x", Measure[slider_max_pos])},
{context(order=1),
rectangle(0.5cx - slider_width/2, 0.0, slider_width, 1h),
fill(foreground_color),
svgclass("zoomslider_thumb"),
jscall(
"""
drag(Gadfly.zoomslider_thumb_dragmove,
Gadfly.zoomslider_thumb_dragstart,
Gadfly.zoomslider_thumb_dragend)
.mousedown(Gadfly.zoomslider_thumb_mousedown)
.mouseup(Gadfly.zoomslider_thumb_mouseup)
"""),
jsdata("mouseout_color", "\"$(foreground_color)\""),
jsdata("mouseover_color", "\"$(highlight_color)\""),
jsdata("min_pos", "%x", Measure[slider_min_pos]),
jsdata("max_pos", "%x", Measure[slider_max_pos])})
plus_button = compose!(
context(1w - edge_pad - button_size, edge_pad,
button_size, button_size),
rectangle(),
stroke(foreground_color),
strokeopacity(0.0),
linewidth(0.3mm),
{context(),
polygon([(0.2, 0.4), (0.4, 0.4), (0.4, 0.2),
(0.6, 0.2), (0.6, 0.4), (0.8, 0.4),
(0.8, 0.6), (0.6, 0.6), (0.6, 0.8),
(0.4, 0.8), (0.4, 0.6), (0.2, 0.6)]),
fill(foreground_color),
svgclass("button_logo")},
fill(background_color),
jscall(
"""
click(Gadfly.zoomslider_zoomin_click)
.mouseenter(Gadfly.zoomslider_button_mouseover)
.mouseleave(Gadfly.zoomslider_button_mouseout)
"""),
jsdata("mouseout_color", "\"$(foreground_color)\""),
jsdata("mouseover_color", "\"$(highlight_color)\""))
root = compose!(
context(withjs=true, units=UnitBox()),
minus_button,
slider,
plus_button,
stroke(nothing),
#stroke(foreground_color),
svgclass("guide zoomslider"),
fillopacity(0.0))
return [PositionedGuide([root], 0, over_guide_position)]
end
immutable ColorKey <: Gadfly.GuideElement
title::Union(String, Nothing)
function ColorKey(title=nothing)
new(title)
end
end
const colorkey = ColorKey
# A helper for render(::ColorKey) for rendering guides for discrete color
# scales.
function render_discrete_color_key(colors::Vector{ColorValue},
labels::Dict{ColorValue, String},
aes_color_label,
title_ctx::Context,
title_width::Measure,
theme::Gadfly.Theme)
# only consider layouts with a reasonable number of columns
maxcols = 4
n = length(colors)
extents = text_extents(theme.key_label_font,
theme.key_label_font_size,
values(labels)...)
title_height = title_ctx.box.height
entry_height = maximum([height for (width, height) in extents])
swatch_size = entry_height / 2
# return a context with a lyout of numcols columns
function make_layout(numcols)
colrows = Array(Int, numcols)
m = n
for i in 1:numcols
colrows[i] = min(m, iceil(n / numcols))
m -= colrows[i]
end
xpad = 1mm
colwidths = Array(Measure, numcols)
m = 0
for (i, nrows) in enumerate(colrows)
if m == n
colwidths[i] = 0mm
else
colwidth = maximum([width for (width, height) in extents[m+1:m+nrows]])
colwidth += swatch_size + 2xpad
colwidths[i] = colwidth
m += nrows
end
end
ctxwidth = sum(colwidths)
ctxheight = entry_height * colrows[1] + title_height
ctxp = ctxpromise() do draw_context
yoff = 0.5h - ctxheight/2
outerctx = context()
compose!(outerctx, {context(xpad, yoff), title_ctx})
ctx = context(0, yoff + title_height,
ctxwidth, ctxheight - title_height,
units=UnitBox(0, 0, 1, colrows[1]))
m = 0
xpos = 0w
for (i, nrows) in enumerate(colrows)
colwidth = colwidths[i]
if theme.colorkey_swatch_shape == :square
swatches_shapes = rectangle(
[xpad], [y*cy - swatch_size/2 for y in 1:nrows],
[swatch_size], [swatch_size])
elseif theme.colorkey_swatch_shape == :circle
swatches_shapes = circle([0.5cy], 1:nrows .- 0.5, [swatch_size/2])
end
cs = colors[m+1:m+nrows]
swatches = compose!(
context(),
swatches_shapes,
stroke(nothing),
fill(cs))
swatch_labels = compose!(
context(),
text([2xpad + swatch_size], [y*cy for y in 1:nrows],
collect(values(labels))[m+1:m+nrows], [hleft], [vcenter]),
font(theme.key_label_font),
fontsize(theme.key_label_font_size),
fill(theme.key_label_color))
col = compose!(context(xpos, yoff), swatches, swatch_labels)
if aes_color_label != nothing
classes = [svg_color_class_from_label(aes_color_label([c])[1]) for c in cs]
#class_jscalls = ["data(\"color_class\", \"$(c)\")"
#for c in classes]
compose!(col,
svgclass(classes),
jscall(["""
data(\"color_class\", \"$(c)\")
.click(Gadfly.colorkey_swatch_click)
""" for c in classes]))
end
compose!(ctx, col)
m += nrows
xpos += colwidths[i]
end
return compose!(outerctx, ctx,
# defeat webkit's asinine default drag behavior
jscall("drag(function() {}, function() {}, function() {})"),
svgclass("guide colorkey"))
end
return compose!(
context(minwidth=max(title_width, ctxwidth),
minheight=ctxheight,
units=UnitBox()),
ctxp)
end
return map(make_layout, 1:maxcols)
end
# A helper for render(::ColorKey) for rendering guides for continuous color
# scales.
function render_continuous_color_key(colors::Dict,
labels::Dict{ColorValue, String},
color_function::Function,
title_context::Context,
title_width::Measure,
theme::Gadfly.Theme)
entry_width, entry_height = max_text_extents(theme.key_label_font,
theme.key_label_font_size,
values(labels)...)
numlabels = length(labels)
title_height = title_context.box.height
total_height = 1.5 * numlabels * entry_height + title_height
swatch_width = entry_height / 2
xoff = 2mm
padding = 1mm
entry_width += 2padding + swatch_width + xoff
ctx = context(minwidth=max(title_width, entry_width),
minheight=total_height, units=UnitBox())
yoff = 0.5h - total_height/2
compose!(ctx, {context(xoff, yoff), title_context})
# color bar
compose!(ctx,
{context(xoff, yoff + title_height,
1w, total_height, units=UnitBox()),
rectangle(
[0],
[1*cy - i*total_height / theme.key_color_gradations
for i in 1:theme.key_color_gradations],
[swatch_width],
[total_height / theme.key_color_gradations]),
#grid lines
{context(),
line([{(0, 1 - y), (swatch_width, 1 - y)} for y in values(colors)]),
linewidth(theme.grid_line_width),
stroke(color("white"))},
fill([color_function((i-1) / (theme.key_color_gradations - 1))
for i in 1:theme.key_color_gradations]),
stroke(nothing),
svgattribute("shape-rendering", "crispEdges")})
compose!(ctx,
{context(xoff + swatch_width + padding, yoff + title_height,
1w, total_height, units=UnitBox()),
text([0],
[1 - y for y in values(colors)],
[labels[c] for c in keys(colors)],
[hleft], [vcenter]),
fill(theme.key_label_color),
font(theme.key_label_font),
fontsize(theme.key_label_font_size)},
svgclass("guide colorkey"))
return [ctx]
end
function render_colorkey_title(title::String, theme::Gadfly.Theme)
title_width, title_height = max_text_extents(theme.key_title_font,
theme.key_title_font_size,
title)
if theme.guide_title_position == :left
title_form = text(0.0w, title_height, title, hleft, vbottom)
elseif theme.guide_title_position == :center
title_form = text(0.5w, title_height, title, hcenter, vbottom)
elseif theme.guide_title_position == :right
title_form = text(1.0w, title_height, title, hright, vbottom)
else
error("$(theme.guide_title_position) is not a valid guide title position")
end
title_padding = 4mm
title_context = compose!(
context(0w, 0h, 1w, title_height + title_padding),
title_form,
stroke(nothing),
font(theme.key_title_font),
fontsize(theme.key_title_font_size),
fill(theme.key_title_color))
return title_context, title_width
end
function render(guide::ColorKey, theme::Gadfly.Theme,
aes::Gadfly.Aesthetics)
if theme.key_position == :none
return PositionedGuide[]
end
used_colors = Set{ColorValue}()
colors = Array(ColorValue, 0) # to preserve ordering
labels = Dict{ColorValue, Set{String}}()
continuous_guide = false
guide_title = guide.title
if guide_title === nothing && !is(aes.color_key_title, nothing)
guide_title = aes.color_key_title
end
if aes.color_key_colors != nothing &&
aes.color_key_continuous != nothing &&
aes.color_key_continuous
continuous_guide = true
end
color_key_labels = aes.color_label(keys(aes.color_key_colors))
for (color, label) in zip(keys(aes.color_key_colors), color_key_labels)
if !in(color, used_colors)
push!(used_colors, color)
push!(colors, color)
labels[color] = Set{String}()
push!(labels[color], label)
else
push!(labels[color], label)
end
end
if guide_title === nothing
guide_title = "Color"
end
pretty_labels = Dict{ColorValue, String}()
for (color, label) in labels
pretty_labels[color] = join(labels[color], ", ")
end
title_context, title_width = render_colorkey_title(guide_title, theme)
if theme.colorkey_swatch_shape != :circle &&
theme.colorkey_swatch_shape != :square
error("$(theme.colorkey_swatch_shape) is not a valid color key swatch shape")
end
if continuous_guide
ctxs = render_continuous_color_key(aes.color_key_colors,
pretty_labels,
aes.color_function,
title_context,
title_width, theme)
else
ctxs = render_discrete_color_key(colors, pretty_labels,
aes.color_label,
title_context,
title_width, theme)
end
position = right_guide_position
if theme.key_position == :left
position = left_guide_position
elseif theme.key_position == :right
position = right_guide_position
elseif theme.key_position == :top
position = top_guide_position
elseif theme.key_position == :bottom
position = bottom_guide_position
end
return [PositionedGuide(ctxs, 0, position)]
end
immutable ManualColorKey <: Gadfly.GuideElement
title::Union(String, Nothing)
labels::Vector{String}
colors::Vector
end
const manual_color_key = ManualColorKey
function render(guide::ManualColorKey, theme::Gadfly.Theme,
aes::Gadfly.Aesthetics)
if theme.key_position == :none
return PositionedGuide[]
end
guide_title = guide.title
if guide_title === nothing && !is(aes.color_key_title, nothing)
guide_title = aes.color_key_title
end
if guide_title === nothing
guide_title = "Color"
end
title_context, title_width = render_colorkey_title(guide_title, theme)
colors = ColorValue[color(c) for c in guide.colors]
labels = (ColorValue => String)[c => l for (c, l) in zip(colors, guide.labels)]
ctxs = render_discrete_color_key(colors, labels, title_context, title_width, theme)
position = right_guide_position
if theme.key_position == :left
position = left_guide_position
elseif theme.key_position == :right
position = right_guide_position
elseif theme.key_position == :top
position = top_guide_position
elseif theme.key_position == :bottom
position = bottom_guide_position
end
return [PositionedGuide(ctxs, 0, position)]
end
immutable XTicks <: Gadfly.GuideElement
label::Bool
ticks::Union(Nothing, AbstractArray)
orientation::Symbol
function XTicks(; label::Bool=true,
ticks::Union(Nothing, AbstractArray)=nothing,
orientation::Symbol=:auto)
return new(label, ticks, orientation)
end
end
const xticks = XTicks
function default_statistic(guide::XTicks)
return Stat.xticks(guide.ticks)
end
function render(guide::XTicks, theme::Gadfly.Theme,
aes::Gadfly.Aesthetics)
if Gadfly.issomething(aes.xtick)
ticks = aes.xtick
tickvisibility = aes.xtickvisible
scale = aes.xtickscale
T = eltype(aes.xtick)
labels = String[]
for scale_ticks in groupby(zip(scale, ticks), x -> x[1])
append!(labels, aes.xtick_label(T[t for (s, t) in scale_ticks]))
end
else
labels = String[]
ticks = {}
tickvisibility = {}
scale = {}
end
if Gadfly.issomething(aes.xgrid)
grids = aes.xgrid
if length(grids) < length(ticks)
gridvisibility = tickvisibility[2:end]
else
gridvisibility = tickvisibility
end
else
grids = {}
gridvisibility = {}
end
# grid lines
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"))
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))\""))
grid_lines = compose!(context(), static_grid_lines, dynamic_grid_lines)
if !guide.label
return [PositionedGuide([grid_lines], 0, under_guide_position)]
end
label_sizes = text_extents(theme.minor_label_font,
theme.minor_label_font_size,
labels...)
label_widths = [width for (width, height) in label_sizes]
label_heights = [height for (width, height) in label_sizes]
padding = 1mm
hlayout = ctxpromise() do draw_context
static_labels = compose!(
context(withoutjs=true),
text(ticks[tickvisibility], [1h + padding], labels[tickvisibility],
[hcenter], [vbottom]),
fill(theme.minor_label_color),
font(theme.minor_label_font),
fontsize(theme.minor_label_font_size),
svgclass("guide xlabels"))
dynamic_labels = compose!(
context(withjs=true),
text(ticks, [1h + padding], labels, [hcenter], [vbottom]),
visible(tickvisibility),
fill(theme.minor_label_color),
font(theme.minor_label_font),
fontsize(theme.minor_label_font_size),
svgattribute("gadfly:scale", scale),
svgclass("guide xlabels"))
return compose!(context(), static_labels, dynamic_labels)
end
hlayout_context = compose!(context(minwidth=sum(label_widths[tickvisibility]),
minheight=maximum(label_heights[tickvisibility])),
hlayout)
vlayout = ctxpromise() do draw_context
static_labels = compose!(
context(withoutjs=true),
text(ticks[tickvisibility],
[padding],
labels[tickvisibility],
[hright], [vcenter],
[Rotation(-0.5pi, tick, padding) for tick in ticks[tickvisibility]]),
fill(theme.minor_label_color),
font(theme.minor_label_font),
fontsize(theme.minor_label_font_size),
svgclass("guide xlabels"))
dynamic_labels = compose!(
context(withjs=true),
text(ticks, [padding], labels, [hright], [vbottom],
[Rotation(-0.5pi, tick, padding) for tick in ticks]),
visible(tickvisibility),
fill(theme.minor_label_color),
font(theme.minor_label_font),
fontsize(theme.minor_label_font_size),
svgattribute("gadfly:scale", scale),
svgclass("guide xlabels"))
return compose!(context(), static_labels, dynamic_labels)
end
vpenalty = 1mm # don't be too eager to flip the x-axis labels
vlayout_context = compose!(context(minwidth=sum(label_heights[tickvisibility]),
minheight=vpenalty + maximum(label_widths[tickvisibility])),
vlayout)
if guide.orientation == :horizontal
contexts = [hlayout_context]
elseif guide.orientation == :vertical
contexts = [vlayout_context]
elseif guide.orientation == :auto
contexts = [hlayout_context, vlayout_context]
else
error("$(guide.layout) is not a valid orientation for Guide.yticks")
end
return [PositionedGuide(contexts, 10,
bottom_guide_position),
PositionedGuide([grid_lines], 0, under_guide_position)]
end
immutable YTicks <: Gadfly.GuideElement
label::Bool
ticks::Union(Nothing, AbstractArray)
orientation::Symbol
function YTicks(; label::Bool=true,
ticks::Union(Nothing, AbstractArray)=nothing,
orientation::Symbol=:horizontal)
new(label, ticks, orientation)
end
end
const yticks = YTicks
function default_statistic(guide::YTicks)
Stat.yticks(guide.ticks)
end
function render(guide::YTicks, theme::Gadfly.Theme,
aes::Gadfly.Aesthetics)
if Gadfly.issomething(aes.ytick)
ticks = aes.ytick
tickvisibility = aes.ytickvisible
scale = aes.ytickscale
T = eltype(aes.ytick)
labels = String[]
for scale_ticks in groupby(zip(scale, ticks), x -> x[1])
append!(labels, aes.ytick_label(T[t for (s, t) in scale_ticks]))
end
else
labels = String[]
ticks = {}
tickvisibility = {}
scale = {}
end
if Gadfly.issomething(aes.ygrid)
grids = aes.ygrid
if length(grids) < length(ticks)
gridvisibility = tickvisibility[2:end]
else
gridvisibility = tickvisibility
end
else
grids = {}
gridvisibility = {}
end
# grid lines
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"))
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))\""))
grid_lines = compose!(context(), static_grid_lines, dynamic_grid_lines)
if !guide.label
return [PositionedGuide([grid_lines], 0, under_guide_position)]
end
label_sizes = text_extents(theme.minor_label_font,
theme.minor_label_font_size,
labels...)
label_widths = [width for (width, height) in label_sizes]
label_heights = [height for (width, height) in label_sizes]
padding = 1mm
hlayout = ctxpromise() do draw_context
static_labels = compose!(
context(withoutjs=true),
text([1.0w - padding], ticks[tickvisibility], labels[tickvisibility],
[hright], [vcenter]),
fill(theme.minor_label_color),
font(theme.minor_label_font),
fontsize(theme.minor_label_font_size),
svgclass("guide ylabels"))
dynamic_labels = compose!(
context(withjs=true),
text([1.0w - padding], ticks, labels,
[hright], [vcenter]),
visible(tickvisibility),
fill(theme.minor_label_color),
font(theme.minor_label_font),
fontsize(theme.minor_label_font_size),
svgattribute("gadfly:scale", scale),
svgclass("guide ylabels"))
return compose!(context(), static_labels, dynamic_labels)
end
hlayout_context = compose!(context(minwidth=maximum(label_widths[tickvisibility]),
minheight=sum(label_heights[tickvisibility])),
hlayout)
vlayout = ctxpromise() do draw_context
static_grid_lines = compose!(
context(),
text([1.0w - padding], ticks[tickvisibility], labels[tickvisibility],
[hcenter], [vbottom],
[Rotation(-0.5pi, (1.0w - padding, tick))
for tick in ticks[tickvisibility]]),
fill(theme.minor_label_color),
font(theme.minor_label_font),
fontsize(theme.minor_label_font_size),
svgclass("guide ylabels"))
dynamic_grid_lines = compose!(
context(),
text([1.0w - padding], ticks, labels,
[hcenter], [vbottom],
[Rotation(-0.5pi, (1.0w - padding, tick))
for tick in ticks[tickvisibility]]),
visible(tickvisibility),
fill(theme.minor_label_color),
font(theme.minor_label_font),
fontsize(theme.minor_label_font_size),
svgattribute("gadfly:scale", scale),
svgclass("guide ylabels"))
return compose!(contetx(), static_grid_lines, dynamic_grid_lines)
end
vlayout_context =
compose!(context(minwidth=maximum(label_heights[tickvisibility]),
minheight=sum(label_widths[tickvisibility])),
vlayout)
if guide.orientation == :horizontal
contexts = [hlayout_context]
elseif guide.orientation == :vertical
contexts = [vlayout_context]
elseif guide.orientation == :auto
contexts = [hlayout_context, vlayout_context]
else
error("$(guide.layout) is not a valid orientation for Guide.yticks")
end
return [PositionedGuide(contexts, 10,
left_guide_position),
PositionedGuide([grid_lines], 0, under_guide_position)]
end
# X-axis label Guide
immutable XLabel <: Gadfly.GuideElement
label::Union(Nothing, String)
orientation::Symbol
function XLabel(label; orientation::Symbol=:auto)
return new(label, orientation)
end
end
const xlabel = XLabel
function render(guide::XLabel, theme::Gadfly.Theme,
aes::Gadfly.Aesthetics)
if guide.label === nothing || isempty(guide.label)
return nothing
end
text_width, text_height = max_text_extents(theme.major_label_font,
theme.major_label_font_size,
guide.label)
padding = 3mm
hlayout = ctxpromise() do draw_context
return compose!(context(),
text(0.5w, 1h - padding, guide.label, hcenter, vbottom),
stroke(nothing),
fill(theme.major_label_color),
font(theme.major_label_font),
fontsize(theme.major_label_font_size))
end
hlayout_context = compose!(context(minwidth=text_width + 2padding,
minheight=text_height + 2padding),
hlayout)
vlayout = ctxpromise() do draw_context
return compose!(context(),
text(0.5w, padding, guide.label, hright, vcenter,
Rotation(-0.5pi, 0.5w, padding)),
stroke(nothing),
fill(theme.major_label_color),
font(theme.major_label_font),
fontsize(theme.major_label_font_size))
end
vlayout_context = compose!(context(minwidth=text_height + 2padding,
minheight=text_width + 2padding),
vlayout)
if guide.orientation == :horizontal
contexts = [hlayout_context]
elseif guide.orientation == :vertical
contexts = [vlayout_context]
elseif guide.orientation == :auto
contexts = [hlayout_context, vlayout_context]
else
error("$(guide.layout) is not a valid orientation for Guide.xlabel")
end
return [PositionedGuide(contexts, 0, bottom_guide_position)]
end
# Y-axis label Guide
immutable YLabel <: Gadfly.GuideElement
label::Union(Nothing, String)
orientation::Symbol
function YLabel(label; orientation::Symbol=:auto)
return new(label, orientation)
end
end
const ylabel = YLabel
function render(guide::YLabel, theme::Gadfly.Theme, aes::Gadfly.Aesthetics)
if guide.label === nothing || isempty(guide.label)
return nothing
end
text_width, text_height = max_text_extents(theme.major_label_font,
theme.major_label_font_size,
guide.label)
padding = 2mm
hlayout = ctxpromise() do draw_context
return compose!(context(),
text(1.0w - padding, 0.5h, guide.label, hright, vcenter),
stroke(nothing),
fill(theme.major_label_color),
font(theme.major_label_font),
fontsize(theme.major_label_font_size))
end
hlayout_context = compose!(context(minwidth=text_width + 2padding,
minheight=text_height + 2padding), hlayout)
vlayout = ctxpromise() do draw_context
return compose!(context(),
text(0.5w, 0.5h - padding, guide.label, hcenter, vcenter, Rotation(-0.5pi)),
stroke(nothing),
fill(theme.major_label_color),
font(theme.major_label_font),
fontsize(theme.major_label_font_size))
end
vlayout_context = compose!(context(minwidth=text_height + 2padding,
minheight=text_width + 2padding), vlayout)
if guide.orientation == :horizontal
contexts = [hlayout_context]
elseif guide.orientation == :vertical
contexts = [vlayout_context]
elseif guide.orientation == :auto
contexts = [hlayout_context, vlayout_context]
else
error("$(guide.layout) is not a valid orientation for Guide.ylabel")
end
return [PositionedGuide(contexts, 0, left_guide_position)]
end
# Title Guide
immutable Title <: Gadfly.GuideElement
label::Union(Nothing, String)
end
const title = Title
function render(guide::Title, theme::Gadfly.Theme,
aes::Gadfly.Aesthetics)
if guide.label === nothing || isempty(guide.label)
return nothing
end
(text_width, text_height) = max_text_extents(theme.major_label_font,
theme.major_label_font_size,
guide.label)
padding = 2mm
ctx = compose!(
context(minwidth=text_width, minheight=text_height + 2padding),
text(0.5w, 1h - padding, guide.label, hcenter, vbottom),
stroke(nothing),
fill(theme.major_label_color),
font(theme.major_label_font),
fontsize(theme.major_label_font_size))
return [PositionedGuide([ctx], 0, top_guide_position)]
end