From e8b3c7b4e228eeff222af97b075ad6d417913556 Mon Sep 17 00:00:00 2001 From: David Eberlein Date: Thu, 25 Apr 2013 09:11:49 +0200 Subject: [PATCH] REFACTORING: cleaned up code and style for new grid per axis feature. --- dygraph.js | 6 +++--- plugins/grid.js | 29 +++++++++++++++-------------- 2 files changed, 18 insertions(+), 17 deletions(-) diff --git a/dygraph.js b/dygraph.js index cc983a6..82725d5 100644 --- a/dygraph.js +++ b/dygraph.js @@ -2732,12 +2732,12 @@ Dygraph.prototype.computeYAxisRanges_ = function(extremes) { opts, this); // Define the first independent axis as primary axis. - if(!p_axis) - p_axis = axis; + if (!p_axis) p_axis = axis; } } - if(p_axis === undefined) + if (p_axis === undefined) { throw ("Configuration Error: At least one axis has to have the \"independentTicks\" option activated."); + } // Add ticks. By default, all axes inherit the tick positions of the // primary axis. However, if an axis is specifically marked as having // independent ticks, then that is permissible as well. diff --git a/plugins/grid.js b/plugins/grid.js index 06bfcd3..425d93f 100644 --- a/plugins/grid.js +++ b/plugins/grid.js @@ -50,20 +50,20 @@ grid.prototype.willDrawChart = function(e) { var x, y, i, ticks; if (g.getOption('drawYGrid')) { - var axes = ["y","y2"]; - var strokeStyles = [], lineWidths = [], drawGrid = [], stroking = [], strokePattern =[]; - for(var index in axes) { - drawGrid[index] = g.getOptionForAxis("drawGrid",axes[index]); - if(drawGrid[index]) { - strokeStyles[index] = g.getOptionForAxis('gridLineColor',axes[index]); - lineWidths[index] = g.getOptionForAxis('gridLineWidth',axes[index]); - strokePattern[index] = g.getOptionForAxis('gridLinePattern',axes[index]); - stroking[index] = strokePattern[index] && (strokePattern[index].length >= 2); + var axes = ["y", "y2"]; + var strokeStyles = [], lineWidths = [], drawGrid = [], stroking = [], strokePattern = []; + for (var i = 0; i < axes.length; i++) { + drawGrid[i] = g.getOptionForAxis("drawGrid", axes[i]); + if (drawGrid[i]) { + strokeStyles[i] = g.getOptionForAxis('gridLineColor', axes[i]); + lineWidths[i] = g.getOptionForAxis('gridLineWidth', axes[i]); + strokePattern[i] = g.getOptionForAxis('gridLinePattern', axes[i]); + stroking[i] = strokePattern[i] && (strokePattern[i].length >= 2); } } ticks = layout.yticks; ctx.save(); - // draw grids for the differen axes + // draw grids for the different y axes for (i = 0; i < ticks.length; i++) { var axis = ticks[i][0]; if(drawGrid[axis]) { @@ -89,16 +89,17 @@ grid.prototype.willDrawChart = function(e) { ctx.restore(); } - if (g.getOption('drawXGrid') && g.getOptionForAxis("drawGrid",'x')) { + // draw grid for x axis + if (g.getOption('drawXGrid') && g.getOptionForAxis("drawGrid", 'x')) { ticks = layout.xticks; ctx.save(); - var strokePattern = g.getOptionForAxis('gridLinePattern','x'); + var strokePattern = g.getOptionForAxis('gridLinePattern', 'x'); var stroking = strokePattern && (strokePattern.length >= 2); if (stroking) { ctx.installPattern(strokePattern); } - ctx.strokeStyle = g.getOptionForAxis('gridLineColor','x'); - ctx.lineWidth = g.getOptionForAxis('gridLineWidth','x'); + ctx.strokeStyle = g.getOptionForAxis('gridLineColor', 'x'); + ctx.lineWidth = g.getOptionForAxis('gridLineWidth', 'x'); for (i = 0; i < ticks.length; i++) { x = halfUp(area.x + ticks[i][0] * area.w); y = halfDown(area.y + area.h); -- 2.7.4