From: Evert Timberg Date: Sun, 8 May 2016 01:28:13 +0000 (-0400) Subject: Ensure that when calculating tick width in the bar controller, we use the number... X-Git-Tag: v2.1.2~1^2 X-Git-Url: http://git.ipfire.org/gitweb/?a=commitdiff_plain;h=refs%2Fpull%2F2493%2Fhead;p=thirdparty%2FChart.js.git Ensure that when calculating tick width in the bar controller, we use the number of ticks in the scale, not the number of data points. --- diff --git a/src/controllers/controller.bar.js b/src/controllers/controller.bar.js index 0af546767..0f7d4a117 100644 --- a/src/controllers/controller.bar.js +++ b/src/controllers/controller.bar.js @@ -180,7 +180,7 @@ module.exports = function(Chart) { var tickWidth = (function() { var min = xScale.getPixelForTick(1) - xScale.getPixelForTick(0); - for (var i = 2; i < this.getDataset().data.length; i++) { + for (var i = 2; i < xScale.ticks.length; i++) { min = Math.min(xScale.getPixelForTick(i) - xScale.getPixelForTick(i - 1), min); } return min; @@ -318,17 +318,17 @@ module.exports = function(Chart) { // including horizontalBar in the bar file, instead of a file of its own // it extends bar (like pie extends doughnut) - Chart.defaults.horizontalBar = { - hover: { - mode: "label" + Chart.defaults.horizontalBar = { + hover: { + mode: "label" }, - scales: { - xAxes: [{ + scales: { + xAxes: [{ type: "linear", - position: "bottom" - }], - yAxes: [{ + position: "bottom" + }], + yAxes: [{ position: "left", type: "category", @@ -337,15 +337,15 @@ module.exports = function(Chart) { barPercentage: 0.9, // grid line settings - gridLines: { - offsetGridLines: true - } + gridLines: { + offsetGridLines: true + } }] - }, + }, }; - Chart.controllers.horizontalBar = Chart.controllers.bar.extend({ - updateElement: function updateElement(rectangle, index, reset, numBars) { + Chart.controllers.horizontalBar = Chart.controllers.bar.extend({ + updateElement: function updateElement(rectangle, index, reset, numBars) { var meta = this.getMeta(); var xScale = this.getScaleForId(meta.xAxisID); var yScale = this.getScaleForId(meta.yAxisID); @@ -354,11 +354,11 @@ module.exports = function(Chart) { if (xScale.min < 0 && xScale.max < 0) { // all less than 0. use the right - xScalePoint = xScale.getPixelForValue(xScale.max); - } else if (xScale.min > 0 && xScale.max > 0) { - xScalePoint = xScale.getPixelForValue(xScale.min); - } else { - xScalePoint = xScale.getPixelForValue(0); + xScalePoint = xScale.getPixelForValue(xScale.max); + } else if (xScale.min > 0 && xScale.max > 0) { + xScalePoint = xScale.getPixelForValue(xScale.min); + } else { + xScalePoint = xScale.getPixelForValue(0); } helpers.extend(rectangle, { @@ -370,7 +370,7 @@ module.exports = function(Chart) { _index: index, // Desired view properties - _model: { + _model: { x: reset ? xScalePoint : this.calculateBarX(index, this.index), y: this.calculateBarY(index, this.index), @@ -384,10 +384,10 @@ module.exports = function(Chart) { backgroundColor: rectangle.custom && rectangle.custom.backgroundColor ? rectangle.custom.backgroundColor : helpers.getValueAtIndexOrDefault(this.getDataset().backgroundColor, index, this.chart.options.elements.rectangle.backgroundColor), borderSkipped: rectangle.custom && rectangle.custom.borderSkipped ? rectangle.custom.borderSkipped : this.chart.options.elements.rectangle.borderSkipped, borderColor: rectangle.custom && rectangle.custom.borderColor ? rectangle.custom.borderColor : helpers.getValueAtIndexOrDefault(this.getDataset().borderColor, index, this.chart.options.elements.rectangle.borderColor), - borderWidth: rectangle.custom && rectangle.custom.borderWidth ? rectangle.custom.borderWidth : helpers.getValueAtIndexOrDefault(this.getDataset().borderWidth, index, this.chart.options.elements.rectangle.borderWidth) + borderWidth: rectangle.custom && rectangle.custom.borderWidth ? rectangle.custom.borderWidth : helpers.getValueAtIndexOrDefault(this.getDataset().borderWidth, index, this.chart.options.elements.rectangle.borderWidth) }, - draw: function () { + draw: function () { var ctx = this._chart.ctx; var vm = this._view; @@ -400,10 +400,10 @@ module.exports = function(Chart) { // Canvas doesn't allow us to stroke inside the width so we can // adjust the sizes to fit if we're setting a stroke on the line - if (vm.borderWidth) { + if (vm.borderWidth) { topY += halfStroke; bottomY -= halfStroke; - right += halfStroke; + right += halfStroke; } ctx.beginPath(); @@ -428,8 +428,8 @@ module.exports = function(Chart) { if (startCorner === -1) startCorner = 0; - function cornerAt(index) { - return corners[(startCorner + index) % 4]; + function cornerAt(index) { + return corners[(startCorner + index) % 4]; } // Draw rectangle from 'startCorner' @@ -438,151 +438,151 @@ module.exports = function(Chart) { ctx.lineTo.apply(ctx, cornerAt(i)); ctx.fill(); - if (vm.borderWidth) { - ctx.stroke(); - } + if (vm.borderWidth) { + ctx.stroke(); + } }, - inRange: function (mouseX, mouseY) { + inRange: function (mouseX, mouseY) { var vm = this._view; var inRange = false; - if (vm) { - if (vm.x < vm.base) { - inRange = (mouseY >= vm.y - vm.height / 2 && mouseY <= vm.y + vm.height / 2) && (mouseX >= vm.x && mouseX <= vm.base); - } else { - inRange = (mouseY >= vm.y - vm.height / 2 && mouseY <= vm.y + vm.height / 2) && (mouseX >= vm.base && mouseX <= vm.x); - } + if (vm) { + if (vm.x < vm.base) { + inRange = (mouseY >= vm.y - vm.height / 2 && mouseY <= vm.y + vm.height / 2) && (mouseX >= vm.x && mouseX <= vm.base); + } else { + inRange = (mouseY >= vm.y - vm.height / 2 && mouseY <= vm.y + vm.height / 2) && (mouseX >= vm.base && mouseX <= vm.x); + } } - return inRange; - } + return inRange; + } }); - rectangle.pivot(); + rectangle.pivot(); }, - calculateBarBase: function (datasetIndex, index) { + calculateBarBase: function (datasetIndex, index) { var meta = this.getMeta(); var xScale = this.getScaleForId(meta.xAxisID); var yScale = this.getScaleForId(meta.yAxisID); var base = 0; - if (xScale.options.stacked) { + if (xScale.options.stacked) { var value = this.chart.data.datasets[datasetIndex].data[index]; - if (value < 0) { - for (var i = 0; i < datasetIndex; i++) { + if (value < 0) { + for (var i = 0; i < datasetIndex; i++) { var negDS = this.chart.data.datasets[i]; var negDSMeta = this.chart.getDatasetMeta(i); - if (negDSMeta.bar && negDSMeta.xAxisID === xScale.id && this.chart.isDatasetVisible(i)) { - base += negDS.data[index] < 0 ? negDS.data[index] : 0; - } - } - } else { - for (var j = 0; j < datasetIndex; j++) { + if (negDSMeta.bar && negDSMeta.xAxisID === xScale.id && this.chart.isDatasetVisible(i)) { + base += negDS.data[index] < 0 ? negDS.data[index] : 0; + } + } + } else { + for (var j = 0; j < datasetIndex; j++) { var posDS = this.chart.data.datasets[j]; var posDSMeta = this.chart.getDatasetMeta(j); - if (posDSMeta.bar && posDSMeta.xAxisID === xScale.id && this.chart.isDatasetVisible(j)) { - base += posDS.data[index] > 0 ? posDS.data[index] : 0; - } - } + if (posDSMeta.bar && posDSMeta.xAxisID === xScale.id && this.chart.isDatasetVisible(j)) { + base += posDS.data[index] > 0 ? posDS.data[index] : 0; + } + } } - return xScale.getPixelForValue(base); + return xScale.getPixelForValue(base); } base = xScale.getPixelForValue(xScale.min); - if (xScale.beginAtZero || ((xScale.min <= 0 && xScale.max >= 0) || (xScale.min >= 0 && xScale.max <= 0))) { - base = xScale.getPixelForValue(0, 0); + if (xScale.beginAtZero || ((xScale.min <= 0 && xScale.max >= 0) || (xScale.min >= 0 && xScale.max <= 0))) { + base = xScale.getPixelForValue(0, 0); } else if (xScale.min < 0 && xScale.max < 0) { // All values are negative. Use the right as the base - base = xScale.getPixelForValue(xScale.max); + base = xScale.getPixelForValue(xScale.max); } - return base; + return base; }, - getRuler: function () { + getRuler: function () { var meta = this.getMeta(); var xScale = this.getScaleForId(meta.xAxisID); var yScale = this.getScaleForId(meta.yAxisID); var datasetCount = this.getBarCount(); - var tickHeight = (function () { + var tickHeight = (function () { var min = yScale.getPixelForTick(1) - yScale.getPixelForTick(0); - for (var i = 2; i < this.getDataset().data.length; i++) { - min = Math.min(yScale.getPixelForTick(i) - yScale.getPixelForTick(i - 1), min); + for (var i = 2; i < this.getDataset().data.length; i++) { + min = Math.min(yScale.getPixelForTick(i) - yScale.getPixelForTick(i - 1), min); } - return min; + return min; }).call(this); var categoryHeight = tickHeight * yScale.options.categoryPercentage; var categorySpacing = (tickHeight - (tickHeight * yScale.options.categoryPercentage)) / 2; var fullBarHeight = categoryHeight / datasetCount; - if (yScale.ticks.length !== this.chart.data.labels.length) { + if (yScale.ticks.length !== this.chart.data.labels.length) { var perc = yScale.ticks.length / this.chart.data.labels.length; - fullBarHeight = fullBarHeight * perc; + fullBarHeight = fullBarHeight * perc; } var barHeight = fullBarHeight * yScale.options.barPercentage; var barSpacing = fullBarHeight - (fullBarHeight * yScale.options.barPercentage); - return { + return { datasetCount: datasetCount, tickHeight: tickHeight, categoryHeight: categoryHeight, categorySpacing: categorySpacing, fullBarHeight: fullBarHeight, barHeight: barHeight, - barSpacing: barSpacing, - }; + barSpacing: barSpacing, + }; }, - calculateBarHeight: function () { + calculateBarHeight: function () { var yScale = this.getScaleForId(this.getMeta().yAxisID); var ruler = this.getRuler(); - return yScale.options.stacked ? ruler.categoryHeight : ruler.barHeight; + return yScale.options.stacked ? ruler.categoryHeight : ruler.barHeight; }, - calculateBarX: function (index, datasetIndex) { + calculateBarX: function (index, datasetIndex) { var meta = this.getMeta(); var xScale = this.getScaleForId(meta.xAxisID); var yScale = this.getScaleForId(meta.yAxisID); var value = this.getDataset().data[index]; - if (xScale.options.stacked) { - + if (xScale.options.stacked) { + var sumPos = 0, sumNeg = 0; - for (var i = 0; i < datasetIndex; i++) { + for (var i = 0; i < datasetIndex; i++) { var ds = this.chart.data.datasets[i]; var dsMeta = this.chart.getDatasetMeta(i); - if (dsMeta.bar && dsMeta.xAxisID === xScale.id && this.chart.isDatasetVisible(i)) { - if (ds.data[index] < 0) { - sumNeg += ds.data[index] || 0; - } else { - sumPos += ds.data[index] || 0; - } - } + if (dsMeta.bar && dsMeta.xAxisID === xScale.id && this.chart.isDatasetVisible(i)) { + if (ds.data[index] < 0) { + sumNeg += ds.data[index] || 0; + } else { + sumPos += ds.data[index] || 0; + } + } } - if (value < 0) { - return xScale.getPixelForValue(sumNeg + value); - } else { - return xScale.getPixelForValue(sumPos + value); + if (value < 0) { + return xScale.getPixelForValue(sumNeg + value); + } else { + return xScale.getPixelForValue(sumPos + value); } } - return xScale.getPixelForValue(value); + return xScale.getPixelForValue(value); }, - calculateBarY: function (index, datasetIndex) { + calculateBarY: function (index, datasetIndex) { var meta = this.getMeta(); var yScale = this.getScaleForId(meta.yAxisID); var xScale = this.getScaleForId(meta.xAxisID); @@ -592,8 +592,8 @@ module.exports = function(Chart) { var topTick = yScale.getPixelForValue(null, index, datasetIndex, this.chart.isCombo); topTick -= this.chart.isCombo ? (ruler.tickHeight / 2) : 0; - if (yScale.options.stacked) { - return topTick + (ruler.categoryHeight / 2) + ruler.categorySpacing; + if (yScale.options.stacked) { + return topTick + (ruler.categoryHeight / 2) + ruler.categorySpacing; } return topTick + @@ -601,7 +601,7 @@ module.exports = function(Chart) { ruler.categorySpacing + (ruler.barHeight * barIndex) + (ruler.barSpacing / 2) + - (ruler.barSpacing * barIndex); - } + (ruler.barSpacing * barIndex); + } }); };