![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/395473/image.png)
change datatip for segments of stacked bar charts
4 views (last 30 days)
Show older comments
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');
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/321922/image.jpeg)
0 Comments
Accepted Answer
Seth Furman
on 28 Oct 2020
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
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/395473/image.png)
0 Comments
More Answers (0)
See Also
Categories
Find more on Bar Plots in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!