From c81a14c1c06109b5e95740f986c0c164bc6f26de Mon Sep 17 00:00:00 2001 From: Brad Overton Date: Thu, 22 Nov 2018 08:28:59 +0000 Subject: [PATCH 1/4] Add display option, to optionally hide annotations --- src/helpers.js | 3 ++- src/index.js | 5 +++++ src/types/box.js | 14 ++++++++++---- src/types/line.js | 7 +++++++ 4 files changed, 24 insertions(+), 5 deletions(-) diff --git a/src/helpers.js b/src/helpers.js index 094d80482..968b9c3a4 100644 --- a/src/helpers.js +++ b/src/helpers.js @@ -91,8 +91,9 @@ module.exports = function(Chart) { function initConfig(config) { config = chartHelpers.configMerge(Chart.Annotation.defaults, config); if (chartHelpers.isArray(config.annotations)) { - config.annotations.forEach(function(annotation) { + config.annotations = config.annotations.map(function(annotation) { annotation.label = chartHelpers.configMerge(Chart.Annotation.labelDefaults, annotation.label); + return chartHelpers.configMerge(Chart.Annotation.annotationDefaults, annotation); }); } return config; diff --git a/src/index.js b/src/index.js index 89d9b3699..9d584fe14 100644 --- a/src/index.js +++ b/src/index.js @@ -12,12 +12,17 @@ Chart.Annotation.drawTimeOptions = { }; Chart.Annotation.defaults = { + display: true, drawTime: 'afterDatasetsDraw', dblClickSpeed: 350, // ms events: [], annotations: [] }; +Chart.Annotation.annotationDefaults = { + display: true +}; + Chart.Annotation.labelDefaults = { backgroundColor: 'rgba(0,0,0,0.8)', fontFamily: Chart.defaults.global.defaultFontFamily, diff --git a/src/types/box.js b/src/types/box.js index 2df0d31b9..dd58928ad 100644 --- a/src/types/box.js +++ b/src/types/box.js @@ -86,10 +86,16 @@ module.exports = function(Chart) { model.right = right; model.bottom = bottom; - // Stylistic options - model.borderColor = options.borderColor; - model.borderWidth = options.borderWidth; - model.backgroundColor = options.backgroundColor; + // Hide if display disabled + if(options.display && chartInstance.annotation.options.display) { + model.borderColor = options.borderColor; + model.borderWidth = options.borderWidth; + model.backgroundColor = options.backgroundColor; + } else { + model.borderColor = 'rgba(0,0,0,0)'; + model.borderWidth = 0; + model.backgroundColor = 'rgba(0,0,0,0)'; + } }, inRange: function(mouseX, mouseY) { var model = this._model; diff --git a/src/types/line.js b/src/types/line.js index acf7f72b9..e3fa2bfb9 100644 --- a/src/types/line.js +++ b/src/types/line.js @@ -166,6 +166,13 @@ module.exports = function(Chart) { model.borderWidth = options.borderWidth; model.borderDash = options.borderDash || []; model.borderDashOffset = options.borderDashOffset || 0; + + // Hide if display is disabled + if(!options.display || !chartInstance.annotation.options.display) { + model.labelEnabled = false; + model.borderColor = 'rgba(0,0,0,0)'; + model.borderWidth = 0; + } }, inRange: function(mouseX, mouseY) { var model = this._model; From db0ce7a92275c8c60ab5915ab9f8e40f1f163849 Mon Sep 17 00:00:00 2001 From: Brad Overton Date: Thu, 22 Nov 2018 08:31:35 +0000 Subject: [PATCH 2/4] Add display option to readme --- README.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/README.md b/README.md index 202d75243..151da9cd2 100644 --- a/README.md +++ b/README.md @@ -18,6 +18,9 @@ To configure the annotations plugin, you can simply add new config options to yo ```javascript { annotation: { + // Set this to false to hide all annotations (events will still trigger) + display: true, // (default) + // Defines when the annotations are drawn. // This allows positioning of the annotation relative to the other // elements of the graph. @@ -40,6 +43,7 @@ To configure the annotations plugin, you can simply add new config options to yo // Array of annotation configuration objects // See below for detailed descriptions of the annotation options annotations: [{ + display: true, // (default) drawTime: 'afterDraw', // overrides annotation.drawTime if set id: 'a-line-1', // optional type: 'line', From 7445c550d8b31e19a8793aae561f7899eab256e6 Mon Sep 17 00:00:00 2001 From: Brad Overton Date: Fri, 14 Dec 2018 07:11:13 +0000 Subject: [PATCH 3/4] Small fix to code style --- src/types/box.js | 2 +- src/types/line.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/types/box.js b/src/types/box.js index dd58928ad..140ae1fc4 100644 --- a/src/types/box.js +++ b/src/types/box.js @@ -87,7 +87,7 @@ module.exports = function(Chart) { model.bottom = bottom; // Hide if display disabled - if(options.display && chartInstance.annotation.options.display) { + if (options.display && chartInstance.annotation.options.display) { model.borderColor = options.borderColor; model.borderWidth = options.borderWidth; model.backgroundColor = options.backgroundColor; diff --git a/src/types/line.js b/src/types/line.js index e3fa2bfb9..84bd9f3e9 100644 --- a/src/types/line.js +++ b/src/types/line.js @@ -168,7 +168,7 @@ module.exports = function(Chart) { model.borderDashOffset = options.borderDashOffset || 0; // Hide if display is disabled - if(!options.display || !chartInstance.annotation.options.display) { + if (!options.display || !chartInstance.annotation.options.display) { model.labelEnabled = false; model.borderColor = 'rgba(0,0,0,0)'; model.borderWidth = 0; From 7ea732db81826003997365b1767bebe219bfb169 Mon Sep 17 00:00:00 2001 From: Jukka Kurkela Date: Fri, 23 Oct 2020 17:14:26 +0300 Subject: [PATCH 4/4] Fix indentation --- README.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 32716cef7..739ba22ec 100644 --- a/README.md +++ b/README.md @@ -19,8 +19,8 @@ To configure the annotations plugin, you can simply add new config options to yo { plugins: { annotation: { - // Set this to false to hide all annotations (events will still trigger) - display: true, // (default) + // Set this to false to hide all annotations (events will still trigger) + display: true, // (default) // Defines when the annotations are drawn. // This allows positioning of the annotation relative to the other @@ -44,7 +44,7 @@ To configure the annotations plugin, you can simply add new config options to yo // Array of annotation configuration objects // See below for detailed descriptions of the annotation options annotations: [{ - display: true, // (default) + display: true, // (default) drawTime: 'afterDraw', // overrides annotation.drawTime if set id: 'a-line-1', // optional type: 'line',