Skip to content

Commit 65044f8

Browse files
committed
Data-Frame and Series plot now have different defaults. Show legend is true for DF and false for series.
1 parent b013ccb commit 65044f8

7 files changed

Lines changed: 86 additions & 24 deletions

File tree

src/apply-defaults.ts

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,29 @@
11
import { IChartDef, ChartType } from "@data-forge-plot/chart-def";
2-
import { expandYSeriesConfigArray } from "./expand-chart-def";
2+
import { expandYSeriesConfigArray, expandPlotConfig } from "./expand-chart-def";
3+
import { IPlotConfig } from "./chart-def";
34

45
//
56
// Apply defaults to a chart definition and patch misssing values.
67
//
7-
export function applyDefaults(inputChartDef: IChartDef): IChartDef {
8+
export function applyDefaults(inputChartDef: IChartDef, plotDefaults?: IPlotConfig): IChartDef {
89

910
const chartDef = Object.assign({}, inputChartDef);
1011

1112
if (!chartDef.plotConfig) {
12-
chartDef.plotConfig = {};
13+
if (plotDefaults) {
14+
chartDef.plotConfig = Object.assign({}, expandPlotConfig(plotDefaults));
15+
}
16+
else {
17+
chartDef.plotConfig = {};
18+
}
1319
}
1420
else {
15-
chartDef.plotConfig = Object.assign({}, chartDef.plotConfig);
21+
if (plotDefaults) {
22+
chartDef.plotConfig = Object.assign({}, expandPlotConfig(plotDefaults), chartDef.plotConfig);
23+
}
24+
else {
25+
chartDef.plotConfig = Object.assign({}, chartDef.plotConfig);
26+
}
1627
}
1728

1829
if (chartDef.plotConfig.chartType === undefined) {

src/expand-chart-def.ts

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -79,11 +79,8 @@ export function expandAxisMap(axisMap: IAxisMap, columns: string[]): IExpandedAx
7979
return expandedAxisMap;
8080
}
8181

82-
export function expandChartDef(data: ISerializedDataFrame, plotConfig: IPlotConfig, axisMap: IAxisMap): IChartDef {
83-
84-
const expandedPlotConfig = Object.assign({}, plotConfig);
85-
const expandedAxisMap = expandAxisMap(axisMap, data.columnOrder);
86-
82+
export function expandPlotConfig(plotConfig: IPlotConfig): IExpandedPlotConfig {
83+
const expandedPlotConfig = Object.assign({}, plotConfig) as IExpandedPlotConfig;
8784
if (plotConfig.x) {
8885
expandedPlotConfig.x = expandAxisConfig(plotConfig.x);
8986
}
@@ -96,9 +93,13 @@ export function expandChartDef(data: ISerializedDataFrame, plotConfig: IPlotConf
9693
expandedPlotConfig.y2 = expandYAxisConfig(plotConfig.y2);
9794
}
9895

96+
return expandedPlotConfig;
97+
}
98+
99+
export function expandChartDef(data: ISerializedDataFrame, plotConfig: IPlotConfig, axisMap: IAxisMap): IChartDef {
99100
return {
100101
data,
101-
plotConfig: expandedPlotConfig as IExpandedPlotConfig,
102-
axisMap: expandedAxisMap as IExpandedAxisMap,
102+
plotConfig: expandPlotConfig(plotConfig),
103+
axisMap: expandAxisMap(axisMap, data.columnOrder),
103104
};
104105
}

src/index.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,17 @@ declare module "data-forge/build/lib/series" {
2222
}
2323
}
2424

25+
const seriesPlotDefaults: IPlotConfig = {
26+
legend: {
27+
show: false,
28+
},
29+
};
30+
2531
function plotSeries(this: ISeries<any, any>, plotConfig?: IPlotConfig, axisMap?: IAxisMap): IPlotAPI {
2632
const serializedData = this
2733
.inflate((value: any) => ({ __value__: value }))
2834
.serialize();
29-
return new PlotAPI(serializedData, plotConfig || {}, axisMap || {});
35+
return new PlotAPI(serializedData, plotConfig || {}, axisMap || {}, seriesPlotDefaults);
3036
}
3137

3238
Series.prototype.startPlot = startPlot;
@@ -52,9 +58,15 @@ declare module "data-forge/build/lib/dataframe" {
5258
}
5359
}
5460

61+
const dataFramePlotDefaults: IPlotConfig = {
62+
legend: {
63+
show: true,
64+
},
65+
};
66+
5567
function plotDataFrame(this: IDataFrame<any, any>, plotDef?: IPlotConfig, axisMap?: IAxisMap): IPlotAPI {
5668
const serializedData = this.serialize();
57-
return new PlotAPI(serializedData, plotDef || {}, axisMap || {});
69+
return new PlotAPI(serializedData, plotDef || {}, axisMap || {}, dataFramePlotDefaults);
5870
}
5971

6072
DataFrame.prototype.startPlot = startPlot;

src/plot-api.ts

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,9 @@ import { ISerializedDataFrame } from "@data-forge/serialization";
44
import { exportTemplate, IExportOptions } from "inflate-template";
55
import { captureImage, ICaptureOptions } from "capture-template";
66
import { IPlotConfig, IAxisMap } from "./chart-def";
7-
import { isObject, isString, isArray } from "./utils";
7+
import { isObject } from "./utils";
88
import { ChartType, IChartDef, AxisType, HorizontalLabelPosition, VerticalLabelPosition, IAxisConfig, IYAxisSeriesConfig, IAxisSeriesConfig } from "@data-forge-plot/chart-def";
9-
import { expandChartDef, expandYSeriesConfigArray } from "./expand-chart-def";
10-
import { ISeriesConfig } from "data-forge/build/lib/series";
11-
import { appendFile } from "fs";
9+
import { expandChartDef } from "./expand-chart-def";
1210
import { applyDefaults } from "./apply-defaults";
1311

1412
const DEFAULT_CHART_PACKAGE = "@data-forge-plot/apex";
@@ -251,13 +249,19 @@ export interface IYAxisConfigAPI extends IAxisConfigAPI<IYAxisConfigAPI> {
251249
*/
252250
export abstract class AbstractPlotAPI implements IPlotAPI {
253251

254-
/**
255-
* The expanded chart def.
256-
*/
252+
//
253+
// The expanded chart def.
254+
//
257255
protected chartDef: IChartDef;
258256

259-
constructor(chartDef: IChartDef) {
257+
//
258+
// Defaults for the chart configuration.
259+
//
260+
private plotDefaults?: IPlotConfig;
261+
262+
constructor(chartDef: IChartDef, plotDefaults?: IPlotConfig) {
260263
this.chartDef = chartDef;
264+
this.plotDefaults = plotDefaults;
261265
}
262266

263267
/**
@@ -414,7 +418,7 @@ export abstract class AbstractPlotAPI implements IPlotAPI {
414418
* The JSON definition of the chart can be used to instantiate the chart in a browser.
415419
*/
416420
serialize(): IChartDef {
417-
return applyDefaults(this.chartDef); // Set missing default values after configuration by the fluent.
421+
return applyDefaults(this.chartDef, this.plotDefaults); // Set missing default values after configuration by the fluent.
418422
}
419423

420424
/**
@@ -439,12 +443,12 @@ export class PlotAPI extends AbstractPlotAPI {
439443
return new PlotAPI(chartDef.data, chartDef.plotConfig, chartDef.axisMap);
440444
}
441445

442-
constructor(data: ISerializedDataFrame, plotConfig: IPlotConfig, axisMap: IAxisMap) {
446+
constructor(data: ISerializedDataFrame, plotConfig: IPlotConfig, axisMap: IAxisMap, plotDefaults?: IPlotConfig) {
443447
if (!isObject(data)) {
444448
throw new Error("Expected 'data' parameter to PlotAPI constructor to be a serialized dataframe.");
445449
}
446450

447-
super(expandChartDef(data, plotConfig, axisMap));
451+
super(expandChartDef(data, plotConfig, axisMap), plotDefaults);
448452
}
449453
}
450454

src/test/apply-defaults.test.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,4 +69,16 @@ describe("apply defaults", () => {
6969
]);
7070
expect(expanded.axisMap.y2).toEqual([]);
7171
});
72+
73+
it("can set plot defaults 1", () => {
74+
const inputChartDef: any = { data: { columnOrder: [], } };
75+
const expanded = applyDefaults(inputChartDef, { chartType: ChartType.Bubble });
76+
expect(expanded.plotConfig!.chartType!).toEqual(ChartType.Bubble);
77+
});
78+
79+
it("can set plot defaults 2", () => {
80+
const inputChartDef: any = { data: { columnOrder: [], }, plotConfig: {} };
81+
const expanded = applyDefaults(inputChartDef, { chartType: ChartType.Bubble });
82+
expect(expanded.plotConfig!.chartType!).toEqual(ChartType.Bubble);
83+
});
7284
});

src/test/dataframe.test.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import "jest";
22
import { DataFrame } from "data-forge";
33
import "../index";
4+
import { ChartType } from "@data-forge-plot/chart-def";
45

56
describe("data-forge-plot - dataframe configuration", () => {
67

@@ -36,6 +37,9 @@ describe("data-forge-plot - dataframe configuration", () => {
3637
chartType: "line",
3738
width: 800,
3839
height: 600,
40+
legend: {
41+
show: true,
42+
},
3943
},
4044
axisMap: {
4145
y: [
@@ -47,4 +51,12 @@ describe("data-forge-plot - dataframe configuration", () => {
4751
},
4852
});
4953
});
54+
55+
it("legend is enabled by default for dataframe", () => {
56+
57+
const df = new DataFrame();
58+
const plotAPI = df.plot();
59+
const serialized = plotAPI.serialize();
60+
expect(serialized.plotConfig.legend!.show).toEqual(true);
61+
});
5062
});

src/test/series.test.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ describe("data-forge-plot - series", () => {
3636
chartType: "line",
3737
width: 800,
3838
height: 600,
39+
legend: {
40+
show: false,
41+
},
3942
},
4043
axisMap: {
4144
y: [
@@ -48,4 +51,11 @@ describe("data-forge-plot - series", () => {
4851
});
4952
});
5053

54+
it("legend is disabled by default for series", () => {
55+
56+
const series = new Series();
57+
const plotAPI = series.plot();
58+
const serialized = plotAPI.serialize();
59+
expect(serialized.plotConfig.legend!.show).toEqual(false);
60+
});
5161
});

0 commit comments

Comments
 (0)