Skip to content

Commit f6bc14d

Browse files
committed
Convered over to basic Apex.
Removed redundant index from plot serialization.
1 parent 2b7a0b8 commit f6bc14d

8 files changed

Lines changed: 276 additions & 372 deletions

File tree

package-lock.json

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

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@
3333
},
3434
"homepage": "https://github.com/data-forge/data-forge-plot#readme",
3535
"dependencies": {
36-
"@data-forge-plot/c3": "^0.0.8",
37-
"@data-forge-plot/chart-def": "^1.0.1",
36+
"@data-forge-plot/apex": "0.0.4",
37+
"@data-forge-plot/chart-def": "^1.0.4",
3838
"@data-forge/serialization": "^1.0.0",
3939
"capture-template": "^1.1.10",
4040
"fs-jetpack": "^1.3.1",
@@ -60,7 +60,7 @@
6060
"nyc": "^11.9.0",
6161
"source-map-support": "^0.5.11",
6262
"ts-node": "^3.3.0",
63-
"tslint": "^5.14.0",
63+
"tslint": "^5.15.0",
6464
"typescript": "2.5.2"
6565
},
6666
"nyc": {

src/chart-def.ts

Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
2+
import { ISerializedDataFrame } from "@data-forge/serialization";
3+
import { HorizontalLabelPosition, VerticalLabelPosition, AxisType, ChartType } from "@data-forge-plot/chart-def";
4+
5+
/**
6+
* Defines the configuration of an axis label.
7+
*/
8+
export interface IAxisLabelConfig {
9+
/**
10+
* The text for the label.
11+
*/
12+
text?: string;
13+
14+
/**
15+
* Position of the label.
16+
*/
17+
position?: HorizontalLabelPosition | VerticalLabelPosition;
18+
}
19+
20+
/**
21+
* Configures an axis of the chart.
22+
*/
23+
export interface IAxisConfig {
24+
/**
25+
* Sets the type of the axis' data.
26+
* Default: AxisType.Indexed ("indexed")
27+
*/
28+
axisType?: AxisType;
29+
30+
/**
31+
* Label for the axis.
32+
*/
33+
label?: string | IAxisLabelConfig;
34+
}
35+
36+
/**
37+
* Configures a Y axis of the chart.
38+
*/
39+
export interface IYAxisConfig extends IAxisConfig {
40+
/**
41+
* The minimum value to render on the axis.
42+
*/
43+
min?: number;
44+
45+
/**
46+
* The maximum value to render on the axis.
47+
*/
48+
max?: number;
49+
}
50+
51+
/**
52+
* Configures the legend of the chart.
53+
*/
54+
export interface ILegendConfig {
55+
56+
/**
57+
* Set to true (default) to show the legend for the chart .
58+
*/
59+
show?: boolean;
60+
}
61+
62+
/**
63+
* Defines the chart.
64+
*/
65+
export interface IPlotConfig {
66+
67+
/**
68+
* The type of chart to render.
69+
* Default to "line".
70+
*/
71+
chartType?: ChartType;
72+
73+
/**
74+
* Width of the plot.
75+
* Default to 800.
76+
*/
77+
width?: number;
78+
79+
/**
80+
* Height of the plot.
81+
* Default to 600.
82+
*/
83+
height?: number;
84+
85+
/**
86+
* Configuration for the x axis.
87+
*/
88+
x?: IAxisConfig;
89+
90+
/**
91+
* Configuration for the y axis.
92+
*/
93+
y?: IYAxisConfig;
94+
95+
/**
96+
* Configuration for the second y axis.
97+
*/
98+
y2?: IYAxisConfig;
99+
100+
/**
101+
* Configures the chart's legend.
102+
*/
103+
legend?: ILegendConfig;
104+
}
105+
106+
/**
107+
* Relates a single axis to data series.
108+
*/
109+
export interface ISingleAxisMap {
110+
111+
/**
112+
* The name of the series to render on the axis.
113+
*/
114+
series: string;
115+
116+
/**
117+
* The label for the series on this axis.
118+
*/
119+
label?: string;
120+
121+
/**
122+
* The format for rendering values of the series.
123+
*/
124+
format?: string;
125+
126+
/**
127+
* The color to render to assign to the series.
128+
*/
129+
color?: string;
130+
}
131+
132+
/**
133+
* Relates a single Y axis to data series.
134+
*/
135+
export interface ISingleYAxisMap extends ISingleAxisMap {
136+
/**
137+
* Configure a separate X axis for the y axis.
138+
*/
139+
x?: string | ISingleAxisMap;
140+
}
141+
142+
/**
143+
* Maps the columns in a dataframe to an axis in the chart.
144+
*/
145+
export interface IAxisMap {
146+
147+
/**
148+
* The x axis for the chart.
149+
*/
150+
x?: string | ISingleAxisMap;
151+
152+
/**
153+
* The y axis for the chart.
154+
*/
155+
y?: string | string[] | ISingleYAxisMap | ISingleYAxisMap[];
156+
157+
/**
158+
* The optional second y axis for the chart.
159+
*/
160+
y2?: string | string[] | ISingleYAxisMap | ISingleYAxisMap[];
161+
}

src/index.ts

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { ISeries, Series } from "data-forge";
22
import { IDataFrame, DataFrame } from "data-forge";
33
import { IPlotAPI, PlotAPI, /*todo: globalChartRenderer,*/ startPlot, endPlot } from "./plot-api";
4-
import { IPlotConfig, IAxisMap } from "@data-forge-plot/chart-def";
4+
import { IPlotConfig, IAxisMap } from "./chart-def";
55

66
//
77
// Augment ISeries and Series with plot function.
@@ -25,11 +25,8 @@ declare module "data-forge/build/lib/series" {
2525
function plotSeries(this: ISeries<any, any>, plotConfig?: IPlotConfig, axisMap?: IAxisMap): IPlotAPI {
2626

2727
const amt = this.count();
28-
const serializedData = this.inflate((value: any) => ({ __value__: value }))
29-
.zip(this.getIndex().head(amt), (row: any, index: any) => {
30-
row.__index__ = index;
31-
return row;
32-
})
28+
const serializedData = this
29+
.inflate((value: any) => ({ __value__: value }))
3330
.serialize();
3431
return new PlotAPI(serializedData, plotConfig || {}, false, axisMap!);
3532
}
@@ -59,12 +56,7 @@ declare module "data-forge/build/lib/dataframe" {
5956

6057
function plotDataFrame(this: IDataFrame<any, any>, plotDef?: IPlotConfig, axisMap?: IAxisMap): IPlotAPI {
6158
const amt = this.count();
62-
const df = this.zip(this.getIndex().head(amt), (row: any, index: any) => {
63-
row.__index__ = index;
64-
return row;
65-
});
66-
67-
const serializedData = df.serialize();
59+
const serializedData = this.serialize();
6860
return new PlotAPI(serializedData, plotDef || {}, true, axisMap);
6961
}
7062

src/plot-api.ts

Lines changed: 14 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,14 @@
1-
import {
2-
ChartType,
3-
IChartDef,
4-
VerticalLabelPosition, HorizontalLabelPosition,
5-
AxisType,
6-
IPlotConfig, IExpandedPlotConfig,
7-
IAxisMap, IAxisConfig, ISingleAxisMap, ISingleYAxisMap
8-
} from "@data-forge-plot/chart-def";
91
import { assert } from "chai";
102
const opn = require("opn");
113
import * as path from "path";
124
import * as Sugar from "sugar";
135
import { ISerializedDataFrame } from "@data-forge/serialization";
146
import { exportTemplate, IExportOptions } from "inflate-template";
157
import { captureImage, ICaptureOptions } from "capture-template";
8+
import { ChartType, IChartDef, AxisType, HorizontalLabelPosition, VerticalLabelPosition, IPlotConfig as IExpandedPlotConfig, IAxisMap as IExpandedAxisMap, ISingleYAxisMap as IExpandedSingleYAxisMap } from "@data-forge-plot/chart-def";
9+
import { ISingleAxisMap, ISingleYAxisMap, IPlotConfig, IAxisMap, IAxisConfig } from "./chart-def";
10+
11+
const DEFAULT_CHART_PACKAGE = "@data-forge-plot/apex";
1612

1713
//
1814
// Reusable chart renderer.
@@ -21,12 +17,12 @@ import { captureImage, ICaptureOptions } from "capture-template";
2117
// TODO :export let globalChartRenderer: IChartRenderer | null = null;
2218

2319
async function findChartTemplatePath(): Promise<string> {
24-
const defaultTemplatePath = require.resolve("@data-forge-plot/c3/build/template/template.json");
20+
const defaultTemplatePath = require.resolve(`${DEFAULT_CHART_PACKAGE}/build/template/template.json`);
2521
const chartTemplatesPath = path.dirname(defaultTemplatePath);
2622
return chartTemplatesPath;
2723
}
2824

29-
export async function startPlot(): Promise<void> {
25+
export async function startPlot(): Promise<void> {
3026
/*TODO:
3127
globalChartRenderer = new ChartRenderer();
3228
@@ -218,27 +214,6 @@ export interface IYAxisConfigAPI extends IAxisConfigAPI<IYAxisConfigAPI> {
218214
max(value: number): IYAxisConfigAPI;
219215
}
220216

221-
//
222-
// Maps the columns in a dataframe to axis in the chart.
223-
//
224-
export interface IInternalAxisMap {
225-
226-
/**
227-
* The x axis for the chart.
228-
*/
229-
x: ISingleAxisMap;
230-
231-
/**
232-
* The y axis for the chart.
233-
*/
234-
y: ISingleYAxisMap[];
235-
236-
/**
237-
* The optional second y axis for the chart.
238-
*/
239-
y2: ISingleYAxisMap[];
240-
}
241-
242217
/**
243218
* Fluent API for configuring the plot.
244219
*/
@@ -257,9 +232,9 @@ export abstract class AbstractPlotAPI implements IPlotAPI {
257232
/**
258233
* Defines how the data is mapped to the axis' in the chart.
259234
*/
260-
protected globalAxisMap: IInternalAxisMap;
235+
protected globalAxisMap: IExpandedAxisMap;
261236

262-
constructor(data: ISerializedDataFrame, plotConfig: IExpandedPlotConfig, globalAxisMap: IInternalAxisMap) {
237+
constructor(data: ISerializedDataFrame, plotConfig: IExpandedPlotConfig, globalAxisMap: IExpandedAxisMap) {
263238
this.data = data;
264239
this.plotConfig = plotConfig;
265240
this.globalAxisMap = globalAxisMap;
@@ -520,7 +495,7 @@ export class PlotAPI extends AbstractPlotAPI {
520495
});
521496
}
522497

523-
const expandedGlobalAxisMap: IInternalAxisMap = {
498+
const expandedGlobalAxisMap: IExpandedAxisMap = {
524499
x: {
525500
series: "__index__",
526501
},
@@ -562,7 +537,7 @@ export class PlotAPI extends AbstractPlotAPI {
562537
}
563538
else {
564539
expandedGlobalAxisMap.y = [
565-
globalAxisMap.y,
540+
globalAxisMap.y as IExpandedSingleYAxisMap,
566541
];
567542
}
568543
}
@@ -592,7 +567,7 @@ export class PlotAPI extends AbstractPlotAPI {
592567
}
593568
else {
594569
expandedGlobalAxisMap.y2 = [
595-
globalAxisMap.y2,
570+
globalAxisMap.y2 as IExpandedSingleYAxisMap,
596571
];
597572
}
598573
}
@@ -634,7 +609,7 @@ class AxisConfigAPI<FluentT, AxisMapT> extends AbstractPlotAPI implements IAxisC
634609
singleAxisMap: AxisMapT,
635610
data: ISerializedDataFrame,
636611
plotConfig: IExpandedPlotConfig,
637-
globalAxisMap: IInternalAxisMap
612+
globalAxisMap: IExpandedAxisMap
638613
) {
639614
super(data, plotConfig, globalAxisMap);
640615

@@ -682,7 +657,7 @@ class XAxisConfigAPI extends AxisConfigAPI<IXAxisConfigAPI, ISingleAxisMap> impl
682657
singleAxisMap: ISingleAxisMap,
683658
data: ISerializedDataFrame,
684659
plotConfig: IExpandedPlotConfig,
685-
globalAxisMap: IInternalAxisMap
660+
globalAxisMap: IExpandedAxisMap
686661
) {
687662
super(axisName, seriesName, axisConfig, singleAxisMap, data, plotConfig, globalAxisMap);
688663
}
@@ -717,7 +692,7 @@ class YAxisConfigAPI extends AxisConfigAPI<IYAxisConfigAPI, ISingleYAxisMap> imp
717692
singleAxisMap: ISingleAxisMap,
718693
data: ISerializedDataFrame,
719694
plotConfig: IExpandedPlotConfig,
720-
globalAxisMap: IInternalAxisMap
695+
globalAxisMap: IExpandedAxisMap
721696
) {
722697
super(axisName, seriesName, axisConfig, singleAxisMap, data, plotConfig, globalAxisMap);
723698
}

src/test/dataframe-fluent.test.ts

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,9 @@ describe("data-forge-plot - dataframe fluent", () => {
1515
data: {
1616
columnOrder: [
1717
"A",
18-
"__index__",
1918
],
2019
columns: {
2120
A: "number",
22-
__index__: "number",
2321
},
2422
index: {
2523
type: "number",
@@ -28,15 +26,12 @@ describe("data-forge-plot - dataframe fluent", () => {
2826
values: [
2927
{
3028
A: 10,
31-
__index__: 1,
3229
},
3330
{
3431
A: 20,
35-
__index__: 2,
3632
},
3733
{
3834
A: 30,
39-
__index__: 3,
4035
},
4136
],
4237
},

0 commit comments

Comments
 (0)