]> git.ipfire.org Git - thirdparty/Chart.js.git/commitdiff
Normalize context creation for option resolution (#7847)
authorJukka Kurkela <jukka.kurkela@gmail.com>
Mon, 5 Oct 2020 13:03:16 +0000 (16:03 +0300)
committerGitHub <noreply@github.com>
Mon, 5 Oct 2020 13:03:16 +0000 (09:03 -0400)
* Normalize context creation for option resolution
* Pass mode to _computeAngle

src/controllers/controller.bubble.js
src/controllers/controller.polarArea.js
src/core/core.datasetController.js
src/core/core.scale.js
src/scales/scale.radialLinear.js

index c0aceecd095178edde61cb79e723b9e38dad9bdd..cbea599adbffa58087371490fd3be4816c9f926b 100644 (file)
@@ -109,18 +109,11 @@ export default class BubbleController extends DatasetController {
        resolveDataElementOptions(index, mode) {
                const me = this;
                const chart = me.chart;
-               const dataset = me.getDataset();
                const parsed = me.getParsed(index);
                let values = super.resolveDataElementOptions(index, mode);
 
                // Scriptable options
-               const context = {
-                       chart,
-                       dataPoint: parsed,
-                       dataIndex: index,
-                       dataset,
-                       datasetIndex: me.index
-               };
+               const context = me.getContext(index, mode === 'active');
 
                // In case values were cached (and thus frozen), we need to clone the values
                if (values.$shared) {
index 26d61ff7e89b642a3222bd253e277547541af1cb..ddff43a490b1263dacd1598051b13d4bf7d4dc68 100644 (file)
@@ -59,12 +59,12 @@ export default class PolarAreaController extends DatasetController {
                me._cachedMeta.count = me.countVisibleElements();
 
                for (i = 0; i < start; ++i) {
-                       angle += me._computeAngle(i);
+                       angle += me._computeAngle(i, mode);
                }
                for (i = start; i < start + count; i++) {
                        const arc = arcs[i];
                        let startAngle = angle;
-                       let endAngle = angle + me._computeAngle(i);
+                       let endAngle = angle + me._computeAngle(i, mode);
                        let outerRadius = this.chart.getDataVisibility(i) ? scale.getDistanceFromCenterForValue(dataset.data[i]) : 0;
                        angle = endAngle;
 
@@ -109,7 +109,7 @@ export default class PolarAreaController extends DatasetController {
        /**
         * @private
         */
-       _computeAngle(index) {
+       _computeAngle(index, mode) {
                const me = this;
                const meta = me._cachedMeta;
                const count = meta.count;
@@ -120,13 +120,7 @@ export default class PolarAreaController extends DatasetController {
                }
 
                // Scriptable options
-               const context = {
-                       chart: me.chart,
-                       dataPoint: this.getParsed(index),
-                       dataIndex: index,
-                       dataset,
-                       datasetIndex: me.index
-               };
+               const context = me.getContext(index, mode === 'active');
 
                return resolve([
                        me.chart.options.elements.arc.angle,
index fde4f110ff04e66e95e92a4c2b19064b19d27047..1277860ac8ff1f8c1f812d2445c209d138d87f57 100644 (file)
@@ -693,9 +693,9 @@ export default class DatasetController {
        }
 
        /**
-        * @private
+        * @protected
         */
-       _getContext(index, active) {
+       getContext(index, active) {
                return {
                        chart: this.chart,
                        dataPoint: this.getParsed(index),
@@ -764,7 +764,7 @@ export default class DatasetController {
                const datasetOpts = me._config;
                const options = me.chart.options.elements[type] || {};
                const values = {};
-               const context = me._getContext(index, active);
+               const context = me.getContext(index, active);
                const keys = optionKeys(optionNames);
 
                for (let i = 0, ilen = keys.length; i < ilen; ++i) {
@@ -798,7 +798,7 @@ export default class DatasetController {
                }
 
                const info = {cacheable: true};
-               const context = me._getContext(index, active);
+               const context = me.getContext(index, active);
                const chartAnim = resolve([chart.options.animation], context, index, info);
                const datasetAnim = resolve([me._config.animation], context, index, info);
                let config = chartAnim && mergeIf({}, [datasetAnim, chartAnim]);
index 98a4f484472288a1d11bc3dd46e19e4285c108fb..99f665a10f00e67a33ed9e1a0804dbb5a109f9c6 100644 (file)
@@ -1013,6 +1013,19 @@ export default class Scale extends Element {
                        0;
        }
 
+       /**
+        * @protected
+        */
+       getContext(index) {
+               const ticks = this.ticks || [];
+               return {
+                       chart: this.chart,
+                       scale: this,
+                       tick: ticks[index],
+                       index
+               };
+       }
+
        /**
         * Returns a subset of ticks to be plotted to avoid overlapping labels.
         * @param {Tick[]} ticks
@@ -1105,12 +1118,7 @@ export default class Scale extends Element {
                const tl = getTickMarkLength(gridLines);
                const items = [];
 
-               let context = {
-                       chart,
-                       scale: me,
-                       tick: ticks[0],
-                       index: 0,
-               };
+               let context = this.getContext(0);
                const axisWidth = gridLines.drawBorder ? resolve([gridLines.borderWidth, gridLines.lineWidth, 0], context, 0) : 0;
                const axisHalfWidth = axisWidth / 2;
                const alignBorderValue = function(pixel) {
@@ -1172,15 +1180,7 @@ export default class Scale extends Element {
                }
 
                for (i = 0; i < ticksLength; ++i) {
-                       /** @type {Tick|object} */
-                       const tick = ticks[i] || {};
-
-                       context = {
-                               chart,
-                               scale: me,
-                               tick,
-                               index: i,
-                       };
+                       context = this.getContext(i);
 
                        const lineWidth = resolve([gridLines.lineWidth], context, i);
                        const lineColor = resolve([gridLines.color], context, i);
@@ -1318,12 +1318,7 @@ export default class Scale extends Element {
                const gridLines = me.options.gridLines;
                const ctx = me.ctx;
                const chart = me.chart;
-               let context = {
-                       chart,
-                       scale: me,
-                       tick: me.ticks[0],
-                       index: 0,
-               };
+               let context = me.getContext(0);
                const axisWidth = gridLines.drawBorder ? resolve([gridLines.borderWidth, gridLines.lineWidth, 0], context, 0) : 0;
                const items = me._gridLineItems || (me._gridLineItems = me._computeGridLineItems(chartArea));
                let i, ilen;
@@ -1364,12 +1359,7 @@ export default class Scale extends Element {
                if (axisWidth) {
                        // Draw the line at the edge of the axis
                        const firstLineWidth = axisWidth;
-                       context = {
-                               chart,
-                               scale: me,
-                               tick: me.ticks[me._ticksLength - 1],
-                               index: me._ticksLength - 1,
-                       };
+                       context = me.getContext(me._ticksLength - 1);
                        const lastLineWidth = resolve([gridLines.lineWidth, 1], context, me._ticksLength - 1);
                        const borderValue = me._borderValue;
                        let x1, x2, y1, y2;
@@ -1597,13 +1587,7 @@ export default class Scale extends Element {
                const me = this;
                const chart = me.chart;
                const options = me.options.ticks;
-               const ticks = me.ticks || [];
-               const context = {
-                       chart,
-                       scale: me,
-                       tick: ticks[index],
-                       index
-               };
+               const context = me.getContext(index);
                return toFont(resolve([options.font], context), chart.options.font);
        }
 }
index 93d60cfad94067432abea318280b35bd5d733a36..1fea73ecf680e64c7850e1189a3ee8e71d99bd86 100644 (file)
@@ -96,12 +96,7 @@ function fitWithPointLabels(scale) {
        for (i = 0; i < valueCount; i++) {
                pointPosition = scale.getPointPosition(i, scale.drawingArea + 5);
 
-               const context = {
-                       chart: scale.chart,
-                       scale,
-                       index: i,
-                       label: scale.pointLabels[i]
-               };
+               const context = scale.getContext(i);
                const plFont = toFont(resolve([scale.options.pointLabels.font], context, i), scale.chart.options.font);
                scale.ctx.font = plFont.string;
                textSize = measureLabelSize(scale.ctx, plFont.lineHeight, scale.pointLabels[i]);
@@ -185,12 +180,7 @@ function drawPointLabels(scale) {
                const extra = (i === 0 ? tickBackdropHeight / 2 : 0);
                const pointLabelPosition = scale.getPointPosition(i, outerDistance + extra + 5);
 
-               const context = {
-                       chart: scale.chart,
-                       scale,
-                       index: i,
-                       label: scale.pointLabels[i],
-               };
+               const context = scale.getContext(i);
                const plFont = toFont(resolve([pointLabelOpts.font], context, i), scale.chart.options.font);
                ctx.font = plFont.string;
                ctx.fillStyle = plFont.color;
@@ -208,12 +198,7 @@ function drawRadiusLine(scale, gridLineOpts, radius, index) {
        const circular = gridLineOpts.circular;
        const valueCount = scale.chart.data.labels.length;
 
-       const context = {
-               chart: scale.chart,
-               scale,
-               index,
-               tick: scale.ticks[index],
-       };
+       const context = scale.getContext(index);
        const lineColor = resolve([gridLineOpts.color], context, index - 1);
        const lineWidth = resolve([gridLineOpts.lineWidth], context, index - 1);
        let pointPosition;
@@ -440,12 +425,7 @@ export default class RadialLinearScale extends LinearScaleBase {
                        ctx.save();
 
                        for (i = me.chart.data.labels.length - 1; i >= 0; i--) {
-                               const context = {
-                                       chart: me.chart,
-                                       scale: me,
-                                       index: i,
-                                       label: me.pointLabels[i],
-                               };
+                               const context = me.getContext(i);
                                const lineWidth = resolve([angleLineOpts.lineWidth, gridLineOpts.lineWidth], context, i);
                                const color = resolve([angleLineOpts.color, gridLineOpts.color], context, i);
 
@@ -496,17 +476,11 @@ export default class RadialLinearScale extends LinearScaleBase {
                ctx.textBaseline = 'middle';
 
                me.ticks.forEach((tick, index) => {
-                       const context = {
-                               chart: me.chart,
-                               scale: me,
-                               index,
-                               tick,
-                       };
-
                        if (index === 0 && !opts.reverse) {
                                return;
                        }
 
+                       const context = me.getContext(index);
                        const tickFont = me._resolveTickFontOptions(index);
                        ctx.font = tickFont.string;
                        offset = me.getDistanceFromCenterForValue(me.ticks[index].value);