-
Notifications
You must be signed in to change notification settings - Fork 1
/
2d-data.js
139 lines (129 loc) · 4.88 KB
/
2d-data.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
import { REFERENCE_TYPE } from '../../shared/utils';
import { cloneDeep } from 'lodash';
import { getYAxis, getXAxis, rountToRange, getXAxisDiscrete } from '../../shared/utils';
import BigNumber from 'bignumber.js';
class Data2D {
apply(globalCtx) {
const name = 'Data2D';
globalCtx.globalData.hooks.initData.tap(name, (dataOptions, source, sourceMeta) => {
this.init(globalCtx, dataOptions, source, sourceMeta);
});
globalCtx.globalData.hooks.resetData.tap(name, (dataOptions, source) => {
this.resetData(dataOptions, source);
});
}
init(globalCtx, dataOptions, source, sourceMeta) {
Object.assign(source, {
data: [],
legend: [],
reference: {},
stack: false,
});
Object.assign(sourceMeta, {
xMinMax: {},
yMinMax: {},
xDataSpan: {},
yDataSpan: {},
});
this.resetData(dataOptions, source);
this.initEffect(globalCtx, source, sourceMeta);
}
resetData(dataOptions, source) {
const s = dataOptions.series;
const reference = dataOptions.reference;
const dataLegend = [];
const ref = s[0].values.map(v => v[0]);
s.forEach(({ name }) => {
dataLegend.push({
name,
disabled: false,
});
});
Object.assign(source, {
// data: cloneDeep(dataSource),
originSeries: dataOptions.series,
legend: cloneDeep(dataLegend),
reference: Object.assign({
ref,
type: REFERENCE_TYPE.continuous,
}, reference),
xAxis: dataOptions.xAxis,
yAxis: dataOptions.yAxis,
stack: dataOptions.stack,
});
}
initEffect(globalCtx, source, sourceMeta) {
globalCtx.effect(() => {
console.log('effect init data')
const {
originSeries,
legend,
xAxis,
yAxis,
stack,
reference,
} = source;
const dataSource = [];
const valueReducer = [];
originSeries.forEach(({ values }, idx) => {
if (stack) {
const lastValue = [];
const isLgDisabled = legend[idx].disabled;
values.forEach((value, idx) => {
if (!valueReducer[idx]) {
valueReducer[idx] = [ value[0], new BigNumber(0) ];
}
lastValue[idx] = [ valueReducer[idx][0], +valueReducer[idx][1] ];
valueReducer[idx][1] = valueReducer[idx][1].plus(isLgDisabled ? 0 : +value[1]);
});
dataSource.push(valueReducer.map((v, i) => [ v[0], +v[1], lastValue[i][1], +values[i][1] ]));
} else {
dataSource.push(values);
}
});
const xSpan = xAxis.span || 5;
const ySpan = yAxis.span || 3;
let xmin = typeof xAxis.min === 'number' ? xAxis.min : Infinity;
let ymin = typeof yAxis.min === 'number' ? yAxis.min : Infinity;
let xmax = typeof xAxis.max === 'number' ? xAxis.max : -Infinity;
let ymax = typeof yAxis.max === 'number' ? yAxis.max : -Infinity;
legend.forEach((l, i) => {
if (!l.disabled) {
const values = dataSource[i];
values.forEach(v => {
const y = v[1];
const x = v[0];
xmin = Math.min(x, xmin);
xmax = Math.max(x, xmax);
ymin = Math.min(y, ymin);
ymax = Math.max(y, ymax);
});
}
});
const ref = dataSource[0].map(v => v[0]);
console.log(ref)
const yrange = rountToRange({ min: ymin, max: ymax }, ySpan);
const yValues = getYAxis(yrange);
const xValues = reference.type === REFERENCE_TYPE.continuous ? getXAxis(xmin, xmax, xSpan) : getXAxisDiscrete(ref, xSpan);
console.log(xValues, xSpan)
Object.assign(source, {
data: cloneDeep(dataSource),
});
Object.assign(sourceMeta, {
yMeta: {
min: yValues[0],
max: yValues[yValues.length - 1],
values: yValues,
formatter: yAxis.format || (d => d),
},
xMeta: {
min: xmin,
max: xmax,
values: xValues,
formatter: xAxis.format || (d => d),
},
});
});
}
}
export default Data2D;