Skip to content

Commit

Permalink
Legend system overhaul
Browse files Browse the repository at this point in the history
- Common function for legend drawing
- New colormaps
  • Loading branch information
piermorel committed Nov 1, 2017
1 parent f18c993 commit 0e12b90
Show file tree
Hide file tree
Showing 5 changed files with 281 additions and 168 deletions.
192 changes: 55 additions & 137 deletions @gramm/draw.m
Original file line number Diff line number Diff line change
Expand Up @@ -649,6 +649,7 @@

% ensuring grouping variables are cellstrs
str_uni_color = cellfun(@num2str,uni_color,'UniformOutput',false);
str_uni_lightness = cellfun(@num2str,uni_lightness,'UniformOutput',false);
str_uni_linestyle = cellfun(@num2str,uni_linestyle,'UniformOutput',false);
str_uni_size = cellfun(@num2str,uni_size,'UniformOutput',false);
str_uni_marker = cellfun(@num2str,uni_marker,'UniformOutput',false);
Expand All @@ -661,6 +662,7 @@
'Parent',obj.parent);
hold on
set(obj.legend_axe_handle,'Visible','off','NextPlot','add');
%set(obj.legend_axe_handle,'PlotBoxAspectRatio',[1 1 1]);
else
axes(obj.legend_axe_handle)
end
Expand All @@ -669,74 +671,42 @@
legend_y_additional_step=0.5;

if obj.with_legend

% Color legend
% If the color groups are the same as marker, linestyle or size groups
% then there will be a common legend (handled by the marker, linestyle
% or size legend).
if length(str_uni_color)>1 && ...
~(length(str_uni_color)==length(str_uni_marker) && all(strcmp(str_uni_color, str_uni_marker))) && ...
~(length(str_uni_color)==length(str_uni_linestyle) && all(strcmp(str_uni_color, str_uni_linestyle))) && ...
~(length(str_uni_color)==length(str_uni_size) && all(strcmp(str_uni_color, str_uni_size)))
~(length(str_uni_color)==length(str_uni_marker) && all(strcmp(str_uni_color, str_uni_marker))) && ...
~(length(str_uni_color)==length(str_uni_linestyle) && all(strcmp(str_uni_color, str_uni_linestyle))) && ...
~(length(str_uni_color)==length(str_uni_size) && all(strcmp(str_uni_color, str_uni_size)))

%Make a colormap with only the colors and no lightness
color_legend_map=get_colormap(length(str_uni_color),1,obj.color_options);

obj.legend_text_handles=[obj.legend_text_handles...
text(1,obj.legend_y,obj.aes_names.color,...
'FontWeight','bold',...
'Interpreter',obj.text_options.interpreter,...
'FontName',obj.text_options.font,...
'FontSize',obj.text_options.base_size*obj.text_options.legend_title_scaling,...
'Parent',obj.legend_axe_handle)];
obj.legend_y=obj.legend_y-legend_y_step;
for ind_color=1:length(str_uni_color)
plot([1 2],[obj.legend_y obj.legend_y],'-','Color',color_legend_map(ind_color,:),'lineWidth',3,'Parent',obj.legend_axe_handle)
%line(1.5,obj.legend_y,'lineStyle','none','Marker','s','MarkerSize',12,'MarkerFaceColor',color_legend_map(ind_color,:),'MarkerEdgeColor','none')
%rectangle('Position',[1.25 obj.legend_y-0.25 0.5 0.5],'EdgeColor','none','FaceColor',color_legend_map(ind_color,:));
obj.legend_text_handles=[obj.legend_text_handles...
text(2.5,obj.legend_y,str_uni_color{ind_color},...
'FontName',obj.text_options.font,...
'FontSize',obj.text_options.base_size*obj.text_options.legend_scaling,...
'Interpreter',obj.text_options.interpreter,...
'Parent',obj.legend_axe_handle)];
obj.legend_y=obj.legend_y-legend_y_step;
end
fill_legend(obj,obj.aes_names.color,...
str_uni_color,...
'line',...
color_legend_map,...
'o','-',3,0);
end


%Lightness legend
if length(uni_lightness)>1
obj.legend_y=obj.legend_y-legend_y_additional_step;

