Commits

Chris Leonello  committed d32cd92

Mods to get trendlines working.

  • Participants
  • Parent commits f439201

Comments (0)

Files changed (4)

File src/jqplot.core.js

 * */
 
 (function($) {
+    // make sure undefined is undefined
+    var undefined;
     // Class: $.jqplot
     // jQuery function called by the user to create a plot.
     //
     $.jqplot.postDrawHooks = [];
     $.jqplot.preDrawSeriesHooks = [];
     $.jqplot.postDrawSeriesHooks = [];
-    $.jqplot.drawLegendHooks = [];
+    $.jqplot.preDrawLegendHooks = [];
+    $.jqplot.postDrawLegendHooks = [];
+    $.jqplot.addLegendRowHooks = [];
     $.jqplot.preSeriesInitHooks = [];
     $.jqplot.postSeriesInitHooks = [];
     $.jqplot.preParseSeriesOptionsHooks = [];
     };
     
     Legend.prototype.draw = function(offsets) {
+        for (var i=0; i<$.jqplot.preDrawLegendHooks.length; i++){
+            $.jqplot.preDrawLegendHooks[i].call(this, offsets);
+        }
         return this.renderer.draw.call(this, offsets);
+        for (var i=0; i<$.jqplot.postDrawLegendHooks.length; i++){
+            $.jqplot.postDrawLegendHooks[i].call(this, offsets);
+        }
     };
     
     Legend.prototype.pack = function(offsets) {

File src/jqplot.lineRenderer.js

         var pointx, pointy;
         var opts = (options != undefined) ? options : {};
         var shadow = (opts.shadow != undefined) ? opts.shadow : this.shadow;
+        var showLine = (opts.showLine != undefined) ? opts.showLine : this.showLine;
         ctx.save();
-        if (this.showLine) {
+        if (showLine) {
             this.renderer.shapeRenderer.draw(ctx, gd);
         
             // now draw the shadows

File src/jqplot.tableLegendRenderer.js

                         addrow.call(this, lt, s.color, pad);
                         pad = true;
                     }
-                    for (var j=0; j<$.jqplot.drawLegendHooks.length; j++) {
-                        var item = $.jqplot.drawLegendHooks[j].call(this, s);
+                    for (var j=0; j<$.jqplot.addLegendRowHooks.length; j++) {
+                        var item = $.jqplot.addLegendRowHooks[j].call(this, s);
                         if (item) {
                             addrow(item.label, item.color, pad);
                             pad = true;

File src/plugins/jqplot.trendLines.js

 (function($) {
-    var debug = 1;
-	// Convienence function that won't hang IE.
-	function log(something) {
-	    if (window.console && debug) {
-	        console.log(something);
-	    }
-	};
     
-    $.jqplot.postParseOptionsHooks.push(parseTrendLineOptions);
-    $.jqplot.postDrawSeriesHooks.push(drawTrendLines);
-    $.jqplot.drawLegendHooks.push(addTrendLineLegend);
+    $.jqplot.Trendline = function() {
+        this.show = true;
+        this.color = '#666666';
+        this.rendererOptions:{marker:{show:false}};
+        this.label:'';
+        this.type = 'linear';
+        // don't rely on series renderer, it may not draw lines.
+        this.renderer = new $.jqplot.lineRenderer();
+        this.shadow = true;
+    };
+    
+    $.jqplot.postParseSeriesOptionsHooks.push(parseTrendLineOptions);
+    $.jqplot.postDrawSeriesHooks.push(drawTrendLine);
+    $.jqplot.addLegendRowHooks.push(drawTrendLineLegend);
     
     // called witin scope of the legend object
     // current series passed in
     // must return null or an object {label:label, color:color}
     function addTrendLineLegend(series) {
-        var lt = series.trendLines.label.toString();
+        var lt = series.trendline.label.toString();
         var ret = null;
-        if (series.trendLines.show && lt) ret = {label:lt, color:series.trendLines.color};
+        if (series.trendline.show && lt) ret = {label:lt, color:series.trendline.color};
         return ret;
-    }
+    };
 
-    // called within scope of ._jqPlot
-    function parseTrendLineOptions () {
-        var s, i;
-        var series = this.series;
-        var options = this.options;
-        var defaults = {
-            show: false,
-            color: '#666666',
-            rendererOptions:{marker:{show:false}},
-            // lineWidth:2.5,
-            // shadows:true,
-            // // shadow angle in degrees
-            // shadowAngle:45,
-            // // shadow offset in pixels
-            // shadowOffset:1,
-            // shadowDepth:3,
-            // shadowAlpha:'0.07',
-            label:'',
-            // linear or exp or exponential
-            type: 'linear'
-        };
-        for (i = 0; i < series.length; ++i) {
-            s = series[i];
-        	// merge user supplied trendline options with defaults.
-        	if (!s.hasOwnProperty('trendLines')) s.trendLines = {};
-        	s.trendLines = $.extend(true, {}, defaults, options.trendLines, s.trendLines);
-    	}
+    // called within scope of a series
+    function parseTrendLineOptions (seriesDefaults, options) {
+        this.trendline = new $.jqplot.Trendline();
+        $.extend(true, this.trendline, seriesDefaults.trendline, options.trendline);
     };
     
     // called within scope of series object
-    function drawTrendLines(grid, ctx) {
-        var fit;
-        if (this.trendLines.show) {
-            fit = fitData(this.data, this.trendLines.type);
-            // make a trendline series
-            var tl = $.extend(true, {}, this, this.trendLines);
-            tl.data = fit.data;
-            tl.gridData = [];
-            var lineRenderer = new $.jqplot.lineRenderer();
-            lineRenderer.init(tl.trendLines.rendererOptions);
-            lineRenderer.draw.call(lineRenderer, tl, grid, ctx); 
-        }  
-    }
+    function drawTrendLines(sctx, options) {
+        if (this.trendline.show) {
+            var fit;
+            // this.renderer.setGridData.call(this);
+            var data = options.data || this.data;
+            fit = fitData(data, this.trendline.type);
+            var gridData = options.gridData || this.renderer.makeGridData.call(this, fit.data);
+        
+            this.trendline.renderer.draw.call(this, sctx, gridData, {showline:true, shadow:this.trendline.shadow});
+        }
+    };
 	
 	function regression(x, y, type)  {
 		var type = (type == null) ? 'linear' : type;