[chronojump] Jumps evolution cairo graph with date as X



commit 16cb337136247f143474268d7714c750dd445675
Author: Xavier de Blas <xaviblas gmail com>
Date:   Thu Jan 9 18:35:04 2020 +0100

    Jumps evolution cairo graph with date as X

 src/gui/cairo/jumpsDjOptimalFall.cs   |  2 +-
 src/gui/cairo/jumpsEvolution.cs       | 66 +++++++++++++++++++++++----
 src/gui/cairo/jumpsWeightFVProfile.cs |  2 +-
 src/gui/cairo/xy.cs                   | 86 +++++++++++++++++++++--------------
 src/jumpsEvolution.cs                 |  2 +-
 src/utilDate.cs                       | 11 +++++
 6 files changed, 122 insertions(+), 47 deletions(-)
---
diff --git a/src/gui/cairo/jumpsDjOptimalFall.cs b/src/gui/cairo/jumpsDjOptimalFall.cs
index 0d1feb85..b31c8273 100644
--- a/src/gui/cairo/jumpsDjOptimalFall.cs
+++ b/src/gui/cairo/jumpsDjOptimalFall.cs
@@ -72,7 +72,7 @@ public class JumpsDjOptimalFallGraph : CairoXY
 
                 findPointMaximums();
                 findAbsoluteMaximums();
-               paintAxisAndGrid();
+               paintAxisAndGrid(gridTypes.BOTH);
 
                LogB.Information(string.Format("coef length:{0}", coefs.Length));
                if(coefs.Length == 3)
diff --git a/src/gui/cairo/jumpsEvolution.cs b/src/gui/cairo/jumpsEvolution.cs
index 19ab54c3..fe9e6701 100644
--- a/src/gui/cairo/jumpsEvolution.cs
+++ b/src/gui/cairo/jumpsEvolution.cs
@@ -17,7 +17,6 @@
  *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  *
  *  Copyright (C) 2004-2020   Xavier de Blas <xaviblas gmail com> 
- *  Copyright (C) 2004-2020   Jordi Rodeiro <jordirodeiro gmail com> 
  */
 
 using System;
@@ -62,7 +61,7 @@ public class JumpsEvolutionGraph : CairoXY
                this.date = date;
 
                axisYLabel = "Height (cm)";
-               axisXLabel = "Date (double)";
+               axisXLabel = "Date";
        }
 
        public override void Do()
@@ -72,7 +71,8 @@ public class JumpsEvolutionGraph : CairoXY
 
                 findPointMaximums();
                 findAbsoluteMaximums();
-               paintAxisAndGrid();
+               paintAxisAndGrid(gridTypes.HORIZONTALLINES);
+               paintGridDatetime();
 
                LogB.Information(string.Format("coef length:{0}", coefs.Length));
                if(coefs.Length == 3)
@@ -90,24 +90,72 @@ public class JumpsEvolutionGraph : CairoXY
                        else
                                writeTextConcaveParabole();
                } else {
-                       writeTextNeed3PointsWithDifferentFall();
+                       //TODO: if two points draw a line, but no need to show error if there are 1 or 2 
points
+                       //writeTextNeed3PointsWithDifferentFall();
                }
                writeTitle();
 
                endGraph();
        }
 
