Chris Leonello avatar Chris Leonello committed b1671a0

Labels off of grid are removed. Added edgeTolerance function to control how close label can be to axis edge and be drawn.

Comments (0)

Files changed (2)

src/plugins/jqplot.pointLabels.js

 	    // true to escape html entities in the labels.
 	    // If you want to include markup in the labels, set to false.
 	    this.escapeHTML = true;
+	    // prop: edgeTolerance
+	    // number of pixels that the label must be away from an axis
+	    // boundary in order to be drawn.
+	    this.edgeTolerance = 0;
 	    
 	    $.extend(true, this, options);
 	};
             }
         
             for (var i=0; i<p.labels.length; i++) {
+                var pd = this._plotData;
+                var xax = this._xaxis;
+                var yax = this._yaxis;
+                
                 var elem = $('<div class="jqplot-point-label" style="position:absolute"></div>');
                 elem.insertAfter(sctx.canvas);
                 if (p.escapeHTML) {
                 else {
                     elem.html(p.labels[i]);
                 }
-                var xp = this._plotData[i][0];
-                var yp = this._plotData[i][1];
-                var x = this._xaxis.u2p(this._plotData[i][0]) + xoffset(elem);
-                var y = this._yaxis.u2p(this._plotData[i][1]) + yoffset(elem);
-                elem.css('left', x);
-                elem.css('top', y);
+                var ell = xax.u2p(pd[i][0]) + xoffset(elem);
+                var elt = yax.u2p(pd[i][1]) + yoffset(elem);
+                elem.css('left', ell);
+                elem.css('top', elt);
+                var elr = ell + $(elem).width();
+                var elb = elt + $(elem).height();
+                var et = p.edgeTolerance;
+                var scl = $(sctx.canvas).position().left;
+                var sct = $(sctx.canvas).position().top;
+                var scr = sctx.canvas.width + scl;
+                var scb = sctx.canvas.height + sct;
+                // if label is outside of allowed area, remove it
+                if (ell - et < scl || elt - et < sct || elr + et > scr || elb + et > scb) {
+                    $(elem).remove();
+                }
             }
             
         }

tests/pointLabelTests.php

 line1 = [14, 32, 41, 44, 40, 47, 53, 67];
 plot1 = $.jqplot('chart1', [line1], {
     title: 'Chart with Point Labels', 
-    seriesDefaults: {showMarker:false}
+    seriesDefaults: {showMarker:false},
+    axesDefaults:{pad:1.3}
 });
 </pre>     
 
     }
 });
 </pre>
+
+<p class="description">Data point labels have an "edgeTolerance" option.  This options controls how close the data point label can be to an axis edge and still be drawn.  The default of 0 allows labels to touch the axis.  Positive values will increase the required distance between the axis and label, negative values will allow labels to overlap axes.</p>
+      
+<div class="jqPlot" id="chart5" style="height:320px; width:540px;"></div>
+
+<pre class="prettyprint plot">
+line1 = [14, 32, 41, 44, 40, 47, 53, 67];
+plot5 = $.jqplot('chart5', [line1], {
+    title: 'Chart with Point Labels', 
+    seriesDefaults: {
+      showMarker:false,
+      pointLabels: {
+        edgeTolerance: 5
+      }},
+    axes:{
+      xaxis:{min:3}
+    }
+});
+</pre>  
  
 <?php require('closer.php') ?>
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.