mirror of
https://github.com/home-assistant/frontend.git
synced 2026-12-03 10:09:48 +00:00
Keep the node size along with an animated view move
Take network-graph-view.ts, network-layout-controller.ts and their tests from #56313 (d3cf4fa2fe). Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
1 parent
bf95b2feab
commit
5acba41905
4 files changed
+136
-12
No files matched your search
@@ -55,6 +55,32 @@ export interface SizedViewTransform extends ViewTransform {
|
||||
height: number;
|
||||
}
|
||||
|
||||
/** How large the nodes are drawn at a scale of the view */
|
||||
export interface NodeSizeStart {
|
||||
viewScale: number;
|
||||
/** Times their symbol size */
|
||||
size: number;
|
||||
}
|
||||
|
||||
/**
|
||||
* The size of the nodes while the view scale moves to its end: from where
|
||||
* they started to their size at the end, as far as the view is along its way
|
||||
*/
|
||||
export const nodeSizeAlong = (
|
||||
start: NodeSizeStart,
|
||||
viewScale: number,
|
||||
endScale: number,
|
||||
endSize: number
|
||||
): number => {
|
||||
const range = endScale - start.viewScale;
|
||||
// A view that does not zoom gives no way to follow
|
||||
const progress =
|
||||
Math.abs(range) < 1e-9
|
||||
? 1
|
||||
: Math.min(Math.max((viewScale - start.viewScale) / range, 0), 1);
|
||||
return start.size + (endSize - start.size) * progress;
|
||||
};
|
||||
|
||||
export const SELECTED_NODE_MAX_ZOOM = 2;
|
||||
|
||||
// Keeps neighbors clear of the search field, buttons and legend
|
||||
|
||||
@@ -7,10 +7,16 @@ import type { HaChartBase } from "./ha-chart-base";
|
||||
import type {
|
||||
FollowRoam,
|
||||
GraphView,
|
||||
NodeSizeStart,
|
||||
SizedViewTransform,
|
||||
ViewTransform,
|
||||
} from "./network-graph-view";
|
||||
import { layoutBounds, layoutRoam, roamSource } from "./network-graph-view";
|
||||
import {
|
||||
layoutBounds,
|
||||
layoutRoam,
|
||||
nodeSizeAlong,
|
||||
roamSource,
|
||||
} from "./network-graph-view";
|
||||
|
||||
// The nodes of another layout move for at most this many ms
|
||||
export const LAYOUT_ANIMATION_TIMEOUT = 1000;
|
||||
@@ -41,6 +47,54 @@ const viewTransform = (chart: EChartsType): ViewTransform => {
|
||||
return { scale: x1 - x, x, y };
|
||||
};
|
||||
|
||||
// The nodes and the group that ECharts moves them in with the view
|
||||
const nodeGroups = (chart: EChartsType) => {
|
||||
const series = graphSeries(chart);
|
||||
if (!series?.coordinateSystem) {
|
||||
return undefined;
|
||||
}
|
||||
const data = series.getData();
|
||||
const node = data.getItemGraphicEl(0);
|
||||
const viewGroup = node?.parent?.parent;
|
||||
return viewGroup ? { series, data, node, viewGroup } : undefined;
|
||||
};
|
||||
|
||||
// How large the nodes are drawn now
|
||||
const drawnNodeSize = (chart: EChartsType): NodeSizeStart | undefined => {
|
||||
const groups = nodeGroups(chart);
|
||||
return groups
|
||||
? {
|
||||
viewScale: groups.viewGroup.scaleX,
|
||||
size: groups.node.scaleX * groups.viewGroup.scaleX,
|
||||
}
|
||||
: undefined;
|
||||
};
|
||||
|
||||
// ECharts animates the scale of the view, but sets the nodes to their scale at
|
||||
// its end at once, so they would jump in size and grow back. Called on each
|
||||
// frame, before it is drawn.
|
||||
const scaleNodesToView = (chart: EChartsType, start: NodeSizeStart) => {
|
||||
const groups = nodeGroups(chart);
|
||||
if (!groups) {
|
||||
return;
|
||||
}
|
||||
const { series, data, viewGroup } = groups;
|
||||
const viewScale: number = viewGroup.scaleX;
|
||||
// As ECharts sizes the nodes at the end
|
||||
const ratio = series.get("nodeScaleRatio") || 1;
|
||||
const endSize =
|
||||
(series.coordinateSystem.getRoamTransform()[0] - 1) * ratio + 1;
|
||||
const size = nodeSizeAlong(
|
||||
start,
|
||||
viewScale,
|
||||
viewTransform(chart).scale,
|
||||
endSize
|
||||
);
|
||||
data.eachItemGraphicEl((el: any) => {
|
||||
el?.setSymbolScale(size / viewScale);
|
||||
});
|
||||
};
|
||||
|
||||
export interface NetworkLayoutControllerConfig {
|
||||
chart: () => HaChartBase | undefined;
|
||||
treeLayout: () => boolean;
|
||||
@@ -172,6 +226,8 @@ export class NetworkLayoutController implements ReactiveController {
|
||||
done();
|
||||
return;
|
||||
}
|
||||
// Before the chart draws the change, which the nodes grow or shrink from
|
||||
const drawnSize = baseChart.chart && drawnNodeSize(baseChart.chart);
|
||||
baseChart.updateComplete.then(() => {
|
||||
if (move !== this._move) {
|
||||
return;
|
||||
@@ -180,7 +236,7 @@ export class NetworkLayoutController implements ReactiveController {
|
||||
const moved = chart && this._setRoam(chart, this._layoutSwitch);
|
||||
// The nodes of another layout move also when the view stays
|
||||
if (moved || this._layoutSwitch) {
|
||||
this._afterAnimation(chart, done);
|
||||
this._afterAnimation(chart, done, drawnSize);
|
||||
} else {
|
||||
done();
|
||||
}
|
||||
@@ -236,7 +292,8 @@ export class NetworkLayoutController implements ReactiveController {
|
||||
|
||||
private _afterAnimation(
|
||||
chart: EChartsType | undefined,
|
||||
callback: () => void
|
||||
callback: () => void,
|
||||
drawnSize?: NodeSizeStart
|
||||
) {
|
||||
// Also without animation while the force layout moves the nodes, as
|
||||
// ECharts then shows each step at once
|
||||
@@ -251,9 +308,17 @@ export class NetworkLayoutController implements ReactiveController {
|
||||
// Not every change of the layout ends in a finished event
|
||||
const timeout = window.setTimeout(done, LAYOUT_ANIMATION_TIMEOUT);
|
||||
chart.on("finished", done);
|
||||
const scaleNodes = () => {
|
||||
if (drawnSize) {
|
||||
scaleNodesToView(chart, drawnSize);
|
||||
}
|
||||
};
|
||||
const animation = chart.getZr().animation;
|
||||
animation.on("frame", scaleNodes);
|
||||
this._stopWaiting = () => {
|
||||
clearTimeout(timeout);
|
||||
chart.off("finished", done);
|
||||
animation.off("frame", scaleNodes);
|
||||
this._stopWaiting = undefined;
|
||||
};
|
||||
}
|
||||
|
||||
@@ -8,6 +8,7 @@ import {
|
||||
labelReach,
|
||||
layoutBounds,
|
||||
layoutRoam,
|
||||
nodeSizeAlong,
|
||||
roamSource,
|
||||
selectedNodeRoam,
|
||||
TREE_LABEL_ROOM,
|
||||
@@ -36,6 +37,29 @@ describe("roamSource", () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe("nodeSizeAlong", () => {
|
||||
const start = { viewScale: 1, size: 1 };
|
||||
|
||||
it("goes from the start size to the end size along with the view", () => {
|
||||
expect(nodeSizeAlong(start, 1, 3, 2)).toBe(1);
|
||||
expect(nodeSizeAlong(start, 2, 3, 2)).toBe(1.5);
|
||||
expect(nodeSizeAlong(start, 3, 3, 2)).toBe(2);
|
||||
});
|
||||
|
||||
it("follows a view that zooms out", () => {
|
||||
expect(nodeSizeAlong({ viewScale: 2, size: 1 }, 1.5, 1, 1.4)).toBe(1.2);
|
||||
});
|
||||
|
||||
it("stays between the start and the end when the view overshoots", () => {
|
||||
expect(nodeSizeAlong(start, 4, 3, 2)).toBe(2);
|
||||
expect(nodeSizeAlong(start, 0.5, 3, 2)).toBe(1);
|
||||
});
|
||||
|
||||
it("is the end size when the view does not zoom", () => {
|
||||
expect(nodeSizeAlong(start, 1, 1, 1.3)).toBe(1.3);
|
||||
});
|
||||
});
|
||||
|
||||
describe("labelReach", () => {
|
||||
it("is the distance from the node center to the right of the label", () => {
|
||||
expect(labelReach({ x: 110, width: 90 }, 100)).toBe(100);
|
||||
|
||||
@@ -11,19 +11,23 @@ import {
|
||||
const createChart = (animated: boolean) => {
|
||||
const handlers = new Map<string, Set<() => void>>();
|
||||
const series = { isAnimationEnabled: () => animated };
|
||||
const on = (event: string, handler: () => void) => {
|
||||
if (!handlers.has(event)) {
|
||||
handlers.set(event, new Set());
|
||||
}
|
||||
handlers.get(event)!.add(handler);
|
||||
};
|
||||
const off = (event: string, handler: () => void) => {
|
||||
handlers.get(event)?.delete(handler);
|
||||
};
|
||||
return {
|
||||
handlers,
|
||||
isDisposed: () => false,
|
||||
getModel: () => ({ getSeriesByIndex: () => series }),
|
||||
on: (event: string, handler: () => void) => {
|
||||
if (!handlers.has(event)) {
|
||||
handlers.set(event, new Set());
|
||||
}
|
||||
handlers.get(event)!.add(handler);
|
||||
},
|
||||
off: (event: string, handler: () => void) => {
|
||||
handlers.get(event)?.delete(handler);
|
||||
},
|
||||
// The animation frames share the handlers, with the frame event
|
||||
getZr: () => ({ animation: { on, off } }),
|
||||
on,
|
||||
off,
|
||||
trigger: (event: string) => {
|
||||
for (const handler of [...(handlers.get(event) ?? [])]) {
|
||||
handler();
|
||||
@@ -99,12 +103,16 @@ describe("NetworkLayoutController", () => {
|
||||
await vi.waitFor(() =>
|
||||
expect(chart.handlers.get("finished")?.size).toBe(1)
|
||||
);
|
||||
// The nodes keep their size on each frame of the move
|
||||
expect(chart.handlers.get("frame")?.size).toBe(1);
|
||||
chart.trigger("frame");
|
||||
chart.trigger("finished");
|
||||
expect(controller.settling).toBe(false);
|
||||
expect(controller.moving).toBe(false);
|
||||
expect(host.requestUpdate).toHaveBeenCalledOnce();
|
||||
expect(config.moveEnded).toHaveBeenCalledOnce();
|
||||
expect(chart.handlers.get("finished")?.size).toBe(0);
|
||||
expect(chart.handlers.get("frame")?.size).toBe(0);
|
||||
});
|
||||
|
||||
it("stops waiting for the nodes after a timeout", async () => {
|
||||
@@ -167,6 +175,7 @@ describe("NetworkLayoutController", () => {
|
||||
expect(controller.settling).toBe(false);
|
||||
expect(controller.moving).toBe(false);
|
||||
expect(chart.handlers.get("finished")?.size).toBe(0);
|
||||
expect(chart.handlers.get("frame")?.size).toBe(0);
|
||||
vi.advanceTimersByTime(LAYOUT_ANIMATION_TIMEOUT);
|
||||
expect(config.moveEnded).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
Reference in new issue
Block a user