From 04362bfe14002702fe1ee0c95d69e2ebe162dc66 Mon Sep 17 00:00:00 2001
From: Jan Lemmer <jan.lemmer@fst.tu-darmstadt.de>
Date: Tue, 22 Feb 2022 10:56:55 +0100
Subject: [PATCH] Add PinToAxis option

---
 +PlotID/TagPlot.m       | 32 +++++++++++++++++++++++++++-----
 CI_files/CI_config.json |  4 ----
 README.md               | 10 +++++++++-
 3 files changed, 36 insertions(+), 10 deletions(-)
 delete mode 100644 CI_files/CI_config.json

diff --git a/+PlotID/TagPlot.m b/+PlotID/TagPlot.m
index b2e5b56..896e26e 100644
--- a/+PlotID/TagPlot.m
+++ b/+PlotID/TagPlot.m
@@ -24,6 +24,7 @@ arguments
     options.Position (1,2) {mustBeVector} = [1,0.4] % default for east
     options.Rotation (1,1) {mustBeReal} = NaN
     options.ConfigFileName (1,:) {mustBeText} = 'config.json'
+    options.PinToLegend (1,1) {mustBeNumericOrLogical} = false % Pins ID on Legend 
     options.QRcode (1,1) {mustBeNumericOrLogical} = false %experimental
     options.QRsize (1,1) {mustBeNonnegative} = 0.15 % size of the QRCode
 end
@@ -36,7 +37,7 @@ end
 
 switch options.Location
     case 'north'
-        y = 1 - options.DistanceToAxis
+        y = 1 - options.DistanceToAxis;
         options.Position = [0.4,y];
         Rotation = 0;
     case 'east'
@@ -76,16 +77,35 @@ for n = 1:numel(figs)
     IDs{n} = PlotID.CreateID; % Create ID
     IDs{n} = [options.ProjectID,'-',IDs{n}]; % add options.ProjectID
     pltAxes = get(figs(n),'CurrentAxes'); % Axes object for text annotation
-    % Limits for relative Positioning
+    % Limits for relative positioning
     ylim =get(pltAxes,'YLim');
-    xlim =get(pltAxes,'XLim');
-    %ID
-    position = [options.Position(1), options.Position(2)];
+    xlim =get(pltAxes,'XLim');   
+
+    if options.PinToLegend
+        if options.QRcode
+            warning(['PinToLegend and QRCode can not be used at the same time',...
+                newline, 'QRcode is deactivated.']);
+            options.QRcode = false;
+        end
+        lobj = findobj(figs(n), 'Type', 'Legend');
+        if isempty(lobj)
+            set(0, 'CurrentFigure', figs(n))
+            lobj = legend();
+        end 
+        % new position based on legend
+        posVec = get(lobj,'Position');
+        options.Position(1) = posVec(1)+.04;
+        options.Position(2) = posVec(2);
+    end
+
+    % add text label
+    position = [options.Position(1), options.Position(2)];    
     t=text(pltAxes,position(1),position(2), IDs{n},'Fontsize',options.Fontsize,...
     'Rotation',Rotation, 'VerticalAlignment','bottom','Color',...
         options.Color,'BackgroundColor','w', 'Units', 'normalized');
     set(figs(n),'Tag', IDs{n}); 
 
+
     if options.QRcode
         % this should be seen and use as a proof of concept
         qrCode = PlotID.plotQR(IDs{n});
@@ -94,6 +114,8 @@ for n = 1:numel(figs)
         imshow(qrCode);
         t.Visible = 'off';
     end
+
+
 end
 
 if numel(figs) == 1
diff --git a/CI_files/CI_config.json b/CI_files/CI_config.json
deleted file mode 100644
index fd412c7..0000000
--- a/CI_files/CI_config.json
+++ /dev/null
@@ -1,4 +0,0 @@
-{
-  "Author": "CI-Test",
-  "ProjectID": "CI-001"
-}
\ No newline at end of file
diff --git a/README.md b/README.md
index 9dd7991..5379313 100644
--- a/README.md
+++ b/README.md
@@ -48,12 +48,20 @@ you find the options for TagPlot below. The data type is in curled braces and th
 
 - ProjectID  {mustBeText}= '' 
 - Fontsize {mustBeInteger} = 8 
+- Color  {mustBeNonnegative} = 0.65*[1 1 1] 
 - Location  {mustBeText} = 'east'
 - Position  {mustBeVector} = [1,0.4] 
--  Rotation  {mustBeInteger} = 0  
+- DistanceToAxis {mustBeReal} = .02 
+- Rotation  {mustBeInteger} = 0  
+- PinToLegend {mustBeNumericOrLogical} = false 
+- QRcode  {mustBeNumericOrLogical} = false 
+- QRsize {mustBeNonnegative} = 0.15
 
 `options.ProjectID` is the project number (string or char), if empty the ID from the config file is used
 The ID is placed on the 'east' of the figure per default, if you want it somwhere else, use the `'Location'` option. 'north', 'east', 'south', 'west' are predefined, otherwise use the `'custom'` option and provide the desired 'Position'  as a vector relative to your x- and y-axis limits `[relX, relY]` .
+
+`options.PinToLegend` pins the ID to the figure's legend. If no legend was defined, plotID will create one.
+
 </details>
 
 ## CreateID()
-- 
GitLab