-
Notifications
You must be signed in to change notification settings - Fork 2.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Statistics Graph: plot against period ends, not beginnings #20903
Statistics Graph: plot against period ends, not beginnings #20903
Conversation
WalkthroughWalkthroughThe primary modification to the Changes
Sequence Diagram(s)sequenceDiagram
participant User
participant StatisticsChart
User->>StatisticsChart: Provide data inputs
StatisticsChart->>StatisticsChart: Process input data
Note over StatisticsChart: New: Directly handle data based on chart type
StatisticsChart-->>User: Rendered Chart with optimized data flow
Recent review detailsConfiguration used: CodeRabbit UI Files selected for processing (1)
Additional context usedBiome
Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media? TipsChatThere are 3 ways to chat with CodeRabbit:
Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (invoked as PR comments)
Additionally, you can add CodeRabbit Configration File (
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 1
Outside diff range comments (5)
src/components/chart/statistics-chart.ts (5)
Line range hint
214-214
: Consider using optional chaining for safer access.- this.hass.locale + this.hass?.locale
Line range hint
215-215
: Replaceany
type with more specific types to improve type safety.- onClick: (e: any) => { + onClick: (e: MouseEvent) => {Also applies to: 280-280
Line range hint
317-319
: ReplaceforEach
withfor...of
for better performance and readability.- statisticsData.forEach(([statistic_id, stats]) => { + for (const [statistic_id, stats] of statisticsData) {Also applies to: 362-522, 446-490, 495-517
Line range hint
478-478
: Use template literals for string concatenation to enhance readability.- label: name ? name + " (" + this.hass.localize(`ui.components.statistics_charts.statistic_types.${type}`) + ")" : this.hass.localize(`ui.components.statistics_charts.statistic_types.${type}`), + label: name ? `${name} (${this.hass.localize(`ui.components.statistics_charts.statistic_types.${type}`)})` : this.hass.localize(`ui.components.statistics_charts.statistic_types.${type}`),
Line range hint
6-14
: Some imports are only used as types. Consider importing them usingimport type
to clarify their usage and potentially optimize bundling.- import { + import type { ChartData, ChartDataset, ChartOptions, ChartType, } from "chart.js";Also applies to: 24-34
Have you looked at how this affects the bar graph? With a bar graph imagine you plot 3 days, you will have 3 bars with three legend labels: May 26, May 27, May 28. I expect the bar over e.g. May 26 label will be the growth during the date of May 26. It sounds like with this change you might plot the may 26th consumption over the May 27 label instead? Similarly in an hourly graph, I would expect the bar over 12:00PM to be the growth from 12:00->12:59PM. |
Indeed, plotting a "change" bar graph no longer shows the May 26 00:00:00-23:59:59 change over the "May 26" label. I would argue that, based on your [entirely reasonable] expectation, the x-axis of a line graph and of a bar graph are semantically different: "6:00" on a line graph is a moment in time, but "6:00" on an hourly bar graph is the entire duration from 6:00-6:59. I might also argue that the bar should be placed between those two labels, at 6:30, but either way - in this context, the plot is wrong for treating them the same. Is it reasonable to plot them differently like that? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 1
Outside diff range comments (4)
src/components/chart/statistics-chart.ts (4)
Line range hint
214-214
: Consider using optional chaining to safely access properties.- if (this.chartType === "bar") { + if (this.chartType?. === "bar") {
Line range hint
215-215
: Specify a more specific type instead of usingany
.- onClick: (e: any) => { + onClick: (e: MouseEvent) => {Also applies to: 280-280
Line range hint
317-319
: Consider usingfor...of
instead offorEach
for better performance and readability.- statisticsData.forEach(([statistic_id, stats]) => { + for (const [statistic_id, stats] of statisticsData) {Also applies to: 362-522, 446-490, 495-517
Line range hint
478-478
: Use template literals for string concatenation to improve readability.- label: name ? name + " (" + this.hass.localize(...) + ")" : this.hass.localize(...), + label: name ? `${name} (${this.hass.localize(...)})` : this.hass.localize(...),
// @ts-expect-error | ||
d.data.push({ x: prevEndTime.getTime(), y: null }); | ||
if (this.chartType === "bar") { | ||
d.data.push({ x: start.getTime(), y: dataValues[i]! }); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Avoid using non-null assertions. Ensure the data is validated before use.
- d.data.push({ x: start.getTime(), y: dataValues[i]! });
+ if (dataValues[i] !== null) {
+ d.data.push({ x: start.getTime(), y: dataValues[i] });
+ }
Also applies to: 408-408, 410-410
Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.
d.data.push({ x: start.getTime(), y: dataValues[i]! }); | |
if (dataValues[i] !== null) { | |
d.data.push({ x: start.getTime(), y: dataValues[i] }); | |
} |
Now the change only applies to line graphs, and bar graphs are unchanged from before. |
That feels like a reasonable solution to me. Thanks. |
Each StatisticValue contains the start and end of a time period, the min/max/mean over that period, and the sum and state at the end of the period. No value corresponds to the beginning of the period. The Statistics Graph was plotting values against the start times, which shifted the data one period left relative to the true values.
c38bd0e
to
7e256e0
Compare
There hasn't been any activity on this pull request recently. This pull request has been automatically marked as stale because of that and will be closed if no further activity occurs within 7 days. |
Proposed change
Each StatisticValue contains the start and end of a time period, the min/max/mean over that period, and the sum and state at the end of the period. No value corresponds to the beginning of the period. The Statistics Graph was plotting values against the start times, which shifted the data one period left relative to the true values.
Type of change
Example configuration
Additional information
Checklist
If user exposed functionality or configuration variables are added/changed:
Summary by CodeRabbit
StatisticsChart
.bar
andline
).