-       //here X is year, add half a year
-       protected override void separateMinXMaxX()
+       //here X is year, add/subtract third of a year
+       protected override void separateMinXMaxXIfNeeded()
        {
-               minX -= .5;
-               maxX += .5;
+               if(minX == maxX || maxX - minX < .1) //<.1 means that maybe we will not see any vertical bar 
on grid, enlarge it
+               {
+                       minX -= .1;
+                       maxX += .1;
+               }
+       }
+
+       //a bit recursive function ;)
+       private void paintGridDatetime()
+       {
+               g.Save();
+               g.SetDash(new double[]{1, 2}, 0);
+
+               bool paintMonths = (Convert.ToInt32(Math.Floor(maxX)) - Convert.ToInt32(Math.Floor(minX)) < 
3); //paint months if few years
+
+               //-1 to start on previous year to see last months (if fit into graph)
+               for(int year = Convert.ToInt32(Math.Floor(minX)) -1; year <= 
Convert.ToInt32(Math.Floor(maxX)); year ++)
+               {
+                       int xtemp = Convert.ToInt32(calculatePaintX(year, graphWidth, maxX, minX, 
outerMargins + innerMargins, outerMargins + innerMargins));
+                       if( ! (xtemp < outerMargins || xtemp > graphWidth - outerMargins) )
+                       {
+                               if(paintMonths)
+                                       paintVerticalGridLine(xtemp, string.Format("{0} {1}", year, 
UtilDate.GetMonthName(0, true)));
+                               else
+                                       paintVerticalGridLine(xtemp, year.ToString());
+                       }
+
+                       if(! paintMonths)
+                               continue;
+
+                       int monthStep = 3;
+                       //1 get de distance between 1 month and the next one
+                       int xtemp1 = Convert.ToInt32(calculatePaintX(year + 1/12.0, graphWidth, maxX, minX, 
outerMargins + innerMargins, outerMargins + innerMargins));
+                       int xtemp2 = Convert.ToInt32(calculatePaintX(year + 2/12.0, graphWidth, maxX, minX, 
outerMargins + innerMargins, outerMargins + innerMargins));
+                       if(xtemp2 - xtemp1 > 100)
+                               monthStep = 1;
+
+                       for(int month = monthStep; month <= 12-monthStep; month += monthStep)
+                       {
+                               LogB.Information(string.Format("year-month: {0}-{1}", year, month));
+                               xtemp = Convert.ToInt32(calculatePaintX(year + month/12.0, graphWidth, maxX, 
minX, outerMargins + innerMargins, outerMargins + innerMargins));
+                               if(xtemp < outerMargins || xtemp > graphWidth - outerMargins)
+                                       continue;
+
+                               paintVerticalGridLine(xtemp, string.Format("{0} {1}", year, 
UtilDate.GetMonthName(month, true)));
+                       }
+               }
+
+               g.Stroke ();
+               g.Restore();
        }
 
        protected override void writeTitle()
        {
                writeTextAtRight(-5, title, true);
-               //writeTextAtRight(-4, "Optimal fall height", false);
                writeTextAtRight(-3, "Jumptype: " + jumpType, false);
                writeTextAtRight(-2, date, false);
        }
diff --git a/src/gui/cairo/jumpsWeightFVProfile.cs b/src/gui/cairo/jumpsWeightFVProfile.cs
index 895b1e7d..f1662534 100644
--- a/src/gui/cairo/jumpsWeightFVProfile.cs
+++ b/src/gui/cairo/jumpsWeightFVProfile.cs
@@ -85,7 +85,7 @@ public class JumpsWeightFVProfileGraph : CairoXY
 
                findPointMaximums();
                //findAbsoluteMaximums();
