diff --git a/.gitignore b/.gitignore index 25a1ef4..3af0387 100644 --- a/.gitignore +++ b/.gitignore @@ -6,3 +6,5 @@ custom/* docs/index.md bower_components + +.idea diff --git a/bower.json b/bower.json index b7b5182..55005b6 100644 --- a/bower.json +++ b/bower.json @@ -1,6 +1,6 @@ { "name": "Chart.StackedBar.js", - "version": "1.0.3", + "version": "1.2.1", "description": "StackedBar implementation for Chart.js", "homepage": "https://github.com/nnnick/Chart.js", "author": "Regaddi", diff --git a/package.json b/package.json index 8dae174..fe09c36 100644 --- a/package.json +++ b/package.json @@ -3,13 +3,13 @@ "homepage": "http://www.chartjs.org", "description": "StackedBar implementation for Chart.js", "private": true, - "version": "1.0.3", + "version": "1.2.1", "repository": { "type": "git", "url": "https://github.com/Regaddi/Chart.StackedBar.js.git" }, "dependences": { - "Chart.js": ">= 1.0.0-beta" + "Chart.js": ">= 1.0.2" }, "main": "src/Chart.StackedBar.js", "devDependencies": { diff --git a/src/Chart.StackedBar.js b/src/Chart.StackedBar.js index a452f04..2c02145 100644 --- a/src/Chart.StackedBar.js +++ b/src/Chart.StackedBar.js @@ -13,7 +13,37 @@ }(function (Chart) { "use strict"; - var helpers = Chart.helpers; + var helpers = Chart.helpers; + + function drawLine(ctx, scale, annotation) { + ctx.beginPath(); + ctx.moveTo(scale.calculateBarX(annotation.value), scale.startPoint); + ctx.strokeStyle = annotation.lineColor; + ctx.lineTo(scale.calculateBarX(annotation.value), scale.endPoint); + ctx.stroke(); + } + + function drawLabel(ctx, scale, annotation) { + ctx.textAlign = 'left'; + ctx.fillStyle = annotation.labelColor; + ctx.fillText(annotation.labelText, scale.calculateBarX(annotation.value), scale.startPoint - 5); + } + + function drawAnnotations(chartType) { + var annotations = chartType.options.annotation ? chartType.options.annotation.annotations : []; + var datasets = chartType.datasets; + var scale = chartType.scale; + var ctx = chartType.chart.ctx; + + helpers.each(annotations, function (annotation) { + if (annotation.type !== 'line') return; + + helpers.each(datasets, function (dataset) { + drawLine(ctx, scale, annotation); + drawLabel(ctx, scale, annotation); + }); + }); + } var defaultConfig = { scaleBeginAtZero : true, @@ -279,12 +309,14 @@ total.value += element.value; - //Include any colour information about the element - tooltipLabels.push(helpers.template(this.options.multiTooltipTemplate, element)); - tooltipColors.push({ - fill: element._saved.fillColor || element.fillColor, - stroke: element._saved.strokeColor || element.strokeColor - }); + //Include any colour information about the element(s) + if (element.value !== 0) { + tooltipLabels.push(helpers.template(this.options.multiTooltipTemplate, element)); + tooltipColors.push({ + fill: element._saved.fillColor || element.fillColor, + stroke: element._saved.strokeColor || element.strokeColor + }); + } }, this); @@ -531,7 +563,9 @@ var ctx = this.chart.ctx; - this.scale.draw(easingDecimal); + this.scale.draw(easingDecimal); + + drawAnnotations(this); //Draw all the bars for each dataset helpers.each(this.datasets,function(dataset,datasetIndex){