Skip to content

Commit 19fc0e7

Browse files
committed
vis: tree display: rework activity animation
closes #1563
1 parent c3a72cd commit 19fc0e7

File tree

1 file changed

+28
-14
lines changed

1 file changed

+28
-14
lines changed

packages/ott-vis-panel/src/components/TreeDisplay.tsx

+28-14
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, { useEffect, useRef, useState } from "react";
1+
import React, { useCallback, useEffect, useRef, useState } from "react";
22
import * as d3 from "d3";
33
import type { Monolith, SystemState } from "ott-vis/types";
44
import { dedupeMonoliths } from "aggregate";
@@ -245,6 +245,16 @@ const TreeDisplay: React.FC<TreeDisplayProps> = ({
245245

246246
const [chartTransform, setChartTransform] = useState("translate(0, 0)");
247247

248+
const getRadius = useCallback((group: string): number => {
249+
if (group === "client") {
250+
return clientNodeRadius;
251+
} else if (group === "balancer") {
252+
return balancerNodeRadius;
253+
} else {
254+
return baseNodeRadius;
255+
}
256+
}, [baseNodeRadius, balancerNodeRadius, clientNodeRadius]);
257+
248258
useEffect(() => {
249259
if (svgRef.current) {
250260
// because d3-hierarchy doesn't support trees with multiple parents, we need to do manual layouts for balancers and monoliths, but we can use the built-in tree layout for monolith down to clients
@@ -536,13 +546,7 @@ const TreeDisplay: React.FC<TreeDisplayProps> = ({
536546
.transition(tr)
537547
.attr("cx", (d: any) => d.x)
538548
.attr("cy", (d: any) => d.y)
539-
.attr("r", d => {
540-
if (d.data.group === "client") {
541-
return clientNodeRadius;
542-
} else {
543-
return baseNodeRadius;
544-
}
545-
});
549+
.attr("r", d => getRadius(d.data.group));
546550

547551
const monolithTexts = monolith
548552
.selectAll(".monolith-text")
@@ -635,6 +639,7 @@ const TreeDisplay: React.FC<TreeDisplayProps> = ({
635639
balancerNodeRadius,
636640
clientNodeRadius,
637641
balancerGroupStyle,
642+
getRadius,
638643
]);
639644

640645
// run this only once after the first render
@@ -650,17 +655,26 @@ const TreeDisplay: React.FC<TreeDisplayProps> = ({
650655
const eventBus = useEventBus();
651656
useEffect(() => {
652657
const sub = eventBus.subscribe(event => {
653-
d3.select(`[data-nodeid="${event.node_id}"]`)
654-
.transition("highlight")
655-
.duration(100)
656-
.attrTween("stroke", () => d3.interpolateRgb("#f00", "#fff"))
657-
.attrTween("stroke-width", () => t => d3.interpolateNumber(4, 1.5)(t).toString());
658+
const node = d3.select(`[data-nodeid="${event.node_id}"]`);
659+
if (node.empty()) {
660+
return;
661+
}
662+
const data = node.data()[0] as d3.HierarchyNode<TreeNode>;
663+
const endRadius = data ? getRadius(data.data.group) : 20;
664+
const radiusCurrent = parseFloat(node.attr("r"));
665+
const newRadius = Math.max(Math.min(radiusCurrent + 5, 40), endRadius);
666+
node.transition("highlight")
667+
.duration(333)
668+
.ease(d3.easeCubicOut)
669+
.attrTween("stroke", () => d3.interpolateRgb("#0f0", "#fff"))
670+
.attrTween("stroke-width", () => t => d3.interpolateNumber(4, 1.5)(t).toString())
671+
.attrTween("r", () => t => d3.interpolateNumber(newRadius, endRadius)(t).toString());
658672
});
659673

660674
return () => {
661675
sub.unsubscribe();
662676
};
663-
}, [eventBus]);
677+
}, [eventBus, getRadius]);
664678

665679
return (
666680
<svg

0 commit comments

Comments
 (0)