From cd5aa5a12e12bfb6537b78c7bcfd415d0991f776 Mon Sep 17 00:00:00 2001 From: Petar Petrov Date: Thu, 12 Dec 2024 10:00:40 +0200 Subject: [PATCH] Remove custom TimeLineScale for chartjs --- .../chart/state-history-chart-timeline.ts | 2 +- src/components/chart/timeline-chart/const.ts | 1 - .../chart/timeline-chart/timeline-scale.ts | 55 ------------------- src/resources/chartjs.ts | 2 - 4 files changed, 1 insertion(+), 59 deletions(-) delete mode 100644 src/components/chart/timeline-chart/timeline-scale.ts diff --git a/src/components/chart/state-history-chart-timeline.ts b/src/components/chart/state-history-chart-timeline.ts index f619bea3b8f0..72808ae1ae34 100644 --- a/src/components/chart/state-history-chart-timeline.ts +++ b/src/components/chart/state-history-chart-timeline.ts @@ -106,7 +106,7 @@ export class StateHistoryChartTimeline extends LitElement { animation: false, scales: { x: { - type: "timeline", + type: "time", position: "bottom", adapters: { date: { diff --git a/src/components/chart/timeline-chart/const.ts b/src/components/chart/timeline-chart/const.ts index 88e29e78632c..81b8bda754ab 100644 --- a/src/components/chart/timeline-chart/const.ts +++ b/src/components/chart/timeline-chart/const.ts @@ -17,7 +17,6 @@ declare module "chart.js" { datasetOptions: BarControllerDatasetOptions; defaultDataPoint: TimeLineData; parsedDataType: any; - scales: "timeline"; }; } } diff --git a/src/components/chart/timeline-chart/timeline-scale.ts b/src/components/chart/timeline-chart/timeline-scale.ts deleted file mode 100644 index 9c41609bbf58..000000000000 --- a/src/components/chart/timeline-chart/timeline-scale.ts +++ /dev/null @@ -1,55 +0,0 @@ -import { TimeScale } from "chart.js"; -import type { TimeLineData } from "./const"; - -export class TimeLineScale extends TimeScale { - static id = "timeline"; - - static defaults = { - position: "bottom", - tooltips: { - mode: "nearest", - }, - ticks: { - autoSkip: true, - }, - }; - - determineDataLimits() { - const options = this.options; - // @ts-ignore - const adapter = this._adapter; - const unit = options.time.unit || "day"; - let { min, max } = this.getUserBounds(); - - const chart = this.chart; - - // Convert data to timestamps - chart.data.datasets.forEach((dataset, index) => { - if (!chart.isDatasetVisible(index)) { - return; - } - for (const data of dataset.data as TimeLineData[]) { - let timestamp0 = adapter.parse(data.start, this); - let timestamp1 = adapter.parse(data.end, this); - if (timestamp0 > timestamp1) { - [timestamp0, timestamp1] = [timestamp1, timestamp0]; - } - if (min > timestamp0 && timestamp0) { - min = timestamp0; - } - if (max < timestamp1 && timestamp1) { - max = timestamp1; - } - } - }); - - // In case there is no valid min/max, var's use today limits - min = - isFinite(min) && !isNaN(min) ? min : +adapter.startOf(Date.now(), unit); - max = isFinite(max) && !isNaN(max) ? max : +adapter.endOf(Date.now(), unit); - - // Make sure that max is strictly higher than min (required by the lookup table) - this.min = adapter.parse(options.min, this) ?? Math.min(min, max - 1); - this.max = adapter.parse(options.max, this) ?? Math.max(min + 1, max); - } -} diff --git a/src/resources/chartjs.ts b/src/resources/chartjs.ts index fc5b44411c04..bf0e2494d7c3 100644 --- a/src/resources/chartjs.ts +++ b/src/resources/chartjs.ts @@ -16,7 +16,6 @@ import { } from "chart.js"; import { TextBarElement } from "../components/chart/timeline-chart/textbar-element"; import { TimelineController } from "../components/chart/timeline-chart/timeline-controller"; -import { TimeLineScale } from "../components/chart/timeline-chart/timeline-scale"; import "../components/chart/chart-date-adapter"; export { Chart } from "chart.js"; @@ -34,7 +33,6 @@ Chart.register( PointElement, LineElement, TextBarElement, - TimeLineScale, TimelineController, CategoryScale, LogarithmicScale