Fix spacing & colors in statistics-graph chart (#24068)

* Fix statistic chart colors

* Fix spacing in statistics-graph

* set start time based on data
This commit is contained in:
Petar Petrov 2025-02-05 11:22:31 +02:00 committed by Bram Kragten
parent e5fea98460
commit 786ff787d1
2 changed files with 77 additions and 62 deletions

View File

@ -248,11 +248,16 @@ export class HaChartBase extends LitElement {
private _createOptions(): ECOption { private _createOptions(): ECOption {
let xAxis = this.options?.xAxis; let xAxis = this.options?.xAxis;
if (xAxis && !Array.isArray(xAxis) && xAxis.type === "time") { if (xAxis) {
if (xAxis.max && xAxis.min) { xAxis = Array.isArray(xAxis) ? xAxis : [xAxis];
xAxis = xAxis.map((axis: XAXisOption) => {
if (axis.type !== "time" || axis.show === false) {
return axis;
}
if (axis.max && axis.min) {
this._minutesDifference = differenceInMinutes( this._minutesDifference = differenceInMinutes(
xAxis.max as Date, axis.max as Date,
xAxis.min as Date axis.min as Date
); );
} }
const dayDifference = this._minutesDifference / 60 / 24; const dayDifference = this._minutesDifference / 60 / 24;
@ -265,8 +270,14 @@ export class HaChartBase extends LitElement {
? 3600 * 24 * 1000 ? 3600 * 24 * 1000
: undefined; : undefined;
} }
xAxis = { return {
...xAxis, axisLine: {
show: false,
},
splitLine: {
show: true,
},
...axis,
axisLabel: { axisLabel: {
formatter: this._formatTimeLabel, formatter: this._formatTimeLabel,
rich: { rich: {
@ -275,16 +286,11 @@ export class HaChartBase extends LitElement {
}, },
}, },
hideOverlap: true, hideOverlap: true,
...xAxis.axisLabel, ...axis.axisLabel,
},
axisLine: {
show: false,
},
splitLine: {
show: true,
}, },
minInterval, minInterval,
} as XAXisOption; } as XAXisOption;
});
} }
const options = { const options = {
animation: !this._reducedMotion, animation: !this._reducedMotion,

View File

@ -128,7 +128,8 @@ export class StatisticsChart extends LitElement {
changedProps.has("hideLegend") || changedProps.has("hideLegend") ||
changedProps.has("startTime") || changedProps.has("startTime") ||
changedProps.has("endTime") || changedProps.has("endTime") ||
changedProps.has("_legendData") changedProps.has("_legendData") ||
changedProps.has("_chartData")
) { ) {
this._createOptions(); this._createOptions();
} }
@ -245,30 +246,39 @@ export class StatisticsChart extends LitElement {
const endTime = this.endTime ?? new Date(); const endTime = this.endTime ?? new Date();
let startTime = this.startTime; let startTime = this.startTime;
if (!startTime) {
// set start time to the earliest point in the chart data
this._chartData.forEach((series) => {
if (!Array.isArray(series.data) || !series.data[0]) return;
const firstPoint = series.data[0] as any;
const timestamp = Array.isArray(firstPoint)
? firstPoint[0]
: firstPoint.value?.[0];
if (timestamp && (!startTime || new Date(timestamp) < startTime)) {
startTime = new Date(timestamp);
}
});
if (!startTime) { if (!startTime) {
// Calculate default start time based on dayDifference // Calculate default start time based on dayDifference
startTime = new Date( startTime = new Date(
endTime.getTime() - dayDifference * 24 * 3600 * 1000 endTime.getTime() - dayDifference * 24 * 3600 * 1000
); );
// Check chart data for earlier points
this._chartData.forEach((series) => {
if (!Array.isArray(series.data)) return;
series.data.forEach((point) => {
const timestamp = Array.isArray(point) ? point[0] : point.value?.[0];
if (new Date(timestamp) < startTime!) {
startTime = new Date(timestamp);
} }
});
});
} }
this._chartOptions = { this._chartOptions = {
xAxis: { xAxis: [
{
type: "time", type: "time",
min: startTime, min: startTime,
max: endTime, max: endTime,
}, },
{
type: "time",
show: false,
},
],
yAxis: { yAxis: {
type: this.logarithmicScale ? "log" : "value", type: this.logarithmicScale ? "log" : "value",
name: this.unit, name: this.unit,
@ -462,6 +472,9 @@ export class StatisticsChart extends LitElement {
displayedLegend = displayedLegend || showLegend; displayedLegend = displayedLegend || showLegend;
} }
statTypes.push(type); statTypes.push(type);
const borderColor =
band && hasMean ? color + (this.hideLegend ? "00" : "7F") : color;
const backgroundColor = band ? color + "3F" : color + "7F";
const series: LineSeriesOption | BarSeriesOption = { const series: LineSeriesOption | BarSeriesOption = {
id: `${statistic_id}-${type}`, id: `${statistic_id}-${type}`,
type: this.chartType, type: this.chartType,
@ -485,21 +498,16 @@ export class StatisticsChart extends LitElement {
this.chartType === "bar" this.chartType === "bar"
? { ? {
borderRadius: [4, 4, 0, 0], borderRadius: [4, 4, 0, 0],
borderColor: borderColor,
band && hasMean
? color + (this.hideLegend ? "00" : "7F")
: color,
borderWidth: 1.5, borderWidth: 1.5,
} }
: undefined, : undefined,
color: color: this.chartType === "bar" ? backgroundColor : borderColor,
band && hasMean ? color + (this.hideLegend ? "00" : "7F") : color,
}; };
if (band && this.chartType === "line") { if (band && this.chartType === "line") {
series.stack = `band-${statistic_id}`; series.stack = `band-${statistic_id}`;
series.stackStrategy = "all"; series.stackStrategy = "all";
(series as LineSeriesOption).symbol = "none"; (series as LineSeriesOption).symbol = "none";
(series as LineSeriesOption).lineStyle = { width: 1.5 };
if (drawBands && type === "max") { if (drawBands && type === "max") {
(series as LineSeriesOption).areaStyle = { (series as LineSeriesOption).areaStyle = {
color: color + "3F", color: color + "3F",
@ -561,6 +569,7 @@ export class StatisticsChart extends LitElement {
color, color,
type: this.chartType, type: this.chartType,
data: [], data: [],
xAxisIndex: 1,
}); });
}); });