Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 949c20e

Browse files
committed
Restructured how defaults are applied to patch missing values after the fluent API has been used.
1 parent 4b2f879 commit 949c20e

9 files changed

+151
-65
lines changed

package-lock.json

Lines changed: 7 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@
3434
},
3535
"homepage": "https://github.com/data-forge/data-forge-plot#readme",
3636
"dependencies": {
37-
"@data-forge-plot/apex": "0.0.18",
38-
"@data-forge-plot/chart-def": "^1.0.7",
37+
"@data-forge-plot/apex": "0.0.20",
38+
"@data-forge-plot/chart-def": "^1.0.9",
3939
"@data-forge/serialization": "^1.0.0",
4040
"capture-template": "^1.1.10",
4141
"inflate-template": "^1.1.6",

src/apply-defaults.ts

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import { IChartDef, ChartType } from "@data-forge-plot/chart-def";
2+
import { expandYSeriesConfigArray } from "./expand-chart-def";
3+
4+
//
5+
// Apply defaults to a chart definition and patch misssing values.
6+
//
7+
export function applyDefaults(inputChartDef: IChartDef): IChartDef {
8+
9+
const chartDef = Object.assign({}, inputChartDef);
10+
11+
if (!chartDef.plotConfig) {
12+
chartDef.plotConfig = {};
13+
}
14+
else {
15+
chartDef.plotConfig = Object.assign({}, chartDef.plotConfig);
16+
}
17+
18+
if (chartDef.plotConfig.chartType === undefined) {
19+
chartDef.plotConfig.chartType = ChartType.Line;
20+
}
21+
22+
if (chartDef.plotConfig.width === undefined) {
23+
chartDef.plotConfig.width = 800;
24+
}
25+
26+
if (chartDef.plotConfig.height === undefined) {
27+
chartDef.plotConfig.height = 600;
28+
}
29+
30+
if (!chartDef.axisMap) {
31+
chartDef.axisMap = { y: [], y2: [] };
32+
}
33+
else {
34+
chartDef.axisMap = Object.assign({}, chartDef.axisMap);
35+
if (!chartDef.axisMap.y) {
36+
chartDef.axisMap.y = [];
37+
}
38+
39+
if (!chartDef.axisMap.y2) {
40+
chartDef.axisMap.y2 = [];
41+
}
42+
}
43+
44+
if (chartDef.axisMap.y.length === 0 &&
45+
chartDef.axisMap.y2.length === 0) {
46+
chartDef.axisMap.y = expandYSeriesConfigArray(chartDef.data.columnOrder);
47+
}
48+
49+
return chartDef;
50+
}

src/chart-def.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,13 +77,13 @@ export interface IPlotConfig {
7777
* Width of the plot.
7878
* Default to 800.
7979
*/
80-
width?: number;
80+
width?: number | string;
8181

8282
/**
8383
* Height of the plot.
8484
* Default to 600.
8585
*/
86-
height?: number;
86+
height?: number | string;
8787

8888
/**
8989
* Configuration for the x axis.

src/expand-chart-def.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -96,10 +96,6 @@ export function expandChartDef(data: ISerializedDataFrame, plotConfig: IPlotConf
9696
expandedPlotConfig.y2 = expandYAxisConfig(plotConfig.y2);
9797
}
9898

99-
if (expandedPlotConfig.chartType === undefined) {
100-
expandedPlotConfig.chartType = ChartType.Line;
101-
}
102-
10399
return {
104100
data,
105101
plotConfig: expandedPlotConfig as IExpandedPlotConfig,

src/plot-api.ts

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import { isObject, isString, isArray } from "./utils";
88
import { ChartType, IChartDef, AxisType, HorizontalLabelPosition, VerticalLabelPosition, IAxisConfig, IYAxisSeriesConfig, IAxisSeriesConfig } from "@data-forge-plot/chart-def";
99
import { expandChartDef, expandYSeriesConfigArray } from "./expand-chart-def";
1010
import { ISeriesConfig } from "data-forge/build/lib/series";
11+
import { appendFile } from "fs";
12+
import { applyDefaults } from "./apply-defaults";
1113

1214
const DEFAULT_CHART_PACKAGE = "@data-forge-plot/apex";
1315

@@ -111,12 +113,12 @@ export interface IPlotAPI {
111113
/**
112114
* Set the width of the chart.
113115
*/
114-
width(width: number): IPlotAPI;
116+
width(width: number | string): IPlotAPI;
115117

116118
/**
117119
* Set the height of the chart.
118120
*/
119-
height(height: number): IPlotAPI;
121+
height(height: number | string): IPlotAPI;
120122

121123
/**
122124
* Configure the x axis.
@@ -271,15 +273,15 @@ export abstract class AbstractPlotAPI implements IPlotAPI {
271273
/**
272274
* Set the width of the chart.
273275
*/
274-
width(width: number): IPlotAPI {
276+
width(width: number | string): IPlotAPI {
275277
this.chartDef.plotConfig.width = width;
276278
return this;
277279
}
278280

279281
/**
280282
* Set the height of the chart.
281283
*/
282-
height(height: number): IPlotAPI {
284+
height(height: number | string): IPlotAPI {
283285
this.chartDef.plotConfig.height = height;
284286
return this;
285287
}
@@ -412,19 +414,7 @@ export abstract class AbstractPlotAPI implements IPlotAPI {
412414
* The JSON definition of the chart can be used to instantiate the chart in a browser.
413415
*/
414416
serialize(): IChartDef {
415-
416-
// Set defaults after configuration by fluent API.
417-
// TODO: This could be better in it's own function.
418-
419-
const chartDef = Object.assign({}, this.chartDef);
420-
chartDef.axisMap = Object.assign({}, this.chartDef.axisMap);
421-
422-
if (chartDef.axisMap.y.length === 0 &&
423-
chartDef.axisMap.y2.length === 0) {
424-
chartDef.axisMap.y = expandYSeriesConfigArray(chartDef.data.columnOrder);
425-
}
426-
427-
return chartDef;
417+
return applyDefaults(this.chartDef); // Set missing default values after configuration by the fluent.
428418
}
429419

430420
/**

src/test/apply-defaults.test.ts

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
import "jest";
2+
import { applyDefaults } from "../apply-defaults";
3+
import { ChartType } from "@data-forge-plot/chart-def";
4+
5+
describe("apply defaults", () => {
6+
7+
it("chart type defaults to line 1", () => {
8+
const inputChartDef: any = { data: { columnOrder: [], }, plotConfig: {} };
9+
const expanded = applyDefaults(inputChartDef);
10+
expect(expanded.plotConfig!.chartType!).toEqual(ChartType.Line);
11+
});
12+
13+
it("chart type defaults to line 2", () => {
14+
const inputChartDef: any = { data: { columnOrder: [], }, plotConfig: {} };
15+
const expanded = applyDefaults(inputChartDef);
16+
expect(expanded.plotConfig!.chartType!).toEqual(ChartType.Line);
17+
});
18+
19+
it("width defaults to 800", () => {
20+
const inputChartDef: any = { data: { columnOrder: [], }, plotConfig: {} };
21+
const expanded = applyDefaults(inputChartDef);
22+
expect(expanded.plotConfig!.width).toEqual(800);
23+
});
24+
25+
it("height defaults to 600", () => {
26+
const inputChartDef: any = { data: { columnOrder: [], }, plotConfig: {} };
27+
const expanded = applyDefaults(inputChartDef);
28+
expect(expanded.plotConfig!.height).toEqual(600);
29+
});
30+
31+
it("y axis defaults to all columns when no y axis series is specified 1", () => {
32+
33+
const data: any = { columnOrder: ["a", "b", "c"] };
34+
const plotConfig: any = {};
35+
const axisMap: any = {};
36+
const inputChartDef: any = { data, plotConfig, axisMap };
37+
const expanded = applyDefaults(inputChartDef);
38+
expect(expanded.axisMap.y).toEqual([
39+
{
40+
series: "a",
41+
},
42+
{
43+
series: "b",
44+
},
45+
{
46+
series: "c",
47+
},
48+
]);
49+
expect(expanded.axisMap.y2).toEqual([]);
50+
});
51+
52+
it("y axis defaults to all columns when no y axis series is specified 2", () => {
53+
54+
const data: any = { columnOrder: ["a", "b", "c"] };
55+
const plotConfig: any = {};
56+
const axisMap: any = { y: [], y2: [] };
57+
const inputChartDef: any = { data, plotConfig, axisMap };
58+
const expanded = applyDefaults(inputChartDef);
59+
expect(expanded.axisMap.y).toEqual([
60+
{
61+
series: "a",
62+
},
63+
{
64+
series: "b",
65+
},
66+
{
67+
series: "c",
68+
},
69+
]);
70+
expect(expanded.axisMap.y2).toEqual([]);
71+
});
72+
});

src/test/expand-chart-def.test.ts

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -142,15 +142,6 @@ describe("expand chart def", () => {
142142
]);
143143
});
144144

145-
it("chart type defaults to line if not specified", () => {
146-
147-
const data: any = {};
148-
const plotConfig: any = {};
149-
const axisMap: any = {};
150-
const chartDef = expandChartDef(data, plotConfig, axisMap);
151-
expect(chartDef.plotConfig.chartType).toBe(ChartType.Line);
152-
});
153-
154145
it("can set chart type", () => {
155146
const data: any = {};
156147
const plotConfig: any = { chartType: ChartType.Donut };

src/test/plot-api.test.ts

Lines changed: 11 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,14 @@ import { IAxisMap, IPlotConfig } from "../chart-def";
99

1010
describe("plot-api", () => {
1111

12+
it("chart type defaults to line when not specified", () => {
13+
const data: any = {};
14+
const plotConfig: IPlotConfig = {};
15+
const axisMap: IAxisMap = {};
16+
const plot = new PlotAPI(data, plotConfig, axisMap);
17+
expect(plot.serialize().plotConfig.chartType).toBe(ChartType.Line);
18+
});
19+
1220
it("can set chart type from def", () => {
1321
const data: any = {};
1422
const plotConfig: IPlotConfig = { chartType: ChartType.Bubble };
@@ -88,7 +96,7 @@ describe("plot-api", () => {
8896
.x()
8997
.label("my label")
9098
.serialize();
91-
expect(serialized.plotConfig.x.label!.text).toBe("my label");
99+
expect(serialized.plotConfig.x!.label!.text).toBe("my label");
92100
});
93101

94102
it("can configure y axis", () => {
@@ -100,7 +108,7 @@ describe("plot-api", () => {
100108
.y()
101109
.label("my label")
102110
.serialize();
103-
expect(serialized.plotConfig.y.label!.text).toBe("my label");
111+
expect(serialized.plotConfig.y!.label!.text).toBe("my label");
104112
});
105113

106114
it("can configure y2 axis", () => {
@@ -112,7 +120,7 @@ describe("plot-api", () => {
112120
.y2()
113121
.label("my label")
114122
.serialize();
115-
expect(serialized.plotConfig.y2.label!.text).toBe("my label");
123+
expect(serialized.plotConfig.y2!.label!.text).toBe("my label");
116124
});
117125

118126
it("can set x axis series", () => {
@@ -452,26 +460,5 @@ describe("plot-api", () => {
452460
expect(exportTemplate).toHaveBeenCalled();
453461
});
454462

455-
it("y axis defaults to all columns when no y axis series is specified", () => {
456-
457-
const data: any = { columnOrder: ["a", "b", "c"] };
458-
const plotConfig: any = {};
459-
const axisMap: any = {};
460-
const plot = new PlotAPI(data, plotConfig, axisMap);
461-
const chartDef = plot.serialize();
462-
expect(chartDef.axisMap.y).toEqual([
463-
{
464-
series: "a",
465-
},
466-
{
467-
series: "b",
468-
},
469-
{
470-
series: "c",
471-
},
472-
]);
473-
expect(chartDef.axisMap.y2).toEqual([]);
474-
});
475-
476463

477464
});

0 commit comments

Comments
 (0)