Skip to content
This repository was archived by the owner on Jul 22, 2024. It is now read-only.

Commit e9c6d6c

Browse files
committed
configurable chart granularity
1 parent 540dd0a commit e9c6d6c

File tree

2 files changed

+29
-15
lines changed

2 files changed

+29
-15
lines changed

README.md

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -24,19 +24,22 @@ entity: weather.home
2424
2525
##### Card options
2626
27-
| Name | Type | Default | Description |
28-
| --------------- | ------- | -------------------------|--------------------------------------------------------------------------------------------------- |
29-
| type | string | **Required** | Should be `custom:weather-chart-card` |
30-
| entity | string | **Required** | An entity_id with the `weather` domain |
31-
| title | string | none | Card title |
32-
| show_main | boolean | true | Show or hide a section with current weather condition and temperature |
33-
| show_attributes | boolean | true | Show or hide a section with attributes such as pressure, humidity, wind direction and speed, etc |
34-
| icons | string | none | Path to the location of custom icons in svg format, for example `/local/weather-icons/` |
35-
| icons_size | number | 25 | The size of custom icons in pixels |
36-
| units | object | none | See [units of measurement](#units-of-measurement) for available options |
37-
| temp1_color | string | rgba(230, 100, 100, 1.0) | Temperature first line chart color |
38-
| temp2_color | string | rgba(68, 115, 158, 1.0) | Temperature second line chart color |
39-
| precip_color | string | rgba(132, 209, 253, 1.0) | Precipitation bar chart color |
27+
| Name | Type | Default | Description |
28+
| ------------------- | ------- | -------------------------|---------------------------------------------------------------------------------------------------- |
29+
| type | string | **Required** | Should be `custom:weather-chart-card` |
30+
| entity | string | **Required** | An entity_id with the `weather` domain |
31+
| title | string | none | Card title |
32+
| show_main | boolean | true | Show or hide a section with current weather condition and temperature |
33+
| show_attributes | boolean | true | Show or hide a section with attributes such as pressure, humidity, wind direction and speed, etc |
34+
| icons | string | none | Path to the location of custom icons in svg format, for example `/local/weather-icons/` |
35+
| icons_size | number | 25 | The size of custom icons in pixels |
36+
| units | object | none | See [units of measurement](#units-of-measurement) for available options |
37+
| temp1_color | string | rgba(230, 100, 100, 1.0) | Temperature first line chart color |
38+
| temp2_color | string | rgba(68, 115, 158, 1.0) | Temperature second line chart color |
39+
| precip_color | string | rgba(132, 209, 253, 1.0) | Precipitation bar chart color |
40+
| max_chart_lookahead | number | 0 | Desired lookahead elements for forecast chart. Default is fist `n` elements based on card width.\* |
41+
42+
\* For the `forecast` in your `entity` object, the maximum number of elements to look ahead to display on the chart that will fit within your card's width swapping granularity for lookahead time. For example, if you have 24 hours of hourly weather in your weather entity, set this value to 18, and your card can display 8 elements, your chart would display 8 elemnts at 2 hour intervals dropping the last 2 hourlies in the desired window, but making sure to display as much data as possible.
4043

4144
##### Units of measurement
4245

src/main.js

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ class WeatherChartCard extends LitElement {
7070
? this.config.chart_options.temperature2_color : 'rgba(68, 115, 158, 1.0)';
7171
this.chartPrecipitationsColor = this.config.chart_options && this.config.chart_options.precipitations_color
7272
? this.config.chart_options.precipitations_color : 'rgba(132, 209, 253, 1.0)';
73+
this.maxChartLookahead = this.config.max_chart_lookahead ? this.config.max_chart_lookahead : 0;
7374
}
7475

7576
constructor() {
@@ -129,6 +130,16 @@ class WeatherChartCard extends LitElement {
129130
this.forecastItems = Math.round(card.offsetWidth / (this.chartLabelsFontSize * 5.5));
130131
}
131132

133+
getForecast(weather, forecastItems) {
134+
if (this.maxChartLookahead == 0 ) {
135+
return weather.attributes.forecast.slice(0, forecastItems);
136+
} else {
137+
var hoursPerEntity = parseInt(Math.min(weather.attributes.forecast.length, this.maxChartLookahead)/forecastItems);
138+
var tmpForecast = weather.attributes.forecast.slice(0, forecastItems * hoursPerEntity);
139+
return tmpForecast.filter((e, i) => i % hoursPerEntity === (hoursPerEntity - 1));
140+
}
141+
}
142+
132143
drawChart({config, language, weather, forecastItems} = this) {
133144
if (!weather || !weather.attributes || !weather.attributes.forecast) {
134145
return [];
@@ -139,7 +150,7 @@ class WeatherChartCard extends LitElement {
139150
var tempUnit = this._hass.config.unit_system.temperature;
140151
var lengthUnit = this._hass.config.unit_system.length;
141152
var precipUnit = lengthUnit === 'km' ? this.ll('units')['mm'] : this.ll('units')['in'];
142-
var forecast = weather.attributes.forecast.slice(0, forecastItems);
153+
var forecast = this.getForecast(weather, forecastItems);
143154
if ((new Date(forecast[1].datetime) - new Date(forecast[0].datetime)) < 864e5)
144155
var mode = 'hourly';
145156
else
@@ -326,7 +337,7 @@ class WeatherChartCard extends LitElement {
326337
if (!weather || !weather.attributes || !weather.attributes.forecast) {
327338
return [];
328339
}
329-
var forecast = weather.attributes.forecast.slice(0, forecastItems);
340+
var forecast = this.getForecast(weather, forecastItems);
330341
var i;
331342
var dateTime = [];
332343
var tempHigh = [];

0 commit comments

Comments
 (0)