[chronojump] CairoBars2HSeries with legend (1 row or 2 adjusted automatically)



commit 48b50e3eae54a3bdde0e728c4d1d821d559a3e4a
Author: Xavier de Blas <xaviblas gmail com>
Date:   Fri Sep 3 20:02:37 2021 +0200

    CairoBars2HSeries with legend (1 row or 2 adjusted automatically)

 src/gui/cairo/bars.cs   | 150 +++++++++++++++++++++++++++++++++++++++++++++---
 src/gui/eventExecute.cs |  13 +++++
 2 files changed, 155 insertions(+), 8 deletions(-)
---
diff --git a/src/gui/cairo/bars.cs b/src/gui/cairo/bars.cs
index 0aa164d69..ffb1b2720 100644
--- a/src/gui/cairo/bars.cs
+++ b/src/gui/cairo/bars.cs
@@ -45,6 +45,10 @@ public abstract class CairoBars : CairoGeneric
        protected string xUnits = "";
        protected string yUnits = "cm";
 
+       //used when there are two series (for legend)
+       protected string variableSerieA = "";
+       protected string variableSerieB = "";
+
        protected double minX = 1000000;
        protected double maxX = 0;
        protected double minY = 1000000;
@@ -250,13 +254,6 @@ public abstract class CairoBars : CairoGeneric
                this.font = font;
                //LogB.Information("Font: " + font);
 
-               leftMargin = 26;
-               rightMargin = 42; //images are 24 px, separate 6 px from grapharea, and 12 px from 
absoluteright
-               if(usePersonGuides && useGroupGuides)
-                       rightMargin = 70;
-               topMargin = 40;
-               bottomMargin = 9;
-
                //1 create context
                g = Gdk.CairoHelper.Create (area.GdkWindow);
 
@@ -285,6 +282,20 @@ public abstract class CairoBars : CairoGeneric
                blue = colorFromRGB(178, 223, 238); //lightblue
                bluePlots = colorFromRGB(0, 0, 200);
                yellow = colorFromRGB(255,238,102);
+
+               //margins
+               leftMargin = 26;
+               rightMargin = 42; //images are 24 px, separate 6 px from grapharea, and 12 px from 
absoluteright
+               if(usePersonGuides && useGroupGuides)
+                       rightMargin = 70;
+               bottomMargin = 9;
+               topMarginSet ();
+       }
+
+       //will be overwritten by graphs with legend
+       protected virtual void topMarginSet ()
+       {
+               topMargin = 40;
        }
 
        protected abstract void findMaximums(); //includes point and guides
@@ -315,7 +326,7 @@ public abstract class CairoBars : CairoGeneric
        {
                return getAxisLabel(xVariable, xUnits);
        }
-       private string getYAxisLabel()
+       protected string getYAxisLabel()
        {
                return getAxisLabel(yVariable, yUnits);
        }
@@ -604,6 +615,14 @@ public abstract class CairoBars : CairoGeneric
        public string YUnits {
                set { yUnits = value; }
        }
+
+       //for CairoBars2HSeries (legend)
+       public string VariableSerieA {
+               set { variableSerieA = value; }
+       }
+       public string VariableSerieB {
+               set { variableSerieB = value; }
+       }
 }
 
 public class CairoBars1Series : CairoBars
@@ -721,6 +740,9 @@ public class CairoBars2HSeries : CairoBars
        private List<string> names_l;
 
        private Cairo.Color colorSerieB;
+       private double oneRowLegendWidth;
+       private bool oneRowLegend;
+       private int boxWidth = 10; //px. Same as boxHeight. box - text sep is .5 boxWidth. 1st text - 2nd box 
sep is 2*boxWidth
 
        //constructor when there are no points
        public CairoBars2HSeries (DrawingArea area, string font)
@@ -744,6 +766,117 @@ public class CairoBars2HSeries : CairoBars
                colorSerieB = colorFromGdk(Config.ColorBackground); //but note if we are using system colors, 
this will not match
        }
 
