Compare commits

..

1 Commits

Author SHA1 Message Date
glitch4347 f6119913fb events refactoring begin 9 months ago
  1. 6
      demo/demo_pod.ts
  2. 2
      package.json
  3. 42
      src/VueChartwerkPodMixin.ts
  4. 50
      src/index.ts
  5. 67
      src/models/options.ts
  6. 1
      src/models/series.ts
  7. 15
      src/models/state.ts
  8. 2
      src/types.ts
  9. 9
      tsconfig.json

6
demo/demo_pod.ts

@ -51,6 +51,12 @@ class DemoPod extends ChartwerkPod<Serie, Options> {
.attr('d', this.lineGenerator); .attr('d', this.lineGenerator);
} }
onMouseMove(): void {}
onMouseOver(): void {}
onMouseOut(): void {}
onMouseClick(): void {}
renderSharedCrosshair(values): void {}
hideSharedCrosshair(): void {}
} }
export { DemoPod }; export { DemoPod };

2
package.json

@ -1,6 +1,6 @@
{ {
"name": "@chartwerk/core", "name": "@chartwerk/core",
"version": "0.6.26", "version": "0.6.21",
"description": "Chartwerk core", "description": "Chartwerk core",
"main": "dist/index.js", "main": "dist/index.js",
"types": "dist/index.d.ts", "types": "dist/index.d.ts",

42
src/VueChartwerkPodMixin.ts

@ -39,9 +39,9 @@ export default {
}, },
methods: { methods: {
// it's "abstract" method. "children" components should override it // it's "abstract" method. "children" components should override it
render() {}, render() { },
renderSharedCrosshair(values: { x?: number, y?: number }) {}, renderSharedCrosshair(values: { x?: number, y?: number }) { },
hideSharedCrosshair() {}, hideSharedCrosshair() { },
onPanningRescale(event) { onPanningRescale(event) {
this.pod.rescaleMetricAndAxis(event); this.pod.rescaleMetricAndAxis(event);
}, },
@ -50,48 +50,41 @@ export default {
this.render(); this.render();
}, },
appendEvents() { appendEvents() {
if(this.options.events === undefined) { if(this.options.eventsCallbacks === undefined) {
if(this.options.eventsCallbacks !== undefined) { this.options.eventsCallbacks = {}
this.options.events = this.options.eventsCallbacks
} else {
this.options.events = {};
}
} }
if(has(this.$listeners, 'zoomIn')) { if(has(this.$listeners, 'zoomIn')) {
this.options.events.zoomIn = this.zoomIn.bind(this); this.options.eventsCallbacks.zoomIn = this.zoomIn.bind(this);
} }
if(has(this.$listeners, 'zoomOut')) { if(has(this.$listeners, 'zoomOut')) {
this.options.events.zoomOut = this.zoomOut.bind(this); this.options.eventsCallbacks.zoomOut = this.zoomOut.bind(this);
}
if(has(this.$listeners, 'mouseOver')) {
this.options.events.mouseOver = this.mouseOver.bind(this);
} }
if(has(this.$listeners, 'mouseMove')) { if(has(this.$listeners, 'mouseMove')) {
this.options.events.mouseMove = this.mouseMove.bind(this); this.options.eventsCallbacks.mouseMove = this.mouseMove.bind(this);
} }
if(has(this.$listeners, 'mouseOut')) { if(has(this.$listeners, 'mouseOut')) {
this.options.events.mouseOut = this.mouseOut.bind(this); this.options.eventsCallbacks.mouseOut = this.mouseOut.bind(this);
} }
if(has(this.$listeners, 'onLegendClick')) { if(has(this.$listeners, 'onLegendClick')) {
this.options.events.onLegendClick = this.onLegendClick.bind(this); this.options.eventsCallbacks.onLegendClick = this.onLegendClick.bind(this);
} }
if(has(this.$listeners, 'panningEnd')) { if(has(this.$listeners, 'panningEnd')) {
this.options.events.panningEnd = this.panningEnd.bind(this); this.options.eventsCallbacks.panningEnd = this.panningEnd.bind(this);
} }
if(has(this.$listeners, 'panning')) { if(has(this.$listeners, 'panning')) {
this.options.events.panning = this.panning.bind(this); this.options.eventsCallbacks.panning = this.panning.bind(this);
} }
if(has(this.$listeners, 'contextMenu')) { if(has(this.$listeners, 'contextMenu')) {
this.options.events.contextMenu = this.contextMenu.bind(this); this.options.eventsCallbacks.contextMenu = this.contextMenu.bind(this);
} }
if(has(this.$listeners, 'sharedCrosshairMove')) { if(has(this.$listeners, 'sharedCrosshairMove')) {
this.options.events.sharedCrosshairMove = this.sharedCrosshairMove.bind(this); this.options.eventsCallbacks.sharedCrosshairMove = this.sharedCrosshairMove.bind(this);
} }
if(has(this.$listeners, 'renderStart')) { if(has(this.$listeners, 'renderStart')) {
this.options.events.renderStart = this.renderStart.bind(this); this.options.eventsCallbacks.renderStart = this.renderStart.bind(this);
} }
if(has(this.$listeners, 'renderEnd')) { if(has(this.$listeners, 'renderEnd')) {
this.options.events.renderEnd = this.renderEnd.bind(this); this.options.eventsCallbacks.renderEnd = this.renderEnd.bind(this);
} }
}, },
zoomIn(range) { zoomIn(range) {
@ -100,9 +93,6 @@ export default {
zoomOut(centers) { zoomOut(centers) {
this.$emit('zoomOut', centers); this.$emit('zoomOut', centers);
}, },
mouseOver() {
this.$emit('mouseOver');
},
mouseMove(evt) { mouseMove(evt) {
this.$emit('mouseMove', evt); this.$emit('mouseMove', evt);
}, },

50
src/index.ts

@ -1,4 +1,3 @@
import { AxisRange } from './types';
import VueChartwerkPodMixin from './VueChartwerkPodMixin'; import VueChartwerkPodMixin from './VueChartwerkPodMixin';
import { Grid } from './components/grid'; import { Grid } from './components/grid';
@ -35,9 +34,9 @@ import debounce from 'lodash/debounce';
const DEFAULT_TICK_SIZE = 2; const DEFAULT_TICK_SIZE = 2;
const MILISECONDS_IN_MINUTE = 60 * 1000;
abstract class ChartwerkPod<T extends Serie, O extends Options> {
class ChartwerkPod<T extends Serie, O extends Options> {
protected series: CoreSeries<T>; protected series: CoreSeries<T>;
protected options: CoreOptions<O>; protected options: CoreOptions<O>;
@ -79,8 +78,7 @@ class ChartwerkPod<T extends Serie, O extends Options> {
_series: T[] = [], _series: T[] = [],
_options: O _options: O
) { ) {
// need to call explicitly because option lazyStyleTag // TODO: test if it's necessary
// in webpack style-loader
styles.use(); styles.use();
this.options = new CoreOptions(_options); this.options = new CoreOptions(_options);
@ -116,7 +114,6 @@ class ChartwerkPod<T extends Serie, O extends Options> {
this.renderLegend(); this.renderLegend();
this.renderYLabel(); this.renderYLabel();
this.renderY1Label();
this.renderXLabel(); this.renderXLabel();
this.options.callbackRenderEnd(); this.options.callbackRenderEnd();
@ -153,13 +150,13 @@ class ChartwerkPod<T extends Serie, O extends Options> {
this.series.updateSeries(newSeries); this.series.updateSeries(newSeries);
} }
protected renderMetrics(): void {} protected abstract renderMetrics(): void;
protected onMouseOver(): void {} protected abstract onMouseOver(): void;
protected onMouseOut(): void {} protected abstract onMouseOut(): void;
protected onMouseMove(): void {} protected abstract onMouseMove(): void;
protected onMouseClick(): void {} protected abstract onMouseClick(): void;
public renderSharedCrosshair(values: { x?: number, y?: number }): void {} public abstract renderSharedCrosshair(values: { x?: number, y?: number }): void;
public hideSharedCrosshair(): void {} public abstract hideSharedCrosshair(): void;
protected initPodState(): void { protected initPodState(): void {
const boxPararms = { const boxPararms = {
@ -510,8 +507,8 @@ class ChartwerkPod<T extends Serie, O extends Options> {
return; return;
} }
this.chartContainer.append('text') this.chartContainer.append('text')
.attr('y', -this.margin.left) .attr('y', 0 - this.margin.left)
.attr('x', -(this.height / 2)) .attr('x', 0 - (this.height / 2))
.attr('dy', '1em') .attr('dy', '1em')
.attr('class', 'y-axis-label') .attr('class', 'y-axis-label')
.attr('transform', 'rotate(-90)') .attr('transform', 'rotate(-90)')
@ -521,22 +518,6 @@ class ChartwerkPod<T extends Serie, O extends Options> {
.text(this.options.axis.y.label); .text(this.options.axis.y.label);
} }
protected renderY1Label(): void {
if(this.options.axis.y1?.label === undefined) {
return;
}
this.chartContainer.append('text')
.attr('y', -this.width - this.margin.right)
.attr('x', (this.height / 2))
.attr('dy', '1em')
.attr('class', 'y-axis-label')
.attr('transform', 'rotate(90)')
.style('text-anchor', 'middle')
.style('font-size', '14px')
.style('fill', 'currentColor')
.text(this.options.axis.y1.label);
}
protected renderXLabel(): void { protected renderXLabel(): void {
if(this.options.axis.x.label === undefined) { if(this.options.axis.x.label === undefined) {
return; return;
@ -817,7 +798,8 @@ class ChartwerkPod<T extends Serie, O extends Options> {
this.state.yValueRange = yRange; this.state.yValueRange = yRange;
this.brushStartSelection = null; this.brushStartSelection = null;
} }
this.zoomIn([xRange, yRange]);
this.options.callbackZoomIn([xRange, yRange]);
} }
protected zoomOut(): void { protected zoomOut(): void {
@ -830,10 +812,6 @@ class ChartwerkPod<T extends Serie, O extends Options> {
this.options.callbackZoomOut(centers); this.options.callbackZoomOut(centers);
} }
protected zoomIn(ranges: AxisRange[]): void {
this.options.callbackZoomIn(ranges);
}
getAxisTicksFormatter(axisOptions: AxisOption): (d: any, i: number) => any { getAxisTicksFormatter(axisOptions: AxisOption): (d: any, i: number) => any {
// TODO: ticksCount === 0 -> suspicious option // TODO: ticksCount === 0 -> suspicious option
if(axisOptions.ticksCount === 0) { if(axisOptions.ticksCount === 0) {

67
src/models/options.ts

@ -90,6 +90,7 @@ export const CORE_DEFAULT_OPTIONS: Options = {
crosshair: DEFAULT_CROSSHAIR_OPTIONS, crosshair: DEFAULT_CROSSHAIR_OPTIONS,
renderLegend: true, renderLegend: true,
margin: DEFAULT_MARGIN, margin: DEFAULT_MARGIN,
eventsCallbacks: {},
} }
export class CoreOptions<O extends Options> { export class CoreOptions<O extends Options> {
@ -105,14 +106,6 @@ export class CoreOptions<O extends Options> {
protected setOptions(options: O): void { protected setOptions(options: O): void {
this._options = lodashDefaultsDeep(lodashCloneDeep(options), this.getDefaults()); this._options = lodashDefaultsDeep(lodashCloneDeep(options), this.getDefaults());
if(this._options.eventsCallbacks !== undefined) {
if(this._options.events !== undefined) {
throw new Error('events and eventsCallbacks are mutually exclusive');
}
this._options.events = this._options.eventsCallbacks;
}
// also bakward compatibility for clients who use "eventsCallbacks" instead of "events"
this._options.eventsCallbacks = this._options.events;
} }
// this getter can be overrited in Pod // this getter can be overrited in Pod
@ -167,86 +160,80 @@ export class CoreOptions<O extends Options> {
// event callbacks // event callbacks
callbackRenderStart(): void { callbackRenderStart(): void {
if(has(this._options.events, 'renderStart')) { if(has(this._options.eventsCallbacks, 'renderStart')) {
this._options.events.renderStart(); this._options.eventsCallbacks.renderStart();
} }
} }
callbackRenderEnd(): void { callbackRenderEnd(): void {
if(has(this._options.events, 'renderEnd')) { if(has(this._options.eventsCallbacks, 'renderEnd')) {
this._options.events.renderEnd(); this._options.eventsCallbacks.renderEnd();
} }
} }
callbackComponentRenderEnd(part: RenderComponent): void { callbackComponentRenderEnd(part: RenderComponent): void {
if(has(this._options.events, 'componentRenderEnd')) { if(has(this._options.eventsCallbacks, 'componentRenderEnd')) {
this._options.events.componentRenderEnd(part); this._options.eventsCallbacks.componentRenderEnd(part);
} }
} }
callbackLegendClick(idx: number): void { callbackLegendClick(idx: number): void {
if(has(this._options.events, 'onLegendClick')) { if(has(this._options.eventsCallbacks, 'onLegendClick')) {
this._options.events.onLegendClick(idx); this._options.eventsCallbacks.onLegendClick(idx);
} }
} }
callbackLegendLabelClick(idx: number): void { callbackLegendLabelClick(idx: number): void {
if(has(this._options.events, 'onLegendLabelClick')) { if(has(this._options.eventsCallbacks, 'onLegendLabelClick')) {
this._options.events.onLegendLabelClick(idx); this._options.eventsCallbacks.onLegendLabelClick(idx);
} }
} }
callbackPanning(event: { ranges: AxisRange[], d3Event: any }): void { callbackPanning(event: { ranges: AxisRange[], d3Event: any }): void {
if(has(this._options.events, 'panning')) { if(has(this._options.eventsCallbacks, 'panning')) {
this._options.events.panning(event); this._options.eventsCallbacks.panning(event);
} }
} }
callbackPanningEnd(ranges: AxisRange[]): void { callbackPanningEnd(ranges: AxisRange[]): void {
if(has(this._options.events, 'panningEnd')) { if(has(this._options.eventsCallbacks, 'panningEnd')) {
this._options.events.panningEnd(ranges); this._options.eventsCallbacks.panningEnd(ranges);
} }
} }
callbackZoomIn(ranges: AxisRange[]): void { callbackZoomIn(ranges: AxisRange[]): void {
if(has(this._options.events, 'zoomIn')) { if(has(this._options.eventsCallbacks, 'zoomIn')) {
this._options.events.zoomIn(ranges); this._options.eventsCallbacks.zoomIn(ranges);
} }
} }
callbackZoomOut(centers: { x: number, y: number }): void { callbackZoomOut(centers: { x: number, y: number }): void {
if(has(this._options.events, 'zoomOut')) { if(has(this._options.eventsCallbacks, 'zoomOut')) {
this._options.events.zoomOut(centers); this._options.eventsCallbacks.zoomOut(centers);
} }
} }
callbackSharedCrosshairMove(event: { datapoints, eventX, eventY }): void { callbackSharedCrosshairMove(event: { datapoints, eventX, eventY }): void {
if(has(this._options.events, 'sharedCrosshairMove')) { if(has(this._options.eventsCallbacks, 'sharedCrosshairMove')) {
this._options.events.sharedCrosshairMove(event); this._options.eventsCallbacks.sharedCrosshairMove(event);
}
}
callbackMouseOver(): void {
if(has(this._options.events, 'mouseOver')) {
this._options.events.mouseOver();
} }
} }
callbackMouseMove(event): void { callbackMouseMove(event): void {
if(has(this._options.events, 'mouseMove')) { if(has(this._options.eventsCallbacks, 'mouseMove')) {
this._options.events.mouseMove(event); this._options.eventsCallbacks.mouseMove(event);
} }
} }
callbackMouseOut(): void { callbackMouseOut(): void {
if(has(this._options.events, 'mouseOut')) { if(has(this._options.eventsCallbacks, 'mouseOut')) {
this._options.events.mouseOut(); this._options.eventsCallbacks.mouseOut();
} }
} }
callbackMouseClick(event): void { callbackMouseClick(event): void {
if(has(this._options.events, 'mouseClick')) { if(has(this._options.eventsCallbacks, 'mouseClick')) {
this._options.events.mouseClick(event); this._options.eventsCallbacks.mouseClick(event);
} }
} }
} }

1
src/models/series.ts

@ -35,7 +35,6 @@ export class CoreSeries<T extends Serie> {
_series: Array<T> = []; _series: Array<T> = [];
constructor(series: T[], private _podDefaults?: Partial<T>) { constructor(series: T[], private _podDefaults?: Partial<T>) {
// TODO: create separate Serie class, and store instances in this._series
this.setSeries(series); this.setSeries(series);
} }

15
src/models/state.ts

@ -1,4 +1,4 @@
import { Serie, Options, yAxisOrientation } from '../types'; import { Serie, Options } from '../types';
import { CoreSeries } from './series'; import { CoreSeries } from './series';
import { CoreOptions } from './options'; import { CoreOptions } from './options';
@ -89,15 +89,6 @@ export class PodState<T extends Serie, O extends Options> {
this._transform = { x: 0, y: 0, k: 1 }; this._transform = { x: 0, y: 0, k: 1 };
} }
getYScaleByOrientation(orientation?: yAxisOrientation): d3.ScaleLinear<number, number> {
// TODO: we set defaults in Series class, so we don't expect `undefined` here
// we can remove this check when we implement Serie class (see TODO in `series.ts`)
if(orientation === undefined) {
return this._yScale;
}
return orientation === yAxisOrientation.LEFT ? this._yScale : this._y1Scale;
}
get yScale(): d3.ScaleLinear<number, number> { get yScale(): d3.ScaleLinear<number, number> {
return this._yScale; return this._yScale;
} }
@ -196,7 +187,7 @@ export class PodState<T extends Serie, O extends Options> {
if(this.coreOptions.axis.y1.range !== undefined) { if(this.coreOptions.axis.y1.range !== undefined) {
return min(this.coreOptions.axis.y1.range); return min(this.coreOptions.axis.y1.range);
} }
return this.coreSeries.minValueY1; return this.coreSeries.minValueY;
} }
public getMaxValueY1(): number { public getMaxValueY1(): number {
@ -206,7 +197,7 @@ export class PodState<T extends Serie, O extends Options> {
if(this.coreOptions.axis.y1.range !== undefined) { if(this.coreOptions.axis.y1.range !== undefined) {
return max(this.coreOptions.axis.y1.range); return max(this.coreOptions.axis.y1.range);
} }
return this.coreSeries.maxValueY1; return this.coreSeries.maxValueY;
} }
// getters for correct transform // getters for correct transform

2
src/types.ts

@ -20,7 +20,6 @@ export type Events = {
panning?: (event: { ranges: AxisRange[], d3Event: any }) => void, panning?: (event: { ranges: AxisRange[], d3Event: any }) => void,
panningEnd?: (range: AxisRange[]) => void, panningEnd?: (range: AxisRange[]) => void,
zoomOut?: (centers: {x: number, y: number}) => void, zoomOut?: (centers: {x: number, y: number}) => void,
mouseOver?: () => void,
mouseMove?: (evt: any) => void, mouseMove?: (evt: any) => void,
mouseClick?: (evt: any) => void, mouseClick?: (evt: any) => void,
mouseOut?: () => void, mouseOut?: () => void,
@ -35,7 +34,6 @@ export type Events = {
export type Options = { export type Options = {
margin?: Margin; margin?: Margin;
// obsolete property, use events instead
eventsCallbacks?: Events; eventsCallbacks?: Events;
events?: Events; events?: Events;
axis?: AxesOptions; axis?: AxesOptions;

9
tsconfig.json

@ -15,11 +15,6 @@
"noImplicitUseStrict": false, "noImplicitUseStrict": false,
"noImplicitAny": false, "noImplicitAny": false,
"noUnusedLocals": false, "noUnusedLocals": false,
"moduleResolution": "node", "moduleResolution": "node"
}, }
"exclude": [
"node_modules",
"dist",
"demo"
]
} }

Loading…
Cancel
Save