/
Plot.js
208 lines (183 loc) · 5.46 KB
/
Plot.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
import React, { PropTypes } from 'react';
import {
ComposedChart,
Bar,
Line,
Area,
XAxis,
YAxis,
CartesianGrid,
Tooltip,
Legend,
Brush
} from 'Recharts';
import DataItemList from './DataItemList';
import CustomToolTip from './CustomToolTip';
import styles from './../assets/plot.css';
import viewEnterTransitions from './../assets/viewEnterTransitions.css';
import ReactTransition from 'react-transition-group/CSSTransitionGroup';
const Plot = (props) => {
const {
compiledGraphData, // holds all graph data exported from each PerfComponent
checkIfTwoGraphsActive,
twoGraphToggler,
dataItems,
onDataItemClick,
componentsActiveOnGraphs,
getComponent,
simData
} = props;
const data = [[], []];
const graphRenders = { '0': {}, '1': {} };
let activeGraphs = [];
let metric;
let metricName;
let metricShouldAnimate;
// used for display in graph key
let componentName;
// used in loops to assemble metric data, which then gets passed to the data const defined above.
let currData;
// loop through each element passed in compiledGraphData (sent as prop from App)
compiledGraphData.forEach((item, i) => {
metric = item[0];
metricName = item[2];
componentName = item[1] + ': ' + metricName;
Object.keys(metric.activeGraphs).forEach(graph_code => {
if (metric.activeGraphs[graph_code]) {
currData = data[graph_code] || 0;
metric.data.forEach((value, j) => {
if (currData.length - 1 < j || !currData.length) {
currData.push({});
currData[currData.length - 1].name = j;
}
currData[j][componentName] = value;
data[graph_code] = currData;
});
if (!graphRenders[graph_code][metricName])
graphRenders[graph_code][metricName] = [];
metricShouldAnimate = metric.animationIsActive;
switch (metric.graphDisplay) {
case 'bar':
graphRenders[graph_code][metricName].push(
<Bar
key={i}
dataKey={componentName}
fill={metric.colorTheme}
isAnimationActive={metricShouldAnimate}
/>)
break;
case 'line':
graphRenders[graph_code][metricName].push(
<Line
key={i}
type='linear'
dataKey={componentName}
stroke={metric.colorTheme}
fill={metric.colorTheme}
strokeWidth={1}
activeDot={{ r: 4 }}
dot={{ r: 1 }}
isAnimationActive={metricShouldAnimate}
/>)
break;
case 'area':
graphRenders[graph_code][metricName].push(
<Area
key={i}
type="linear"
dataKey={componentName}
stroke={"'"+metric.colorTheme+"'"}
fill={metric.colorTheme}
strokeWidth={3}
activeDot={{ r: 8 }}
isAnimationActive={metricShouldAnimate}
/>)
break;
}
}
});
});
const getGraphComponentForRender = (num) => {
let arr = [];
Object.keys(graphRenders[num]).forEach(el => {
arr.push(graphRenders[num][el]);
})
return arr;
}
const getGraphParams = (num) => {
return {
code: num,
id: num
? 'comparisonGraphContainer'
: 'graphContainer',
data: data[num],
graphRenders: getGraphComponentForRender(num),
brushComponent: num
? (<Brush height={13} fill='#333940' />)
: [],
graphHeight: checkIfTwoGraphsActive()
? 170
: 335
}
}
let mainGraphParams, comparisonGraphParams;
let graphOutput = [];
let graphPlaceholder = [];
if (compiledGraphData.length) {
let iterator = [];
let graphHeight = 335;
iterator = [getGraphParams(0)];
if (checkIfTwoGraphsActive()) {
iterator.push(getGraphParams(1));
graphHeight=170;
}else{
}
iterator.forEach(graph => {
graphOutput.push((
<ComposedChart
margin={{right:60, bottom:10, top: 30}}
key={graph.code}
width={500}
height={graphHeight}
data={data[graph.code]}
fill={'transparent'}
syncId="anyId"
>
<XAxis dataKey={"name"} label={'Renders'}/>
<YAxis label={"ms"}/>
<Tooltip
cursor={{fill: 'rgba(255,255,255,.05)'}}
/>
{graph.graphRenders}
{graph.brushComponent}
</ComposedChart>
),
)
});
}else{
graphPlaceholder=(
<div id={styles.graphPlaceholder}>
<p id={styles.rpm_instructionsTitle}>react-rpm | component view</p>
<p id={styles.rpm_instructions}>1. Select Metric</p>
<p id={styles.rpm_instructions}>2. Select Component</p>
<p id={styles.rpm_instructions}>3. Select Graph Style</p>
<p id={styles.rpm_instructions}>4. Select Color</p>
<p id={styles.rpm_instructions}>5. Press 'Graph I' to Render To Graph</p>
</div>
)
}
return (
<div>
<ReactTransition
transitionName={viewEnterTransitions}
transitionAppear={true}
transitionAppearTimeout={2000} transitionEnterTimeout={100} transitionLeaveTimeout={500}>
<div id={styles.graphContainer}>
{graphOutput}
{graphPlaceholder}
</div>
</ReactTransition>
</div>
)
}
export default Plot;