From 07827d81c8a6d42d9fbd8dbab943c7312ffc6eee Mon Sep 17 00:00:00 2001 From: Adam Pine Date: Fri, 8 Jul 2016 14:06:43 -0400 Subject: [PATCH 1/6] adds label option to Line annotation. --- Chart.Annotation.js | 25 ++++++++++++++++++++----- Chart.Annotation.min.js | 2 +- samples/horizontal-line.html | 5 +++-- src/box.js | 3 ++- src/chart.annotation.js | 6 ++++-- src/line.js | 16 ++++++++++++++-- 6 files changed, 44 insertions(+), 13 deletions(-) diff --git a/Chart.Annotation.js b/Chart.Annotation.js index 54825850b..11e4cbb36 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; @@ -151,11 +152,13 @@ var AnnotationPlugin = Chart.PluginBase.extend({ afterDraw: 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); }); } } @@ -172,9 +175,12 @@ module.exports = function(Chart) { var LineAnnotation = Chart.Element.extend({ - draw: function(ctx) { + 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; @@ -184,10 +190,16 @@ module.exports = function(Chart) { ctx.moveTo(view.x1, view.y1); ctx.lineTo(view.x2, view.y2); ctx.stroke(); + + ctx.fillText(options.label ? options.label : 'test', chartArea.left, pixel+5); + + console.info(ctx); + } }); function lineUpdate(obj, options, chartInstance) { + console.info(chartInstance.chart.ctx); var model = obj._model = obj._model || {}; var scale = chartInstance.scales[options.scaleID]; @@ -199,15 +211,18 @@ module.exports = function(Chart) { 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; + } diff --git a/Chart.Annotation.min.js b/Chart.Annotation.min.js index 70196460d..5d6689b5e 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 - +