-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBar.js
82 lines (77 loc) · 2.15 KB
/
Bar.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
/**
* @file 蜡烛图底层实现
* @author liuliang<[email protected]>
*/
import linear from './Linear';
import rectangle from './Rectangle';
import {id, enhance} from './Ops';
export default function ({
data,
accessor = id,
width,
height,
min = 0,
max = 0,
gutter = 10,
offset = [0, 0],
barWidth,
compute}) {
let groups = [];
let minUnset = !min;
let maxUnset = !max;
let [offX] = offset;
let candleWidth = 0.5;
for (let [i, d] of data.entries()) {
for (let [j, el] of d.entries()) {
if (groups[j] == null) {
groups[j] = [];
}
let val = accessor(el);
if (minUnset && (val < min)) {
min = val;
}
if (maxUnset && (val > max)) {
max = val;
}
groups[j][i] = val;
}
}
let n = data[0].length;
let scale = linear([min, max], [height, 0]);
barWidth = barWidth || (width - (n - 1) * gutter) / n;
let curves = [];
for (let [i, d] of data.entries()) {
// let curve = {lines: null, index: 0, group: i, item: d, centroid: 0};
let shift = (barWidth + gutter) * i + offX;
let xCenterPoint = 0;
let reactangles = [];
let left = 0;
let right = 0;
let top = 0;
let bottom = 0;
for (let j = 0, length = d.length, half = length / 2; j < length; j += 2) {
left = j < half ? shift : xCenterPoint;
right = j < half ? shift + barWidth : xCenterPoint + candleWidth;
top = scale(groups[j][i]);
bottom = scale(groups[j + 1][i]);
reactangles.push(
rectangle({
left,
right,
top,
bottom
})
);
xCenterPoint = (shift * 2 + barWidth) / 2;
}
let curve = {
lines: reactangles.reverse(),
index: 0,
group: i,
item: d,
centroid: xCenterPoint
};
curves.push(enhance(compute, curve));
}
return {curves, scale};
}