diff --git a/samples/autoRotation.html b/samples/autoRotation.html
index db5d67595..bc263f467 100644
--- a/samples/autoRotation.html
+++ b/samples/autoRotation.html
@@ -73,6 +73,9 @@
display: true,
text: 'Chart.js Bar Chart'
},
+ font: {
+ size: 16,
+ },
plugins: {
annotation: {
drawTime: 'afterDatasetsDraw',
@@ -104,6 +107,9 @@
borderColor: 'black',
borderWidth: 5,
label: {
+ font: {
+ size: 12,
+ },
rotation: 'auto',
backgroundColor: 'red',
content: ['Test Multi', 'Line Label'],
diff --git a/src/annotation.js b/src/annotation.js
index f264f5df4..57ee7a015 100644
--- a/src/annotation.js
+++ b/src/annotation.js
@@ -98,6 +98,18 @@ function resolveAnimations(chart, animOpts, mode) {
return new Animations(chart, animOpts);
}
+function initElement(chart, element, animations) {
+ const {options} = element;
+ const display = typeof options.display === 'function' ? callCallback(options.display, [{chart, element}]) : valueOrDefault(options.display, true);
+
+ options.display = !!display;
+
+ if (options.display) {
+ element.resolveOptions(chart);
+ animations.update(element, element.resolveElementProperties(chart));
+ }
+}
+
function updateElements(chart, state, options, mode) {
const chartAnims = chart.options.animation;
const animOpts = chartAnims && merge({}, [chartAnims, options.animation]);
@@ -121,20 +133,11 @@ function updateElements(chart, state, options, mode) {
if (!el || !(el instanceof elType)) {
el = elements[i] = new elType();
}
- const properties = calculateElementProperties(chart, el, annotation, elType.defaults);
- animations.update(el, properties);
-
- const display = typeof annotation.display === 'function' ? callCallback(annotation.display, [{chart, element: el}]) : valueOrDefault(annotation.display, true);
- el._display = !!display;
+ el.options = merge(Object.create(null), [elType.defaults, annotation]);
+ initElement(chart, el, animations);
}
}
-function calculateElementProperties(chart, element, options, defaults) {
- const elementProperties = element.resolveElementProperties(chart, options);
- elementProperties.options = merge(Object.create(null), [defaults, options]);
- return elementProperties;
-}
-
function draw(chart, options, caller) {
const {ctx, chartArea} = chart;
const elements = chartStates.get(chart).elements;
@@ -142,7 +145,7 @@ function draw(chart, options, caller) {
clipArea(ctx, chartArea);
for (let i = 0; i < elements.length; i++) {
const el = elements[i];
- if (el._display && (el.options.drawTime || options.drawTime || caller) === caller) {
+ if (el.options.display && (el.options.drawTime || options.drawTime || caller) === caller) {
el.draw(ctx);
}
}
diff --git a/src/types/box.js b/src/types/box.js
index 1555c813c..d1b39e9a0 100644
--- a/src/types/box.js
+++ b/src/types/box.js
@@ -36,7 +36,8 @@ export default class BoxAnnotation extends Element {
ctx.restore();
}
- resolveElementProperties(chart, options) { // eslint-disable-line class-methods-use-this
+ resolveElementProperties(chart) {
+ const {options} = this;
const xScale = chart.scales[options.xScaleID];
const yScale = chart.scales[options.yScaleID];
let {top: y, left: x, bottom: y2, right: x2} = chart.chartArea;
@@ -69,6 +70,14 @@ export default class BoxAnnotation extends Element {
height: y2 - y
};
}
+
+ /**
+ * @param {chart} chart - chart instance
+ * @todo temporaly empty waiting to implement callbacks
+ */
+ resolveOptions() { // eslint-disable-line class-methods-use-this
+ // nothing
+ }
}
BoxAnnotation.id = 'boxAnnotation';
diff --git a/src/types/line.js b/src/types/line.js
index 36a3dcc5a..7f419b66a 100644
--- a/src/types/line.js
+++ b/src/types/line.js
@@ -1,5 +1,5 @@
-import {Element, defaults} from 'chart.js';
-import {isArray, fontString, toRadians} from 'chart.js/helpers';
+import {Element} from 'chart.js';
+import {isArray, toFontString, toRadians, mergeIf} from 'chart.js/helpers';
import {scaleValue, roundedRect, inTriangle} from '../helpers';
const pointInLine = (p1, p2, t) => ({x: p1.x + t * (p2.x - p1.x), y: p1.y + t * (p2.y - p1.y)});
@@ -73,7 +73,8 @@ export default class LineAnnotation extends Element {
ctx.restore();
}
- resolveElementProperties(chart, options) { // eslint-disable-line class-methods-use-this
+ resolveElementProperties(chart) {
+ const {options} = this;
const scale = chart.scales[options.scaleID];
let {top: y, left: x, bottom: y2, right: x2} = chart.chartArea;
let min, max;
@@ -98,6 +99,12 @@ export default class LineAnnotation extends Element {
height: y2 - y
};
}
+
+ resolveOptions(chart) {
+ const {options} = this;
+
+ options.label.font = mergeIf(options.label.font, chart.options.font);
+ }
}
LineAnnotation.id = 'lineAnnotation';
@@ -108,8 +115,6 @@ LineAnnotation.defaults = {
label: {
backgroundColor: 'rgba(0,0,0,0.8)',
font: {
- family: defaults.font.family,
- size: defaults.font.size,
style: 'bold',
color: '#fff',
},
@@ -141,11 +146,7 @@ function calculateAutoRotation(line) {
function drawLabel(ctx, line) {
const label = line.options.label;
- ctx.font = fontString(
- label.font.size,
- label.font.style,
- label.font.family
- );
+ ctx.font = toFontString(label.font);
ctx.textAlign = 'center';
const {width, height} = measureLabel(ctx, label);
diff --git a/src/types/point.js b/src/types/point.js
index 50a5b6aba..5443cf57e 100644
--- a/src/types/point.js
+++ b/src/types/point.js
@@ -29,7 +29,8 @@ export default class PointAnnotation extends Element {
ctx.restore();
}
- resolveElementProperties(chart, options) { // eslint-disable-line class-methods-use-this
+ resolveElementProperties(chart) {
+ const {options} = this;
const xScale = chart.scales[options.xScaleID];
const yScale = chart.scales[options.yScaleID];
let x = chart.chartArea.width / 2;
@@ -56,6 +57,14 @@ export default class PointAnnotation extends Element {
height: options.radius * 2
};
}
+
+ /**
+ * @param {chart} chart - chart instance
+ * @todo temporaly empty waiting to implement callbacks
+ */
+ resolveOptions() { // eslint-disable-line class-methods-use-this
+ // nothing
+ }
}
PointAnnotation.id = 'pointAnnotation';