Skip to content

Commit

Permalink
Created a function for drawing the continuous color legend.
Browse files Browse the repository at this point in the history
  • Loading branch information
piermorel committed Nov 1, 2017
1 parent 0e12b90 commit cbf1a7e
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 53 deletions.
58 changes: 5 additions & 53 deletions @gramm/draw.m
Original file line number Diff line number Diff line change
Expand Up @@ -707,60 +707,12 @@

%Continuous color legend
if obj.continuous_color_options.active

fill_gradient_legend(obj,obj.aes_names.color,...
linspace(obj.continuous_color_options.CLim(1), obj.continuous_color_options.CLim(2), 3),...
linspace(obj.continuous_color_options.CLim(1), obj.continuous_color_options.CLim(2), 5),...
4);

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; %HACK here, we have to multiply by 2 ??

tmp_N=100;

gradient_height=4;
%imagesc coordinates correspond to centers of patches, hence the
%x=[1.5 1.5] to get [1 2] borders
imagesc([1.5 1.5],...
[obj.legend_y-legend_y_step*gradient_height obj.legend_y],...
linspace(obj.continuous_color_options.CLim(1),obj.continuous_color_options.CLim(2),tmp_N)',...
'Parent',obj.legend_axe_handle);

line([1.8 2;1.8 2;1.8 2 ; 1 1.2 ; 1 1.2 ; 1 1.2]',...
[obj.legend_y-legend_y_step*gradient_height/4 obj.legend_y-legend_y_step*gradient_height/4 ;...
obj.legend_y-legend_y_step*gradient_height/2 obj.legend_y-legend_y_step*gradient_height/2 ;...
obj.legend_y-legend_y_step*3*gradient_height/4 obj.legend_y-legend_y_step*3*gradient_height/4;...
obj.legend_y-legend_y_step*gradient_height/4 obj.legend_y-legend_y_step*gradient_height/4 ;...
obj.legend_y-legend_y_step*gradient_height/2 obj.legend_y-legend_y_step*gradient_height/2 ;...
obj.legend_y-legend_y_step*3*gradient_height/4 obj.legend_y-legend_y_step*3*gradient_height/4]',...
'Color','w','Parent',obj.legend_axe_handle)


colormap(obj.continuous_color_options.colormap)
caxis(obj.continuous_color_options.CLim);

obj.legend_text_handles=[obj.legend_text_handles...
text(2.5,obj.legend_y,num2str(obj.continuous_color_options.CLim(2)),...
'FontName',obj.text_options.font,...
'FontSize',obj.text_options.base_size*obj.text_options.legend_scaling,...
'Parent',obj.legend_axe_handle)];

obj.legend_text_handles=[obj.legend_text_handles...
text(2.5,obj.legend_y-legend_y_step*gradient_height/2,num2str(mean(obj.continuous_color_options.CLim)),...
'FontName',obj.text_options.font,...
'FontSize',obj.text_options.base_size*obj.text_options.legend_scaling,...
'Parent',obj.legend_axe_handle)];

obj.legend_text_handles=[obj.legend_text_handles...
text(2.5,obj.legend_y-legend_y_step*gradient_height,num2str(obj.continuous_color_options.CLim(1)),...
'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*(gradient_height+1);
obj.legend_y=obj.legend_y-legend_y_additional_step;
end

%marker legend
Expand Down
56 changes: 56 additions & 0 deletions @gramm/private/fill_gradient_legend.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
function [] = fill_gradient_legend(obj, title_text, legend_values, legend_ticks, gradient_height)

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;

tmp_N = 100;

%imagesc coordinates correspond to centers of patches, hence the
%x=[1.5 1.5] to get [1 2] borders
imagesc([1.5 1.5], ...
[obj.legend_y - gradient_height obj.legend_y], ...
linspace(obj.continuous_color_options.CLim(1), obj.continuous_color_options.CLim(2), tmp_N)', ...
'Parent', obj.legend_axe_handle);


obj.legend_y = obj.legend_y - gradient_height;

for k = 1:length(legend_ticks)
line([1.8 2; 1 1.2]', ...
[1 1] * (obj.legend_y + val2y(legend_ticks(k), gradient_height, obj.continuous_color_options.CLim)), ...
'Color', 'w', 'Parent', obj.legend_axe_handle)
end

%We need to fill from the top so that lowest text is last in the handle
%array
legend_values = sort(legend_values,'descend');
for k = 1:length(legend_values)
obj.legend_text_handles = [obj.legend_text_handles ...
text(2.5, ...
obj.legend_y + val2y(legend_values(k), gradient_height, obj.continuous_color_options.CLim), ...
num2str(legend_values(k)), ...
'FontName', obj.text_options.font, ...
'FontSize', obj.text_options.base_size * obj.text_options.legend_scaling, ...
'Parent', obj.legend_axe_handle)];
end

colormap(obj.continuous_color_options.colormap)
caxis(obj.continuous_color_options.CLim);

obj.legend_y = obj.legend_y - legend_y_step - legend_y_additional_step;

end

function res = val2y(val, height, clim)
res = height * (val - clim(1)) / (clim(2) - clim(1));
end

0 comments on commit cbf1a7e

Please sign in to comment.