Skip to content

Commit 4b2f879

Browse files
committed
Defaults are now applied at serialization time. This allows the fluent API a chance to configure before defaults are applied to patch missing values.
1 parent 6e4d883 commit 4b2f879

5 files changed

Lines changed: 41 additions & 43 deletions

File tree

src/expand-chart-def.ts

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -73,15 +73,8 @@ export function expandAxisMap(axisMap: IAxisMap, columns: string[]): IExpandedAx
7373
expandedAxisMap.x = expandSeriesConfig(axisMap.x);
7474
}
7575

76-
if ((!axisMap.y || (isArray(axisMap.y) && axisMap.y.length === 0)) &&
77-
(!axisMap.y2 || (isArray(axisMap.y2) && axisMap.y2.length === 0))) {
78-
expandedAxisMap.y = expandYSeriesConfigArray(columns);
79-
expandedAxisMap.y2 = [];
80-
}
81-
else {
82-
expandedAxisMap.y = expandYSeriesConfigArray(axisMap.y);
83-
expandedAxisMap.y2 = expandYSeriesConfigArray(axisMap.y2);
84-
}
76+
expandedAxisMap.y = expandYSeriesConfigArray(axisMap.y);
77+
expandedAxisMap.y2 = expandYSeriesConfigArray(axisMap.y2);
8578

8679
return expandedAxisMap;
8780
}

src/plot-api.ts

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { captureImage, ICaptureOptions } from "capture-template";
66
import { IPlotConfig, IAxisMap } from "./chart-def";
77
import { isObject, isString, isArray } from "./utils";
88
import { ChartType, IChartDef, AxisType, HorizontalLabelPosition, VerticalLabelPosition, IAxisConfig, IYAxisSeriesConfig, IAxisSeriesConfig } from "@data-forge-plot/chart-def";
9-
import { expandChartDef } from "./expand-chart-def";
9+
import { expandChartDef, expandYSeriesConfigArray } from "./expand-chart-def";
1010
import { ISeriesConfig } from "data-forge/build/lib/series";
1111

1212
const DEFAULT_CHART_PACKAGE = "@data-forge-plot/apex";
@@ -412,9 +412,21 @@ export abstract class AbstractPlotAPI implements IPlotAPI {
412412
* The JSON definition of the chart can be used to instantiate the chart in a browser.
413413
*/
414414
serialize(): IChartDef {
415-
return this.chartDef;
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;
416428
}
417-
429+
418430
/**
419431
* Used to external detect the type of this object.
420432
*/
Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
TODO: Want to get this test working again.
2-
Should also get this type of test working for series.
3-
41
import "jest";
52
import { DataFrame } from "data-forge";
63
import "../index";
@@ -9,7 +6,7 @@ describe("data-forge-plot - dataframe fluent", () => {
96

107
it("can explicity set y axis", () => {
118

12-
const df = new DataFrame({ index: [1, 2, 3], values: [{ A: 10 }, { A: 20 }, { A: 30 } ] });
9+
const df = new DataFrame({ index: [1, 2, 3], values: [{ A: 10, }, { A: 20 }, { A: 30 } ] });
1310
const plotAPI = df.plot()
1411
.y()
1512
.addSeries("A");
@@ -40,15 +37,8 @@ describe("data-forge-plot - dataframe fluent", () => {
4037
},
4138
plotConfig: {
4239
chartType: "line",
43-
x: {
44-
},
4540
y: {
4641
},
47-
y2: {
48-
},
49-
legend: {
50-
show: true,
51-
},
5242
},
5343
axisMap: {
5444
y: [

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

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -114,26 +114,6 @@ describe("expand chart def", () => {
114114
});
115115
});
116116

117-
it("y axis defaults to all columns when no y axis series is specified", () => {
118-
119-
const data: any = { columnOrder: ["a", "b", "c"] };
120-
const plotConfig: any = {};
121-
const axisMap: any = {};
122-
const chartDef = expandChartDef(data, plotConfig, axisMap);
123-
expect(chartDef.axisMap.y).toEqual([
124-
{
125-
series: "a",
126-
},
127-
{
128-
series: "b",
129-
},
130-
{
131-
series: "c",
132-
},
133-
]);
134-
expect(chartDef.axisMap.y2).toEqual([]);
135-
});
136-
137117
it("y does not default when y is specified", () => {
138118

139119
const data: any = { columnOrder: ["a", "b", "c"] };

src/test/plot-api.test.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -451,4 +451,27 @@ describe("plot-api", () => {
451451
await plot.exportWeb(outputPath);
452452
expect(exportTemplate).toHaveBeenCalled();
453453
});
454+
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+
476+
454477
});

0 commit comments

Comments
 (0)