lightness_legend_map=pa_LCH2RGB([linspace(obj.color_options.lightness_range(1),obj.color_options.lightness_range(2),length(uni_lightness))' ...
zeros(length(uni_lightness),1)...
zeros(length(uni_lightness),1)]);
obj.legend_text_handles=[obj.legend_text_handles...
text(1,obj.legend_y,obj.aes_names.lightness,...
'FontWeight','bold',...
'Interpreter',obj.text_options.interpreter,...
'FontName',obj.text_options.font,...
'FontSize',obj.text_options.base_size*obj.text_options.legend_title_scaling,...
'Parent',obj.legend_axe_handle)];

obj.legend_y=obj.legend_y-legend_y_step;
for ind_lightness=1:length(uni_lightness)
plot([1 2],[obj.legend_y obj.legend_y],'-','Color',lightness_legend_map(ind_lightness,:),'lineWidth',3,'Parent',obj.legend_axe_handle)
%line(1.5,obj.legend_y,'lineStyle','none','Marker','s','MarkerSize',12,'MarkerFaceColor',lightness_legend_map(ind_lightness,:),'MarkerEdgeColor','none')

obj.legend_text_handles=[obj.legend_text_handles...
text(2.5,obj.legend_y,num2str(uni_lightness{ind_lightness}),...
'FontName',obj.text_options.font,...
'FontSize',obj.text_options.base_size*obj.text_options.legend_scaling,...
'Interpreter',obj.text_options.interpreter,...
'Parent',obj.legend_axe_handle)];

obj.legend_y=obj.legend_y-legend_y_step;
end
fill_legend(obj,obj.aes_names.lightness,...
str_uni_lightness,...
'line',...
lightness_legend_map,...
'o','-',3,0);
end

%Continuous color legend
if obj.continuous_color_options.active
obj.legend_y=obj.legend_y-legend_y_additional_step;

obj.legend_text_handles=[obj.legend_text_handles...
text(1,obj.legend_y,obj.aes_names.color,...
Expand Down Expand Up @@ -790,138 +760,86 @@
'Parent',obj.legend_axe_handle)];

obj.legend_y=obj.legend_y-legend_y_step*(gradient_height+1);
obj.legend_y=obj.legend_y-legend_y_additional_step;
end

%marker legend
if length(str_uni_marker)>1
obj.legend_y=obj.legend_y-legend_y_additional_step;


% If marker groups are the same as color groups we combine the
% legends by drawing each marker legend the corresponding color
if length(str_uni_color)==length(str_uni_marker) && all(strcmp(str_uni_color, str_uni_marker))
color_legend_map = get_colormap(length(str_uni_marker), 1, obj.color_options);
else
color_legend_map = zeros(length(str_uni_marker), 3); %Otherwise in black
color_legend_map = [0 0 0]; %zeros(length(str_uni_marker), 3); %Otherwise in black
end

obj.legend_text_handles=[obj.legend_text_handles...
text(1,obj.legend_y,obj.aes_names.marker,...
'FontWeight','bold',...
'Interpreter',obj.text_options.interpreter,...
'FontName',obj.text_options.font,...
'FontSize',obj.text_options.base_size*obj.text_options.legend_title_scaling,...
'Parent',obj.legend_axe_handle)];

obj.legend_y=obj.legend_y-legend_y_step;
for ind_marker=1:length(str_uni_marker)
plot(1.5,obj.legend_y,obj.point_options.markers{ind_marker},'MarkerEdgeColor','none','MarkerFaceColor', color_legend_map(ind_marker, :), 'Parent',obj.legend_axe_handle)

obj.legend_text_handles=[obj.legend_text_handles...
text(2.5,obj.legend_y,str_uni_marker{ind_marker},...
'Interpreter',obj.text_options.interpreter,...
'FontName',obj.text_options.font,...
'FontSize',obj.text_options.base_size*obj.text_options.legend_scaling,...
'Parent',obj.legend_axe_handle)];

obj.legend_y=obj.legend_y-legend_y_step;
end
fill_legend(obj,obj.aes_names.marker,...
str_uni_marker,...
'point',...
color_legend_map,...
obj.point_options.markers,'-',0,obj.point_options.base_size);
end

