Skip to content

Commit

Permalink
Make all options scriptable
Browse files Browse the repository at this point in the history
  • Loading branch information
kurkle committed Nov 10, 2020
1 parent e00dc1e commit 2eedf6f
Show file tree
Hide file tree
Showing 20 changed files with 315 additions and 413 deletions.
32 changes: 12 additions & 20 deletions src/controllers/controller.bar.js
Original file line number Diff line number Diff line change
Expand Up @@ -266,9 +266,8 @@ export default class BarController extends DatasetController {
me.updateSharedOptions(sharedOptions, mode, firstOpts);

for (let i = start; i < start + count; i++) {
const options = sharedOptions || me.resolveDataElementOptions(i, mode);
const vpixels = me._calculateBarValuePixels(i, options);
const ipixels = me._calculateBarIndexPixels(i, ruler, options);
const vpixels = me._calculateBarValuePixels(i);
const ipixels = me._calculateBarIndexPixels(i, ruler);

const properties = {
horizontal,
Expand All @@ -280,7 +279,7 @@ export default class BarController extends DatasetController {
};

if (includeOptions) {
properties.options = options;
properties.options = sharedOptions || me.resolveDataElementOptions(i, mode);
}
me.updateElement(bars[i], i, properties, mode);
}
Expand Down Expand Up @@ -400,11 +399,11 @@ export default class BarController extends DatasetController {
* Note: pixel values are not clamped to the scale area.
* @private
*/
_calculateBarValuePixels(index, options) {
_calculateBarValuePixels(index) {
const me = this;
const meta = me._cachedMeta;
const vScale = meta.vScale;
const {base: baseValue, minBarLength} = options;
const {base: baseValue, minBarLength} = me.options;
const parsed = me.getParsed(index);
const custom = parsed._custom;
const floating = isFloatBar(custom);
Expand Down Expand Up @@ -459,8 +458,9 @@ export default class BarController extends DatasetController {
/**
* @private
*/
_calculateBarIndexPixels(index, ruler, options) {
_calculateBarIndexPixels(index, ruler) {
const me = this;
const options = me.options;
const stackCount = me.chart.options.skipNull ? me._getStackCount(index) : ruler.stackCount;
const range = options.barThickness === 'flex'
? computeFlexCategoryTraits(index, ruler, options, stackCount)
Expand Down Expand Up @@ -510,19 +510,7 @@ BarController.id = 'bar';
BarController.defaults = {
datasetElementType: false,
dataElementType: 'bar',
dataElementOptions: [
'backgroundColor',
'borderColor',
'borderSkipped',
'borderWidth',
'borderRadius',
'barPercentage',
'barThickness',
'base',
'categoryPercentage',
'maxBarThickness',
'minBarLength',
],

interaction: {
mode: 'index'
},
Expand All @@ -532,6 +520,10 @@ BarController.defaults = {
datasets: {
categoryPercentage: 0.8,
barPercentage: 0.9,
barThickness: undefined,
base: undefined,
maxBarThickness: undefined,
minBarLength: undefined,
animation: {
numbers: {
type: 'number',
Expand Down
53 changes: 23 additions & 30 deletions src/controllers/controller.bubble.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import DatasetController from '../core/core.datasetController';
import {resolve} from '../helpers/helpers.options';
import {resolveObjectKey} from '../helpers/helpers.core';

export default class BubbleController extends DatasetController {
Expand Down Expand Up @@ -104,34 +103,37 @@ export default class BubbleController extends DatasetController {
/**
* @param {number} index
* @param {string} [mode]
* @param {string} [prefix]
* @protected
*/
resolveDataElementOptions(index, mode) {
const me = this;
const chart = me.chart;
const parsed = me.getParsed(index);
let values = super.resolveDataElementOptions(index, mode);

// Scriptable options
const context = me.getContext(index, mode === 'active');
resolveDataElementOptions(index, mode, prefix) {
const active = mode === 'active';
let options = super.resolveDataElementOptions(index, mode, prefix);

// In case values were cached (and thus frozen), we need to clone the values
if (values.$shared) {
values = Object.assign({}, values, {$shared: false});
if (options.$shared) {
options = Object.assign({}, options, {$shared: false});
}
options.radius = this._determineRadius(options, index, active, prefix);

return options;
}

// Custom radius resolution
if (mode !== 'active') {
values.radius = 0;
/**
* @private
*/
_determineRadius(options, index, active, prefix) {
const me = this;
const parsed = me.getParsed(index);
const customRadius = parsed && parsed._custom;
let radius = options.radius;
if (customRadius !== undefined) {
radius = (active ? radius : 0) + customRadius;
} else if (active) {
const normal = super.resolveDataElementOptions(index, '', prefix);
radius += normal.radius;
}
values.radius += resolve([
parsed && parsed._custom,
me._config.radius,
chart.options.elements.point.radius
], context, index);

return values;
return radius;
}
}

Expand All @@ -143,15 +145,6 @@ BubbleController.id = 'bubble';
BubbleController.defaults = {
datasetElementType: false,
dataElementType: 'point',
dataElementOptions: [
'backgroundColor',
'borderColor',
'borderWidth',
'hitRadius',
'radius',
'pointStyle',
'rotation'
],
animation: {
numbers: {
properties: ['x', 'y', 'borderWidth', 'radius']
Expand Down
6 changes: 3 additions & 3 deletions src/controllers/controller.doughnut.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,14 +80,14 @@ export default class DoughnutController extends DatasetController {
* @private
*/
_getRotation() {
return toRadians(valueOrDefault(this._config.rotation, this.chart.options.rotation) - 90);
return toRadians(this.options.rotation - 90);
}

/**
* @private
*/
_getCircumference() {
return toRadians(valueOrDefault(this._config.circumference, this.chart.options.circumference));
return toRadians(this.options.circumference);
}

/**
Expand Down Expand Up @@ -156,7 +156,7 @@ export default class DoughnutController extends DatasetController {
*/
_circumference(i, reset) {
const me = this;
const opts = me.chart.options;
const opts = me.options;
const meta = me._cachedMeta;
const circumference = me._getCircumference();
return reset && opts.animation.animateRotate ? 0 : this.chart.getDataVisibility(i) ? me.calculateCircumference(meta._parsed[i] * circumference / TAU) : 0;
Expand Down
62 changes: 11 additions & 51 deletions src/controllers/controller.line.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import DatasetController from '../core/core.datasetController';
import {valueOrDefault} from '../helpers/helpers.core';
import {isNumber, _limitValue} from '../helpers/helpers.math';
import {resolve} from '../helpers/helpers.options';
import {_lookupByKey} from '../helpers/helpers.collection';

export default class LineController extends DatasetController {
Expand Down Expand Up @@ -32,7 +30,7 @@ export default class LineController extends DatasetController {
if (mode !== 'resize') {
const properties = {
points,
options: me.resolveDatasetElementOptions()
options: me.resolveDatasetElementOptions(mode)
};

me.updateElement(line, undefined, properties, mode);
Expand All @@ -49,7 +47,7 @@ export default class LineController extends DatasetController {
const firstOpts = me.resolveDataElementOptions(start, mode);
const sharedOptions = me.getSharedOptions(firstOpts);
const includeOptions = me.includeOptions(mode, sharedOptions);
const spanGaps = valueOrDefault(me._config.spanGaps, me.chart.options.spanGaps);
const spanGaps = me.options.spanGaps;
const maxGapLength = isNumber(spanGaps) ? spanGaps : Number.POSITIVE_INFINITY;
let prevParsed = start > 0 && me.getParsed(start - 1);

Expand Down Expand Up @@ -78,31 +76,19 @@ export default class LineController extends DatasetController {
}

/**
* @param {boolean} [active]
* @param {string} [mode]
* @param {string} [prefix]
* @protected
*/
resolveDatasetElementOptions(active) {
const me = this;
const config = me._config;
const options = me.chart.options;
const lineOptions = options.elements.line;
const values = super.resolveDatasetElementOptions(active);
const showLine = valueOrDefault(config.showLine, options.showLine);

// The default behavior of lines is to break at null values, according
// to https://github.com/chartjs/Chart.js/issues/2435#issuecomment-216718158
// This option gives lines the ability to span gaps
values.spanGaps = valueOrDefault(config.spanGaps, options.spanGaps);
values.tension = valueOrDefault(config.tension, lineOptions.tension);
values.stepped = resolve([config.stepped, lineOptions.stepped]);

if (!showLine) {
resolveDatasetElementOptions(mode, prefix) {
const values = super.resolveDatasetElementOptions(mode, prefix);

if (!this.options.showLine) {
values.borderWidth = 0;
}

return values;
}

/**
* @protected
*/
Expand Down Expand Up @@ -132,37 +118,11 @@ LineController.id = 'line';
*/
LineController.defaults = {
datasetElementType: 'line',
datasetElementOptions: [
'backgroundColor',
'borderCapStyle',
'borderColor',
'borderDash',
'borderDashOffset',
'borderJoinStyle',
'borderWidth',
'capBezierPoints',
'cubicInterpolationMode',
'fill'
],

dataElementType: 'point',
dataElementOptions: {
backgroundColor: 'pointBackgroundColor',
borderColor: 'pointBorderColor',
borderWidth: 'pointBorderWidth',
hitRadius: 'pointHitRadius',
hoverHitRadius: 'pointHitRadius',
hoverBackgroundColor: 'pointHoverBackgroundColor',
hoverBorderColor: 'pointHoverBorderColor',
hoverBorderWidth: 'pointHoverBorderWidth',
hoverRadius: 'pointHoverRadius',
pointStyle: 'pointStyle',
radius: 'pointRadius',
rotation: 'pointRotation'
},

showLine: true,
spanGaps: false,
datasets: {
showLine: true,
},

interaction: {
mode: 'index'
Expand Down
40 changes: 6 additions & 34 deletions src/controllers/controller.polarArea.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import DatasetController from '../core/core.datasetController';
import {resolve, toRadians, PI} from '../helpers/index';
import {toRadians, PI} from '../helpers/index';

function getStartAngleRadians(deg) {
// radialLinear scale draws angleLines using startAngle. 0 is expected to be at top.
Expand Down Expand Up @@ -55,15 +55,16 @@ export default class PolarAreaController extends DatasetController {
let angle = datasetStartAngle;
let i;

me._cachedMeta.count = me.countVisibleElements();
const defaultAngle = 360 / me.countVisibleElements();

for (i = 0; i < start; ++i) {
angle += me._computeAngle(i, mode);
angle += toRadians(me.resolveDataElementOptions(i, mode).angle || defaultAngle);
}
for (i = start; i < start + count; i++) {
const options = me.resolveDataElementOptions(i, mode);
const arc = arcs[i];
let startAngle = angle;
let endAngle = angle + me._computeAngle(i, mode);
let endAngle = angle + toRadians(options.angle || defaultAngle);
let outerRadius = this.chart.getDataVisibility(i) ? scale.getDistanceFromCenterForValue(dataset.data[i]) : 0;
angle = endAngle;

Expand All @@ -84,7 +85,7 @@ export default class PolarAreaController extends DatasetController {
outerRadius,
startAngle,
endAngle,
options: me.resolveDataElementOptions(i, mode)
options
};

me.updateElement(arc, i, properties, mode);
Expand All @@ -104,28 +105,6 @@ export default class PolarAreaController extends DatasetController {

return count;
}

/**
* @private
*/
_computeAngle(index, mode) {
const me = this;
const meta = me._cachedMeta;
const count = meta.count;
const dataset = me.getDataset();

if (isNaN(dataset.data[index]) || !this.chart.getDataVisibility(index)) {
return 0;
}

// Scriptable options
const context = me.getContext(index, mode === 'active');

return toRadians(resolve([
me.chart.options.elements.arc.angle,
360 / count
], context, index));
}
}

PolarAreaController.id = 'polarArea';
Expand All @@ -135,13 +114,6 @@ PolarAreaController.id = 'polarArea';
*/
PolarAreaController.defaults = {
dataElementType: 'arc',
dataElementOptions: [
'backgroundColor',
'borderColor',
'borderWidth',
'borderAlign',
'offset'
],

animation: {
numbers: {
Expand Down
Loading

0 comments on commit 2eedf6f

Please sign in to comment.