Skip to content

Commit

Permalink
use helper methods
Browse files Browse the repository at this point in the history
  • Loading branch information
sgratzl committed May 19, 2020
1 parent 4cafadd commit bb822a5
Show file tree
Hide file tree
Showing 7 changed files with 147 additions and 131 deletions.
64 changes: 54 additions & 10 deletions src/chart.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,67 @@
import ChartNS from 'chart.js';

export const Chart = ChartNS;
export const plugins = ChartNS.plugins;
export const controllers = ChartNS.controllers;
// export const plugins = ChartNS.plugins;
export const defaults = ChartNS.defaults;
export const helpers = ChartNS.helpers;
// export const scaleService = ChartNS.scaleService;

// export const DataSetController = ChartNS.DataSetController;
export const BarController = controllers.bar;
export const HorizontalBarController = controllers.horizontalBar;
// export const LineController = controllers.line;
// export const PolarAreaController = controllers.polarArea;
// export const ScatterController = controllers.scatter;
export function registerScale(scale) {
ChartNS.scaleService.registerScale(scale);
return scale;
}

// export const Scale = ChartNS.Scale;
// export const LinearScale = ChartNS.scaleService.getScaleConstructor('linear');
// export const LogarithmicScale = ChartNS.scaleService.getScaleConstructor('logarithmic');
// export const CategoryScale = ChartNS.scaleService.getScaleConstructor('category');

// export const DatasetController = ChartNS.DatasetController;
export const BarController = ChartNS.controllers.bar;
// export const BubbleController = ChartNS.controllers.bubble;
export const HorizontalBarController = ChartNS.controllers.horizontalBar;
// export const LineController = ChartNS.controllers.line;
// export const PolarAreaController = ChartNS.controllers.polarArea;
// export const ScatterController = ChartNS.controllers.scatter;

export function registerController(controller) {
ChartNS.controllers[controller.id] = controller;
defaults.set(controller.id, controller.defaults);
return controller;
}

export const Element = ChartNS.Element;
// export const Rectangle = ChartNS.elements.Rectangle;
// export const Point = ChartNS.elements.Point;
// export const Line = ChartNS.elements.Line;
// export const Arc = ChartNS.elements.Arc;

export function registerElement(element) {
defaults.set('elements', {
[element.id]: element.defaults,
});
return element;
}

export const merge = ChartNS.helpers.merge;
export const drawPoint = ChartNS.helpers.canvas.drawPoint;
// export const resolve = ChartNS.helpers.options.resolve;
// export const color = ChartNS.helpers.color;
// export const valueOrDefault = ChartNS.helpers.valueOrDefault;
// export const clipArea = ChartNS.helpers.canvas.clipArea;
// export const unclipArea = ChartNS.helpers.canvas.unclipArea;
// export const _parseFont = ChartNS.helpers.options._parseFont;
// export const splineCurve = ChartNS.helpers.curve.splineCurve;

export function patchControllerConfig(config, controller) {
controller.register();
config.type = controller.id;
return config;
}

export function registerTooltipPositioner(positioner) {
// register my position logic
const tooltip = ChartNS.plugins.getAll().find((d) => d.id === 'tooltip');
if (tooltip) {
tooltip.positioners[positioner.id] = positioner;
}
return positioner;
}
79 changes: 39 additions & 40 deletions src/controllers/boxplot.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
import { asBoxPlotStats } from '../data';
import { Chart, controllers, defaults, BarController, HorizontalBarController, merge } from '../chart';
import {
Chart,
patchControllerConfig,
registerController,
defaults,
BarController,
HorizontalBarController,
merge,
} from '../chart';
import { baseDefaults, StatsBase } from './base';
import { BoxAndWiskers, boxOptionsKeys } from '../elements';
import { patchControllerConfig } from './utils';