%linestyle legend
if length(str_uni_linestyle)>1
obj.legend_y=obj.legend_y-legend_y_additional_step;

% If linestyle groups are the same as color groups we combine the
% legends by drawing each linestyle legend the corresponding color
if length(str_uni_color)==length(str_uni_linestyle) && all(strcmp(str_uni_color, str_uni_linestyle))
color_legend_map = get_colormap(length(str_uni_linestyle), 1, obj.color_options);
else
color_legend_map = zeros(length(str_uni_linestyle), 3); %Otherwise in black
color_legend_map = [0 0 0]; %Otherwise in black
end

obj.legend_text_handles=[obj.legend_text_handles...
text(1,obj.legend_y,obj.aes_names.linestyle,...
'FontWeight','bold',...
'Interpreter',obj.text_options.interpreter,...
'FontName',obj.text_options.font,...
'FontSize',obj.text_options.base_size*obj.text_options.legend_title_scaling,...
'Parent',obj.legend_axe_handle)];

obj.legend_y=obj.legend_y-legend_y_step;
for ind_linestyle=1:length(str_uni_linestyle)
plot([1 2],[obj.legend_y obj.legend_y],obj.line_options.styles{ind_linestyle}, 'Color', color_legend_map(ind_linestyle, :) ,'Parent',obj.legend_axe_handle,'lineWidth',3)

obj.legend_text_handles=[obj.legend_text_handles...
text(2.5,obj.legend_y,str_uni_linestyle{ind_linestyle},...
'Interpreter',obj.text_options.interpreter,...
'FontName',obj.text_options.font,...
'FontSize',obj.text_options.base_size*obj.text_options.legend_scaling,...
'Parent',obj.legend_axe_handle)];

obj.legend_y=obj.legend_y-legend_y_step;
end

fill_legend(obj,obj.aes_names.linestyle,...
str_uni_linestyle,...
'line',...
color_legend_map,...
'o',obj.line_options.styles,3,0);
end

%Size legend
if length(str_uni_size)>1
obj.legend_y=obj.legend_y-legend_y_additional_step;


% If size groups are the same as color groups we combine the
% legends by drawing each size legend the corresponding color
if length(str_uni_color)==length(str_uni_size) && all(strcmp(str_uni_color, str_uni_size))
color_legend_map = get_colormap(length(str_uni_size), 1, obj.color_options);
else
color_legend_map = zeros(length(str_uni_size), 3); %Otherwise in black
color_legend_map = [0 0 0]; %Otherwise in black
end


obj.legend_text_handles=[obj.legend_text_handles...
text(1,obj.legend_y,obj.aes_names.size,...
'FontWeight','bold',...
'Interpreter',obj.text_options.interpreter,...
'FontName',obj.text_options.font,...
'FontSize',obj.text_options.base_size*obj.text_options.legend_title_scaling,...
'Parent',obj.legend_axe_handle)];

obj.legend_y=obj.legend_y-legend_y_step;
for ind_size=1:length(str_uni_size)

if obj.line_options.use_input
temp_lw=obj.line_options.input_fun(uni_size{ind_size});
else
temp_lw=obj.line_options.base_size+(ind_size-1)*obj.line_options.step_size;
end

plot([1 2],[obj.legend_y obj.legend_y],'lineWidth',temp_lw,...
'Color',color_legend_map(ind_size,:),'Parent',obj.legend_axe_handle)

if obj.point_options.use_input
temp_ps=obj.point_options.input_fun(uni_size{ind_size});
else
temp_ps=obj.point_options.base_size+(ind_size-1)*obj.point_options.step_size;
end
plot(1,obj.legend_y,'o','markerSize',temp_ps,...
'MarkerEdgeColor','none','MarkerFaceColor',color_legend_map(ind_size,:),'Parent',obj.legend_axe_handle)

obj.legend_text_handles=[obj.legend_text_handles...
text(2.5,obj.legend_y,str_uni_size{ind_size},...
'Interpreter',obj.text_options.interpreter,...
'FontName',obj.text_options.font,...
'FontSize',obj.text_options.base_size*obj.text_options.legend_scaling,...
'Parent',obj.legend_axe_handle)];

