vba - Excel Bar Chart - Same Color and Legend Entry for Same Names -


i chart states of machine on period of time. example may "running" 2 hours , "stopped" 1 hour, , there may several times each state occurs. using stacked bar chart i'd display state , amount of time stays in state. i'm finding excel assigning new color , legend entry each new state instance if state has occurred.
how can make same-named states within chart have same color (e.g. every time "running" displayed has same color , single legend entry)? thanks

the state name stored series name. there series each stack in chart. possible iterate through series , style them based on series name. possible remove entries legend using legendentries object.

combining these loop, can update series color if matches title , remove item legend if not 1 of first 2 series. assumes "running" , "stopped" alternate @ start , entries keep in legend. if not case, more logic spot entries keep.

sub style_chart()      dim cht chart     dim ser series      'uses active chart... assume selected     set cht = activechart      cht         'reset legend matches series         .haslegend = false         .haslegend = true          'iterate backwards delete         = .seriescollection.count 1 step -1             set ser = .seriescollection(i)              'set series colors based on name             if ser.name = "running"                 ser.format.fill.forecolor.rgb = rgb(0, 176, 80)             elseif ser.name = "stopped"                 ser.format.fill.forecolor.rgb = rgb(255, 0, 0)             end if              'delete legend entry if after first 2             if > 2                 .legend.legendentries(i).delete             end if         next     end  end sub 

before

before

after

after


Comments

Popular posts from this blog

javascript - AngularJS custom datepicker directive -

javascript - jQuery date picker - Disable dates after the selection from the first date picker -