Skip to content

Commit afe05e7

Browse files
committed
feat: miniChart progress支持添加浮标
1 parent b10719c commit afe05e7

File tree

3 files changed

+81
-6
lines changed

3 files changed

+81
-6
lines changed

__tests__/unit/tinyPlot/progress-spec.ts

+17-5
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,30 @@ it.only('initialize & destory', () => {
1313
width: 200,
1414
height: 100,
1515
percent: 0.1,
16-
animation: {
16+
/*animation: {
1717
update: {
1818
duration: 1000,
1919
},
20-
},
20+
},*/
21+
marker: [
22+
{
23+
value: 0.3,
24+
/*style:{
25+
stroke:'red',
26+
lineWidth: 2
27+
}*/
28+
},
29+
{
30+
value: 0.5,
31+
},
32+
],
2133
});
2234
progress.render();
23-
window.setTimeout(() => {
35+
36+
/*window.setTimeout(() => {
2437
progress.update(0.6, [{ fill: 'red' }, { fill: 'green' }]);
2538
}, 2000);
26-
27-
/* expect(progress).toBeInstanceOf(Progress);
39+
expect(progress).toBeInstanceOf(Progress);
2840
const canvas = progress.plot.get('canvas');
2941
expect(canvas.get('width')).toBe(200);
3042
expect(canvas.get('height')).toBe(100);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import * as _ from '@antv/util';
2+
import { View, Coord } from '@antv/g2';
3+
import { Group, Shape, Canvas } from '@antv/g';
4+
5+
export interface IMarker {
6+
value: number;
7+
style?: any;
8+
}
9+
10+
export default class Marker {
11+
protected canvas: Canvas;
12+
protected view: View;
13+
protected coord: Coord;
14+
protected container: Group;
15+
protected shape: Shape;
16+
protected progressSize: number;
17+
18+
constructor(cfg) {
19+
_.assign(this, cfg);
20+
this.init();
21+
}
22+
23+
public clear() {}
24+
25+
public destory() {}
26+
27+
protected init() {
28+
this.coord = this.view.get('coord');
29+
this.container = this.view.get('container');
30+
const x = this.coord.convert({ x: 0, y: this.value }).x; // progress坐标系是转置坐标系
31+
const y0 = this.coord.center.y - this.progressSize / 2 - 2;
32+
const y1 = this.coord.center.y + this.progressSize / 2 + 2;
33+
const style = _.deepMix({}, { stroke: 'grey', lineWidth: 1 }, this.style);
34+
this.shape = this.container.addShape('path', {
35+
attrs: {
36+
path: [
37+
['M', x, y0],
38+
['L', x, y1],
39+
],
40+
...style,
41+
},
42+
name: 'progress-marker',
43+
});
44+
this.canvas.draw();
45+
}
46+
}

src/tiny-plots/progress/layer.ts

+18-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { registerPlotType } from '../../base/global';
33
import { LayerConfig } from '../../base/layer';
44
import { getGeom } from '../../geoms/factory';
55
import TinyLayer, { TinyViewConfig } from '../tiny-layer';
6+
import Marker from './component/marker';
67
import * as EventParser from './event';
78

89
export interface ProgressViewConfig extends TinyViewConfig {
@@ -31,7 +32,6 @@ export default class ProgressLayer<T extends ProgressLayerConfig = ProgressLayer
3132

3233
public type: string = 'progress';
3334
public processProps() {
34-
this.getSize();
3535
let props = this.options;
3636
props.data = this.processData();
3737
const cfg = {
@@ -60,6 +60,23 @@ export default class ProgressLayer<T extends ProgressLayerConfig = ProgressLayer
6060
}
6161
}
6262

63+
public afterRender() {
64+
if (this.options.marker) {
65+
_.each(this.options.marker, (cfg) => {
66+
const marker = new Marker(
67+
_.mix(
68+
{
69+
canvas: this.canvas,
70+
view: this.view,
71+
progressSize: this.options.barSize,
72+
},
73+
cfg
74+
)
75+
);
76+
});
77+
}
78+
}
79+
6380
protected geometryParser(dim: string, type: string): string {
6481
if (dim === 'g2') {
6582
return G2_GEOM_MAP[type];

0 commit comments

Comments
 (0)