export class BoxPlotController extends StatsBase {
_parseStats(value, config) {
Expand Down Expand Up @@ -30,28 +37,24 @@ BoxPlotController.register = () => {
BoxPlotController.prototype.dataElementType = BoxAndWiskers.register();
BoxPlotController.prototype.dataElementOptions = BarController.prototype.dataElementOptions.concat(boxOptionsKeys);

defaults.set(
BoxPlotController.id,
merge({}, [
defaults.bar,
baseDefaults(boxOptionsKeys),
{
datasets: {
animation: {
numbers: {
type: 'number',
properties: defaults.bar.datasets.animation.numbers.properties.concat(
['q1', 'q3', 'min', 'max', 'median', 'whiskerMin', 'whiskerMax'],
boxOptionsKeys.filter((c) => !c.endsWith('Color'))
),
},
BoxPlotController.defaults = merge({}, [
defaults.bar,
baseDefaults(boxOptionsKeys),
{
datasets: {
animation: {
numbers: {
type: 'number',
properties: defaults.bar.datasets.animation.numbers.properties.concat(
['q1', 'q3', 'min', 'max', 'median', 'whiskerMin', 'whiskerMax'],
boxOptionsKeys.filter((c) => !c.endsWith('Color'))
),
},
},
},
])
);
controllers[BoxPlotController.id] = BoxPlotController;
return BoxPlotController;
},
]);
return registerController(BoxPlotController);
};

export class BoxPlotChart extends Chart {
Expand All @@ -77,28 +80,24 @@ HorizontalBoxPlotController.register = () => {
boxOptionsKeys
);

defaults.set(
HorizontalBoxPlotController.id,
merge({}, [
defaults.horizontalBar,
baseDefaults(boxOptionsKeys),
{
datasets: {
animation: {
numbers: {
type: 'number',
properties: defaults.bar.datasets.animation.numbers.properties.concat(
['q1', 'q3', 'min', 'max', 'median', 'whiskerMin', 'whiskerMax'],
boxOptionsKeys.filter((c) => !c.endsWith('Color'))
),
},
HorizontalBoxPlotController.defaults = merge({}, [
defaults.horizontalBar,
baseDefaults(boxOptionsKeys),
{
datasets: {
animation: {
numbers: {
type: 'number',
properties: defaults.bar.datasets.animation.numbers.properties.concat(
['q1', 'q3', 'min', 'max', 'median', 'whiskerMin', 'whiskerMax'],
boxOptionsKeys.filter((c) => !c.endsWith('Color'))
),
},
},
},
])
);
controllers[HorizontalBoxPlotController.id] = HorizontalBoxPlotController;
return HorizontalBoxPlotController;
},
]);
return registerController(HorizontalBoxPlotController);
};

export class HorizontalBoxPlotChart extends Chart {
Expand Down
5 changes: 0 additions & 5 deletions src/controllers/utils.js

This file was deleted.

91 changes: 41 additions & 50 deletions src/controllers/violin.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import { asViolinStats } from '../data';
import { Chart, controllers, defaults, helpers, BarController, HorizontalBarController } from '../chart';
import { Chart, defaults, merge, BarController, registerController, patchControllerConfig } from '../chart';
import { StatsBase, baseDefaults } from './base';
import { baseOptionKeys } from '../elements/base';
import { ViolinElement } from '../elements';
import { interpolateKdeCoords } from '../animation';
import { patchControllerConfig } from './utils';

export class ViolinController extends StatsBase {
_parseStats(value, config) {
Expand Down Expand Up @@ -36,33 +35,29 @@ ViolinController.register = () => {
ViolinController.prototype.dataElementType = ViolinElement.register();
ViolinController.prototype.dataElementOptions = BarController.prototype.dataElementOptions.concat(baseOptionKeys);

defaults.set(
ViolinController.id,
helpers.merge({}, [
defaults.bar,
baseDefaults(baseOptionKeys),
{
datasets: {
points: 100,
animation: {
numbers: {
type: 'number',
properties: defaults.bar.datasets.animation.numbers.properties.concat(
['q1', 'q3', 'min', 'max', 'median', 'maxEstimate'],
baseOptionKeys.filter((c) => !c.endsWith('Color'))
),
},
kdeCoords: {
fn: interpolateKdeCoords,
properties: ['coords'],
},
ViolinController.defaults = merge({}, [
defaults.bar,
baseDefaults(baseOptionKeys),
{
datasets: {
points: 100,
animation: {
numbers: {
type: 'number',
properties: defaults.bar.datasets.animation.numbers.properties.concat(
['q1', 'q3', 'min', 'max', 'median', 'maxEstimate'],
baseOptionKeys.filter((c) => !c.endsWith('Color'))
),
},
kdeCoords: {
fn: interpolateKdeCoords,
properties: ['coords'],
},
},
},
])
);
controllers[ViolinController.id] = ViolinController;
return ViolinController;
},
]);
return registerController(ViolinController);
};

export class ViolinChart extends Chart {
Expand All @@ -88,33 +83,29 @@ HorizontalViolinController.register = () => {
baseOptionKeys
);

defaults.set(
HorizontalViolinController.id,
helpers.merge({}, [
defaults.horizontalBar,
baseDefaults(baseOptionKeys),
{
datasets: {
points: 100,
animation: {
numbers: {
type: 'number',
properties: defaults.bar.datasets.animation.numbers.properties.concat(
['q1', 'q3', 'min', 'max', 'median', 'maxEstimate'],
baseOptionKeys.filter((c) => !c.endsWith('Color'))
),
},
kdeCoords: {
fn: interpolateKdeCoords,
properties: ['coords'],
},
HorizontalViolinController.defaults = merge({}, [
defaults.horizontalBar,
baseDefaults(baseOptionKeys),
{
datasets: {
points: 100,
animation: {
numbers: {
type: 'number',
properties: defaults.bar.datasets.animation.numbers.properties.concat(
['q1', 'q3', 'min', 'max', 'median', 'maxEstimate'],
baseOptionKeys.filter((c) => !c.endsWith('Color'))
),
},
kdeCoords: {
fn: interpolateKdeCoords,
properties: ['coords'],
},
},
},
])
);
controllers[HorizontalViolinController.id] = HorizontalViolinController;
return HorizontalViolinController;
},
]);
return registerController(HorizontalViolinController);
};

export class HorizontalViolinChart extends Chart {
Expand Down
18 changes: 7 additions & 11 deletions src/elements/boxandwhiskers.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { defaults } from '../chart';
import { defaults, registerElement } from '../chart';
import { StatsBase, baseDefaults, baseOptionKeys } from './base';

export const boxOptionsKeys = baseOptionKeys.concat(['medianColor', 'lowerBackgroundColor']);
Expand Down Expand Up @@ -184,13 +184,9 @@ export class BoxAndWiskers extends StatsBase {
}
}

BoxAndWiskers._type = 'boxandwhiskers';
BoxAndWiskers.register = () => {
defaults.set('elements', {
[BoxAndWiskers._type]: Object.assign({}, defaults.elements.rectangle, baseDefaults, {
medianColor: 'transparent',
lowerBackgroundColor: 'transparent',
}),
});
return BoxAndWiskers;
};
BoxAndWiskers.id = BoxAndWiskers._type = 'boxandwhiskers';
BoxAndWiskers.defaults = Object.assign({}, defaults.elements.rectangle, baseDefaults, {
medianColor: 'transparent',
lowerBackgroundColor: 'transparent',
});
BoxAndWiskers.register = () => registerElement(BoxAndWiskers);
12 changes: 4 additions & 8 deletions src/elements/violin.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { defaults, drawPoint } from '../chart';
import { defaults, drawPoint, registerElement } from '../chart';
import { StatsBase, baseDefaults } from './base';

export class ViolinElement extends StatsBase {
Expand Down Expand Up @@ -90,10 +90,6 @@ export class ViolinElement extends StatsBase {
}
}

ViolinElement._type = 'violin';
ViolinElement.register = () => {
defaults.set('elements', {
[ViolinElement._type]: Object.assign({}, defaults.elements.rectangle, baseDefaults),
});
return ViolinElement;
};
ViolinElement.id = ViolinElement._type = 'violin';
ViolinElement.defaults = Object.assign({}, defaults.elements.rectangle, baseDefaults);
ViolinElement.register = () => registerElement(ViolinElement);
9 changes: 2 additions & 7 deletions src/tooltip.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { plugins } from './chart';
import { registerTooltipPositioner } from './chart';

export function patchInHoveredOutlier(item) {
const value = item.value;
Expand Down Expand Up @@ -32,9 +32,4 @@ export function outlierPositioner(items, eventPosition) {
}

outlierPositioner.id = 'averageInstance';
outlierPositioner.register = () => {
// register my position logic
const tooltip = plugins.getAll().find((d) => d.id === 'tooltip');
tooltip.positioners[outlierPositioner.id] = outlierPositioner;
return outlierPositioner;
};
outlierPositioner.register = () => registerTooltipPositioner(outlierPositioner);

0 comments on commit bb822a5

Please sign in to comment.