From af86650c391894bf882a00b42822fc49c0d84e8c Mon Sep 17 00:00:00 2001 From: wimrijnders Date: Thu, 2 Mar 2017 16:30:41 +0100 Subject: [PATCH] Fix for issue #2536 (#2803) * Fix for issue #2536 * Adjusted documentation for fix. * Adjustments due to review * Grrrrr whitespace * Fixed Travis issue --- docs/graph3d/index.html | 8 ++++---- lib/graph3d/Graph3d.js | 37 +++++++++++++++++++++++++++++++------ 2 files changed, 35 insertions(+), 10 deletions(-) diff --git a/docs/graph3d/index.html b/docs/graph3d/index.html index fd9ba49a..974e2d27 100644 --- a/docs/graph3d/index.html +++ b/docs/graph3d/index.html @@ -532,8 +532,8 @@ var options = { xBarWidth number none - The width of bars in x direction. By default, the width is equal to the distance - between the data points, such that bars adjoin each other. + The width of bars in x direction. By default, the width is equal to the smallest distance + between the data points. Only applicable for styles 'bar' and 'bar-color'. @@ -575,8 +575,8 @@ var options = { yBarWidth number none - The width of bars in y direction. By default, the width is equal to the distance - between the data points, such that bars adjoin each other. + The width of bars in y direction. By default, the width is equal to the smallest distance + between the data points. Only applicable for styles 'bar' and 'bar-color'. diff --git a/lib/graph3d/Graph3d.js b/lib/graph3d/Graph3d.js index e355c9db..7a1884cc 100755 --- a/lib/graph3d/Graph3d.js +++ b/lib/graph3d/Graph3d.js @@ -327,7 +327,34 @@ Graph3d.prototype.getDistinctValues = function(data, column) { distinctValues.push(data[i][column]); } } - return distinctValues; + return distinctValues.sort(function(a,b) { return a - b; }); +} + + +/** + * Determine the smallest difference between the values for given + * column in the passed data set. + * + * @returns {Number|null} Smallest difference value or + * null, if it can't be determined. + */ +Graph3d.prototype.getSmallestDifference = function(data, column) { + var values = this.getDistinctValues(data, column); + var diffs = []; + + // Get all the distinct diffs + // Array values is assumed to be sorted here + var smallest_diff = null; + + for (var i = 1; i < values.length; i++) { + var diff = values[i] - values[i - 1]; + + if (smallest_diff == null || smallest_diff > diff ) { + smallest_diff = diff; + } + } + + return smallest_diff; } @@ -467,16 +494,14 @@ Graph3d.prototype._dataInitialize = function (rawData, style) { this.xBarWidth = this.defaultXBarWidth; } else { - var dataX = this.getDistinctValues(data,this.colX); - this.xBarWidth = (dataX[1] - dataX[0]) || 1; + this.xBarWidth = this.getSmallestDifference(data, this.colX) || 1; } if (this.defaultYBarWidth !== undefined) { this.yBarWidth = this.defaultYBarWidth; } else { - var dataY = this.getDistinctValues(data,this.colY); - this.yBarWidth = (dataY[1] - dataY[0]) || 1; + this.yBarWidth = this.getSmallestDifference(data, this.colY) || 1; } } @@ -2449,4 +2474,4 @@ Graph3d.prototype.setSize = function(width, height) { // ----------------------------------------------------------------------------- -module.exports = Graph3d; \ No newline at end of file +module.exports = Graph3d;