← Back to team overview

graphite-dev team mailing list archive

[Merge] lp:~whd/graphite/configurable-minor-gridlines into lp:graphite

 

Wesley Dawson has proposed merging lp:~whd/graphite/configurable-minor-gridlines into lp:graphite.

Requested reviews:
  graphite-dev (graphite-dev)

For more details, see:
https://code.launchpad.net/~whd/graphite/configurable-minor-gridlines/+merge/84390

Implement support for minor gridlines when logBase is set, as well as support for altering the number of minor gridlines between each major gridline on the y-axis.

Minor gridlines are calculated by taking each major Y axis value (from smallest to second largest), adding one half of the Y step value, and then drawing a minor gridline at that position.

This is facilitated by two key changes:

First, rather than adding one half of the Y step value, we calculate the midpoint of the two values.  This produces the correct minor gridline value for either linear or log Y axis.

Second, when logscale Y, append a calculated "next value" to the list of Y label values.  The extra value ensures that the final minor gridline is calculated at all and provides the required next value for the calculation itself.  yTop comparisons are doubled to permit the last two minor gridlines to be drawn.

When working correctly on log Y, each minor Y line will be about two thirds of the way up from the previous major Y line, and each gap between two major Y lines will have a minor Y line.

By default, graphite hard-codes one minor gridline between major ones. A new url parameter "minorY" has been added to permit its variance, and is supported when using logBase as well.

Results:
http://people.mozilla.org/~wdawson/minorY_0.png
http://people.mozilla.org/~wdawson/minorY_1.png (original behaviour)
http://people.mozilla.org/~wdawson/minorY_5.png

http://people.mozilla.org/~wdawson/minorY_0_with_logBase.png (original behaviour)
http://people.mozilla.org/~wdawson/minorY_1_with_logBase.png
http://people.mozilla.org/~wdawson/minorY_5_with_logBase.png

(primarily contributed by rsoderberg@xxxxxxxxxxx)
-- 
https://code.launchpad.net/~whd/graphite/configurable-minor-gridlines/+merge/84390
Your team graphite-dev is requested to review the proposed merge of lp:~whd/graphite/configurable-minor-gridlines into lp:graphite.
=== modified file 'docs/url-api.rst'
--- docs/url-api.rst	2011-07-28 11:38:06 +0000
+++ docs/url-api.rst	2011-12-04 01:24:24 +0000
@@ -524,6 +524,17 @@
   &minorGridLineColor=darkgrey
 
 
+minorY
+------------------
+Sets the number of minor grid lines per major line on the y-axis.
+
+Example:
+
+.. code-block:: none
+
+  &minorY=3
+
+
 thickness
 ---------
 Alias for lineWidth

=== modified file 'webapp/content/js/composer_widgets.js'
--- webapp/content/js/composer_widgets.js	2011-11-17 18:43:47 +0000
+++ webapp/content/js/composer_widgets.js	2011-12-04 01:24:24 +0000
@@ -1058,6 +1058,7 @@
       menuInputItem("Label", "vtitle"),
       menuInputItem("Minimum", "yMin"),
       menuInputItem("Maximum", "yMax"),
+      menuInputItem("Minor Lines", "minorY"),
       menuInputItem("Logarithmic Scale", "logBase", "Please enter the logarithmic base to use (ie. 10, e, etc...)"),
       {text: "Unit", menu: yAxisUnitMenu},
       {text: "Side", menu: yAxisSideMenu},

=== modified file 'webapp/graphite/render/glyph.py'
--- webapp/graphite/render/glyph.py	2011-11-22 02:19:03 +0000
+++ webapp/graphite/render/glyph.py	2011-12-04 01:24:24 +0000
@@ -133,6 +133,7 @@
     self.margin = int( params.get('margin',10) )
     self.userTimeZone = params.get('tz')
     self.logBase = params.get('logBase', None)