obj.legend_y=obj.legend_y-legend_y_step;


if obj.line_options.use_input
temp_lw=obj.line_options.input_fun([uni_size{:}]);
else
temp_lw=obj.line_options.base_size+(0:length(str_uni_size)-1)*obj.line_options.step_size;
end
if obj.point_options.use_input
temp_ps=obj.point_options.input_fun([uni_size{:}]);
else
temp_ps=obj.point_options.base_size+(0:length(str_uni_size)-1)*obj.point_options.step_size;
end

fill_legend(obj,obj.aes_names.size,...
str_uni_size,...
'both',...
color_legend_map,...
'o','-',temp_lw,temp_ps);
end
end

%Set size of legend axes
set(obj.legend_axe_handle,'XLim',[1 8])
set(obj.legend_axe_handle,'XLim',[1 8]);
%xlim([1 8])
if obj.legend_y<0
set(obj.legend_axe_handle,'YLim',[obj.legend_y 1])
set(obj.legend_axe_handle,'YLim',[obj.legend_y 1]);
%ylim([obj.legend_y 1])
end

%set(obj.legend_axe_handle,'DataAspectRatio',[1 1 1]);

%If we go from many to one facet, the new content is drawn only
%on the first facet so needs to be copied in the other one
Expand Down
84 changes: 84 additions & 0 deletions @gramm/private/fill_legend.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
function []= fill_legend(obj,title_text,legend_text,legend_type,legend_color,legend_marker,legend_linestyle,legend_line_size,legend_point_size)

legend_y_step=1;
legend_y_additional_step=0.5;

obj.legend_text_handles=[obj.legend_text_handles...
text(1,obj.legend_y,title_text,...
'FontWeight','bold',...
'Interpreter',obj.text_options.interpreter,...
'FontName',obj.text_options.font,...
'FontSize',obj.text_options.base_size*obj.text_options.legend_title_scaling,...
'Parent',obj.legend_axe_handle)];

obj.legend_y=obj.legend_y-legend_y_step;

for ind_legend=1:length(legend_text)

if size(legend_color,1)>1
temp_color = legend_color(ind_legend,:);
else
temp_color = legend_color;
end

if length(legend_marker)>1
temp_marker = legend_marker{ind_legend};
else
temp_marker = legend_marker;
end

if length(legend_linestyle)>1
temp_linestyle = legend_linestyle{ind_legend};
else
temp_linestyle = legend_linestyle;
end

if length(legend_line_size)>1
temp_line_size = legend_line_size(ind_legend);
else
temp_line_size = legend_line_size;
end


if length(legend_point_size)>1
temp_point_size = legend_point_size(ind_legend);
else
temp_point_size = legend_point_size;
end

if strcmp(legend_type,'point')
plot(1.5,obj.legend_y,temp_marker,...
'MarkerEdgeColor','none',...
'MarkerFaceColor', temp_color,...
'Parent',obj.legend_axe_handle,...
'MarkerSize',temp_point_size);
end
if strcmp(legend_type,'line') || strcmp(legend_type,'both')
plot([1 2],[obj.legend_y obj.legend_y],temp_linestyle,...
'Color',temp_color,...
'lineWidth',temp_line_size,...
'Parent',obj.legend_axe_handle)
end
if strcmp(legend_type,'both')
plot(1,obj.legend_y,temp_marker,...
'MarkerEdgeColor','none',...
'MarkerFaceColor', temp_color,...
'Parent',obj.legend_axe_handle,...
'MarkerSize',temp_point_size);
end

obj.legend_text_handles=[obj.legend_text_handles...
text(2.5,obj.legend_y,legend_text{ind_legend},...
'Interpreter',obj.text_options.interpreter,...
'FontName',obj.text_options.font,...
'FontSize',obj.text_options.base_size*obj.text_options.legend_scaling,...
'Parent',obj.legend_axe_handle)];

obj.legend_y=obj.legend_y-legend_y_step;
end

obj.legend_y=obj.legend_y-legend_y_additional_step;

end


Loading

0 comments on commit 0e12b90

Please sign in to comment.