Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable fallback to common defaults #745

Draft
wants to merge 5 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 6 additions & 9 deletions src/annotation.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import {Chart} from 'chart.js';
import {clipArea, unclipArea, isObject, isArray} from 'chart.js/helpers';
import {clipArea, isArray, isObject, unclipArea} from 'chart.js/helpers';
import {version} from '../package.json';
import {commonDefaults} from './commonDefaults';
import {resolveType, updateElements} from './elements';
import {handleEvent, hooks, updateListeners} from './events';
import {requireVersion} from './helpers';
import {adjustScaleRange, verifyScaleOptions} from './scale';
import {updateElements, resolveType} from './elements';
import {annotationTypes} from './types';
import {requireVersion} from './helpers';
import {version} from '../package.json';

const chartStates = new Map();

Expand Down Expand Up @@ -113,11 +114,7 @@ export default {
axis: undefined,
intersect: undefined
},
common: {
drawTime: 'afterDatasetsDraw',
label: {
}
}
common: commonDefaults
},

descriptors: {
Expand Down
14 changes: 14 additions & 0 deletions src/commonDefaults.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
export const commonDefaults = {
drawTime: 'afterDatasetsDraw',
label: {
},
xMax: undefined,
xMin: undefined,
xScaleID: undefined,
xValue: undefined,
yMax: undefined,
yMin: undefined,
yScaleID: undefined,
yValue: undefined,
z: 0
};
35 changes: 11 additions & 24 deletions src/elements.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import {Animations} from 'chart.js';
import {isObject, defined} from 'chart.js/helpers';
import {hooks} from './events';
import {defined, isObject} from 'chart.js/helpers';
import {annotationTypes} from './types';

const directUpdater = {
Expand Down Expand Up @@ -61,7 +60,7 @@ export function updateElements(chart, state, options, mode) {
Object.assign(element, properties);
}

properties.options = resolveAnnotationOptions(resolver);
properties.options = resolveOptions(resolver);

animations.update(element, properties);
}
Expand All @@ -86,7 +85,7 @@ function updateSubElements(mainElement, {elements, initProperties}, resolver, an
const properties = definition.properties;
const subElement = getOrCreateElement(subElements, i, definition.type, initProperties);
const subResolver = resolver[definition.optionScope].override(definition);
properties.options = resolveAnnotationOptions(subResolver);
properties.options = resolveOptions(subResolver);
animations.update(subElement, properties);
}
}
Expand All @@ -103,27 +102,15 @@ function getOrCreateElement(elements, index, type, initProperties) {
return element;
}

function resolveAnnotationOptions(resolver) {
const elementClass = annotationTypes[resolveType(resolver.type)];
function resolveOptions(resolver, path = []) {
const result = {};
result.id = resolver.id;
result.type = resolver.type;
result.drawTime = resolver.drawTime;
Object.assign(result,
resolveObj(resolver, elementClass.defaults),
resolveObj(resolver, elementClass.defaultRoutes));
for (const hook of hooks) {
result[hook] = resolver[hook];
}
return result;
}

function resolveObj(resolver, defs) {
const result = {};
for (const prop of Object.keys(defs)) {
const optDefs = defs[prop];
const value = resolver[prop];
result[prop] = isObject(optDefs) ? resolveObj(value, optDefs) : value;
for (const key of Object.getOwnPropertyNames(resolver)) {
if (path.includes(key)) {
// TODO: this is slow, should figure out why the keys start looping instead.
continue;
}
const value = resolver[key];
result[key] = isObject(value) ? resolveOptions(value, [...path, key]) : value;
}
return result;
}
Expand Down
7 changes: 0 additions & 7 deletions src/types/box.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,13 +97,6 @@ BoxAnnotation.defaults = {
shadowBlur: 0,
shadowOffsetX: 0,
shadowOffsetY: 0,
xMax: undefined,
xMin: undefined,
xScaleID: undefined,
yMax: undefined,
yMin: undefined,
yScaleID: undefined,
z: 0
};

BoxAnnotation.defaultRoutes = {
Expand Down
7 changes: 0 additions & 7 deletions src/types/ellipse.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,6 @@ EllipseAnnotation.defaults = {
shadowBlur: 0,
shadowOffsetX: 0,
shadowOffsetY: 0,
xMax: undefined,
xMin: undefined,
xScaleID: undefined,
yMax: undefined,
yMin: undefined,
yScaleID: undefined,
z: 0
};

EllipseAnnotation.defaultRoutes = {
Expand Down
9 changes: 0 additions & 9 deletions src/types/label.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,16 +102,7 @@ LabelAnnotation.defaults = {
textStrokeWidth: 0,
width: undefined,
xAdjust: 0,
xMax: undefined,
xMin: undefined,
xScaleID: undefined,
xValue: undefined,
yAdjust: 0,
yMax: undefined,
yMin: undefined,
yScaleID: undefined,
yValue: undefined,
z: 0
};

LabelAnnotation.defaultRoutes = {
Expand Down
9 changes: 0 additions & 9 deletions src/types/point.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,16 +61,7 @@ PointAnnotation.defaults = {
shadowOffsetX: 0,
shadowOffsetY: 0,
xAdjust: 0,
xMax: undefined,
xMin: undefined,
xScaleID: undefined,
xValue: undefined,
yAdjust: 0,
yMax: undefined,
yMin: undefined,
yScaleID: undefined,
yValue: undefined,
z: 0
};

PointAnnotation.defaultRoutes = {
Expand Down
9 changes: 0 additions & 9 deletions src/types/polygon.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,16 +83,7 @@ PolygonAnnotation.defaults = {
shadowOffsetY: 0,
sides: 3,
xAdjust: 0,
xMax: undefined,
xMin: undefined,
xScaleID: undefined,
xValue: undefined,
yAdjust: 0,
yMax: undefined,
yMin: undefined,
yScaleID: undefined,
yValue: undefined,
z: 0
};

PolygonAnnotation.defaultRoutes = {
Expand Down