function htmlBarh(filename,data,labels,maxWidth)
%htmlBarh Write the HTML for a horizontal stacked bar graph.
%
% HTMLBARH(DATA,FILENAME,LABELS,WIDTH) writes DATA out to FILENAME as an
% HTML fragment that creates a horizontal stacked bar graph labeled with
% the cell array LABELS. WIDTH scales the bar graph to be so many pixels
% wide. It returns the full pathname of the file.
%
% DATA defaults to "magic(5)". FILENAME defaults to barh.html. LABELS
% defaults to {'1','2', ... , 'n'}. WIDTH defaults to 400.
%
% Examples:
% web(htmlBarh)
% web(htmlBarh(rand(10,5)))
% web(htmlBarh(rand(10,5),'foo.html'))
% web(htmlBarh(rand(2,5),'foo.html',{'one','two'}))
% web(htmlBarh(rand(2,5),'foo.html',{'one','two'},200))
colormap = floor(jet(size(data,2))*255);
colors = cell(size(colormap,1),1);
for i = 1:size(colormap,1);
colors{i,1} = triple2rgb(colormap(i,1),colormap(i,2),colormap(i,3));
end
if (nargin > 3)
scaleFactor = 400/max(sum(data,2));
else
scaleFactor = 1;
end
f = fopen(filename,'w');
if (f==-1)
error('Cannot open file.');
end
fprintf(f,'<table cellspacing="2" cellpadding="0" border="0">\n');
for i = 1:size(data,1)
label = labels{i};
if length(label) > 30
label = [label(1:29) '…'];
end
label = strrep(label,' ',' ');
fprintf(f,'<tr><td align="right">%s </td><td>', label);
fprintf(f,'<table cellspacing="0" cellpadding="0"><tr valign="right">');
for j = 1:size(data,2)
if data(i,j) > 0
fprintf(f,'<td width="%.0f" bgcolor=%s></td>', ...
round(scaleFactor * data(i,j)),colors{j});
end
end
fprintf(f,'<td> %s</td>',num2str(sum(data(i,:))));
fprintf(f,'</tr></table>');
fprintf(f,'</td></tr>\n');
end
fprintf(f,'</table>\n');
fclose(f);
function s = triple2rgb(r,g,b)
s = [ '#' ...
dec2hex(r,2) ...
dec2hex(g,2) ...
dec2hex(b,2)];