Skip to content

Commit 63f7a25

Browse files
(updated, validated) replaced PerfView component with visx. renders successfully
1 parent 8f20880 commit 63f7a25

File tree

2 files changed

+35
-32
lines changed

2 files changed

+35
-32
lines changed

src/app/components/PerfView.tsx

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,22 @@ const PerfView = (props: PerfViewProps) => {
3939
const svgRef = useRef(null);
4040

4141
console.log(snapshots)
42+
43+
// const performances = []
44+
// snapshot = {
45+
// component1: 10,
46+
// component2: 24,
47+
// component3: 35,
48+
// others: 32
49+
// }
50+
51+
const performances = snapshots.reduce((acc,cur)=>{
52+
const snapshot = {};
53+
while (snapshot.keys.lenght
54+
}, []
55+
// iterate snapshots array
56+
// if i.children exists
57+
//
4258

4359
let indexToDisplay: number | null = null;
4460
if (viewIndex < 0) indexToDisplay = snapshots.length - 1;

src/app/components/PerformanceVisx.tsx

Lines changed: 19 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,17 @@ import { SeriesPoint } from "@visx/shape/lib/types";
44
import { Group } from "@visx/group";
55
import { Grid } from "@visx/grid";
66
import { AxisBottom } from "@visx/axis";
7-
// import cityTemperature from "./cityTemperature";
8-
import snapshots from "./snapshots";
97
import { scaleBand, scaleLinear, scaleOrdinal } from "@visx/scale";
10-
import { timeParse, timeFormat } from "d3-time-format";
118
import { useTooltip, useTooltipInPortal, defaultStyles } from "@visx/tooltip";
129
import { LegendOrdinal } from "@visx/legend";
10+
import snapshots from "./snapshots";
11+
1312

1413
/* TYPESCRIPT */
1514
type CityName = "New York" | "San Francisco" | "Austin";
16-
15+
type snapshot = any;
1716
type TooltipData = {
18-
bar: SeriesPoint<snapshots>;
17+
bar: SeriesPoint<snapshot>;
1918
key: CityName;
2019
index: number;
2120
height: number;
@@ -48,12 +47,9 @@ const tooltipStyles = {
4847

4948
/* DATA PREP */
5049
const data = [...snapshots];
51-
console.log('data', data)
5250

5351
// array of all object keys
5452
const keys = Object.keys(data[0]).filter((d) => d !== "snapshot") as CityName[];
55-
console.log('keys', keys)
56-
5753

5854
// ARRAY OF TOTAL VALUES PER SNAPSHOT
5955
const temperatureTotals = data.reduce((allTotals, currentDate) => {
@@ -65,18 +61,13 @@ const temperatureTotals = data.reduce((allTotals, currentDate) => {
6561
return allTotals;
6662
}, [] as number[]);
6763

68-
// console.log(temperatureTotals)
69-
70-
const parseDate = timeParse("%Y-%m-%d");
71-
const format = timeFormat("%b %d");
72-
const formatDate = (date: string) => format(parseDate(date) as Date);
7364

7465
/* ACCESSORS */
75-
const getDate = (d: CityTemperature) => d.snapshot;
66+
const getSnapshot = (d: snapshot) => d.snapshot;
7667

7768
/* SCALE */
7869
const dateScale = scaleBand<string>({
79-
domain: data.map(getDate),
70+
domain: data.map(getSnapshot),
8071
padding: 0.2
8172
});
8273
const temperatureScale = scaleLinear<number>({
@@ -88,7 +79,6 @@ const colorScale = scaleOrdinal<CityName, string>({
8879
range: [purple1, purple2, purple3, purple4]
8980
});
9081

91-
9282
let tooltipTimeout: number;
9383

9484
/* EXPORT COMPONENT */
@@ -112,16 +102,15 @@ export default function PerformanceVisx({
112102
if (width < 10) return null;
113103
// bounds
114104

115-
// width, height
105+
// width, height
116106
const xMax = width;
117107
const yMax = height - margin.top - 100;
118108

119109
dateScale.rangeRound([0, xMax]);
120110
temperatureScale.range([yMax, 0]);
121111

122112
return width < 10 ? null : (
123-
// relative position is needed for correct tooltip positioning
124-
113+
// relative position is needed for correct tooltip positioning
125114

126115
<div style={{ position: "relative" }}>
127116
<svg ref={containerRef} width={width} height={height}>
@@ -145,17 +134,15 @@ export default function PerformanceVisx({
145134
xOffset={dateScale.bandwidth() / 2}
146135
/>
147136
<Group top={margin.top}>
148-
<BarStack <CityTemperature, CityName>
137+
<BarStack <snapshot, CityName>
149138
data={data}
150139
keys={keys}
151-
x={getDate}
140+
x={getSnapshot}
152141
xScale={dateScale}
153142
yScale={temperatureScale}
154143
color={colorScale}
155144
>
156-
{(barStacks) =>
157-
barStacks.map((barStack) =>
158-
barStack.bars.map((bar) => (
145+
{(barStacks) => barStacks.map(barStack => barStack.bars.map((bar => (
159146
<rect
160147
key={`bar-stack-${barStack.index}-${bar.index}`}
161148
x={bar.x}
@@ -171,18 +158,18 @@ export default function PerformanceVisx({
171158
hideTooltip();
172159
}, 300);
173160
}}
174-
onMouseMove={(event) => {
161+
onMouseMove={event => {
175162
if (tooltipTimeout) clearTimeout(tooltipTimeout);
176163
const top = event.clientY - margin.top - bar.height;
177164
const left = bar.x + bar.width / 2;
178165
showTooltip({
179166
tooltipData: bar,
180167
tooltipTop: top,
181-
tooltipLeft: left
168+
tooltipLeft: left,
182169
});
183170
}}
184171
/>
185-
))
172+
)),
186173
)
187174
}
188175
</BarStack>
@@ -196,7 +183,7 @@ export default function PerformanceVisx({
196183
tickLabelProps={() => ({
197184
fill: purple1,
198185
fontSize: 11,
199-
textAnchor: "middle"
186+
textAnchor: 'middle',
200187
})}
201188
/>
202189
</svg>
@@ -211,14 +198,12 @@ export default function PerformanceVisx({
211198
fontSize: "14px"
212199
}}
213200
>
214-
215201
<LegendOrdinal
216202
scale={colorScale}
217203
direction="row"
218204
labelMargin="0 15px 0 0"
219205
/>
220206
</div> */}
221-
222207
{/* FOR HOVER OVER DISPLAY */}
223208
{tooltipOpen && tooltipData && (
224209
<TooltipInPortal
@@ -230,9 +215,11 @@ export default function PerformanceVisx({
230215
<div style={{ color: colorScale(tooltipData.key) }}>
231216
<strong>{tooltipData.key}</strong>
232217
</div>
233-
<div>{tooltipData.bar.data[tooltipData.key]}</div>
234218
<div>
235-
<small>{formatDate(getDate(tooltipData.bar.data))}</small>
219+
{tooltipData.bar.data[tooltipData.key]}
220+
</div>
221+
<div>
222+
<small>{getSnapshot(tooltipData.bar.data)}</small>
236223
</div>
237224
</TooltipInPortal>
238225
)}

0 commit comments

Comments
 (0)