Skip to content

Commit e3da1f6

Browse files
Added option objectLimit (#45)
* Added objects limit * Fixed issues * Fixed consolidating array * fixed originalIndex
1 parent d7aa4d2 commit e3da1f6

File tree

2 files changed

+137
-40
lines changed

2 files changed

+137
-40
lines changed
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import { InteractiveGraphics } from "site/components/InteractiveGraphics/InteractiveGraphics"
2+
3+
export default () => {
4+
return (
5+
<InteractiveGraphics
6+
objectLimit={3}
7+
graphics={{
8+
rects: [
9+
{
10+
center: { x: 200, y: 100 },
11+
width: 50,
12+
height: 100,
13+
layer: "layer1",
14+
step: 0,
15+
},
16+
{
17+
center: { x: 250, y: 100 },
18+
width: 50,
19+
height: 100,
20+
layer: "layer1",
21+
step: 1,
22+
},
23+
{
24+
center: { x: 300, y: 100 },
25+
width: 50,
26+
height: 100,
27+
layer: "layer1",
28+
step: 2,
29+
},
30+
],
31+
points: [
32+
{
33+
x: 0,
34+
y: 0,
35+
layer: "layer1",
36+
},
37+
{
38+
x: 50,
39+
y: 0,
40+
layer: "layer2",
41+
},
42+
{
43+
x: 100,
44+
y: 0,
45+
layer: "layer3",
46+
},
47+
],
48+
circles: [
49+
{
50+
center: { x: 400, y: 100 },
51+
radius: 25,
52+
fill: "blue",
53+
stroke: "black",
54+
layer: "layer1",
55+
step: 0,
56+
label: "Circle 1",
57+
},
58+
],
59+
}}
60+
/>
61+
)
62+
}

site/components/InteractiveGraphics/InteractiveGraphics.tsx

Lines changed: 75 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,11 @@ export type GraphicsObjectClickEvent = {
2929
export const InteractiveGraphics = ({
3030
graphics,
3131
onObjectClicked,
32+
objectLimit,
3233
}: {
3334
graphics: GraphicsObject
3435
onObjectClicked?: (event: GraphicsObjectClickEvent) => void
36+
objectLimit?: number
3537
}) => {
3638
const [activeLayers, setActiveLayers] = useState<string[] | null>(null)
3739
const [activeStep, setActiveStep] = useState<number | null>(null)
@@ -144,6 +146,41 @@ export const InteractiveGraphics = ({
144146
size,
145147
)
146148

149+
const filterAndLimit = <T,>(
150+
objects: T[] | undefined,
151+
filterFn: (obj: T) => boolean,
152+
): (T & { originalIndex: number })[] => {
153+
if (!objects) return []
154+
const filtered = objects
155+
.map((obj, index) => ({ ...obj, originalIndex: index }))
156+
.filter(filterFn)
157+
return objectLimit ? filtered.slice(-objectLimit) : filtered
158+
}
159+
160+
const filteredLines = useMemo(
161+
() => filterAndLimit(graphics.lines, filterLines),
162+
[graphics.lines, filterLines, objectLimit],
163+
)
164+
const filteredRects = useMemo(
165+
() => filterAndLimit(graphics.rects, filterRects),
166+
[graphics.rects, filterRects, objectLimit],
167+
)
168+
const filteredPoints = useMemo(
169+
() => filterAndLimit(graphics.points, filterPoints),
170+
[graphics.points, filterPoints, objectLimit],
171+
)
172+
const filteredCircles = useMemo(
173+
() => filterAndLimit(graphics.circles, filterCircles),
174+
[graphics.circles, filterCircles, objectLimit],
175+
)
176+
177+
const totalFilteredObjects =
178+
filteredLines.length +
179+
filteredRects.length +
180+
filteredPoints.length +
181+
filteredCircles.length
182+
const isLimitReached = objectLimit && totalFilteredObjects > objectLimit
183+
147184
return (
148185
<div>
149186
{showToolbar && (
@@ -193,6 +230,12 @@ export const InteractiveGraphics = ({
193230
/>
194231
Filter by step
195232
</label>
233+
{isLimitReached && (
234+
<span style={{ color: "red", fontSize: "12px" }}>
235+
Display limited to {objectLimit} objects. Received:{" "}
236+
{totalFilteredObjects}.
237+
</span>
238+
)}
196239
</div>
197240
)}
198241
</div>
@@ -207,46 +250,38 @@ export const InteractiveGraphics = ({
207250
}}
208251
>
209252
<DimensionOverlay transform={realToScreen}>
210-
{graphics.lines?.map((l, originalIndex) =>
211-
filterLines(l) ? (
212-
<Line
213-
key={originalIndex}
214-
line={l}
215-
index={originalIndex}
216-
interactiveState={interactiveState}
217-
/>
218-
) : null,
219-
)}
220-
{graphics.rects?.map((r, originalIndex) =>
221-
filterRects(r) ? (
222-
<Rect
223-
key={originalIndex}
224-
rect={r}
225-
index={originalIndex}
226-
interactiveState={interactiveState}
227-
/>
228-
) : null,
229-
)}
230-
{graphics.points?.map((p, originalIndex) =>
231-
filterPoints(p) ? (
232-
<Point
233-
key={originalIndex}
234-
point={p}
235-
index={originalIndex}
236-
interactiveState={interactiveState}
237-
/>
238-
) : null,
239-
)}
240-
{graphics.circles?.map((c, originalIndex) =>
241-
filterCircles(c) ? (
242-
<Circle
243-
key={originalIndex}
244-
circle={c}
245-
index={originalIndex}
246-
interactiveState={interactiveState}
247-
/>
248-
) : null,
249-
)}
253+
{filteredLines.map((line) => (
254+
<Line
255+
key={line.originalIndex}
256+
line={line}
257+
index={line.originalIndex}
258+
interactiveState={interactiveState}
259+
/>
260+
))}
261+
{filteredRects.map((rect) => (
262+
<Rect
263+
key={rect.originalIndex}
264+
rect={rect}
265+
index={rect.originalIndex}
266+
interactiveState={interactiveState}
267+
/>
268+
))}
269+
{filteredPoints.map((point) => (
270+
<Point
271+
key={point.originalIndex}
272+
point={point}
273+
index={point.originalIndex}
274+
interactiveState={interactiveState}
275+
/>
276+
))}
277+
{filteredCircles.map((circle) => (
278+
<Circle
279+
key={circle.originalIndex}
280+
circle={circle}
281+
index={circle.originalIndex}
282+
interactiveState={interactiveState}
283+
/>
284+
))}
250285
<SuperGrid
251286
stringifyCoord={(x, y) => `${x.toFixed(2)}, ${y.toFixed(2)}`}
252287
width={size.width}

0 commit comments

Comments
 (0)