Intermediate.tsx 11.1 KB
Newer Older
1
import * as React from 'react';
2
import { Stack, PrimaryButton, Toggle, IStackTokens } from '@fluentui/react';
Lijiaoa's avatar
Lijiaoa committed
3
4
import { TooltipForIntermediate, TableObj, Intermedia, EventMap } from '@static/interface';
import { reformatRetiariiParameter } from '@static/function';
5
import ReactEcharts from 'echarts-for-react';
6
7
8
9
10
11
import 'echarts/lib/component/tooltip';
import 'echarts/lib/component/title';

const stackTokens: IStackTokens = {
    childrenGap: 20
};
12
13

interface IntermediateState {
14
    detailSource: Array<TableObj>;
15
16
17
18
    interSource: object;
    filterSource: Array<TableObj>;
    eachIntermediateNum: number; // trial's intermediate number count
    isLoadconfirmBtn: boolean;
19
    isFilter?: boolean | undefined;
20
    length: number;
21
    clickCounts: number; // user filter intermediate click confirm btn's counts
Lijiao's avatar
Lijiao committed
22
23
    startMediaY: number;
    endMediaY: number;
24
25
26
27
28
29
30
31
}

interface IntermediateProps {
    source: Array<TableObj>;
}

class Intermediate extends React.Component<IntermediateProps, IntermediateState> {
    static intervalMediate = 1;
32
33
34
    public pointInput!: HTMLInputElement | null;
    public minValInput!: HTMLInputElement | null;
    public maxValInput!: HTMLInputElement | null;
35
36
37
38

    constructor(props: IntermediateProps) {
        super(props);
        this.state = {
39
            detailSource: [],
40
41
42
43
            interSource: {},
            filterSource: [],
            eachIntermediateNum: 1,
            isLoadconfirmBtn: false,
44
45
            isFilter: false,
            length: 100000,
Lijiao's avatar
Lijiao committed
46
47
48
            clickCounts: 0,
            startMediaY: 0,
            endMediaY: 100
49
50
51
        };
    }

Lijiao's avatar
Lijiao committed
52
    drawIntermediate = (source: Array<TableObj>): void => {
53
        if (source.length > 0) {
54
55
56
57
            this.setState({
                length: source.length,
                detailSource: source
            });
Lijiao's avatar
Lijiao committed
58
            const { startMediaY, endMediaY } = this.state;
59
60
61
62
63
            const trialIntermediate: Array<Intermedia> = [];
            Object.keys(source).map(item => {
                const temp = source[item];
                trialIntermediate.push({
                    name: temp.id,
64
                    trialNum: temp.sequenceId,
65
66
67
68
69
70
                    data: temp.description.intermediate,
                    type: 'line',
                    hyperPara: temp.description.parameters
                });
            });
            // find max intermediate number
71
72
73
            trialIntermediate.sort((a, b) => {
                return b.data.length - a.data.length;
            });
74
            const legend: string[] = [];
75
76
            // max length
            const length = trialIntermediate[0].data.length;
Lijiao's avatar
Lijiao committed
77
            const xAxis: number[] = [];
78
79
80
81
82
83
84
85
86
87
88
            Object.keys(trialIntermediate).map(item => {
                const temp = trialIntermediate[item];
                legend.push(temp.name);
            });
            for (let i = 1; i <= length; i++) {
                xAxis.push(i);
            }
            const option = {
                tooltip: {
                    trigger: 'item',
                    enterable: true,
89
                    confine: true,
90
                    formatter: function (data: TooltipForIntermediate): React.ReactNode {
91
                        const trialId = data.seriesName;
92
93
                        let parameters = {};
                        let trialNum = 0;
94
95
                        const temp = trialIntermediate.find(key => key.name === trialId);
                        if (temp !== undefined) {
96
97
                            parameters = temp.hyperPara;
                            trialNum = temp.trialNum;
98
                        }
99
100
101
102
103
                        return `
                            <div class="tooldetailAccuracy">
                                <div>Trial No.: ${trialNum}</div> 
                                <div>Trial ID: ${trialId}</div>
                                <div>Intermediate: ${data.data}</div>
104
105
106
107
108
                                <div>Parameters: <pre>${JSON.stringify(
                                    reformatRetiariiParameter(parameters),
                                    null,
                                    4
                                )}</pre>
109
110
111
                                </div>
                            </div>
                        `;
112
113
114
115
116
117
118
119
120
                    }
                },
                grid: {
                    left: '5%',
                    top: 40,
                    containLabel: true
                },
                xAxis: {
                    type: 'category',
121
                    // name: '# Intermediate',
122
123
124
125
126
                    boundaryGap: false,
                    data: xAxis
                },
                yAxis: {
                    type: 'value',
127
                    name: 'Metric',
128
                    scale: true
129
                },
Lijiao's avatar
Lijiao committed
130
131
132
133
134
                dataZoom: [
                    {
                        id: 'dataZoomY',
                        type: 'inside',
                        yAxisIndex: [0],
135
                        filterMode: 'none',
Lijiao's avatar
Lijiao committed
136
137
138
139
                        start: startMediaY,
                        end: endMediaY
                    }
                ],
140
141
                series: trialIntermediate
            };
142
143
144
            this.setState({
                interSource: option
            });
145
        } else {
146
147
148
149
150
151
152
153
            const nullData = {
                grid: {
                    left: '5%',
                    top: 40,
                    containLabel: true
                },
                xAxis: {
                    type: 'category',
154
                    boundaryGap: false
155
156
157
                },
                yAxis: {
                    type: 'value',
158
                    name: 'Metric'
159
160
                }
            };
161
            this.setState({ interSource: nullData });
162
        }
163
    };
164
165

