Compare commits

...
1 Commits
Author SHA1 Message Date
Petar Petrov 9f9c693d0c Add reset zoom button to network graphs 2026-10-01 14:21:52 +03:00
+45 -2
View File
@@ -166,6 +166,9 @@ export class HaChartBase extends MobileAwareMixin(LitElement) {
@state() private _isZoomed = false;
// Separate from _isZoomed, whose touch handling reruns graph layouts
@state() private _isGraphRoamed = false;
@state() private _zoomRatio = 1;
@state() private _minutesDifference = 24 * 60;
@@ -568,7 +571,7 @@ export class HaChartBase extends MobileAwareMixin(LitElement) {
class="chart-controls ${classMap({ small: this.smallControls })}"
>
${
this._isZoomed && !this.hideResetButton
(this._isZoomed || this._isGraphRoamed) && !this.hideResetButton
? html`<ha-icon-button
class="zoom-reset"
.path=${mdiRestart}
@@ -838,6 +841,7 @@ export class HaChartBase extends MobileAwareMixin(LitElement) {
this._zoomRatio = 1;
fireEvent(this, "chart-sankeyroam", { zoom: 1 });
}
this._isGraphRoamed = false;
this.chart.on("datazoom", (e: any) => {
this._handleDataZoomEvent(e);
});
@@ -872,6 +876,13 @@ export class HaChartBase extends MobileAwareMixin(LitElement) {
this.chart!.dispatchAction({ type: "downplay" });
});
this.chart.on("graphroam", () => {
this._isGraphRoamed = this._getGraphRoams().some(
({ zoom, x, y }) =>
Math.abs(zoom - 1) > 1e-6 || Math.abs(x) > 0.5 || Math.abs(y) > 0.5
);
});
if (!this.options?.dataZoom) {
this.chart.getZr().on("dblclick", this._handleClickZoom);
}
@@ -1469,7 +1480,10 @@ export class HaChartBase extends MobileAwareMixin(LitElement) {
}
private _handleZoomReset() {
this.chart?.dispatchAction({ type: "dataZoom", start: 0, end: 100 });
// A dataZoom action reruns the layout of every series, graphs included
if (this._isZoomed) {
this.chart?.dispatchAction({ type: "dataZoom", start: 0, end: 100 });
}
// Reset sankey roam zoom
const option = this.chart?.getOption();
const sankeySeries = (option?.series as any[])?.filter(
@@ -1486,6 +1500,35 @@ export class HaChartBase extends MobileAwareMixin(LitElement) {
this._isZoomed = false;
fireEvent(this, "chart-sankeyroam", { zoom: 1 });
}
this._getGraphRoams().forEach(({ series, zoom, x, y }) => {
this.chart!.dispatchAction({
type: "graphRoam",
seriesId: series.id,
dx: -x,
dy: -y,
zoom: 1 / zoom,
originX: 0,
originY: 0,
});
// The action stores an absolute center, which a later re-fit would keep
series.option.center = null;
series.option.zoom = 1;
});
}
private _getGraphRoams(): {
series: any;
zoom: number;
x: number;
y: number;
}[] {
const graphSeries: any[] =
// @ts-ignore private method but no other way to get the roam transform
this.chart?.getModel().getSeriesByType("graph") ?? [];
return graphSeries.flatMap((series) => {
const roam = series.coordinateSystem?.getRoamTransform?.();
return roam ? [{ series, zoom: roam[0], x: roam[4], y: roam[5] }] : [];
});
}
private _updateSankeyRoam() {