+    self.minorY = int(params.get('minorY', 1))
     if self.logBase:
       if self.logBase == 'e':
         self.logBase = math.e
@@ -390,7 +391,7 @@
                   'yUnitSystem', 'logBase','yMinLeft','yMinRight','yMaxLeft', \
                   'yMaxRight', 'yLimitLeft', 'yLimitRight', 'yStepLeft', \
                   'yStepRight', 'rightWidth', 'rightColor', 'rightDashed', \
-                  'leftWidth', 'leftColor', 'leftDashed', 'xFormat')
+                  'leftWidth', 'leftColor', 'leftDashed', 'xFormat', 'minorY')
   validLineModes = ('staircase','slope','connected')
   validAreaModes = ('none','first','all','stacked')
   validPieModes = ('maximum', 'minimum', 'average')
@@ -1180,6 +1181,8 @@
       labels = self.yLabelValuesL
     else:
       labels = self.yLabelValues
+    if self.logBase:
+      labels.append(self.logBase * max(labels))
 
     for i, value in enumerate(labels):
       self.ctx.set_line_width(0.4)
@@ -1195,37 +1198,47 @@
       self.ctx.move_to(leftSide, y)
       self.ctx.line_to(rightSide, y)
       self.ctx.stroke()
-      self.ctx.set_line_width(0.3)
-      self.setColor( self.params.get('minorGridLineColor',self.defaultMinorGridLineColor) )
-
-      # If this is the last label or we are using a log scale no minor grid line.
-      if self.secondYAxis:
-        if self.logBase or i == len(self.yLabelValuesL) - 1:
-          continue
-      else:
-        if self.logBase or i == len(self.yLabelValues) - 1:
-          continue
-        
-      # Draw the minor grid lines for linear scales.
-      if self.secondYAxis:
-        value += (self.yStepL / 2.0)
-        if value >= self.yTopL:
-          continue
-      else:
-        value += (self.yStep / 2.0)
-        if value >= self.yTop:
-          continue
-
-      if self.secondYAxis:
-        y = self.getYCoord(value,"left")
-      else:
-        y = self.getYCoord(value)
-      if y is None or y < 0:
-          continue
-
-      self.ctx.move_to(leftSide, y)
-      self.ctx.line_to(rightSide, y)
-      self.ctx.stroke()
+
+      # draw minor gridlines if this isn't the last label
+      if self.minorY >= 1 and i < (len(labels) - 1):
+        # in case graphite supports inverted Y axis now or someday
+        (valueLower, valueUpper) = sorted((value, labels[i+1]))
+
+        # each minor gridline is 1/minorY apart from the nearby gridlines.
+        # we calculate that distance, for adding to the value in the loop.
+        distance = ((valueUpper - valueLower) / float(1 + self.minorY))
+
+        # starting from the initial valueLower, we add the minor distance
+        # for each minor gridline that we wish to draw, and then draw it.
+        for minor in range(self.minorY):
+          self.ctx.set_line_width(0.3)
+          self.setColor( self.params.get('minorGridLineColor',self.defaultMinorGridLineColor) )
+
+          # the current minor gridline value is halfway between the current and next major gridline values
+          value = (valueLower + ((1+minor) * distance))
+
+          if self.logBase:
+            yTopFactor = self.logBase * self.logBase
+          else:
+            yTopFactor = 1
+
+          if self.secondYAxis:
+            if value >= (yTopFactor * self.yTopL):
+              continue
+          else:
+            if value >= (yTopFactor * self.yTop):
+              continue
+
+          if self.secondYAxis:
+            y = self.getYCoord(value,"left")
+          else:
+            y = self.getYCoord(value)
+          if y is None or y < 0:
+              continue
+
+          self.ctx.move_to(leftSide, y)
+          self.ctx.line_to(rightSide, y)
+          self.ctx.stroke()
 
     #Vertical grid lines
     top = self.area['ymin']