Improve graph tooltips (#15887

* Improve graph tooltips

* Use xy for state charts

* intersect, bigger hitRadius

* improve energy

* fix position tooltips
This commit is contained in:
Bram Kragten 2023-03-30 16:12:26 +02:00 committed by GitHub
parent 0df096d68b
commit dcac853b71
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 31 additions and 42 deletions

View File

@ -302,6 +302,7 @@ export default class HaChartBase extends LitElement {
return css` return css`
:host { :host {
display: block; display: block;
position: relative;
} }
.chartContainer { .chartContainer {
overflow: hidden; overflow: hidden;

View File

@ -61,6 +61,10 @@ class StateHistoryChartLine extends LitElement {
this._chartOptions = { this._chartOptions = {
parsing: false, parsing: false,
animation: false, animation: false,
interaction: {
mode: "nearest",
axis: "x",
},
scales: { scales: {
x: { x: {
type: "time", type: "time",
@ -108,7 +112,6 @@ class StateHistoryChartLine extends LitElement {
}, },
plugins: { plugins: {
tooltip: { tooltip: {
mode: "nearest",
callbacks: { callbacks: {
label: (context) => label: (context) =>
`${context.dataset.label}: ${formatNumber( `${context.dataset.label}: ${formatNumber(
@ -127,16 +130,13 @@ class StateHistoryChartLine extends LitElement {
}, },
}, },
}, },
hover: {
mode: "nearest",
},
elements: { elements: {
line: { line: {
tension: 0.1, tension: 0.1,
borderWidth: 1.5, borderWidth: 1.5,
}, },
point: { point: {
hitRadius: 5, hitRadius: 50,
}, },
}, },
// @ts-expect-error // @ts-expect-error

View File

@ -102,6 +102,7 @@ class StatisticsChart extends LitElement {
if ( if (
changedProps.has("statisticsData") || changedProps.has("statisticsData") ||
changedProps.has("statTypes") || changedProps.has("statTypes") ||
changedProps.has("chartType") ||
changedProps.has("hideLegend") changedProps.has("hideLegend")
) { ) {
this._generateData(); this._generateData();
@ -149,6 +150,10 @@ class StatisticsChart extends LitElement {
this._chartOptions = { this._chartOptions = {
parsing: false, parsing: false,
animation: false, animation: false,
interaction: {
mode: "nearest",
axis: "x",
},
scales: { scales: {
x: { x: {
type: "time", type: "time",
@ -186,7 +191,6 @@ class StatisticsChart extends LitElement {
}, },
plugins: { plugins: {
tooltip: { tooltip: {
mode: "nearest",
callbacks: { callbacks: {
label: (context) => label: (context) =>
`${context.dataset.label}: ${formatNumber( `${context.dataset.label}: ${formatNumber(
@ -208,9 +212,6 @@ class StatisticsChart extends LitElement {
}, },
}, },
}, },
hover: {
mode: "nearest",
},
elements: { elements: {
line: { line: {
tension: 0.4, tension: 0.4,
@ -219,7 +220,7 @@ class StatisticsChart extends LitElement {
}, },
bar: { borderWidth: 1.5, borderRadius: 4 }, bar: { borderWidth: 1.5, borderRadius: 4 },
point: { point: {
hitRadius: 5, hitRadius: 50,
}, },
}, },
// @ts-expect-error // @ts-expect-error
@ -316,6 +317,7 @@ class StatisticsChart extends LitElement {
} }
statDataSets.forEach((d, i) => { statDataSets.forEach((d, i) => {
if ( if (
this.chartType === "line" &&
prevEndTime && prevEndTime &&
prevValues && prevValues &&
prevEndTime.getTime() !== start.getTime() prevEndTime.getTime() !== start.getTime()

View File

@ -157,6 +157,9 @@ export class HuiEnergyGasGraphCard
const options: ChartOptions = { const options: ChartOptions = {
parsing: false, parsing: false,
animation: false, animation: false,
interaction: {
mode: "x",
},
scales: { scales: {
x: { x: {
type: "time", type: "time",
@ -171,9 +174,6 @@ export class HuiEnergyGasGraphCard
maxRotation: 0, maxRotation: 0,
sampleSize: 5, sampleSize: 5,
autoSkipPadding: 20, autoSkipPadding: 20,
major: {
enabled: true,
},
font: (context) => font: (context) =>
context.tick && context.tick.major context.tick && context.tick.major
? ({ weight: "bold" } as any) ? ({ weight: "bold" } as any)
@ -213,7 +213,7 @@ export class HuiEnergyGasGraphCard
}, },
plugins: { plugins: {
tooltip: { tooltip: {
mode: "nearest", position: "nearest",
callbacks: { callbacks: {
title: (datasets) => { title: (datasets) => {
if (dayDifference > 0) { if (dayDifference > 0) {
@ -244,13 +244,10 @@ export class HuiEnergyGasGraphCard
}, },
}, },
}, },
hover: {
mode: "nearest",
},
elements: { elements: {
bar: { borderWidth: 1.5, borderRadius: 4 }, bar: { borderWidth: 1.5, borderRadius: 4 },
point: { point: {
hitRadius: 5, hitRadius: 50,
}, },
}, },
// @ts-expect-error // @ts-expect-error

View File

@ -154,6 +154,9 @@ export class HuiEnergySolarGraphCard
const options: ChartOptions = { const options: ChartOptions = {
parsing: false, parsing: false,
animation: false, animation: false,
interaction: {
mode: "x",
},
scales: { scales: {
x: { x: {
type: "time", type: "time",
@ -168,9 +171,6 @@ export class HuiEnergySolarGraphCard
maxRotation: 0, maxRotation: 0,
sampleSize: 5, sampleSize: 5,
autoSkipPadding: 20, autoSkipPadding: 20,
major: {
enabled: true,
},
font: (context) => font: (context) =>
context.tick && context.tick.major context.tick && context.tick.major
? ({ weight: "bold" } as any) ? ({ weight: "bold" } as any)
@ -209,7 +209,7 @@ export class HuiEnergySolarGraphCard
}, },
plugins: { plugins: {
tooltip: { tooltip: {
mode: "nearest", position: "nearest",
callbacks: { callbacks: {
title: (datasets) => { title: (datasets) => {
if (dayDifference > 0) { if (dayDifference > 0) {
@ -240,9 +240,6 @@ export class HuiEnergySolarGraphCard
}, },
}, },
}, },
hover: {
mode: "nearest",
},
elements: { elements: {
line: { line: {
tension: 0.3, tension: 0.3,

View File

@ -148,6 +148,9 @@ export class HuiEnergyUsageGraphCard
const options: ChartOptions = { const options: ChartOptions = {
parsing: false, parsing: false,
animation: false, animation: false,
interaction: {
mode: "x",
},
scales: { scales: {
x: { x: {
type: "time", type: "time",
@ -162,9 +165,6 @@ export class HuiEnergyUsageGraphCard
maxRotation: 0, maxRotation: 0,
sampleSize: 5, sampleSize: 5,
autoSkipPadding: 20, autoSkipPadding: 20,
major: {
enabled: true,
},
font: (context) => font: (context) =>
context.tick && context.tick.major context.tick && context.tick.major
? ({ weight: "bold" } as any) ? ({ weight: "bold" } as any)
@ -204,8 +204,6 @@ export class HuiEnergyUsageGraphCard
}, },
plugins: { plugins: {
tooltip: { tooltip: {
mode: "x",
intersect: true,
position: "nearest", position: "nearest",
filter: (val) => val.formattedValue !== "0", filter: (val) => val.formattedValue !== "0",
callbacks: { callbacks: {
@ -265,13 +263,10 @@ export class HuiEnergyUsageGraphCard
}, },
}, },
}, },
hover: {
mode: "nearest",
},
elements: { elements: {
bar: { borderWidth: 1.5, borderRadius: 4 }, bar: { borderWidth: 1.5, borderRadius: 4 },
point: { point: {
hitRadius: 5, hitRadius: 50,
}, },
}, },
// @ts-expect-error // @ts-expect-error

View File

@ -157,6 +157,9 @@ export class HuiEnergyWaterGraphCard
const options: ChartOptions = { const options: ChartOptions = {
parsing: false, parsing: false,
animation: false, animation: false,
interaction: {
mode: "x",
},
scales: { scales: {
x: { x: {
type: "time", type: "time",
@ -171,9 +174,6 @@ export class HuiEnergyWaterGraphCard
maxRotation: 0, maxRotation: 0,
sampleSize: 5, sampleSize: 5,
autoSkipPadding: 20, autoSkipPadding: 20,
major: {
enabled: true,
},
font: (context) => font: (context) =>
context.tick && context.tick.major context.tick && context.tick.major
? ({ weight: "bold" } as any) ? ({ weight: "bold" } as any)
@ -213,7 +213,7 @@ export class HuiEnergyWaterGraphCard
}, },
plugins: { plugins: {
tooltip: { tooltip: {
mode: "nearest", position: "nearest",
callbacks: { callbacks: {
title: (datasets) => { title: (datasets) => {
if (dayDifference > 0) { if (dayDifference > 0) {
@ -244,13 +244,10 @@ export class HuiEnergyWaterGraphCard
}, },
}, },
}, },
hover: {
mode: "nearest",
},
elements: { elements: {
bar: { borderWidth: 1.5, borderRadius: 4 }, bar: { borderWidth: 1.5, borderRadius: 4 },
point: { point: {
hitRadius: 5, hitRadius: 50,
}, },
}, },
// @ts-expect-error // @ts-expect-error