@@ -31,7 +31,7 @@ BAR_DEFAULT = Dict{Symbol,Any}(:x => 0, :y => 0, :group => nothing,
3131 # data label
3232 :label => :none , # :height or :category
3333 :labelfont => nothing ,
34- :labelbaseline => :middle ,
34+ :labelbaseline => nothing ,
3535 :labelfontweight => nothing ,
3636 :labelitalic => nothing ,
3737 :labelsize => nothing ,
@@ -40,7 +40,7 @@ BAR_DEFAULT = Dict{Symbol,Any}(:x => 0, :y => 0, :group => nothing,
4040 :labeldir => :ltr ,
4141 :labellimit => nothing ,
4242 :labeloffset => 0 ,
43- :labelpos => :end , # :end, :start
43+ :labelpos => :end , # :end, :start, :middle
4444 :labelloc => 0.5 , # between 0 and 1
4545 :labeld3format => " " ,
4646 :labelopacity => 1 ,
@@ -65,6 +65,16 @@ mutable struct Bar <: SGMarks
6565 length (cp_BAR_DEFAULT[:barcorner ]) != 4 && throw (ArgumentError (" the barcorner option must be a single value or a vector of length four of values" ))
6666 end
6767 ! (cp_BAR_DEFAULT[:groupdisplay ] in (:stack , :none , :cluster , :step )) && throw (ArgumentError (" the groupdisplay option can be one of :stack, :cluster, :step, or :none" ))
68+ if cp_BAR_DEFAULT[:x ] == 0
69+ cp_BAR_DEFAULT[:labelbaseline ] = something (cp_BAR_DEFAULT[:labelbaseline ], :middle )
70+ _tmp_align = Dict (:end => :right , :start => :left , :middle => :center )
71+ cp_BAR_DEFAULT[:labelalign ] = something (cp_BAR_DEFAULT[:labelalign ], _tmp_align[cp_BAR_DEFAULT[:labelpos ]])
72+
73+ else
74+ _tmp_align = Dict (:end => :top , :start => :bottom , :middle => :middle )
75+ cp_BAR_DEFAULT[:labelbaseline ] = something (cp_BAR_DEFAULT[:labelbaseline ], _tmp_align[cp_BAR_DEFAULT[:labelpos ]])
76+ cp_BAR_DEFAULT[:labelalign ] = something (cp_BAR_DEFAULT[:labelalign ], :center )
77+ end
6878 new (cp_BAR_DEFAULT)
6979 end
7080end
@@ -512,8 +522,14 @@ function _segment_label!(mk, cat, var, all_args, opts)
512522 end
513523
514524 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'" )
525+ if opts[:labelcolor ] == :auto && opts[:group ] != = nothing
526+ mk_encode[:fill ] = Dict {Symbol, Any} (:signal => " isValid(datum['__height__bar__']) ? contrast('black', scale('group_scale', datum['$(opts[:group ]) '])) > contrast('white', scale('group_scale', datum['$(opts[:group ]) '])) ? 'black' : 'white' : 'transparent'" )
527+ else
528+ if opts[:labelcolor ] == :auto
529+ opts[:labelcolor ] = :black
530+ end
531+ mk_encode[:fill ] = Dict {Symbol, Any} (:signal => " isValid(datum['__height__bar__']) ? '$(opts[:labelcolor ]) ' : 'transparent'" )
532+ end
517533 mk_encode[:text ] = deepcopy (mk_encode[var])
518534 delete! (mk_encode[:text ], :scale )
519535 delete! (mk_encode[:text ], :field )
@@ -533,8 +549,10 @@ function _segment_label!(mk, cat, var, all_args, opts)
533549 mk_encode[var][:offset ] = opts[:labeloffset ]
534550 if opts[:labelpos ] == :end
535551 mk_encode[var][:field ] = " __height__bar__"
536- else
552+ elseif opts[ :labelpos ] == :start
537553 mk_encode[var][:field ] = " __height__bar__start__"
554+ elseif opts[:labelpos ] == :middle
555+ mk_encode[var][:signal ] = " (datum['__height__bar__'] + datum['__height__bar__start__'])/2"
538556 end
539557 delete! (mk_encode, Symbol (var,2 ))
540558
0 commit comments