From 61d8ae231699617f71a0f6a37c35de99b80da711 Mon Sep 17 00:00:00 2001
From: Michael Innerberger <michael.innerberger@asc.tuwien.ac.at>
Date: Wed, 9 Aug 2023 14:41:19 -0400
Subject: [PATCH] Introduce DataCategory that encapsulates plotting behavior

---
 lib/storage/@LevelData/plot.m         | 12 +-----------
 lib/storage/@LevelData/plotAbsolute.m | 12 +-----------
 lib/storage/@LevelData/plotLevel.m    | 20 +++++++++++++-------
 lib/storage/@LevelData/plotTime.m     | 12 +-----------
 lib/storage/DataCategory.m            | 23 +++++++++++++++++++++++
 5 files changed, 39 insertions(+), 40 deletions(-)
 create mode 100644 lib/storage/DataCategory.m

diff --git a/lib/storage/@LevelData/plot.m b/lib/storage/@LevelData/plot.m
index f9e1e0e..008d133 100644
--- a/lib/storage/@LevelData/plot.m
+++ b/lib/storage/@LevelData/plot.m
@@ -43,15 +43,5 @@ function ax = plot(obj, xVariable, yVariable)
     yVariable = setdiff(yVariable, obj.timeVariable);
 
     % Creates double logarithmic splot 
-    ax = obj.plotLevel(@loglog, xVariable, yVariable);
-
-    % Add title
-    title(ax, 'Convergence history plot');
-
-    % Add axes labels
-    xlabel(ax, xVariable);
-    ylabel(ax, 'error');
-
-    % Update legend
-    configureLegend(ax, 'northeast');
+    ax = obj.plotLevel(DataCategory.ERROR, xVariable, yVariable);
 end
\ No newline at end of file
diff --git a/lib/storage/@LevelData/plotAbsolute.m b/lib/storage/@LevelData/plotAbsolute.m
index 3c57f9f..949b85f 100644
--- a/lib/storage/@LevelData/plotAbsolute.m
+++ b/lib/storage/@LevelData/plotAbsolute.m
@@ -40,15 +40,5 @@ function ax = plotAbsolute(obj, xVariable, yVariable)
     yVariable = intersect(yVariable, obj.absoluteVariable);
 
     % Creates semilogx plot 
-    ax = obj.plotLevel(@semilogx, xVariable, yVariable);
-
-    % Add title
-    title(ax, 'Value plot');
-
-    % Add axes labels
-    xlabel(ax, xVariable);
-    ylabel(ax, 'value');
-
-    % Update legend
-    configureLegend(ax, 'northeast');
+    ax = obj.plotLevel(DataCategory.ABSOLUTE, xVariable, yVariable);
 end
\ No newline at end of file
diff --git a/lib/storage/@LevelData/plotLevel.m b/lib/storage/@LevelData/plotLevel.m
index 0434807..0394ee4 100644
--- a/lib/storage/@LevelData/plotLevel.m
+++ b/lib/storage/@LevelData/plotLevel.m
@@ -1,9 +1,9 @@
-function ax = plotLevel(obj, plotFunction, xVariable, yVariable)
+function ax = plotLevel(obj, category, xVariable, yVariable)
 %%PLOTLEVEL auxiliary private function for creation of plots for usage in
-%LevelData/plot, LevelData/plotTime, and LevelData/plotAbsolute, calls the
-%specified plotFunction for the plot of variables in yVariable with
+%LevelData/plot, LevelData/plotTime, and LevelData/plotAbsolute, uses the
+%specified DataCategory for the plot of variables in yVariable with
 %respect to xVariable, returns handle to axis object
-%   ax = PLOTLEVEL(obj, plotFunction, xVariable, yVariable, ...)
+%   ax = PLOTLEVEL(obj, category, xVariable, yVariable, ...)
 
 % Copyright 2023 Philipp Bringmann
 %
@@ -23,7 +23,7 @@ function ax = plotLevel(obj, plotFunction, xVariable, yVariable)
 
     arguments
         obj
-        plotFunction function_handle
+        category DataCategory
         xVariable {mustBeTextScalar}
         yVariable cell
     end
@@ -39,7 +39,7 @@ function ax = plotLevel(obj, plotFunction, xVariable, yVariable)
 
     % Iterate over given variables
     for j = 1:length(yVariable)
-        if obj.type.(yVariable{j}).rawType ~= RawType.FLOAT
+        if obj.type(yVariable{j}).rawType ~= RawType.FLOAT
             continue
         end
 
@@ -52,7 +52,7 @@ function ax = plotLevel(obj, plotFunction, xVariable, yVariable)
             variableLabel = yVariable{j};
         end
         % Create plot
-        plotFunction( ...
+        category.plotFunction( ...
                 ax, xValue, yValue, '-', ...
                 'Marker', MARKER{mod(j, length(MARKER))}, ...
                 'Color', COLOURORDER(j, :), ...
@@ -61,4 +61,10 @@ function ax = plotLevel(obj, plotFunction, xVariable, yVariable)
         % Add new line into the current figure when calling plotConvergence again
         hold(ax, 'on');
     end
+
+    % Set plot appearance
+    title(ax, category.title);
+    xlabel(ax, xVariable);
+    ylabel(ax, category.yLabel);
+    configureLegend(ax, category.legendLocation);
 end
diff --git a/lib/storage/@LevelData/plotTime.m b/lib/storage/@LevelData/plotTime.m
index 86a80e8..be72731 100644
--- a/lib/storage/@LevelData/plotTime.m
+++ b/lib/storage/@LevelData/plotTime.m
@@ -39,15 +39,5 @@ function ax = plotTime(obj, xVariable, yVariable)
     yVariable = intersect(yVariable, obj.timeVariable);
 
     % Creates double logarithmic splot 
-    ax = obj.plotLevel(@loglog, xVariable, yVariable);
-
-    % Add title
-    title(ax, 'Time plot');
-
-    % Add axes labels
-    xlabel(ax, xVariable);
-    ylabel(ax, 'runtime');
-
-    % Update legend
-    configureLegend(ax, 'northwest');
+    ax = obj.plotLevel(DataCategory.TIME, xVariable, yVariable);
 end
\ No newline at end of file
diff --git a/lib/storage/DataCategory.m b/lib/storage/DataCategory.m
new file mode 100644
index 0000000..f4ea739
--- /dev/null
+++ b/lib/storage/DataCategory.m
@@ -0,0 +1,23 @@
+classdef DataCategory
+    enumeration
+        ERROR(@loglog, 'Convergence history plot', 'error',  'northeast')
+        ABSOLUTE(@semilogx, 'Value plot', 'value',  'northeast')
+        TIME(@loglog, 'Time plot', 'runtime',  'northwest')
+    end
+
+    properties (SetAccess=immutable)
+        plotFunction function_handle
+        title string
+        yLabel string
+        legendLocation string
+    end
+
+    methods
+        function obj = DataCategory(plotFunction, title, yLabel, legendLocation)
+            obj.plotFunction = plotFunction;
+            obj.title = title;
+            obj.yLabel = yLabel;
+            obj.legendLocation = legendLocation;
+        end
+    end
+end
\ No newline at end of file
-- 
GitLab