Normalize context creation for option resolution (#7847)

* Normalize context creation for option resolution
* Pass mode to _computeAngle
This commit is contained in:
Jukka Kurkela
2020-10-05 16:03:16 +03:00
committed by GitHub
parent 1a9b452cda
commit 8d36927b29
5 changed files with 32 additions and 87 deletions

View 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) {

View 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,

View 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]);

View 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);
}
}

View 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);