+       protected override void topMarginSet ()
+       {
+               topMargin = 50; //to accomodate legend under title
+               oneRowLegend = true;
+
+               calculateOneRowLegendWidth();
+
+               g.SetFontSize(textHeight-2);
+               Cairo.TextExtents teYLabel = g.TextExtents(getYAxisLabel());
+
+               //check oneRowLegend does not crash with left axis label or rightMargin (icons)
+               if(graphWidth/2 - oneRowLegendWidth /2 -2*boxWidth < teYLabel.Width ||
+                               graphWidth/2 + oneRowLegendWidth /2 + 2*boxWidth > graphWidth - rightMargin)
+               {
+                       //topMargin really does not change, what is reduced is the space below
+                       //topMargin += Convert.ToInt32(.5*textHeight); //.5 because font is smaller
+                       oneRowLegend = false;
+               }
+
+               g.SetFontSize(textHeight);
+       }
+       private void calculateOneRowLegendWidth ()
+       {
+               g.SetFontSize(textHeight-2);
+
+               Cairo.TextExtents te = g.TextExtents(variableSerieA);
+               double serieAWidth = te.Width;
+
+               te = g.TextExtents(variableSerieB);
+               double serieBWidth = te.Width;
+
+               oneRowLegendWidth = 1.5*boxWidth + serieAWidth + 2*boxWidth + 1.5*boxWidth + serieBWidth;
+
+               g.SetFontSize(textHeight);
+       }
+
+       private void writeLegend ()
+       {
+               g.SetFontSize(textHeight-2);
+
+               Cairo.TextExtents te = g.TextExtents(variableSerieA);
+               double serieAWidth = te.Width;
+
+               te = g.TextExtents(variableSerieB);
+               double serieBWidth = te.Width;
+
+               int boxWidth = 10; //px. Same as boxHeight. box - text sep is .5 boxWidth. 1st text - 2nd box 
sep is 2*boxWidth
+
+               if(oneRowLegend)
+               {
+                       double legendWidth = 1.5*boxWidth + serieAWidth + 2*boxWidth + 1.5*boxWidth + 
serieBWidth;
+                       double xStart = .5*graphWidth -.5*legendWidth;
+
+                       //paint 1st box
+                       g.Color = colorSerieA;
+                       g.Rectangle(xStart, topMargin -1.25*textHeight, boxWidth, boxWidth);
+                       g.FillPreserve();
+                       g.Color = black;
+                       g.Stroke();
+
+                       //write 1st variable
+                       xStart += 1.5*boxWidth;
+                       printText(xStart, topMargin -textHeight, 0, textHeight-2, variableSerieA, g, 
alignTypes.LEFT);
+
+                       //paint 2nd box
+                       xStart += serieAWidth + 2*boxWidth;
+                       g.Color = colorSerieB;
+                       g.Rectangle(xStart, topMargin -1.25*textHeight, boxWidth, boxWidth);
+                       g.FillPreserve();
+                       g.Color = black;
+                       g.Stroke();
+
+                       //write 2nd variable
+                       xStart += 1.5*boxWidth;
+                       printText(xStart, topMargin -textHeight, 0, textHeight-2, variableSerieB, g, 
alignTypes.LEFT);
+               } else
+               {
+                       //1st row
+                       double rowWidth = 1.5*boxWidth + serieAWidth;
+                       double xStart = .5*graphWidth -.5*rowWidth;
+
+                       //paint 1st box
+                       g.Color = colorSerieA;
+                       g.Rectangle(xStart, topMargin -1.25*textHeight, boxWidth, boxWidth);
+                       g.FillPreserve();
+                       g.Color = black;
+                       g.Stroke();
+
+                       //write 1st variable
+                       xStart += 1.5*boxWidth;
+                       printText(xStart, topMargin -textHeight, 0, textHeight-2, variableSerieA, g, 
alignTypes.LEFT);
+
+                       //2nd row
+                       rowWidth = 1.5*boxWidth + serieBWidth;
+                       xStart = .5*graphWidth -.5*rowWidth;
+
+                       //paint 2nd box (1.25*textHeight below)
+                       g.Color = colorSerieB;
+                       g.Rectangle(xStart, topMargin -1.25*textHeight +1.25*textHeight, boxWidth, boxWidth);
+                       g.FillPreserve();
+                       g.Color = black;
+                       g.Stroke();
+
+                       //write 2nd variable
+                       xStart += 1.5*boxWidth;
+                       printText(xStart, topMargin -textHeight +1.25*textHeight, 0, textHeight-2, 
variableSerieB, g, alignTypes.LEFT);
+               }
+
+               g.SetFontSize(textHeight);
+       }
+
        protected override void findMaximums()
        {
                foreach(PointF p in pointA_l)
@@ -866,6 +999,7 @@ public class CairoBars2HSeries : CairoBars
                plotBars();
 
                writeTitleAtTop ();
+               writeLegend ();
 
                endGraphDisposing(g);
        }
diff --git a/src/gui/eventExecute.cs b/src/gui/eventExecute.cs
index 502ad054d..45b70748a 100644
--- a/src/gui/eventExecute.cs
+++ b/src/gui/eventExecute.cs
@@ -3533,9 +3533,19 @@ public class CairoPaintBarsPreJumpSimple : CairoPaintBarsPre
                if(UseHeights) {
                        cb.YVariable = Catalog.GetString("Height");
                        cb.YUnits = "cm";
+                       if(showBarA && showBarB) //Dja, Djna
+                       {
+                               cb.VariableSerieA = Catalog.GetString("Falling height");
+                               cb.VariableSerieB = Catalog.GetString("Jump height");
+                       }
                } else {
                        cb.YVariable = Catalog.GetString("Time");
                        cb.YUnits = "s";
+                       if(showBarA && showBarB) //Dja, Djna
+                       {
+                               cb.VariableSerieA = Catalog.GetString("Contact time");
+                               cb.VariableSerieB = Catalog.GetString("Flight time");
+                       }
                }
 
                cb.GraphInit(fontStr, ! ShowPersonNames, true); //usePersonGuides, useGroupGuides
@@ -3775,6 +3785,9 @@ public class CairoPaintBarsPreJumpReactiveCapture : CairoPaintBarsPre
                cb.YVariable = Catalog.GetString("Time");
                cb.YUnits = "s";
 
+               cb.VariableSerieA = Catalog.GetString("Contact time");
+               cb.VariableSerieB = Catalog.GetString("Flight time");
+
                cb.GraphInit(fontStr, true, false); //usePersonGuides, useGroupGuides
 
                List<PointF> pointA_l = new List<PointF>();


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]