Skip to content
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

feat: add grid lines #1179

Open
wants to merge 17 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand Down Expand Up @@ -168,6 +167,8 @@ All properties are optional.
| labels_secondary | `hover` | `true` / `false` / `hover` | Display secondary Y-axis labels.
| 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.
| grid_line_type | `hour` | `5minute` / `hour` / `day` / `week` | Show grids lines using and the value of hours_to_show.
JulienDeveaux marked this conversation as resolved.
Show resolved Hide resolved
| grid_line_frequency | 2 | | Define the frequecy of thin / thick lines you want.
JulienDeveaux marked this conversation as resolved.
Show resolved Hide resolved

#### Line color object
See [dynamic line color](#dynamic-line-color) for example usage.
Expand Down
47 changes: 46 additions & 1 deletion src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -507,11 +507,13 @@ class MiniGraphCard extends LitElement {
}

renderSvg() {
const { height } = this.config;
const { height, show } = this.config;
const grid_line_type = show.grid_line_type ? show.grid_line_type : false;
JulienDeveaux marked this conversation as resolved.
Show resolved Hide resolved
return svg`
<svg width='100%' height=${height !== 0 ? '100%' : 0} viewBox='0 0 500 ${height}'
@click=${e => e.stopPropagation()}>
<g>
${grid_line_type ? this.renderGridLines() : ''}
JulienDeveaux marked this conversation as resolved.
Show resolved Hide resolved
JulienDeveaux marked this conversation as resolved.
Show resolved Hide resolved
<defs>
${this.renderSvgGradient(this.gradient)}
</defs>
Expand All @@ -525,6 +527,49 @@ class MiniGraphCard extends LitElement {
</svg>`;
}

renderGridLines() {
const {
height, hours_to_show, show,
} = this.config;
const grid_line_type = show.grid_line_type ? show.grid_line_type : false;
JulienDeveaux marked this conversation as resolved.
Show resolved Hide resolved
const grid_line_frequency = show.grid_line_frequency ? show.grid_line_frequency : 2;
JulienDeveaux marked this conversation as resolved.
Show resolved Hide resolved

const containerWidth = 500;
let numLines;

let rounded_hours_to_show = Math.round(hours_to_show);
if (rounded_hours_to_show < 1) rounded_hours_to_show = 1;

switch (grid_line_type) {
JulienDeveaux marked this conversation as resolved.
Show resolved Hide resolved
case '5minute':
numLines = rounded_hours_to_show * 12;
break;
case 'hour':
default:
numLines = rounded_hours_to_show;
break;
case 'day':
numLines = Math.round(rounded_hours_to_show / 24);
break;
case 'week':
numLines = Math.round(rounded_hours_to_show / 168);
break;
}
const xRatio = containerWidth / numLines;
const lines = [];

for (let i = 0; i < numLines; i += 1) {
const x = xRatio * (i + 0.5);
ildar170975 marked this conversation as resolved.
Show resolved Hide resolved
if (i % grid_line_frequency > 0) {
JulienDeveaux marked this conversation as resolved.
Show resolved Hide resolved
lines.push(svg`<line x1=${x} y1="0" x2=${x} y2=${height} stroke="var(--divider-color)" stroke-width="0.5" opacity="0.5"/>`);
} else {
lines.push(svg`<line x1=${x} y1="0" x2=${x} y2=${height} stroke="rgb(from var(--divider-color) R G B /0.5)" stroke-width="0.5" opacity="1"/>`);
JulienDeveaux marked this conversation as resolved.
Show resolved Hide resolved
}
}

return lines;
}

setTooltip(entity, index, value, label = null) {
const {
group_by,
Expand Down
Loading