-               paintAxisAndGrid();
+               paintAxisAndGrid(gridTypes.BOTH);
 
                /*
                LogB.Information(string.Format("coef length:{0}", coefs.Length));
diff --git a/src/gui/cairo/xy.cs b/src/gui/cairo/xy.cs
index 8f09a153..04e77595 100644
--- a/src/gui/cairo/xy.cs
+++ b/src/gui/cairo/xy.cs
@@ -49,14 +49,14 @@ public abstract class CairoXY
        double yAtMMaxY;
        double absoluteMaxX;
        double absoluteMaxY;
-       int graphWidth;
-       int graphHeight;
+       protected int graphWidth;
+       protected int graphHeight;
        Cairo.Color red;
        Cairo.Color blue;
 
        //for all 4 sides
-       protected int outerMargins = 30; //blank space outside the axis
-       int innerMargins = 30; //space between the axis and the real coordinates
+       protected int outerMargins = 40; //blank space outside the axis
+       protected int innerMargins = 30; //space between the axis and the real coordinates
        int totalMargins;
 
        public abstract void Do();
@@ -101,24 +101,28 @@ public abstract class CairoXY
                }
 
                //if there is only one point, or by any reason mins == maxs, have mins and maxs separated
-               if(minX == maxX)
-                       separateMinXMaxX();
-               if(minY == maxY)
-                       separateMinYMaxY();
+               separateMinXMaxXIfNeeded();
+               separateMinYMaxYIfNeeded();
 
                absoluteMaxX = maxX;
                absoluteMaxY = maxY;
        }
 
-       protected virtual void separateMinXMaxX()
+       protected virtual void separateMinXMaxXIfNeeded()
        {
-               minX -= .5 * minX;
-               maxX += .5 * maxX;
+               if(minX == maxX)
+               {
+                       minX -= .5 * minX;
+                       maxX += .5 * maxX;
+               }
        }
-       protected virtual void separateMinYMaxY()
+       protected virtual void separateMinYMaxYIfNeeded()
        {
-               minY -= .5 * minY;
-               maxY += .5 * maxY;
+               if(minY == maxY)
+               {
+                       minY -= .5 * minY;
+                       maxY += .5 * maxY;
+               }
        }
 
        //includes point  and model
@@ -139,7 +143,7 @@ public abstract class CairoXY
                }
        }
 
-       protected void paintAxisAndGrid()
+       protected void paintAxisAndGrid(gridTypes gridType)
        {
                //1 paint axis
                g.MoveTo(outerMargins, outerMargins);
@@ -149,7 +153,7 @@ public abstract class CairoXY
                printText(2, Convert.ToInt32(outerMargins/2), 0, textHeight, axisYLabel, g, false);
                printText(graphWidth - Convert.ToInt32(outerMargins/2), graphHeight - outerMargins, 0, 
textHeight, axisXLabel, g, false);
 
-               paintGrid (minX, absoluteMaxX, minY, absoluteMaxY, 5);
+               paintGrid (minX, absoluteMaxX, minY, absoluteMaxY, 5, gridType);
        }
 
        protected void plotPredictedLine()
@@ -287,7 +291,8 @@ public abstract class CairoXY
        }
 
        //TODO: fix if min == max (crashes)
-       protected void paintGrid (double minX, double maxX, double minY, double maxY, int seps)
+       protected enum gridTypes { BOTH, HORIZONTALLINES, VERTICALLINES }
+       protected void paintGrid (double minX, double maxX, double minY, double maxY, int seps, gridTypes 
gridType)
        {
                //LogB.Information(string.Format("paintGrid: {0}, {1}, {2}, {3}", min, max, seps, horiz));
 
@@ -298,31 +303,42 @@ public abstract class CairoXY
                g.SetDash(new double[]{1, 2}, 0);
                // i <= max*1.5 to allow to have grid just above the maxpoint if it's below innermargins
                // see: if(ytemp < outerMargins) continue;
-               for(double i = minX; i <= maxX *1.5 ; i += stepX)
-               {
-                       int xtemp = Convert.ToInt32(calculatePaintX(i, graphWidth, maxX, minX, outerMargins + 
innerMargins, outerMargins + innerMargins));
-                       if(xtemp < outerMargins || xtemp > graphWidth - outerMargins)
-                               continue;
+               if(gridType != gridTypes.HORIZONTALLINES)
+                       for(double i = minX; i <= maxX *1.5 ; i += stepX)
+                       {
+                               int xtemp = Convert.ToInt32(calculatePaintX(i, graphWidth, maxX, minX, 
outerMargins + innerMargins, outerMargins + innerMargins));
+                               if(xtemp < outerMargins || xtemp > graphWidth - outerMargins)
+                                       continue;
 
-                       g.MoveTo(xtemp, graphHeight - outerMargins);
-                       g.LineTo(xtemp, outerMargins);
-                       printText(xtemp, graphHeight - Convert.ToInt32(outerMargins/2), 0, textHeight, 
Util.TrimDecimals(i, 1), g, true);
-               }
+                               paintVerticalGridLine(xtemp, Util.TrimDecimals(i, 1));
+                       }
 
-               for(double i = minY; i <= maxY *1.5 ; i += stepY)
-               {
-                       int ytemp = Convert.ToInt32(calculatePaintY(i, graphHeight, maxY, minY, outerMargins 
+ innerMargins, outerMargins + innerMargins));
-                       if(ytemp < outerMargins || ytemp > graphHeight - outerMargins)
-                               continue;
+               if(gridType != gridTypes.VERTICALLINES)
+                       for(double i = minY; i <= maxY *1.5 ; i += stepY)
+                       {
+                               int ytemp = Convert.ToInt32(calculatePaintY(i, graphHeight, maxY, minY, 
outerMargins + innerMargins, outerMargins + innerMargins));
+                               if(ytemp < outerMargins || ytemp > graphHeight - outerMargins)
+                                       continue;
 
-                       g.MoveTo(outerMargins, ytemp);
-                       g.LineTo(graphWidth - outerMargins, ytemp);
-                       printText(Convert.ToInt32(outerMargins/2), ytemp, 0, textHeight, Util.TrimDecimals(i, 
1), g, true);
-               }
+                               paintHorizontalGridLine(ytemp, Util.TrimDecimals(i, 1));
+                       }
                g.Stroke ();
                g.Restore();
        }
 
+       protected void paintHorizontalGridLine(int ytemp, string text)
+       {
+               g.MoveTo(outerMargins, ytemp);
+               g.LineTo(graphWidth - outerMargins, ytemp);
+               printText(Convert.ToInt32(outerMargins/2), ytemp, 0, textHeight, text, g, true);
+       }
+       protected void paintVerticalGridLine(int xtemp, string text)
+       {
+               g.MoveTo(xtemp, graphHeight - outerMargins);
+               g.LineTo(xtemp, outerMargins);
+               printText(xtemp, graphHeight - Convert.ToInt32(outerMargins/2), 0, textHeight, text, g, true);
+       }
+
        private double getGridStep(double min, double max, int seps)
        {
                //show 5 steps positive, 5 negative (if possible)
diff --git a/src/jumpsEvolution.cs b/src/jumpsEvolution.cs
index 3a4ffc12..e7e767b1 100644
--- a/src/jumpsEvolution.cs
+++ b/src/jumpsEvolution.cs
@@ -15,7 +15,7 @@
  *  along with this program; if not, write to the Free Software
  *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  *
- *  Copyright (C) 2019   Xavier de Blas <xaviblas gmail com>, Jordi Rodeiro <jordirodeiro gmail com>
+ *  Copyright (C) 2019   Xavier de Blas <xaviblas gmail com>
  */
 
 using System;
diff --git a/src/utilDate.cs b/src/utilDate.cs
index 6c815afd..0bbfbf55 100644
--- a/src/utilDate.cs
+++ b/src/utilDate.cs
@@ -127,4 +127,15 @@ public class UtilDate
                return dt.Year + UtilAll.DivideSafe(dt.DayOfYear, 360);
        }
 
+       //month from 0 to 11
+       public static string GetMonthName(int month, bool abbreviate)
+       {
+               string [] monthNames = { "January", "February", "March", "April", "May", "June", "July", 
"August", "September", "October", "November", "December" };
+               string [] monthNamesAbbr = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sept", 
"Oct", "Nov", "Dec" };
+
+               if(abbreviate)
+                       return monthNamesAbbr[month];
+               else
+                       return monthNames[month];
+       }
 }


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