change datatip for segments of stacked bar charts

조회 수: 17 (최근 30일)
endystrike
endystrike 2020년 6월 25일
답변: Seth Furman 2020년 10월 28일
Hi everyone! ;)
I'm struggling in trying to change data tips for segments of a stacked bar chart (and of course I'd like to change the format and label also for X data of the data tip itself).
From the following codes, I'd like to:
  • change the Y segment label to the corresponding name of the ticker from "all_i(idx)" (of course I'd do it for each element of included in "idx" vector)
  • change the format value of Y segment to '%.2f%%'
  • to change the label "X" to "Date"
  • change the format of the corresponding date as 'dd/MM/yyyy'
Two important notes:
  • "date_ax" is a datetime vector
  • "all_i" is a string vector
  • "idx" is a numeric vector
I'm sorry but reading the data tip help I could do everything on other types of charts, but I'm facing issues with stacked bar charts... :(
Thanks a lot for your precious help! :)
alloc_f = figure('units','normalized','position',[0 0 1 1]); %#ok<NASGU>
ax = bar(date_ax,100*stocks_glob_perc(1:end-1,idx),'stacked','BarWidth',1,'FaceColor','flat');
newColors = maxdistcolor(length(idx),@srgb_to_Jab,'sort','hue',...
'exc',[0,0,0; 1,1,1; 0.59,0.54,0.54; 0.30,0.24,0]);
for k=1:length(idx)
ax(k).CData = newColors(k,:);
end
xlim([date_ax(FirstDayOfPtot)-5 date_ax(end)+5]);
set(gca,'XTick',date_ax(EoYi+1));
ytickformat('percentage');
ylim([0 100]);
datetick('x','mm/YYYY','keeplimits','keepticks');
legend(all_i(idx),'Location','northwest');

채택된 답변

Seth Furman
Seth Furman 2020년 10월 28일
Take a look at the documentation for creating custom data tips.
x = datetime('2020-01-01'):days(1):datetime('2020-01-03');
x.Format = 'dd/MM/uuuu';
y = [2 2 3; 2 5 6; 2 8 9; 2 11 12];
b = bar(x,y,'stacked','BarWidth',1,'FaceColor','flat','LineStyle','none');
templates = [b.DataTipTemplate];
names = ["Name1","Name2","Name3","Name4"];
for i = 1:length(templates)
template = templates(i);
template.DataTipRows(1).Label = 'Date:';
template.DataTipRows(3).Label = names(i)+":";
template.DataTipRows(3).Format = '%.2f%%';
end

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Bar Plots에 대해 자세히 알아보기

제품


릴리스

R2019a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by