mirror of
https://github.com/home-assistant/frontend.git
synced 2026-12-01 09:09:11 +00:00
Compare commits
3
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
a28b8dfe8d | ||
|
|
0f5d03da2a | ||
|
|
e359e09317 |
No files matched your search
@@ -9,6 +9,7 @@ import {
|
||||
import { differenceInMinutes } from "date-fns";
|
||||
import type {
|
||||
DataZoomComponentOption,
|
||||
GridComponentOption,
|
||||
MarkAreaComponentOption,
|
||||
} from "echarts/components";
|
||||
import type { EChartsType } from "echarts/core";
|
||||
@@ -27,6 +28,7 @@ import {
|
||||
eventOptions,
|
||||
property,
|
||||
query,
|
||||
queryAll,
|
||||
state,
|
||||
} from "lit/decorators";
|
||||
import { classMap } from "lit/directives/class-map";
|
||||
@@ -150,6 +152,9 @@ const processSeriesTooltipFormatter = (s: HaECSeriesItem): RawSeriesOption => {
|
||||
|
||||
export type CustomLegendOption = ECOption["legend"] & {
|
||||
type: "custom";
|
||||
// "y-axis" lists the items beside the chart, each one next to its category,
|
||||
// in place of the y-axis labels.
|
||||
position?: "bottom" | "y-axis";
|
||||
data?: {
|
||||
id?: string;
|
||||
secondaryIds?: string[]; // Other dataset IDs that should be controlled by this legend item.
|
||||
@@ -248,6 +253,9 @@ export class HaChartBase extends MobileAwareMixin(LitElement) {
|
||||
|
||||
@query(".chart") private _chartContainer?: HTMLDivElement;
|
||||
|
||||
@queryAll(".chart-legend button[data-id]")
|
||||
private _legendButtons!: NodeListOf<HTMLElement>;
|
||||
|
||||
@query(".sonification-output")
|
||||
private _sonificationOutput?: HTMLDivElement;
|
||||
|
||||
@@ -319,6 +327,25 @@ export class HaChartBase extends MobileAwareMixin(LitElement) {
|
||||
},
|
||||
});
|
||||
|
||||
// The axis legend takes width from the chart without resizing the host
|
||||
private _chartResizeController = new ResizeController(this, {
|
||||
target: null,
|
||||
callback: (entries) => {
|
||||
const contentRect = entries[entries.length - 1]?.contentRect;
|
||||
if (
|
||||
this.chart &&
|
||||
contentRect &&
|
||||
(Math.round(contentRect.width) !== this.chart.getWidth() ||
|
||||
Math.round(contentRect.height) !== this.chart.getHeight())
|
||||
) {
|
||||
this._shouldResizeChart = true;
|
||||
this._resizeChartIfNeeded();
|
||||
}
|
||||
},
|
||||
});
|
||||
|
||||
private _focusedLegendButton?: { id: string; toggle: boolean };
|
||||
|
||||
private _loading = false;
|
||||
|
||||
private _reducedMotion = false;
|
||||
@@ -441,6 +468,7 @@ export class HaChartBase extends MobileAwareMixin(LitElement) {
|
||||
if (!this.isConnected) {
|
||||
return;
|
||||
}
|
||||
this._chartResizeController.observe(this._chartContainer!);
|
||||
// The only measurement taken here: neither observer has reported yet, and a
|
||||
// chart first rendered inside a hidden container has to defer its setup
|
||||
// rather than build against a guessed width.
|
||||
@@ -489,6 +517,16 @@ export class HaChartBase extends MobileAwareMixin(LitElement) {
|
||||
}
|
||||
|
||||
public willUpdate(changedProps: PropertyValues): void {
|
||||
const focused = this.shadowRoot?.activeElement as HTMLElement | null;
|
||||
this._focusedLegendButton =
|
||||
focused?.dataset.id &&
|
||||
focused.closest(".chart-legend") &&
|
||||
focused.matches(":focus-visible")
|
||||
? {
|
||||
id: focused.dataset.id,
|
||||
toggle: focused.classList.contains("legend-toggle"),
|
||||
}
|
||||
: undefined;
|
||||
if (!this.chart) {
|
||||
return;
|
||||
}
|
||||
@@ -596,6 +634,7 @@ export class HaChartBase extends MobileAwareMixin(LitElement) {
|
||||
this._resizeChartIfNeeded();
|
||||
}
|
||||
this._renderedLegendFit = this._legendFit.fit;
|
||||
this._restoreLegendFocus();
|
||||
if (changedProps.has("options") || changedProps.has("data")) {
|
||||
// New values keep the widest widths, but other items are fitted afresh
|
||||
const legendItemsKey = JSON.stringify(
|
||||
@@ -610,11 +649,30 @@ export class HaChartBase extends MobileAwareMixin(LitElement) {
|
||||
}
|
||||
}
|
||||
|
||||
// The legend reuses its buttons when items move, which would leave focus on
|
||||
// whichever item now sits where the focused one was
|
||||
private _restoreLegendFocus() {
|
||||
const focused = this._focusedLegendButton;
|
||||
const active = this.shadowRoot?.activeElement as HTMLElement | null;
|
||||
if (!focused || active?.dataset.id === focused.id) {
|
||||
return;
|
||||
}
|
||||
const className = focused.toggle ? "legend-toggle" : "label";
|
||||
Array.from(this._legendButtons)
|
||||
.find(
|
||||
(button) =>
|
||||
button.dataset.id === focused.id &&
|
||||
button.classList.contains(className)
|
||||
)
|
||||
?.focus();
|
||||
}
|
||||
|
||||
protected render() {
|
||||
const sonifiable =
|
||||
!this._sonificationUnavailable &&
|
||||
canSonifyChart(this.data, this._hiddenDatasets);
|
||||
const showZoomSlider = this._showZoomSlider();
|
||||
const axisLegend = this._hasAxisLegend();
|
||||
return html`
|
||||
<div
|
||||
class="container ${classMap({ "has-height": !!this.height })}"
|
||||
@@ -626,11 +684,14 @@ export class HaChartBase extends MobileAwareMixin(LitElement) {
|
||||
})}
|
||||
>
|
||||
<div
|
||||
class="chart-container"
|
||||
class="chart-container ${classMap({
|
||||
"has-axis-legend": axisLegend,
|
||||
})}"
|
||||
style=${styleMap({
|
||||
height: this.height ? undefined : `${this._getDefaultHeight()}px`,
|
||||
})}
|
||||
>
|
||||
${axisLegend ? this._renderAxisLegend() : nothing}
|
||||
<div
|
||||
class="chart ${classMap({
|
||||
"touch-scrub": this._isTouchDevice && this._hasZoomableXAxis(),
|
||||
@@ -656,7 +717,7 @@ export class HaChartBase extends MobileAwareMixin(LitElement) {
|
||||
${showZoomSlider ? this._renderZoomSlider() : nothing}
|
||||
</div>
|
||||
<div class="sonification-output"></div>
|
||||
${this._renderLegend()}
|
||||
${axisLegend ? nothing : this._renderLegend()}
|
||||
<div class="top-controls ${classMap({ small: this.smallControls })}">
|
||||
<slot name="search"></slot>
|
||||
<div
|
||||
@@ -683,14 +744,22 @@ export class HaChartBase extends MobileAwareMixin(LitElement) {
|
||||
`;
|
||||
}
|
||||
|
||||
private _getLegendItems() {
|
||||
if (!this.options?.legend || !this.data) {
|
||||
private _getCustomLegend() {
|
||||
if (!this.options?.legend) {
|
||||
return undefined;
|
||||
}
|
||||
const legend = ensureArray(this.options.legend).find(
|
||||
return ensureArray(this.options.legend).find(
|
||||
(l) => l.show && l.type === "custom"
|
||||
) as CustomLegendOption | undefined;
|
||||
if (!legend) {
|
||||
}
|
||||
|
||||
private _hasAxisLegend() {
|
||||
return this._getCustomLegend()?.position === "y-axis";
|
||||
}
|
||||
|
||||
private _getLegendItems() {
|
||||
const legend = this._getCustomLegend();
|
||||
if (!legend || !this.data) {
|
||||
return undefined;
|
||||
}
|
||||
const datasets = ensureArray(this.data);
|
||||
@@ -707,20 +776,6 @@ export class HaChartBase extends MobileAwareMixin(LitElement) {
|
||||
if (!items) {
|
||||
return nothing;
|
||||
}
|
||||
const datasets = ensureArray(this.data!);
|
||||
// Index datasets by id and name so each legend item is an O(1) lookup
|
||||
// instead of scanning every dataset twice. Charts can have many series.
|
||||
const datasetById = new Map<unknown, (typeof datasets)[number]>();
|
||||
const datasetByName = new Map<unknown, (typeof datasets)[number]>();
|
||||
for (const dataset of datasets) {
|
||||
if (dataset.id !== undefined && !datasetById.has(dataset.id)) {
|
||||
datasetById.set(dataset.id, dataset);
|
||||
}
|
||||
if (dataset.name !== undefined && !datasetByName.has(dataset.name)) {
|
||||
datasetByName.set(dataset.name, dataset);
|
||||
}
|
||||
}
|
||||
|
||||
const fit = this._legendFit.fit ?? items.length;
|
||||
return html`<div
|
||||
class=${classMap({
|
||||
@@ -729,73 +784,7 @@ export class HaChartBase extends MobileAwareMixin(LitElement) {
|
||||
})}
|
||||
>
|
||||
<ul ${this._legendFit.target()}>
|
||||
${items.map((item, index) => {
|
||||
let itemStyle: Record<string, any> = {};
|
||||
let value = "";
|
||||
let noLabelClick = false;
|
||||
const name = typeof item === "string" ? item : (item.name ?? "");
|
||||
let id: string;
|
||||
if (typeof item === "string") {
|
||||
id = item;
|
||||
} else {
|
||||
id = item.id ?? name;
|
||||
value = item.value ?? "";
|
||||
itemStyle = item.itemStyle ?? {};
|
||||
noLabelClick = item.noLabelClick ?? false;
|
||||
}
|
||||
const labelClickable = this.clickLabelForMoreInfo && !noLabelClick;
|
||||
const dataset = datasetById.get(id) ?? datasetByName.get(id);
|
||||
itemStyle = {
|
||||
color: dataset?.color as string,
|
||||
...(dataset?.itemStyle as { borderColor?: string }),
|
||||
...itemStyle,
|
||||
};
|
||||
const color = itemStyle?.color as string;
|
||||
return html`<li
|
||||
.id=${id}
|
||||
@pointerdown=${this._legendPointerDown}
|
||||
@pointerup=${this._legendPointerCancel}
|
||||
@pointerleave=${this._legendPointerCancel}
|
||||
@pointercancel=${this._legendPointerCancel}
|
||||
@contextmenu=${this._legendContextMenu}
|
||||
class=${classMap({
|
||||
hidden: this._hiddenDatasets.has(id),
|
||||
overflow: !this.expandLegend && index >= fit,
|
||||
})}
|
||||
>
|
||||
<button
|
||||
type="button"
|
||||
class="legend-toggle"
|
||||
data-id=${id}
|
||||
aria-pressed=${!this._hiddenDatasets.has(id)}
|
||||
.title=${this._i18n.localize(
|
||||
"ui.components.history_charts.toggle_visibility"
|
||||
)}
|
||||
@click=${this._toggleDataset}
|
||||
>
|
||||
<ha-svg-icon
|
||||
.path=${
|
||||
this._hiddenDatasets.has(id)
|
||||
? mdiCircleOutline
|
||||
: mdiCheckCircle
|
||||
}
|
||||
style=${styleMap({
|
||||
color: this._hiddenDatasets.has(id) ? undefined : color,
|
||||
})}
|
||||
></ha-svg-icon>
|
||||
</button>
|
||||
<button
|
||||
type="button"
|
||||
class=${classMap({ label: true, clickable: labelClickable })}
|
||||
data-id=${id}
|
||||
.title=${name}
|
||||
@click=${this._labelClick}
|
||||
>
|
||||
${name}
|
||||
</button>
|
||||
${value ? html`<div class="value">${value}</div>` : nothing}
|
||||
</li>`;
|
||||
})}
|
||||
${this._renderLegendItems(items, fit)}
|
||||
${
|
||||
items.length
|
||||
? html`<li
|
||||
@@ -829,6 +818,107 @@ export class HaChartBase extends MobileAwareMixin(LitElement) {
|
||||
</div>`;
|
||||
}
|
||||
|
||||
private _renderAxisLegend() {
|
||||
const items = this._getLegendItems() ?? [];
|
||||
const grid = ensureArray(this._getGridOption()!)[0];
|
||||
return html`<div class="chart-legend axis-legend">
|
||||
<ul
|
||||
@pointerover=${this._axisLegendPointerOver}
|
||||
@pointerleave=${this._axisLegendPointerLeave}
|
||||
style=${styleMap({
|
||||
paddingTop: `${grid.top}px`,
|
||||
paddingBottom: `${grid.bottom}px`,
|
||||
})}
|
||||
>
|
||||
${this._renderLegendItems(items, items.length)}
|
||||
</ul>
|
||||
</div>`;
|
||||
}
|
||||
|
||||
private _renderLegendItems(
|
||||
items: NonNullable<ReturnType<HaChartBase["_getLegendItems"]>>,
|
||||
fit: number
|
||||
) {
|
||||
const datasets = ensureArray(this.data!);
|
||||
// Index datasets by id and name so each legend item is an O(1) lookup
|
||||
// instead of scanning every dataset twice. Charts can have many series.
|
||||
const datasetById = new Map<unknown, (typeof datasets)[number]>();
|
||||
const datasetByName = new Map<unknown, (typeof datasets)[number]>();
|
||||
for (const dataset of datasets) {
|
||||
if (dataset.id !== undefined && !datasetById.has(dataset.id)) {
|
||||
datasetById.set(dataset.id, dataset);
|
||||
}
|
||||
if (dataset.name !== undefined && !datasetByName.has(dataset.name)) {
|
||||
datasetByName.set(dataset.name, dataset);
|
||||
}
|
||||
}
|
||||
return items.map((item, index) => {
|
||||
let itemStyle: Record<string, any> = {};
|
||||
let value = "";
|
||||
let noLabelClick = false;
|
||||
const name = typeof item === "string" ? item : (item.name ?? "");
|
||||
let id: string;
|
||||
if (typeof item === "string") {
|
||||
id = item;
|
||||
} else {
|
||||
id = item.id ?? name;
|
||||
value = item.value ?? "";
|
||||
itemStyle = item.itemStyle ?? {};
|
||||
noLabelClick = item.noLabelClick ?? false;
|
||||
}
|
||||
const labelClickable = this.clickLabelForMoreInfo && !noLabelClick;
|
||||
const dataset = datasetById.get(id) ?? datasetByName.get(id);
|
||||
itemStyle = {
|
||||
color: dataset?.color as string,
|
||||
...(dataset?.itemStyle as { borderColor?: string }),
|
||||
...itemStyle,
|
||||
};
|
||||
const color = itemStyle?.color as string;
|
||||
return html`<li
|
||||
.id=${id}
|
||||
@pointerdown=${this._legendPointerDown}
|
||||
@pointerup=${this._legendPointerCancel}
|
||||
@pointerleave=${this._legendPointerCancel}
|
||||
@pointercancel=${this._legendPointerCancel}
|
||||
@contextmenu=${this._legendContextMenu}
|
||||
class=${classMap({
|
||||
hidden: this._hiddenDatasets.has(id),
|
||||
overflow: !this.expandLegend && index >= fit,
|
||||
})}
|
||||
>
|
||||
<button
|
||||
type="button"
|
||||
class="legend-toggle"
|
||||
data-id=${id}
|
||||
aria-pressed=${!this._hiddenDatasets.has(id)}
|
||||
.title=${this._i18n.localize(
|
||||
"ui.components.history_charts.toggle_visibility"
|
||||
)}
|
||||
@click=${this._toggleDataset}
|
||||
>
|
||||
<ha-svg-icon
|
||||
.path=${
|
||||
this._hiddenDatasets.has(id) ? mdiCircleOutline : mdiCheckCircle
|
||||
}
|
||||
style=${styleMap({
|
||||
color: this._hiddenDatasets.has(id) ? undefined : color,
|
||||
})}
|
||||
></ha-svg-icon>
|
||||
</button>
|
||||
<button
|
||||
type="button"
|
||||
class=${classMap({ label: true, clickable: labelClickable })}
|
||||
data-id=${id}
|
||||
.title=${name}
|
||||
@click=${this._labelClick}
|
||||
>
|
||||
${name}
|
||||
</button>
|
||||
${value ? html`<div class="value">${value}</div>` : nothing}
|
||||
</li>`;
|
||||
});
|
||||
}
|
||||
|
||||
// Chart2Music adds ~45 kB gzipped, so it is only fetched once someone actually
|
||||
// moves keyboard focus into a chart.
|
||||
private async _handleChartFocus() {
|
||||
@@ -1309,14 +1399,28 @@ export class HaChartBase extends MobileAwareMixin(LitElement) {
|
||||
|
||||
private _getGridOption() {
|
||||
const grid = this.options?.grid;
|
||||
if (!grid || !this._zoomSliderRoom) {
|
||||
const axisLegend = this._hasAxisLegend();
|
||||
if (!axisLegend && (!grid || !this._zoomSliderRoom)) {
|
||||
return grid;
|
||||
}
|
||||
const [first, ...rest] = ensureArray(grid);
|
||||
return [
|
||||
{ ...first, bottom: this._getGridBottom() + this._getZoomSliderSpace() },
|
||||
...rest,
|
||||
];
|
||||
const [first, ...rest] = ensureArray(grid ?? {});
|
||||
const result: GridComponentOption = { ...first };
|
||||
if (axisLegend) {
|
||||
// The legend rows are laid out from these, so labels must not move them
|
||||
result.top = this._getGridTop();
|
||||
result.bottom = this._getGridBottom();
|
||||
result.containLabel = false;
|
||||
result.outerBoundsMode = "none";
|
||||
}
|
||||
if (this._zoomSliderRoom) {
|
||||
result.bottom = this._getGridBottom() + this._getZoomSliderSpace();
|
||||
}
|
||||
return [result, ...rest];
|
||||
}
|
||||
|
||||
private _getGridTop() {
|
||||
const top = this.options?.grid && ensureArray(this.options.grid)[0].top;
|
||||
return typeof top === "number" ? top : 0;
|
||||
}
|
||||
|
||||
private _getGridBottom() {
|
||||
@@ -1389,6 +1493,22 @@ export class HaChartBase extends MobileAwareMixin(LitElement) {
|
||||
} as XAXisOption;
|
||||
});
|
||||
}
|
||||
let yAxis = this.options?.yAxis;
|
||||
if (this._hasAxisLegend()) {
|
||||
const [first, ...rest] = ensureArray(yAxis ?? {});
|
||||
yAxis = [
|
||||
{
|
||||
...first,
|
||||
type: "category",
|
||||
inverse: true,
|
||||
data: this._getLegendItems()?.map((item) =>
|
||||
typeof item === "string" ? item : (item.id ?? item.name)
|
||||
),
|
||||
axisLabel: { ...first.axisLabel, show: false },
|
||||
} as YAXisOption,
|
||||
...rest,
|
||||
];
|
||||
}
|
||||
let legend = this.options?.legend;
|
||||
if (legend) {
|
||||
legend = ensureArray(legend).map((l) =>
|
||||
@@ -1418,6 +1538,7 @@ export class HaChartBase extends MobileAwareMixin(LitElement) {
|
||||
grid: this._getGridOption(),
|
||||
legend,
|
||||
xAxis,
|
||||
yAxis,
|
||||
};
|
||||
|
||||
if (options.tooltip) {
|
||||
@@ -2018,6 +2139,24 @@ export class HaChartBase extends MobileAwareMixin(LitElement) {
|
||||
}
|
||||
};
|
||||
|
||||
private _axisLegendPointerOver(ev: PointerEvent) {
|
||||
if (ev.pointerType !== "mouse" || !this.chart) {
|
||||
return;
|
||||
}
|
||||
const id = (ev.target as HTMLElement).closest("li")?.id;
|
||||
if (!id || this._hiddenDatasets.has(id)) {
|
||||
this._hideTooltip();
|
||||
return;
|
||||
}
|
||||
this.chart.dispatchAction({ type: "showTip", seriesIndex: 0, name: id });
|
||||
}
|
||||
|
||||
private _axisLegendPointerLeave(ev: PointerEvent) {
|
||||
if (ev.pointerType === "mouse") {
|
||||
this._hideTooltip();
|
||||
}
|
||||
}
|
||||
|
||||
private _hideTooltip() {
|
||||
this.chart?.dispatchAction({ type: "hideTip" });
|
||||
this.chart?.dispatchAction({
|
||||
@@ -2322,6 +2461,15 @@ export class HaChartBase extends MobileAwareMixin(LitElement) {
|
||||
.has-height .chart-container {
|
||||
flex: 1;
|
||||
}
|
||||
.chart-container.has-axis-legend {
|
||||
display: flex;
|
||||
/* The chart is not mirrored in RTL, so its legend stays on the left */
|
||||
direction: ltr;
|
||||
}
|
||||
.has-axis-legend .chart {
|
||||
flex: 1;
|
||||
min-width: 0;
|
||||
}
|
||||
.chart {
|
||||
height: 100%;
|
||||
width: 100%;
|
||||
@@ -2422,6 +2570,29 @@ export class HaChartBase extends MobileAwareMixin(LitElement) {
|
||||
position: absolute;
|
||||
visibility: hidden;
|
||||
}
|
||||
.chart-legend.axis-legend {
|
||||
flex: none;
|
||||
max-width: min(200px, 40%);
|
||||
max-height: none;
|
||||
padding: 0;
|
||||
overflow: visible;
|
||||
direction: var(--direction);
|
||||
}
|
||||
.chart-legend.axis-legend ul {
|
||||
height: 100%;
|
||||
box-sizing: border-box;
|
||||
flex-direction: column;
|
||||
flex-wrap: nowrap;
|
||||
align-items: stretch;
|
||||
gap: 0;
|
||||
}
|
||||
.chart-legend.axis-legend li,
|
||||
.chart-legend.axis-legend li:has(.legend-toggle) {
|
||||
flex: 1 1 0;
|
||||
height: auto;
|
||||
min-height: 0;
|
||||
max-width: none;
|
||||
}
|
||||
.chart-legend .hidden {
|
||||
color: var(--secondary-text-color);
|
||||
}
|
||||
|
||||
@@ -14,7 +14,6 @@ import { consume } from "../../../../common/decorators/consume";
|
||||
import { formatNumberWithUnit } from "../../../../common/number/format_number";
|
||||
import "../../../../components/chart/ha-chart-base";
|
||||
import "../../../../components/chart/ha-chart-tooltip-marker";
|
||||
import { wrapLitTooltipFormatter } from "../../../../components/chart/lit-tooltip-formatter";
|
||||
import {
|
||||
formattersContext,
|
||||
internationalizationContext,
|
||||
@@ -50,7 +49,6 @@ import type { HaECOption } from "../../../../resources/echarts/echarts";
|
||||
import "../../../../components/ha-card";
|
||||
import type { HASSDomEvent } from "../../../../common/dom/fire_event";
|
||||
import { fireEvent } from "../../../../common/dom/fire_event";
|
||||
import { measureTextWidth } from "../../../../util/text";
|
||||
import "../../../../components/ha-icon-button";
|
||||
import { storage } from "../../../../common/decorators/storage";
|
||||
import { getEnergyColor } from "./common/color";
|
||||
@@ -198,9 +196,9 @@ export class HuiEnergyDevicesGraphCard
|
||||
<ha-chart-base
|
||||
.data=${this._chartData}
|
||||
.options=${this._createOptions(
|
||||
this._chartData,
|
||||
this._chartType,
|
||||
this._legendData
|
||||
this._legendData,
|
||||
this._hiddenStats
|
||||
)}
|
||||
.height=${`${Math.max(modes.includes("pie") ? 300 : 100, (this._legendData?.length || 0) * 28 + 50)}px`}
|
||||
.sonificationLabelFormatter=${this._sonificationLabel}
|
||||
@@ -252,97 +250,61 @@ export class HuiEnergyDevicesGraphCard
|
||||
<div style="direction:ltr; display: inline;">${value}</div>`;
|
||||
};
|
||||
|
||||
private _renderLabelTooltip = wrapLitTooltipFormatter(
|
||||
(params: { value: string }) => {
|
||||
const series = this._chartData[0];
|
||||
const item = (series?.data as any[] | undefined)?.find(
|
||||
(d) => d.id === params.value
|
||||
);
|
||||
return item
|
||||
? this._renderTooltip({
|
||||
name: item.name,
|
||||
value: item.value,
|
||||
color: item.itemStyle.color,
|
||||
seriesName: series.name,
|
||||
})
|
||||
: nothing;
|
||||
}
|
||||
);
|
||||
|
||||
private _createOptions = memoizeOne(
|
||||
(
|
||||
data: (BarSeriesOption | PieSeriesOption)[],
|
||||
chartType: "bar" | "pie",
|
||||
legendData: typeof this._legendData
|
||||
legendData: typeof this._legendData,
|
||||
hiddenStats: string[]
|
||||
): HaECOption => {
|
||||
const options: HaECOption = {
|
||||
grid: {
|
||||
top: 5,
|
||||
left: 5,
|
||||
right: 40,
|
||||
bottom: 0,
|
||||
containLabel: true,
|
||||
},
|
||||
const selected = Object.fromEntries(hiddenStats.map((id) => [id, false]));
|
||||
if (chartType === "bar") {
|
||||
const legend: CustomLegendOption = {
|
||||
type: "custom",
|
||||
show: true,
|
||||
position: "y-axis",
|
||||
data: [
|
||||
...legendData.filter((d) => !hiddenStats.includes(d.id!)),
|
||||
...legendData.filter((d) => hiddenStats.includes(d.id!)),
|
||||
].map(({ value: _value, ...item }) => item),
|
||||
selected,
|
||||
};
|
||||
return {
|
||||
grid: {
|
||||
top: 0,
|
||||
bottom: 24,
|
||||
left: 8,
|
||||
right: 40,
|
||||
},
|
||||
tooltip: {
|
||||
show: true,
|
||||
formatter: this._renderTooltip,
|
||||
},
|
||||
xAxis: {
|
||||
show: true,
|
||||
type: "value",
|
||||
name: "kWh",
|
||||
axisPointer: {
|
||||
show: false,
|
||||
},
|
||||
},
|
||||
yAxis: { show: true, type: "category" },
|
||||
legend,
|
||||
};
|
||||
}
|
||||
return {
|
||||
tooltip: {
|
||||
show: true,
|
||||
formatter: this._renderTooltip,
|
||||
},
|
||||
xAxis: { show: false },
|
||||
yAxis: { show: false },
|
||||
legend: { type: "custom", show: false },
|
||||
};
|
||||
if (chartType === "bar") {
|
||||
options.xAxis = {
|
||||
show: true,
|
||||
type: "value",
|
||||
name: "kWh",
|
||||
axisPointer: {
|
||||
show: false,
|
||||
},
|
||||
};
|
||||
options.yAxis = {
|
||||
show: true,
|
||||
type: "category",
|
||||
inverse: true,
|
||||
triggerEvent: true,
|
||||
// ECharts types only list `show`, but the label tooltip takes the
|
||||
// formatter too
|
||||
tooltip: {
|
||||
show: true,
|
||||
formatter: this._renderLabelTooltip,
|
||||
} as { show: boolean },
|
||||
// take order from data
|
||||
data: data[0]?.data?.map((d: any) => d.name),
|
||||
axisLabel: {
|
||||
formatter: this._getDeviceName.bind(this),
|
||||
overflow: "truncate",
|
||||
fontSize: 12,
|
||||
margin: 5,
|
||||
width: Math.min(
|
||||
this._isMobileSize ? 100 : 200,
|
||||
Math.max(
|
||||
...(data[0]?.data?.map(
|
||||
(d: any) =>
|
||||
measureTextWidth(this._getDeviceName(d.name), 12) + 5
|
||||
) || [])
|
||||
)
|
||||
),
|
||||
},
|
||||
};
|
||||
} else {
|
||||
options.legend = {
|
||||
legend: {
|
||||
type: "custom",
|
||||
show: true,
|
||||
data: legendData,
|
||||
selected: legendData
|
||||
.filter((d) => d.id && this._hiddenStats.includes(d.id))
|
||||
.reduce((acc, d) => {
|
||||
acc[d.id!] = false;
|
||||
return acc;
|
||||
}, {}),
|
||||
};
|
||||
}
|
||||
return options;
|
||||
selected,
|
||||
},
|
||||
};
|
||||
}
|
||||
);
|
||||
|
||||
@@ -657,14 +619,6 @@ export class HuiEnergyDevicesGraphCard
|
||||
|
||||
private _handleChartClick(e: CustomEvent<ECElementEvent>): void {
|
||||
if (
|
||||
e.detail.targetType === "axisLabel" &&
|
||||
e.detail.value &&
|
||||
!isExternalStatistic(e.detail.value as string)
|
||||
) {
|
||||
fireEvent(this, "hass-more-info", {
|
||||
entityId: e.detail.value as string,
|
||||
});
|
||||
} else if (
|
||||
e.detail.seriesType === "pie" &&
|
||||
e.detail.event?.target?.type === "tspan" // label
|
||||
) {
|
||||
|
||||
Reference in new issue
Block a user