@@ -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+
210237end
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
0 commit comments