Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion src/components/figures/chart/gauge/gauge_chart_component.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Component, useEffect, useRef } from "@odoo/owl";
import { Component, onMounted, onWillUnmount, useEffect, useRef } from "@odoo/owl";
import { drawGaugeChart } from "../../../../helpers/figures/charts/gauge_chart_rendering";
import { deepEquals } from "../../../../helpers/misc";
import { EASING_FN } from "../../../../registries/cell_animation_registry";
Expand Down Expand Up @@ -67,6 +67,15 @@ export class GaugeChartComponent extends Component<Props, SpreadsheetChildEnv> {
return [rect.width, rect.height, this.runtime, this.canvas.el, window.devicePixelRatio];
}
);
const resizeObserver = new ResizeObserver(() => {
if (animation) {
animation.stop();
animation = null;
}
drawGaugeChart(this.canvasEl, this.runtime, this.env.model.getters.getViewportZoomLevel());
});
onMounted(() => resizeObserver.observe(this.canvas.el as HTMLCanvasElement));
onWillUnmount(() => resizeObserver.disconnect());
}

drawGaugeWithAnimation() {
Expand Down
5 changes: 4 additions & 1 deletion src/components/figures/chart/scorecard/chart_scorecard.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Component, useEffect, useRef } from "@odoo/owl";
import { Component, onMounted, onWillUnmount, useEffect, useRef } from "@odoo/owl";
import { drawScoreChart } from "../../../../helpers/figures/charts/scorecard_chart";
import { getScorecardConfiguration } from "../../../../helpers/figures/charts/scorecard_chart_config_builder";
import { ScorecardChartRuntime } from "../../../../types/chart/scorecard_chart";
Expand Down Expand Up @@ -34,6 +34,9 @@ export class ScorecardChart extends Component<Props, SpreadsheetChildEnv> {
const rect = canvas.getBoundingClientRect();
return [rect.width, rect.height, this.runtime, this.canvas.el, window.devicePixelRatio];
});
const resizeObserver = new ResizeObserver(() => this.createChart());
onMounted(() => resizeObserver.observe(this.canvas.el as HTMLCanvasElement));
onWillUnmount(() => resizeObserver.disconnect());
}

private createChart() {
Expand Down
16 changes: 16 additions & 0 deletions tests/figures/chart/gauge/gauge_rendering.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -293,3 +293,19 @@ describe("Gauge chart component animation", () => {
readonlyAllowedCommands.delete("UPDATE_CELL");
});
});

test("Gauge is re-render on element size change", async () => {
const model = new Model();
createGaugeChart(model, {});
const { fixture } = await mountSpreadsheet({ model });

const canvas = fixture.querySelector<HTMLCanvasElement>("canvas.o-gauge-chart")!;
const ctx = canvas.getContext("2d")!;

const spy = jest.spyOn(ctx, "scale"); // ctx.scale is the first thing the gauge rendering process calls
window.resizers.resize();
expect(spy).toHaveBeenCalledTimes(1);

window.resizers.resize();
expect(spy).toHaveBeenCalledTimes(2);
});
22 changes: 21 additions & 1 deletion tests/figures/chart/scorecard/scorecard_chart_component.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,11 @@ import {
} from "../../../test_helpers/commands_helpers";
import { FR_LOCALE } from "../../../test_helpers/constants";
import { getCellContent } from "../../../test_helpers/getters_helpers";
import { mountComponentWithPortalTarget, nextTick } from "../../../test_helpers/helpers";
import {
mountComponentWithPortalTarget,
mountSpreadsheet,
nextTick,
} from "../../../test_helpers/helpers";

let model: Model;
let chartId: string;
Expand Down Expand Up @@ -767,3 +771,19 @@ describe("Scorecard charts rendering", () => {
expect(scorecardChartStyle.baseline.bold).not.toEqual(true);
});
});

test("Scorecard is re-render on element size change", async () => {
const model = new Model();
createScorecardChart(model, {});
const { fixture } = await mountSpreadsheet({ model });

const canvas = fixture.querySelector<HTMLCanvasElement>("canvas.o-scorecard")!;
const ctx = canvas.getContext("2d")!;

const spy = jest.spyOn(ctx, "scale"); // ctx.scale is the first thing the scorecard rendering process calls
window.resizers.resize();
expect(spy).toHaveBeenCalledTimes(1);

window.resizers.resize();
expect(spy).toHaveBeenCalledTimes(2);
});
6 changes: 5 additions & 1 deletion tests/setup/canvas.mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,12 @@ export class MockCanvasRenderingContext2D {
}

const patch = {
_mockContext: null as MockCanvasRenderingContext2D | null,
getContext: function () {
return new MockCanvasRenderingContext2D() as any as CanvasRenderingContext2D;
if (!this._mockContext) {
this._mockContext = new MockCanvasRenderingContext2D();
}
return this._mockContext;
},
toDataURL: function () {
return "data:image/png;base64,randomDataThatIsActuallyABase64Image";
Expand Down