Skip to content

Commit 0d44609

Browse files
committed
feat: Pass scale factor down to the graph to have correct rendering
1 parent aa65a5c commit 0d44609

File tree

3 files changed

+17
-3
lines changed

3 files changed

+17
-3
lines changed

README.md

+3
Original file line numberDiff line numberDiff line change
@@ -140,13 +140,16 @@ properties of the Entity object detailed in the following table (as per `sensor.
140140
| y_axis | string | | If 'secondary', displays using the secondary y-axis on the right.
141141
| fixed_value | boolean | | Set to true to graph the entity's current state as a fixed value instead of graphing its state history.
142142
| smoothing | boolean | | Override for a flag indicating whether to make graph line smooth.
143+
| value_multipler | number | 1 | Set a multiplier to use on the graph's value
144+
| value_factor | number | 0 | Scale value by order of magnitude (e.g. convert Watts to kilo Watts), use negative value to scale down.
143145

144146
```yaml
145147
entities:
146148
- sensor.temperature
147149
- entity: sensor.pressure
148150
name: Pressure
149151
show_state: true
152+
value_multipler: -2.1
150153
- sensor.humidity
151154
```
152155

src/graph.js

+11-3
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ export default class Graph {
1212
hours = 24,
1313
points = 1,
1414
aggregateFuncName = 'avg',
15+
valueMultipler = 1,
1516
groupBy = 'interval',
1617
smoothing = true,
1718
logarithmic = false,
@@ -38,6 +39,7 @@ export default class Graph {
3839
this.points = points;
3940
this.hours = hours;
4041
this.aggregateFuncName = aggregateFuncName;
42+
this._valueMultipler = valueMultipler;
4143
this._calcPoint = aggregateFuncMap[aggregateFuncName] || this._average;
4244
this._smoothing = smoothing;
4345
this._logarithmic = logarithmic;
@@ -74,8 +76,9 @@ export default class Graph {
7476
histGroups.length = requiredNumOfPoints;
7577

7678
this.coords = this._calcPoints(histGroups);
77-
this.min = Math.min(...this.coords.map(item => Number(item[V])));
78-
this.max = Math.max(...this.coords.map(item => Number(item[V])));
79+
this.min = this._scale(Math.min(...this.coords.map(item => Number(item[V]))));
80+
this.max = this._scale(Math.max(...this.coords.map(item => Number(item[V]))));
81+
if (this.min > this.max) [this.min, this.max] = [this.max, this.min];
7982
}
8083

8184
_reducer(res, item) {
@@ -107,14 +110,19 @@ export default class Graph {
107110
return coords;
108111
}
109112

113+
_scale(value) {
114+
return this._valueMultipler * value;
115+
}
116+
110117
_calcY(coords) {
111118
// account for logarithmic graph
112119
const max = this._logarithmic ? Math.log10(Math.max(1, this.max)) : this.max;
113120
const min = this._logarithmic ? Math.log10(Math.max(1, this.min)) : this.min;
114121

115122
const yRatio = ((max - min) / this.height) || 1;
116123
const coords2 = coords.map((coord) => {
117-
const val = this._logarithmic ? Math.log10(Math.max(1, coord[V])) : coord[V];
124+
const val = this._logarithmic
125+
? Math.log10(Math.max(1, this._scale(coord[V]))) : this._scale(coord[V]);
118126
const coordY = this.height - ((val - min) / yRatio) + this.margin[Y] * 2;
119127
return [coord[X], coordY, coord[V]];
120128
});

src/main.js

+3
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ class MiniGraphCard extends LitElement {
106106
const entitiesChanged = !compareArray(this.config.entities || [], config.entities);
107107
if (!this.Graph || entitiesChanged) {
108108
if (this._hass) this.hass = this._hass;
109+
const valueFactor = 10 ** this.config.value_factor;
109110
this.Graph = this.config.entities.map(
110111
entity => new Graph({
111112
width: 500,
@@ -121,6 +122,8 @@ class MiniGraphCard extends LitElement {
121122
!entity.entity.startsWith('binary_sensor.'), // turn off for binary sensor by default
122123
),
123124
logarithmic: this.config.logarithmic,
125+
valueMultipler: (entity.value_multipler ? entity.value_multipler : 1)
126+
* (entity.value_factor ? 10 ** entity.value_factor : valueFactor),
124127
}),
125128
);
126129
}

0 commit comments

Comments
 (0)