    // confirm btn function [filter data]
Lijiao's avatar
Lijiao committed
166
    filterLines = (): void => {
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
        const filterSource: Array<TableObj> = [];
        this.setState({ isLoadconfirmBtn: true }, () => {
            const { source } = this.props;
            // get input value
            const pointVal = this.pointInput !== null ? this.pointInput.value : '';
            const minVal = this.minValInput !== null ? this.minValInput.value : '';
            const maxVal = this.maxValInput !== null ? this.maxValInput.value : '';
            // user not input message
            if (pointVal === '' || minVal === '') {
                alert('Please input filter message');
            } else {
                // user not input max value
                const position = JSON.parse(pointVal);
                const min = JSON.parse(minVal);
                if (maxVal === '') {
                    Object.keys(source).map(item => {
                        const temp = source[item];
                        const val = temp.description.intermediate[position - 1];
                        if (val >= min) {
                            filterSource.push(temp);
                        }
                    });
189
                } else {
190
191
192
193
194
195
196
197
                    const max = JSON.parse(maxVal);
                    Object.keys(source).map(item => {
                        const temp = source[item];
                        const val = temp.description.intermediate[position - 1];
                        if (val >= min && val <= max) {
                            filterSource.push(temp);
                        }
                    });
198
                }
199
200
201
202
203
204
                this.setState({ filterSource: filterSource });
                this.drawIntermediate(filterSource);
            }
            const counts = this.state.clickCounts + 1;
            this.setState({ isLoadconfirmBtn: false, clickCounts: counts });
        });
205
    };
206

207
    switchTurn = (ev: React.MouseEvent<HTMLElement>, checked?: boolean): void => {
208
        this.setState({ isFilter: checked });
209
210
211
        if (checked === false) {
            this.drawIntermediate(this.props.source);
        }
212
    };
213

Lijiao's avatar
Lijiao committed
214
    componentDidMount(): void {
215
216
217
218
        const { source } = this.props;
        this.drawIntermediate(source);
    }

219
220
221
    componentDidUpdate(prevProps: IntermediateProps, prevState: any): void {
        if (this.props.source !== prevProps.source || this.state.isFilter !== prevState.isFilter) {
            const { isFilter, filterSource } = this.state;
222
            const { source } = this.props;
223

224
225
226
227
            if (isFilter === true) {
                const pointVal = this.pointInput !== null ? this.pointInput.value : '';
                const minVal = this.minValInput !== null ? this.minValInput.value : '';
                if (pointVal === '' && minVal === '') {
228
                    this.drawIntermediate(source);
229
230
                } else {
                    this.drawIntermediate(filterSource);
231
                }
232
233
            } else {
                this.drawIntermediate(source);
234
235
236
237
            }
        }
    }

Lijiao's avatar
Lijiao committed
238
    render(): React.ReactNode {
239
        const { interSource, isLoadconfirmBtn, isFilter } = this.state;
240
        const IntermediateEvents = { dataZoom: this.intermediateDataZoom };
241

242
243
244
        return (
            <div>
                {/* style in para.scss */}
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
                <Stack horizontal horizontalAlign='end' tokens={stackTokens} className='meline intermediate'>
                    {isFilter ? (
                        <div>
                            <span className='filter-x'># Intermediate result</span>
                            <input
                                // placeholder="point"
                                ref={(input): any => (this.pointInput = input)}
                                className='strange'
                            />
                            <span>Metric range</span>
                            <input
                                // placeholder="range"
                                ref={(input): any => (this.minValInput = input)}
                            />
                            <span className='hyphen'>-</span>
                            <input
                                // placeholder="range"
                                ref={(input): any => (this.maxValInput = input)}
                            />
                            <PrimaryButton text='Confirm' onClick={this.filterLines} disabled={isLoadconfirmBtn} />
                        </div>
                    ) : null}
Lijiao's avatar
Lijiao committed
267
                    {/* filter message */}
268
                    <Stack horizontal className='filter-toggle'>
269
270
271
272
                        <span>Filter</span>
                        <Toggle onChange={this.switchTurn} />
                    </Stack>
                </Stack>
273
                <div className='intermediate-graph graph'>
274
275
                    <ReactEcharts
                        option={interSource}
276
                        style={{ width: '100%', height: 400, margin: '0 auto' }}
277
                        notMerge={true} // update now
Lijiao's avatar
Lijiao committed
278
                        onEvents={IntermediateEvents}
279
                    />
280
                    <div className='xAxis'># Intermediate result</div>
281
                </div>
282
283
284
            </div>
        );
    }
Lijiao's avatar
Lijiao committed
285

Lijiao's avatar
Lijiao committed
286
    private intermediateDataZoom = (e: EventMap): void => {
Lijiao's avatar
Lijiao committed
287
288
        if (e.batch !== undefined) {
            this.setState(() => ({
289
290
                startMediaY: e.batch[0].start !== null ? e.batch[0].start : 0,
                endMediaY: e.batch[0].end !== null ? e.batch[0].end : 100
Lijiao's avatar
Lijiao committed
291
292
            }));
        }
293
    };
294
295
}

v-liguo's avatar
v-liguo committed
296
export default Intermediate;