Skip to content

Commit c2a9861

Browse files
committed
add labelling feature to Bar
1 parent 0efc12f commit c2a9861

File tree

3 files changed

+97
-9
lines changed

3 files changed

+97
-9
lines changed

.github/workflows/ci.yml

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,15 @@ jobs:
1010
runs-on: ${{ matrix.os }}
1111
strategy:
1212
fail-fast: false
13-
# matrix:
14-
# version:
15-
# - '1.6'
16-
# - '1' # automatically expands to the latest stable 1.x release of Julia
13+
matrix:
14+
version:
15+
- '1.6'
16+
- '1' # automatically expands to the latest stable 1.x release of Julia
1717
# - 'nightly'
18-
# os:
18+
os:
1919
# - ubuntu-latest
20-
# arch:
21-
# - x64
20+
arch:
21+
- x64
2222
# include:
2323
# - os: windows-latest
2424
# version: '1'

src/charts/bar.jl

Lines changed: 88 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,27 @@ BAR_DEFAULT = Dict{Symbol,Any}(:x => 0, :y => 0, :group => nothing,
2727
:baseline => 0,
2828
:baselineresponse => nothing, # each bar (or each group when groupped) can have its own baseline
2929
:baselinestat => nothing, # same rule as :stat
30+
31+
#data label
32+
:label=>:none, # :height or :category
33+
:labelfont=>nothing,
34+
:labelbaseline=>:middle,
35+
:labelfontweight=>nothing,
36+
:labelitalic=>nothing,
37+
:labelsize=>nothing,
38+
:labelcolor=>:black,# allow :group, :colorresponse to use their color if available
39+
:labelangle=>nothing,
40+
:labeldir=>:ltr,
41+
:labellimit=>nothing,
42+
:labeloffset=>0,
43+
:labelpos => :end, # :end, :start
44+
:labelloc=>0.5, # between 0 and 1
45+
:labeld3format=>"",
46+
:labelopacity=>1,
47+
:labelalign=>nothing,
48+
:tooltip => false, # it can be true, only if labelresponse is provided
49+
50+
3051
:legend => nothing, :barcorner => [0, 0, 0, 0], #corner radius (cornerRadiusTopLeft, cornerRadiusTopRight, cornerRadiusBottomLeft, cornerRadiusBottomRight)
3152
:clip => nothing
3253
)
@@ -207,6 +228,12 @@ function _push_plots!(vspec, plt::Bar, all_args; idx=1)
207228
end
208229
s_spec[:marks] = [s_spec_marks]
209230
push!(vspec[:marks], s_spec)
231+
if opts[:label] in (:height, :category)
232+
whole_mk = deepcopy(s_spec)
233+
_segment_label!(whole_mk[:marks][1], _var_, _var_2_, all_args, opts)
234+
push!(vspec[:marks], whole_mk)
235+
end
236+
210237
end
211238

212239

@@ -472,4 +499,64 @@ function _add_legends!(plt::Bar, all_args, idx)
472499
_build_legen!(leg_spec_cp, leg_spec.opts, "square", _title, "$(legend_id)_color_scale_legend_$idx", all_args)
473500
push!(all_args.out_legends, leg_spec_cp)
474501
end
475-
end
502+
end
503+
504+
505+
function _segment_label!(mk, cat, var, all_args, opts)
506+
mk[:type] = "text"
507+
508+
mk_encode = mk[:encode][:enter]
509+
510+
for prop in [:stroke, :strokeWidth, :height, :width, :cornerRadiusBottomLeft, :cornerRadiusTopLeft, :cornerRadiusBottomRight, :cornerRadiusTopRight]
511+
delete!(mk_encode, prop)
512+
end
513+
514+
mk_encode[:opacity] = Dict{Symbol, Any}(:value => opts[:labelopacity])
515+
516+
mk_encode[:fill] = Dict{Symbol, Any}(:signal => "isValid(datum['__height__bar__']) ? '$(opts[:labelcolor])' : 'transparent'" )
517+
mk_encode[:text] = deepcopy(mk_encode[var])
518+
delete!(mk_encode[:text], :scale)
519+
delete!(mk_encode[:text], :field)
520+
delete!(mk_encode[var], :field)
521+
522+
if opts[:label] == :height
523+
mk_encode[:text][:signal] = "format(datum['__height__bar__'] - datum['__height__bar__start__'], '$(opts[:labeld3format])')"
524+
else
525+
if opts[:group] === nothing
526+
mk_encode[:text][:field] = "$(opts[cat])"
527+
else
528+
mk_encode[:text][:field] = "$(opts[:group])"
529+
end
530+
end
531+
532+
533+
mk_encode[var][:offset] = opts[:labeloffset]
534+
if opts[:labelpos] == :end
535+
mk_encode[var][:field] = "__height__bar__"
536+
else
537+
mk_encode[var][:field] = "__height__bar__start__"
538+
end
539+
delete!(mk_encode, Symbol(var,2))
540+
541+
mk_encode[cat][:band] = opts[:labelloc]
542+
543+
if opts[:labelangle] !== nothing
544+
mk_encode[:angle] = Dict{Symbol, Any}(:value => opts[:labelangle])
545+
end
546+
if opts[:labelalign] !== nothing
547+
mk_encode[:align] = Dict{Symbol, Any}(:value => opts[:labelalign])
548+
end
549+
mk_encode[:baseline] = Dict{Symbol, Any}(:value => opts[:labelbaseline])
550+
mk_encode[:font] = Dict{Symbol, Any}(:value => something(opts[:labelfont], all_args.opts[:font]))
551+
mk_encode[:fontWeight] = Dict{Symbol, Any}(:value => something(opts[:labelfontweight], all_args.opts[:fontweight]))
552+
mk_encode[:fontStyle] = Dict{Symbol, Any}(:value => something(opts[:labelitalic], all_args.opts[:italic] ? "italic" : "normal"))
553+
554+
if opts[:labelsize] !== nothing
555+
mk_encode[:fontSize] = Dict{Symbol, Any}(:value => opts[:labelsize])
556+
end
557+
mk_encode[:dir] = Dict{Symbol,Any}(:value => opts[:labeldir])
558+
if opts[:labellimit] !== nothing
559+
mk_encode[:limit] = Dict{Symbol,Any}(:value => opts[:labellimit])
560+
end
561+
mk
562+
end

src/util.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -848,7 +848,8 @@ function _modify_data_for_panel!(vspec, marks, info, idx)
848848
else
849849
needchange = false
850850
end
851-
if needchange
851+
# if the new_name already exist we do not need any change
852+
if needchange && !any(x->x[:name] == new_name, vspec[:data])
852853
push!(vspec[:data], Dict{Symbol, Any}(:name => new_name, :source => old_name, :transform => Dict{Symbol, Any}[Dict{Symbol, Any}(:type=>:filter, :expr => info["$(sg_col_prefix)__filtering_formula__"])]))
853854
end
854855
end

0 commit comments

Comments
 (0)