Skip to content

Commit e474fb6

Browse files
committed
feat(core): specify config as interfaces
- add interfaces for each chart types specific config - add interface for the data format - clean up unused variables
1 parent 5865774 commit e474fb6

10 files changed

+475
-85
lines changed

Diff for: packages/core/src/bar-chart.ts

+14-12
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
// D3 Imports
22
import { select, mouse } from "d3-selection";
3-
import { scaleBand } from "d3-scale";
3+
import { scaleBand, ScaleBand } from "d3-scale";
44
import { min } from "d3-array";
55

66
import { BaseAxisChart } from "./base-axis-chart";
77
import { StackedBarChart } from "./stacked-bar-chart";
88
import * as Configuration from "./configuration";
9+
import { ChartConfig, BarChartOptions, ChartTypes } from "./configuration";
910

1011
import { Tools } from "./tools";
1112

@@ -53,12 +54,11 @@ const isWidthConstrained = (maxWidth, currentBandWidth) => {
5354
};
5455

5556
export class BarChart extends BaseAxisChart {
56-
x: any;
57-
x1?: any;
58-
y: any;
59-
colorScale: any;
57+
x1?: ScaleBand<any>;
6058

61-
constructor(holder: Element, configs: any) {
59+
options: BarChartOptions;
60+
61+
constructor(holder: Element, configs: ChartConfig<BarChartOptions>) {
6262
// If this is a stacked bar chart, change the object prototype
6363
if (configs.options.scales.y.stacked) {
6464
if (getYMin(configs) >= 0) {
@@ -70,6 +70,13 @@ export class BarChart extends BaseAxisChart {
7070

7171
super(holder, configs);
7272

73+
// initialize options
74+
if (configs.options) {
75+
this.options = Object.assign({}, Configuration.options.BAR, configs.options);
76+
} else {
77+
this.options = Object.assign({}, Configuration.options.BAR);
78+
}
79+
7380
// To be used for combo chart instances of a bar chart
7481
const { axis } = configs.options;
7582
if (axis) {
@@ -82,7 +89,7 @@ export class BarChart extends BaseAxisChart {
8289
.rangeRound([0, getMaxBarWidth(Tools.getProperty(this.options, "bars", "maxWidth"), this.x.bandwidth())]);
8390
}
8491

85-
this.options.type = "bar";
92+
this.options.type = ChartTypes.BAR;
8693
}
8794

8895
setXScale(xScale?: any) {
@@ -237,11 +244,6 @@ export class BarChart extends BaseAxisChart {
237244
}
238245

239246
updateElements(animate: boolean, rect?: any, g?: any) {
240-
const { scales } = this.options;
241-
242-
const chartSize = this.getChartSize();
243-
const height = chartSize.height - this.getBBox(".x.axis").height;
244-
245247
if (!rect) {
246248
rect = this.innerWrap.selectAll("rect.bar");
247249
}

Diff for: packages/core/src/base-axis-chart.ts

+7-6
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,24 @@
11
// D3 Imports
22
import { select } from "d3-selection";
3-
import { scaleBand, scaleLinear } from "d3-scale";
4-
import { axisBottom, axisLeft, axisRight } from "d3-axis";
3+
import { scaleBand, scaleLinear, ScaleBand, ScaleLinear } from "d3-scale";
4+
import { axisBottom, axisLeft, axisRight, AxisScale, AxisDomain } from "d3-axis";
55
import { min, max } from "d3-array";
66

77
import { BaseChart } from "./base-chart";
88

99
import * as Configuration from "./configuration";
10+
import { ChartConfig, AxisChartOptions } from "./configuration";
1011
import { Tools } from "./tools";
1112

1213
export class BaseAxisChart extends BaseChart {
13-
x: any;
14-
y: any;
15-
y2: any;
14+
x: ScaleBand<any>;
15+
y: ScaleLinear<any, any>;
16+
y2: ScaleLinear<any, any>;
1617
thresholdDimensions: any;
1718

1819
options: any = Object.assign({}, Configuration.options.AXIS);
1920

20-
constructor(holder: Element, configs: any) {
21+
constructor(holder: Element, configs: ChartConfig<AxisChartOptions>) {
2122
super(holder, configs);
2223

2324
if (configs.options) {

Diff for: packages/core/src/base-chart.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import { transition, Transition } from "d3-transition";
1010

1111
// Internal Imports
1212
import * as Configuration from "./configuration";
13+
import { ChartConfig, BaseChartOptions, ChartData } from "./configuration";
1314
import { Tools } from "./tools";
1415
import PatternsService from "./services/patterns";
1516

@@ -28,11 +29,11 @@ export class BaseChart {
2829
svg: any;
2930
innerWrap: any;
3031

31-
options: any = Object.assign({}, Configuration.options.BASE);
32+
options: BaseChartOptions = Object.assign({}, Configuration.options.BASE);
3233

3334
// Data
34-
data: any;
35-
displayData: any;
35+
data: ChartData;
36+
displayData: ChartData;
3637
fixedDataLabels;
3738

3839
// Fill scales & fill related objects
@@ -46,7 +47,7 @@ export class BaseChart {
4647
tooltips: null
4748
};
4849

49-
constructor(holder: Element, configs: any) {
50+
constructor(holder: Element, configs: ChartConfig<BaseChartOptions>) {
5051
this.id = `chart-${BaseChart.chartCount++}`;
5152

5253
if (configs.options) {
@@ -165,7 +166,6 @@ export class BaseChart {
165166
}
166167

167168
getKeysFromData() {
168-
const { datasets } = this.displayData;
169169
const keys = {};
170170

171171
if (this.getLegendType() === Configuration.legend.basedOn.LABELS) {

Diff for: packages/core/src/combo-chart.ts

+10-6
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,20 @@
11
import { BaseAxisChart } from "./base-axis-chart";
22

3-
import * as ChartTypes from "./index";
3+
import * as ChartInstances from "./index";
4+
import * as Configuration from "./configuration";
5+
import { ChartConfig, ComboChartOptions, ChartTypes } from "./configuration";
46

57
// TODO - Support adding/removing charts when updating data
68
export class ComboChart extends BaseAxisChart {
79
// Includes all the sub-charts
810
charts = [];
911

10-
constructor(holder: Element, configs: any) {
12+
options: ComboChartOptions = Object.assign({}, Configuration.options.COMBO);
13+
14+
constructor(holder: Element, configs: ChartConfig<ComboChartOptions>) {
1115
super(holder, configs);
1216

13-
this.options.type = "combo";
17+
this.options.type = ChartTypes.COMBO;
1418
}
1519

1620
// Extract data related to the specific sub-chart
@@ -47,10 +51,10 @@ export class ComboChart extends BaseAxisChart {
4751

4852
this.displayData.datasets.forEach(dataset => {
4953
// If the chart type is valid
50-
if (ChartTypes[dataset.chartType]) {
54+
if (ChartInstances[dataset.chartType]) {
5155
// If the chart for this dataset has not already been created
5256
if (this.charts.findIndex(chart => chart.type === dataset.chartType) === -1) {
53-
if (ChartTypes[dataset.chartType].prototype instanceof BaseAxisChart) {
57+
if (ChartInstances[dataset.chartType].prototype instanceof BaseAxisChart) {
5458
const chartConfigs = {
5559
data: this.extractDataForChart(dataset.chartType),
5660
options: Object.assign({}, this.options, {
@@ -62,7 +66,7 @@ export class ComboChart extends BaseAxisChart {
6266
})
6367
};
6468

65-
const chart = new ChartTypes[dataset.chartType](
69+
const chart = new ChartInstances[dataset.chartType](
6670
this.holder,
6771
chartConfigs
6872
);

0 commit comments

Comments
 (0)