change datatip for segments of stacked bar charts

11 次查看(过去 30 天)
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