From bb822a5136a7bf636f1d885441b7a4603075d5f3 Mon Sep 17 00:00:00 2001 From: Samuel Gratzl Date: Tue, 19 May 2020 10:47:58 +0200 Subject: [PATCH] use helper methods --- src/chart.js | 64 ++++++++++++++++++++---- src/controllers/boxplot.js | 79 +++++++++++++++-------------- src/controllers/utils.js | 5 -- src/controllers/violin.js | 91 +++++++++++++++------------------- src/elements/boxandwhiskers.js | 18 +++---- src/elements/violin.js | 12 ++--- src/tooltip.js | 9 +--- 7 files changed, 147 insertions(+), 131 deletions(-) delete mode 100644 src/controllers/utils.js diff --git a/src/chart.js b/src/chart.js index ab55056..cddddff 100644 --- a/src/chart.js +++ b/src/chart.js @@ -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; +} diff --git a/src/controllers/boxplot.js b/src/controllers/boxplot.js index abaafcc..60edd24 100644 --- a/src/controllers/boxplot.js +++ b/src/controllers/boxplot.js @@ -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) { @@ -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 { @@ -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 { diff --git a/src/controllers/utils.js b/src/controllers/utils.js deleted file mode 100644 index 157d22b..0000000 --- a/src/controllers/utils.js +++ /dev/null @@ -1,5 +0,0 @@ -export function patchControllerConfig(config, controller) { - controller.register(); - config.type = controller.id; - return config; -} diff --git a/src/controllers/violin.js b/src/controllers/violin.js index 17e1ce1..cfc9bf1 100644 --- a/src/controllers/violin.js +++ b/src/controllers/violin.js @@ -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) { @@ -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 { @@ -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 { diff --git a/src/elements/boxandwhiskers.js b/src/elements/boxandwhiskers.js index 92d0a43..fdd8a1b 100644 --- a/src/elements/boxandwhiskers.js +++ b/src/elements/boxandwhiskers.js @@ -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']); @@ -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); diff --git a/src/elements/violin.js b/src/elements/violin.js index 4b4771a..a24bbc6 100644 --- a/src/elements/violin.js +++ b/src/elements/violin.js @@ -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 { @@ -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); diff --git a/src/tooltip.js b/src/tooltip.js index ddf8541..607a27b 100644 --- a/src/tooltip.js +++ b/src/tooltip.js @@ -1,4 +1,4 @@ -import { plugins } from './chart'; +import { registerTooltipPositioner } from './chart'; export function patchInHoveredOutlier(item) { const value = item.value; @@ -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);