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 16 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ We recommend looking at the [Example usage section](#example-usage) to understan
| state_map | [state map object](#state-map-object) | | v0.8.0 | List of entity states to convert (order matters as position becomes a value on the graph).
| 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
| grid_line_type | string | | v0.12.x | Show grids lines using `hour` / `day` / `week` and the value of hours_to_show
JulienDeveaux marked this conversation as resolved.
Show resolved Hide resolved


#### Entities object
Expand Down
41 changes: 40 additions & 1 deletion src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -507,11 +507,12 @@ class MiniGraphCard extends LitElement {
}

renderSvg() {
const { height } = this.config;
const { height, grid_line_type = false } = this.config;
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 +526,44 @@ class MiniGraphCard extends LitElement {
</svg>`;
}

renderGridLines() {
const {
height, hours_to_show, grid_line_type,
JulienDeveaux marked this conversation as resolved.
Show resolved Hide resolved
} = this.config;

const containerWidth = 500;
let numLines;
let xRatio;

switch (grid_line_type) {
JulienDeveaux marked this conversation as resolved.
Show resolved Hide resolved
case 'hour':
default:
numLines = hours_to_show;
JulienDeveaux marked this conversation as resolved.
Show resolved Hide resolved
xRatio = containerWidth / numLines;
JulienDeveaux marked this conversation as resolved.
Show resolved Hide resolved
break;
case 'day':
numLines = Math.round(hours_to_show / 24);
xRatio = (containerWidth / numLines);
break;
case 'week':
numLines = Math.round(hours_to_show / 168);
xRatio = containerWidth / numLines;
break;
}
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 % 2 === 0) {
lines.push(svg`<line x1=${x} y1="0" x2=${x} y2=${height} stroke="lightgray" stroke-width="0.5" opacity="0.5"/>`);
JulienDeveaux marked this conversation as resolved.
Show resolved Hide resolved
} else {
lines.push(svg`<line x1=${x} y1="0" x2=${x} y2=${height} stroke="lightgray" stroke-width="0.5" opacity="1"/>`);
}
}

return lines;
}

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