Skip to content

Commit dbc191e

Browse files
committed
feat(chart): bar chart date options
1 parent 26dff8f commit dbc191e

File tree

2 files changed

+63
-47
lines changed

2 files changed

+63
-47
lines changed

src/core/chart/BarChart.ts

Lines changed: 42 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,16 @@ import { colorPicker } from "../ColorPicker";
99
import { canvasHelper } from "../CanvasHelper";
1010
import { Stage } from "../Stage";
1111
import { BaseChart, BaseChartOptions, KeyGener } from "./BaseChart";
12+
13+
interface BarChartOptions extends BaseChartOptions {
14+
itemCount?: number;
15+
barPadding?: number;
16+
barGap?: number;
17+
barInfoFormat?: KeyGener;
18+
dataLabelSize?: number;
19+
showDateLabel?: boolean;
20+
}
21+
1222
interface BarOptions {
1323
id: string;
1424
data: any;
@@ -20,14 +30,21 @@ interface BarOptions {
2030
alpha: number;
2131
image?: string;
2232
}
23-
24-
interface BarChartOptions extends BaseChartOptions {
25-
itemCount?: number;
26-
barPadding?: number;
27-
barGap?: number;
28-
barInfoFormat?: KeyGener;
29-
}
3033
export class BarChart extends BaseChart {
34+
constructor(options?: BarChartOptions) {
35+
super(options);
36+
if (!options) return;
37+
if (options.itemCount) this.itemCount = options.itemCount;
38+
if (options.barPadding !== undefined) this.barPadding = options.barPadding;
39+
if (options.barGap !== undefined) this.barGap = options.barGap;
40+
if (options.barInfoFormat !== undefined)
41+
this.barInfoFormat = options.barInfoFormat;
42+
if (options.dataLabelSize !== undefined)
43+
this.dataLabelSize = options.dataLabelSize;
44+
if (options.showDateLabel !== undefined)
45+
this.showDateLabel = options.showDateLabel;
46+
}
47+
3148
itemCount = 20;
3249

3350
barPadding = 8;
@@ -36,6 +53,8 @@ export class BarChart extends BaseChart {
3653
lastValue = new Map<string, number>();
3754
labelPlaceholder: number;
3855
valuePlaceholder: number;
56+
dataLabelSize: number = 60;
57+
showDateLabel: boolean = true;
3958

4059
get sampling() {
4160
if (this.stage) {
@@ -55,13 +74,6 @@ export class BarChart extends BaseChart {
5574

5675
historyIndex: Map<any, any>;
5776
ids: string[];
58-
constructor(options?: BarChartOptions) {
59-
super(options);
60-
if (!options) return;
61-
if (options.itemCount) this.itemCount = options.itemCount;
62-
if (options.barPadding !== undefined) this.barPadding = options.barPadding;
63-
if (options.barGap !== undefined) this.barGap = options.barGap;
64-
}
6577
setup(stage: Stage) {
6678
super.setup(stage);
6779
this.ids = [...this.dataScales.keys()];
@@ -167,20 +179,22 @@ export class BarChart extends BaseChart {
167179
}
168180
});
169181

170-
const dateLabel = new Text({
171-
text: d3.timeFormat(this.dateFormat)(this.secToDate(sec)),
172-
font: "Sarasa Mono Slab SC",
173-
fontSize: 45,
174-
fillStyle: "#777",
175-
textAlign: "right",
176-
fontWeight: "bolder",
177-
textBaseline: "bottom",
178-
position: {
179-
x: this.shape.width - this.margin.right,
180-
y: this.shape.height - this.margin.bottom,
181-
},
182-
});
183-
res.children.push(dateLabel);
182+
if (this.showDateLabel) {
183+
const dateLabel = new Text({
184+
text: d3.timeFormat(this.dateFormat)(this.secToDate(sec)),
185+
font: "Sarasa Mono Slab SC",
186+
fontSize: this.dataLabelSize,
187+
fillStyle: "#777",
188+
textAlign: "right",
189+
fontWeight: "bolder",
190+
textBaseline: "bottom",
191+
position: {
192+
x: this.shape.width - this.margin.right,
193+
y: this.shape.height - this.margin.bottom,
194+
},
195+
});
196+
res.children.push(dateLabel);
197+
}
184198
return res;
185199
}
186200
private get barHeight() {

src/core/chart/BaseChart.ts

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,27 @@ export type KeyGener =
3939
| ((id: string, meta: Map<string, any>) => string)
4040
| ((id: string, meta: Map<string, any>, data: Map<string, any>) => string);
4141
export abstract class BaseChart extends Ani {
42+
constructor(options?: BaseChartOptions) {
43+
super();
44+
if (!options) return;
45+
if (options.fadeTime) this.fadeTime = options.fadeTime;
46+
if (options.aniTime) this.aniTime = options.aniTime;
47+
if (options.freezeTime) this.freezeTime = options.freezeTime;
48+
if (options.idField) this.idField = options.idField;
49+
if (options.colorField) this.colorField = options.colorField;
50+
if (options.dateField) this.dateField = options.dateField;
51+
if (options.valueKeys) this.valueKeys = options.valueKeys;
52+
if (options.valueField) this.valueField = options.valueField;
53+
if (options.imageField) this.imageField = options.imageField;
54+
if (options.margin !== undefined) this.margin = options.margin;
55+
if (options.shape) this.shape = options.shape;
56+
if (options.dateFormat) this.dateFormat = options.dateFormat;
57+
if (options.labelFormat) this.labelFormat = options.labelFormat;
58+
if (options.valueFormat) this.valueFormat = options.valueFormat;
59+
if (options.data) this.dataName = options.data;
60+
if (options.meta) this.metaName = options.meta;
61+
if (options.position) this.position = options.position;
62+
}
4263
dataScales: Map<string, any>;
4364
idField = "id";
4465
colorField: string | KeyGener = "id";
@@ -67,25 +88,6 @@ export abstract class BaseChart extends Ani {
6788
xTickFormat = d3.format(",d");
6889
yTickFormat = d3.format(",d");
6990

70-
constructor(options?: BaseChartOptions) {
71-
super();
72-
if (!options) return;
73-
if (options.fadeTime) this.fadeTime = options.fadeTime;
74-
if (options.aniTime) this.aniTime = options.aniTime;
75-
if (options.freezeTime) this.freezeTime = options.freezeTime;
76-
if (options.idField) this.idField = options.idField;
77-
if (options.colorField) this.colorField = options.colorField;
78-
if (options.dateField) this.dateField = options.dateField;
79-
if (options.valueKeys) this.valueKeys = options.valueKeys;
80-
if (options.valueField) this.valueField = options.valueField;
81-
if (options.imageField) this.imageField = options.imageField;
82-
if (options.margin !== undefined) this.margin = options.margin;
83-
if (options.shape) this.shape = options.shape;
84-
if (options.dateFormat) this.dateFormat = options.dateFormat;
85-
if (options.data) this.dataName = options.data;
86-
if (options.meta) this.metaName = options.meta;
87-
if (options.position) this.position = options.position;
88-
}
8991
setup(stage: Stage) {
9092
super.setup(stage);
9193
this.setData();

0 commit comments

Comments
 (0)