diff --git a/Chart.Annotation.js b/Chart.Annotation.js index 54825850b..30b9bf67a 100644 --- a/Chart.Annotation.js +++ b/Chart.Annotation.js @@ -13,8 +13,9 @@ // Box Annotation implementation module.exports = function(Chart) { var BoxAnnotation = Chart.Element.extend({ - draw: function(ctx) { + draw: function(chartInstance, options) { var view = this._view; + var ctx = chartInstance.chart.ctx; // Canvas setup ctx.lineWidth = view.borderWidth; @@ -111,7 +112,7 @@ var updateFunctions = Chart.Annotation.updateFunctions = { box: boxAnnotation.update }; -// Chartjs Zoom Plugin +// Chartjs Plugin hooks. var AnnotationPlugin = Chart.PluginBase.extend({ beforeInit: function(chartInstance) { var options = chartInstance.options; @@ -148,14 +149,16 @@ var AnnotationPlugin = Chart.PluginBase.extend({ } }, - afterDraw: function(chartInstance, easingDecimal) { + beforeDraw: function(chartInstance, easingDecimal) { // If we have annotations, draw them var annotationObjects = chartInstance._annotationObjects; + var annotationOpts = chartInstance.options.annotation; + if (isArray(annotationObjects)) { var ctx = chartInstance.chart.ctx; - annotationObjects.forEach(function(obj) { - obj.transition(easingDecimal).draw(ctx); + var opts = annotationOpts.annotations[obj._index]; + obj.transition(easingDecimal).draw(chartInstance, opts); }); } } @@ -166,55 +169,74 @@ Chart.pluginService.register(new AnnotationPlugin()); },{"./box.js":2,"./line.js":4,"chart.js":1}],4:[function(require,module,exports){ // Line Annotation implementation -module.exports = function(Chart) { - var horizontalKeyword = 'horizontal'; - var verticalKeyword = 'vertical'; - - var LineAnnotation = Chart.Element.extend({ - - draw: function(ctx) { - var view = this._view; - - // Canvas setup - ctx.lineWidth = view.borderWidth; - ctx.strokeStyle = view.borderColor; - - // Draw - ctx.beginPath(); - ctx.moveTo(view.x1, view.y1); - ctx.lineTo(view.x2, view.y2); - ctx.stroke(); - } - }); - - function lineUpdate(obj, options, chartInstance) { - var model = obj._model = obj._model || {}; - - var scale = chartInstance.scales[options.scaleID]; - var pixel = scale ? scale.getPixelForValue(options.value) : NaN; - var chartArea = chartInstance.chartArea; - - if (!isNaN(pixel)) { - if (options.mode == horizontalKeyword) { - model.x1 = chartArea.left; - model.x2 = chartArea.right; - model.y1 = model.y2 = pixel; - } else { - model.y1 = chartArea.top; - model.y2 = chartArea.bottom; - model.x1 = model.x2 = pixel; - } - } - - model.borderColor = options.borderColor; - model.borderWidth = options.borderWidth; - } - - - return { - Constructor: LineAnnotation, - update: lineUpdate - }; +module.exports = function (Chart) { + var horizontalKeyword = 'horizontal'; + var verticalKeyword = 'vertical'; + + var LineAnnotation = Chart.Element.extend({ + + draw: function (chartInstance, options) { + var view = this._view; + var ctx = chartInstance.chart.ctx; + var scale = chartInstance.scales[options.scaleID]; + var pixel = scale ? scale.getPixelForValue(options.value) : NaN; + var chartArea = chartInstance.chartArea; + // Canvas setup + ctx.lineWidth = view.borderWidth; + ctx.strokeStyle = view.borderColor; + + // Draw + ctx.beginPath(); + ctx.moveTo(view.x1, view.y1); + ctx.lineTo(view.x2, view.y2); + ctx.stroke(); + //Verify that label has been specified, so that we don't add a null label. + //TODO: Added customizable badge options, like: where to display the badge, left/right/center. or, Badge Border Color and Badge Background color. + //I would also like to add an option for different directional offsets, horizontalOffset, and VertOffset for example. + //Current issue with background color is that it likes to disappear when you hover over any other element on the chart. + if (options.label) { + //draw the background of the badge. + ctx.fillStyle = 'white'; + ctx.fillRect((chartArea.right / 2), pixel - 5, 35, 15); + //Draw the border around the badge. + ctx.fillStyle = view.borderColor; + ctx.strokeRect((chartArea.right / 2), pixel - 5, 35, 15); + //Draw the text of the badge. + ctx.fillStyle = 'black'; + ctx.textAlign = 'left'; + ctx.fillText(options.label ? options.label : '', (chartArea.right / 2) + 5, pixel - 5); + } + } + }); + + function lineUpdate(obj, options, chartInstance) { + var model = obj._model = obj._model || {}; + + var scale = chartInstance.scales[options.scaleID]; + var pixel = scale ? scale.getPixelForValue(options.value) : NaN; + var chartArea = chartInstance.chartArea; + + if (!isNaN(pixel)) { + if (options.mode == horizontalKeyword) { + model.x1 = chartArea.left; + model.x2 = chartArea.right; + model.y1 = model.y2 = pixel; + + } else { + model.y1 = chartArea.top; + model.y2 = chartArea.bottom; + model.x1 = model.x2 = pixel; + } + } + model.borderColor = options.borderColor; + model.borderWidth = options.borderWidth; + + } + + + return { + Constructor: LineAnnotation, + update: lineUpdate + }; }; - },{}]},{},[3]); diff --git a/Chart.Annotation.min.js b/Chart.Annotation.min.js index 70196460d..b730cb709 100644 --- a/Chart.Annotation.min.js +++ b/Chart.Annotation.min.js @@ -7,4 +7,4 @@ * Released under the MIT license * https://github.com/chartjs/Chart.Annotation.js/blob/master/LICENSE.md */ -!function t(o,n,r){function e(i,l){if(!n[i]){if(!o[i]){var u="function"==typeof require&&require;if(!l&&u)return u(i,!0);if(a)return a(i,!0);var c=new Error("Cannot find module '"+i+"'");throw c.code="MODULE_NOT_FOUND",c}var f=n[i]={exports:{}};o[i][0].call(f.exports,function(t){var n=o[i][1][t];return e(n?n:t)},f,f.exports,t,o,n,r)}return n[i].exports}for(var a="function"==typeof require&&require,i=0;i Scatter Chart - +