diff --git a/README.md b/README.md index e943d2b..58b8a3e 100644 --- a/README.md +++ b/README.md @@ -116,7 +116,6 @@ We recommend looking at the [Example usage section](#example-usage) to understan | value_factor | number | 0 | v0.9.4 | Scale value by order of magnitude (e.g. convert Watts to kilo Watts), use negative value to scale down. | logarithmic | boolean | `false` | v0.10.0 | Use a Logarithmic scale for the graph - #### Entities object Entities may be listed directly (as per `sensor.temperature` in the example below), or defined using properties of the Entity object detailed in the following table (as per `sensor.pressure` in the example below). @@ -170,6 +169,8 @@ All properties are optional. | name_adaptive_color | `false` | `true` / `false` | Make the name color adapt with the primary entity color. | icon_adaptive_color | `false` | `true` / `false` | Make the icon color adapt with the primary entity color. | loading_indicator | `true` | `true` / `false` | Show loading indicator while attempting to retrieve a history. +| grid_lines_type | `hour` | `5minute` / `hour` / `day` / `week` | Show grid lines dependently on `hours_to_show` value. +| grid_lines_ratio | 2 | | Grid lines: thin / thick lines amount ratio (0 - no thin lines) #### Line color object @@ -262,6 +263,8 @@ The following theme variables can be set in your HA theme to customize the appea |------|:-------:|-------------| | mcg-title-letter-spacing | | Letter spacing of the card title (`name` option). | mcg-title-font-weight | 500 | Font weight of the card title. +| mcg-grid-line-thick-color | rgb(from var(--divider-color) R G B /0.5) | Grid "thick" line color. +| mcg-grid-line-thin-color | var(--divider-color) | Grid "thin" line color. ### Example usage diff --git a/src/main.js b/src/main.js index 77152ed..ce2c0e1 100644 --- a/src/main.js +++ b/src/main.js @@ -539,10 +539,12 @@ class MiniGraphCard extends LitElement { } renderSvg() { - const { height } = this.config; + const { height, show } = this.config; + const grid_lines_type = show.grid_lines_type ? show.grid_lines_type : false; return svg` e.stopPropagation()}> + ${grid_lines_type ? this.renderGridLines() : ''} ${this.renderSvgGradient(this.gradient)} @@ -557,6 +559,56 @@ class MiniGraphCard extends LitElement { `; } + renderGridLines() { + const { + height, hours_to_show, show, + } = this.config; + const grid_lines_type = show.grid_lines_type ? show.grid_lines_type : false; + const grid_lines_ratio = (show.grid_lines_ratio && Number.isInteger(show.grid_lines_ratio)) + ? show.grid_lines_ratio + : 2; + + const containerWidth = 500; + let spanInHours; + + switch (grid_lines_type) { + case '5minute': + spanInHours = 1 / 12; + break; + case 'hour': + default: + spanInHours = 1; + break; + case 'day': + spanInHours = 24; + break; + case 'week': + spanInHours = 168; + } + + let numLines = hours_to_show / spanInHours; + const spanFactor = Math.ceil(hours_to_show / spanInHours) / (hours_to_show / spanInHours); + const thickPart = containerWidth * spanFactor / Math.ceil(numLines); + const thinPart = thickPart / (grid_lines_ratio + 1); + numLines *= (grid_lines_ratio + 1); + + const lines = []; + for (let i = 0; i < numLines - 1; i += 1) { + const x = containerWidth - thinPart * (i + 1); + // const timeLabel = hours_to_show / numLines * (i + 1); + + let stroke; + if ((i + 1) % (grid_lines_ratio + 1) > 0) { + stroke = 'var(--mcg-grid-line-thin-color, var(--divider-color))'; + } else { + stroke = 'var(--mcg-grid-line-thick-color, rgb(from var(--divider-color) R G B /0.5))'; + } + lines.push(svg``); + } + + return svg`${lines}`; + } + setTooltip(entity, index, value, label = null) { const { group_by,