mirror of
https://github.com/home-assistant/frontend.git
synced 2026-11-29 08:10:09 +00:00
Compare commits
2
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
29bf0afa00 | ||
|
|
86f1496e62 |
No files matched your search
@@ -67,7 +67,7 @@ import type {
|
||||
import { isMac } from "../../util/is_mac";
|
||||
import "../chips/ha-assist-chip";
|
||||
import "../ha-icon-button";
|
||||
import { ZOOM_SLIDER_HEIGHT } from "./ha-chart-zoom-slider";
|
||||
import { RANGE_MIN_WIDTH, ZOOM_SLIDER_HEIGHT } from "./ha-chart-zoom-slider";
|
||||
import { formatTimeLabel } from "./axis-label";
|
||||
import type { ChartSonification } from "./chart-sonification";
|
||||
import { canSonifyChart, sonifyChart } from "./chart-sonification";
|
||||
@@ -546,6 +546,10 @@ export class HaChartBase extends MobileAwareMixin(LitElement) {
|
||||
if (changedProps.has("options")) {
|
||||
chartOptions = { ...chartOptions, ...this._createOptions() };
|
||||
this._disposeSonification();
|
||||
const [start, end] = this._zoomRange;
|
||||
if (!this._getZoomAxis() && (start !== 0 || end !== 100)) {
|
||||
this._setZoomRange(0, 100);
|
||||
}
|
||||
}
|
||||
let legendChanged = false;
|
||||
if (changedProps.has("options") || changedProps.has("data")) {
|
||||
@@ -1072,7 +1076,7 @@ export class HaChartBase extends MobileAwareMixin(LitElement) {
|
||||
private _updateDragToZoom() {
|
||||
const active =
|
||||
!this._touchInput &&
|
||||
this._hasZoomableXAxis() &&
|
||||
this._getZoomAxis() !== undefined &&
|
||||
this.selectMode !== "range";
|
||||
if (!this.chart || active === this._dragToZoom) {
|
||||
return;
|
||||
@@ -1209,31 +1213,34 @@ export class HaChartBase extends MobileAwareMixin(LitElement) {
|
||||
}
|
||||
}
|
||||
|
||||
private _hasZoomableXAxis() {
|
||||
// Horizontal bar charts zoom their categories, which rescales the values
|
||||
private _getZoomAxis(): "x" | "y" | undefined {
|
||||
const xAxis = ensureArray(this.options?.xAxis)?.[0] as
|
||||
XAXisOption | undefined;
|
||||
const yAxis = ensureArray(this.options?.yAxis)?.[0] as
|
||||
YAXisOption | undefined;
|
||||
return Boolean(
|
||||
xAxis &&
|
||||
xAxis.show !== false &&
|
||||
!this.options?.dataZoom &&
|
||||
// vertical data zoom doesn't work well in this case and horizontal is pointless
|
||||
!(xAxis.type === "value" && yAxis?.type === "category")
|
||||
);
|
||||
if (!xAxis || xAxis.show === false || this.options?.dataZoom) {
|
||||
return undefined;
|
||||
}
|
||||
return xAxis.type === "value" && yAxis?.type === "category" ? "y" : "x";
|
||||
}
|
||||
|
||||
private _hasZoomableXAxis() {
|
||||
return this._getZoomAxis() === "x";
|
||||
}
|
||||
|
||||
private _getDataZoomConfig(): DataZoomComponentOption[] | undefined {
|
||||
if (!this._hasZoomableXAxis()) {
|
||||
const zoomAxis = this._getZoomAxis();
|
||||
if (!zoomAxis) {
|
||||
return undefined;
|
||||
}
|
||||
return [
|
||||
{
|
||||
id: "dataZoom",
|
||||
type: "inside",
|
||||
orient: "horizontal",
|
||||
filterMode: this._getDataZoomFilterMode() as any,
|
||||
xAxisIndex: 0,
|
||||
xAxisIndex: zoomAxis === "x" ? 0 : undefined,
|
||||
yAxisIndex: zoomAxis === "y" ? 0 : undefined,
|
||||
// Panning is left to the slider and horizontal scrolling
|
||||
moveOnMouseMove: false,
|
||||
preventDefaultMouseMove: false,
|
||||
@@ -1243,9 +1250,18 @@ export class HaChartBase extends MobileAwareMixin(LitElement) {
|
||||
|
||||
private _showZoomSlider() {
|
||||
const [start, end] = this._zoomRange;
|
||||
const zoomAxis = this._getZoomAxis();
|
||||
// A short chart leaves a vertical slider no room to move
|
||||
const fits =
|
||||
zoomAxis === "x" ||
|
||||
(zoomAxis === "y" &&
|
||||
(this.chart?.getHeight() ?? 0) -
|
||||
ZOOM_RESET_OFFSET -
|
||||
X_AXIS_LABEL_SPACE >=
|
||||
2 * RANGE_MIN_WIDTH);
|
||||
return (
|
||||
this.zoomSlider !== "never" &&
|
||||
this._hasZoomableXAxis() &&
|
||||
fits &&
|
||||
(this.zoomSlider === "always" ||
|
||||
this._zoomSliderHeld ||
|
||||
start !== 0 ||
|
||||
@@ -1257,11 +1273,12 @@ export class HaChartBase extends MobileAwareMixin(LitElement) {
|
||||
const resetLabel = this._i18n.localize(
|
||||
"ui.components.history_charts.zoom_reset"
|
||||
);
|
||||
const vertical = this._getZoomAxis() === "y";
|
||||
return html`
|
||||
${
|
||||
this._isZoomed
|
||||
? html`<ha-icon-button
|
||||
class="slider-reset"
|
||||
class="slider-reset ${classMap({ vertical })}"
|
||||
.path=${mdiRestart}
|
||||
.label=${resetLabel}
|
||||
@click=${this._handleZoomReset}
|
||||
@@ -1270,6 +1287,7 @@ export class HaChartBase extends MobileAwareMixin(LitElement) {
|
||||
}
|
||||
<ha-chart-zoom-slider
|
||||
class="zoom-slider"
|
||||
.vertical=${vertical}
|
||||
.start=${this._zoomRange[0]}
|
||||
.end=${this._zoomRange[1]}
|
||||
@zoom-slider-changed=${this._handleZoomSliderChanged}
|
||||
@@ -1313,10 +1331,11 @@ export class HaChartBase extends MobileAwareMixin(LitElement) {
|
||||
return grid;
|
||||
}
|
||||
const [first, ...rest] = ensureArray(grid);
|
||||
return [
|
||||
{ ...first, bottom: this._getGridBottom() + this._getZoomSliderSpace() },
|
||||
...rest,
|
||||
];
|
||||
const room =
|
||||
this._getZoomAxis() === "y"
|
||||
? { right: (Number(first.right) || 0) + ZOOM_SLIDER_SPACE }
|
||||
: { bottom: this._getGridBottom() + this._getZoomSliderSpace() };
|
||||
return [{ ...first, ...room }, ...rest];
|
||||
}
|
||||
|
||||
private _getGridBottom() {
|
||||
@@ -1395,6 +1414,7 @@ export class HaChartBase extends MobileAwareMixin(LitElement) {
|
||||
l.type === "custom" ? { show: false } : l
|
||||
);
|
||||
}
|
||||
const verticalZoom = this._getZoomAxis() === "y";
|
||||
const options = {
|
||||
animation: !this._reducedMotion,
|
||||
animationDuration: 500,
|
||||
@@ -1407,7 +1427,8 @@ export class HaChartBase extends MobileAwareMixin(LitElement) {
|
||||
feature: {
|
||||
dataZoom: {
|
||||
show: true,
|
||||
yAxisIndex: false,
|
||||
xAxisIndex: verticalZoom ? false : undefined,
|
||||
yAxisIndex: verticalZoom ? undefined : false,
|
||||
filterMode: "none",
|
||||
showTitle: false,
|
||||
},
|
||||
@@ -1735,6 +1756,10 @@ export class HaChartBase extends MobileAwareMixin(LitElement) {
|
||||
}
|
||||
|
||||
const replaceMerge = options.series ? ["series"] : [];
|
||||
// A bar chart turned into a pie drops its zoom
|
||||
if ("dataZoom" in options) {
|
||||
replaceMerge.push("dataZoom");
|
||||
}
|
||||
this.chart.setOption(options, { replaceMerge });
|
||||
}
|
||||
|
||||
@@ -1763,14 +1788,16 @@ export class HaChartBase extends MobileAwareMixin(LitElement) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
if (!this._hasZoomableXAxis()) {
|
||||
const zoomAxis = this._getZoomAxis();
|
||||
if (!zoomAxis) {
|
||||
return;
|
||||
}
|
||||
const position =
|
||||
zoomAxis === "y"
|
||||
? e.offsetY / this.chart.getHeight()
|
||||
: e.offsetX / this.chart.getWidth();
|
||||
// Kept within the axis, as ECharts does, so the datazoom event matches it
|
||||
const start = Math.min(
|
||||
Math.max((e.offsetX / this.chart.getWidth()) * 100 - 15, 0),
|
||||
70
|
||||
);
|
||||
const start = Math.min(Math.max(position * 100 - 15, 0), 70);
|
||||
const range = this._isZoomed ? [0, 100] : [start, start + 30];
|
||||
this.chart.dispatchAction({
|
||||
type: "dataZoom",
|
||||
@@ -1798,7 +1825,11 @@ export class HaChartBase extends MobileAwareMixin(LitElement) {
|
||||
ev.stopPropagation();
|
||||
// Not _isZoomed, which a Sankey roam sets without an axis to pan
|
||||
const [start, end] = this._zoomRange;
|
||||
if (!this.chart || (start === 0 && end === 100)) {
|
||||
if (
|
||||
!this.chart ||
|
||||
!this._hasZoomableXAxis() ||
|
||||
(start === 0 && end === 100)
|
||||
) {
|
||||
return;
|
||||
}
|
||||
const delta = ev.shiftKey ? ev.deltaX || ev.deltaY : ev.deltaX;
|
||||
@@ -1824,7 +1855,7 @@ export class HaChartBase extends MobileAwareMixin(LitElement) {
|
||||
return;
|
||||
}
|
||||
const [start, end] = this._zoomRange;
|
||||
const shift = (pixels * (end - start)) / this._getXAxisPixelWidth();
|
||||
const shift = (pixels * (end - start)) / this._getZoomAxisLength();
|
||||
const clamped = Math.max(-start, Math.min(100 - end, shift));
|
||||
if (clamped) {
|
||||
this._setZoomWindow(start + clamped, end + clamped);
|
||||
@@ -2026,15 +2057,18 @@ export class HaChartBase extends MobileAwareMixin(LitElement) {
|
||||
});
|
||||
}
|
||||
|
||||
private _getXAxisPixelWidth(): number {
|
||||
private _getZoomAxisLength(): number {
|
||||
const vertical = this._getZoomAxis() === "y";
|
||||
const axisModel = this.chart
|
||||
// @ts-ignore private method but no public way to get the axis extent
|
||||
?.getModel()
|
||||
.getComponent("xAxis", 0) as
|
||||
.getComponent(vertical ? "yAxis" : "xAxis", 0) as
|
||||
{ axis?: { getExtent(): [number, number] } } | undefined;
|
||||
const extent = axisModel?.axis?.getExtent();
|
||||
const width = extent ? Math.abs(extent[1] - extent[0]) : 0;
|
||||
return width || this.chart!.getWidth();
|
||||
const length = extent ? Math.abs(extent[1] - extent[0]) : 0;
|
||||
return (
|
||||
length || (vertical ? this.chart!.getHeight() : this.chart!.getWidth())
|
||||
);
|
||||
}
|
||||
|
||||
private _setZoomRange(start: number, end: number) {
|
||||
@@ -2056,7 +2090,7 @@ export class HaChartBase extends MobileAwareMixin(LitElement) {
|
||||
[start, end] = dataZoom?.getPercentRange() ?? [0, 100];
|
||||
const [prevStart, prevEnd] = this._zoomRange;
|
||||
const pixels =
|
||||
((end - start) / (prevEnd - prevStart)) * this._getXAxisPixelWidth();
|
||||
((end - start) / (prevEnd - prevStart)) * this._getZoomAxisLength();
|
||||
if (pixels < MIN_DRAG_ZOOM_WIDTH) {
|
||||
this.chart!.dispatchAction(
|
||||
{
|
||||
@@ -2309,6 +2343,18 @@ export class HaChartBase extends MobileAwareMixin(LitElement) {
|
||||
right: 0;
|
||||
bottom: ${ZOOM_SLIDER_BOTTOM}px;
|
||||
}
|
||||
.slider-reset.vertical {
|
||||
left: auto;
|
||||
right: ${ZOOM_SLIDER_BOTTOM + ZOOM_SLIDER_HEIGHT / 2 - ZOOM_RESET_SIZE / 2}px;
|
||||
top: 0;
|
||||
bottom: auto;
|
||||
}
|
||||
.zoom-slider[vertical] {
|
||||
left: auto;
|
||||
right: ${ZOOM_SLIDER_BOTTOM}px;
|
||||
top: ${ZOOM_RESET_OFFSET}px;
|
||||
bottom: ${X_AXIS_LABEL_SPACE}px;
|
||||
}
|
||||
.slider-reset,
|
||||
.zoom-slider {
|
||||
animation: zoom-slider-fade-in var(--ha-animation-duration-normal)
|
||||
|
||||
@@ -17,7 +17,8 @@ export const ZOOM_SLIDER_HEIGHT = 24;
|
||||
const THUMB_SIZE = 20;
|
||||
const THUMB_INSET = (ZOOM_SLIDER_HEIGHT - THUMB_SIZE) / 2;
|
||||
// Leaves room to grab the range between its thumbs, however small it gets
|
||||
const RANGE_MIN_WIDTH = 2 * (THUMB_INSET + THUMB_SIZE) + ZOOM_SLIDER_HEIGHT;
|
||||
export const RANGE_MIN_WIDTH =
|
||||
2 * (THUMB_INSET + THUMB_SIZE) + ZOOM_SLIDER_HEIGHT;
|
||||
// The smallest visible range the thumbs can be dragged to, in percent
|
||||
const MIN_RANGE = 1;
|
||||
// A press this soon after a tap's release shows the full range again
|
||||
@@ -33,24 +34,31 @@ export class HaChartZoomSlider extends LitElement {
|
||||
|
||||
@property({ type: Number }) public end = 100;
|
||||
|
||||
// Top to bottom, for charts that zoom a category axis drawn top down
|
||||
@property({ type: Boolean, reflect: true }) public vertical = false;
|
||||
|
||||
@state()
|
||||
@consume({ context: internationalizationContext, subscribe: true })
|
||||
private _i18n!: HomeAssistantInternationalization;
|
||||
|
||||
private _pressX?: number;
|
||||
private _pressPosition?: number;
|
||||
|
||||
private _lastTapTime?: number;
|
||||
|
||||
private _drag?: {
|
||||
pointerId: number;
|
||||
part: SliderPart;
|
||||
x: number;
|
||||
position: number;
|
||||
range: [number, number];
|
||||
};
|
||||
|
||||
protected render() {
|
||||
const { start, end } = this;
|
||||
const { localize, locale } = this._i18n;
|
||||
const [offset, size] = this.vertical
|
||||
? ["top", "height"]
|
||||
: ["left", "width"];
|
||||
const orientation = this.vertical ? "vertical" : "horizontal";
|
||||
const at = (percent: number) =>
|
||||
`calc((100% - ${RANGE_MIN_WIDTH}px) * ${percent / 100})`;
|
||||
const percent = (value: number) =>
|
||||
@@ -70,7 +78,7 @@ export class HaChartZoomSlider extends LitElement {
|
||||
class="range"
|
||||
data-part="range"
|
||||
role="slider"
|
||||
aria-orientation="horizontal"
|
||||
aria-orientation=${orientation}
|
||||
aria-valuemin="0"
|
||||
aria-valuemax="100"
|
||||
aria-valuenow=${Math.round(start)}
|
||||
@@ -78,8 +86,8 @@ export class HaChartZoomSlider extends LitElement {
|
||||
aria-label=${localize("ui.components.history_charts.zoom_range")}
|
||||
tabindex="0"
|
||||
style=${styleMap({
|
||||
left: at(start),
|
||||
width: `calc(${at(end - start)} + ${RANGE_MIN_WIDTH}px)`,
|
||||
[offset]: at(start),
|
||||
[size]: `calc(${at(end - start)} + ${RANGE_MIN_WIDTH}px)`,
|
||||
})}
|
||||
@pointerdown=${this._handlePointerDown}
|
||||
@pointermove=${this._handlePointerMove}
|
||||
@@ -93,7 +101,7 @@ export class HaChartZoomSlider extends LitElement {
|
||||
class="thumb"
|
||||
data-part=${part}
|
||||
role="slider"
|
||||
aria-orientation="horizontal"
|
||||
aria-orientation=${orientation}
|
||||
aria-valuemin=${
|
||||
part === "start"
|
||||
? 0
|
||||
@@ -109,7 +117,7 @@ export class HaChartZoomSlider extends LitElement {
|
||||
aria-label=${localize(`ui.components.history_charts.zoom_${part}`)}
|
||||
tabindex="0"
|
||||
style=${styleMap({
|
||||
left:
|
||||
[offset]:
|
||||
part === "start"
|
||||
? `calc(${at(start)} + ${THUMB_INSET}px)`
|
||||
: `calc(${at(end)} + ${RANGE_MIN_WIDTH - THUMB_INSET - THUMB_SIZE}px)`,
|
||||
@@ -129,7 +137,12 @@ export class HaChartZoomSlider extends LitElement {
|
||||
|
||||
// Percent of the full range per pixel along the track
|
||||
private _getScale() {
|
||||
return 100 / Math.max(this.clientWidth - RANGE_MIN_WIDTH, 1);
|
||||
const length = this.vertical ? this.clientHeight : this.clientWidth;
|
||||
return 100 / Math.max(length - RANGE_MIN_WIDTH, 1);
|
||||
}
|
||||
|
||||
private _getPosition(ev: PointerEvent) {
|
||||
return this.vertical ? ev.clientY : ev.clientX;
|
||||
}
|
||||
|
||||
// Pressing the track beside the visible range centers the range there.
|
||||
@@ -146,18 +159,19 @@ export class HaChartZoomSlider extends LitElement {
|
||||
ev.timeStamp - this._lastTapTime < DOUBLE_PRESS_TIME
|
||||
) {
|
||||
this._lastTapTime = undefined;
|
||||
this._pressX = undefined;
|
||||
this._pressPosition = undefined;
|
||||
this._drag = undefined;
|
||||
// The press ends here, so the chart does not keep holding the slider open
|
||||
ev.stopPropagation();
|
||||
fireEvent(this, "zoom-slider-reset");
|
||||
return;
|
||||
}
|
||||
this._pressX = ev.clientX;
|
||||
this._pressPosition = this._getPosition(ev);
|
||||
if (ev.target !== ev.currentTarget) {
|
||||
return;
|
||||
}
|
||||
const position = (ev.offsetX - RANGE_MIN_WIDTH / 2) * this._getScale();
|
||||
const offset = this.vertical ? ev.offsetY : ev.offsetX;
|
||||
const position = (offset - RANGE_MIN_WIDTH / 2) * this._getScale();
|
||||
this._move(
|
||||
"range",
|
||||
[this.start, this.end],
|
||||
@@ -173,10 +187,10 @@ export class HaChartZoomSlider extends LitElement {
|
||||
}
|
||||
const tapped =
|
||||
ev.type === "pointerup" &&
|
||||
this._pressX !== undefined &&
|
||||
Math.abs(ev.clientX - this._pressX) <= TAP_MAX_DISTANCE;
|
||||
this._pressPosition !== undefined &&
|
||||
Math.abs(this._getPosition(ev) - this._pressPosition) <= TAP_MAX_DISTANCE;
|
||||
this._lastTapTime = tapped ? ev.timeStamp : undefined;
|
||||
this._pressX = undefined;
|
||||
this._pressPosition = undefined;
|
||||
}
|
||||
|
||||
private _handlePointerDown(
|
||||
@@ -189,7 +203,7 @@ export class HaChartZoomSlider extends LitElement {
|
||||
this._drag = {
|
||||
pointerId: ev.pointerId,
|
||||
part: ev.currentTarget.dataset.part as SliderPart,
|
||||
x: ev.clientX,
|
||||
position: this._getPosition(ev),
|
||||
range: [this.start, this.end],
|
||||
};
|
||||
}
|
||||
@@ -203,8 +217,12 @@ export class HaChartZoomSlider extends LitElement {
|
||||
this._drag = undefined;
|
||||
return;
|
||||
}
|
||||
const { part, x, range } = this._drag;
|
||||
this._move(part, range, (ev.clientX - x) * this._getScale());
|
||||
const { part, position, range } = this._drag;
|
||||
this._move(
|
||||
part,
|
||||
range,
|
||||
(this._getPosition(ev) - position) * this._getScale()
|
||||
);
|
||||
}
|
||||
|
||||
private _handlePointerUp(ev: PointerEvent) {
|
||||
@@ -218,20 +236,19 @@ export class HaChartZoomSlider extends LitElement {
|
||||
) {
|
||||
const part = ev.currentTarget.dataset.part as SliderPart;
|
||||
const span = this.end - this.start;
|
||||
const decrease = ev.key === "ArrowLeft" || ev.key === "ArrowDown";
|
||||
const decrease =
|
||||
ev.key === "ArrowLeft" ||
|
||||
ev.key === (this.vertical ? "ArrowUp" : "ArrowDown");
|
||||
// A ninth of a shrunk span undoes a tenth of the original one
|
||||
const step =
|
||||
part !== "range" && (part === "start") === decrease
|
||||
? span / 9
|
||||
: span / 10;
|
||||
const shift = {
|
||||
ArrowLeft: -step,
|
||||
ArrowDown: -step,
|
||||
ArrowRight: step,
|
||||
ArrowUp: step,
|
||||
Home: -100,
|
||||
End: 100,
|
||||
}[ev.key];
|
||||
const shift = ev.key.startsWith("Arrow")
|
||||
? decrease
|
||||
? -step
|
||||
: step
|
||||
: { Home: -100, End: 100 }[ev.key];
|
||||
if (shift === undefined) {
|
||||
return;
|
||||
}
|
||||
@@ -318,6 +335,21 @@ export class HaChartZoomSlider extends LitElement {
|
||||
--mdc-icon-size: ${THUMB_SIZE}px;
|
||||
cursor: ew-resize;
|
||||
}
|
||||
:host([vertical]) {
|
||||
width: ${ZOOM_SLIDER_HEIGHT}px;
|
||||
height: auto;
|
||||
}
|
||||
:host([vertical]) .range {
|
||||
left: 0;
|
||||
right: 0;
|
||||
}
|
||||
:host([vertical]) .thumb {
|
||||
left: ${THUMB_INSET}px;
|
||||
cursor: ns-resize;
|
||||
}
|
||||
:host([vertical]) ha-svg-icon {
|
||||
transform: rotate(90deg);
|
||||
}
|
||||
.range:focus-visible,
|
||||
.thumb:focus-visible {
|
||||
outline: 2px solid var(--primary-color);
|
||||
|
||||
Reference in new issue
Block a user