Skip to content

Commit 0385492

Browse files
committed
Added option to set custom color schemes
1 parent 390ae3a commit 0385492

File tree

5 files changed

+34
-20
lines changed

5 files changed

+34
-20
lines changed

dist/plotly-graph-card.js

Lines changed: 7 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

readme.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,10 @@ entities:
138138
- sensor.temperature1
139139
- sensor.temperature2
140140
color_scheme: dutch_field
141-
# color_scheme: 1 # or use numbers instead 0 to 24 available
141+
# or use numbers instead 0 to 24 available:
142+
# color_scheme: 1
143+
# or pass your color scheme
144+
# color_scheme: ["#1b9e77","#d95f02","#7570b3","#e7298a","#66a61e","#e6ab02","#a6761d","red"]
142145
```
143146

144147
### Attribute values
@@ -335,6 +338,8 @@ Update data every `refresh_interval` seconds. Use `0` or delete the line to disa
335338
- run `npm start`
336339
- From a dashboard in edit mode, go to `Manage resources` and add `http://127.0.0.1:8000/plotly-graph-card.js` as url with resource type JavaScript
337340
- ATTENTION: The development card is `type: custom:plotly-graph-dev`
341+
- Either use Safari or Disbale [chrome://flags/#block-insecure-private-network-requests](chrome://flags/#block-insecure-private-network-requests): Chrome doesn't allow public network resources from requesting private-network resources - unless the public-network resource is secure (HTTPS) and the private-network resource provides appropriate (yet-undefined) CORS headers. More [here](https://stackoverflow.com/questions/66534759/chrome-cors-error-on-request-to-localhost-dev-server-from-remote-site)
342+
338343

339344
# Build
340345

src/color-schemes.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ Usage example in YAML:
44
color_scheme: accent
55
color_scheme: 0 # both mean the same
66
*/
7-
7+
export type ColorSchemeArray = string[]
88
const colorSchemes = {
99
// https://vega.github.io/vega/docs/schemes/#categorical
1010
accent: ["#7fc97f","#beaed4","#fdc086","#ffff99","#386cb0","#f0027f","#bf5b17","#666666"],
@@ -34,5 +34,9 @@ const colorSchemes = {
3434
pink_foam: ["#54bebe", "#76c8c8", "#98d1d1", "#badbdb", "#dedad2", "#e4bcad", "#df979e", "#d7658b", "#c80064"],
3535
salmon_to_aqua: ["#e27c7c", "#a86464", "#6d4b4b", "#503f3f", "#333333", "#3c4e4b", "#466964", "#599e94", "#6cd4c5"],
3636
}
37+
export function isColorSchemeArray(obj: any): obj is ColorSchemeArray{
38+
return Array.isArray(obj)
39+
}
40+
3741
export default colorSchemes
3842
export type ColorSchemeNames = keyof typeof colorSchemes

src/plotly-graph-card.ts

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ import getThemedLayout from "./themed-layout";
1212
import isProduction from "./is-production";
1313
import { sleep } from "./utils";
1414
import { Datum } from "plotly.js";
15-
import colorSchemes from "./color-schemes";
16-
import {parseISO} from 'date-fns'
15+
import colorSchemes, { isColorSchemeArray } from "./color-schemes";
16+
import { parseISO } from "date-fns";
1717

1818
const componentName = isProduction ? "plotly-graph" : "plotly-graph-dev";
1919

@@ -195,10 +195,11 @@ export class PlotlyGraph extends HTMLElement {
195195
},
196196
};
197197
}
198-
const colorScheme =
199-
colorSchemes[schemeName] ||
200-
colorSchemes[Object.keys(colorSchemes)[schemeName]] ||
201-
colorSchemes.category10;
198+
const colorScheme = isColorSchemeArray(schemeName)
199+
? schemeName
200+
: colorSchemes[schemeName] ||
201+
colorSchemes[Object.keys(colorSchemes)[schemeName]] ||
202+
colorSchemes.category10;
202203
const newConfig: Config = {
203204
hours_to_show: config.hours_to_show ?? 1,
204205
refresh_interval: config.refresh_interval ?? 0,
@@ -347,9 +348,13 @@ export class PlotlyGraph extends HTMLElement {
347348
);
348349
const traces: Plotly.Data[] = [mergedTrace];
349350
if (mergedTrace.show_value) {
350-
merge(mergedTrace, {
351-
legendgroup: "group" + traceIdx,
352-
}, mergedTrace);
351+
merge(
352+
mergedTrace,
353+
{
354+
legendgroup: "group" + traceIdx,
355+
},
356+
mergedTrace
357+
);
353358
traces.push({
354359
// @ts-expect-error (texttemplate missing in plotly typings)
355360
texttemplate: `%{y}%{customdata.unit_of_measurement}`, // here so it can be overwritten

src/types.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import { Datum } from "plotly.js";
2-
import { ColorSchemeNames } from "./color-schemes";
2+
import { ColorSchemeArray, ColorSchemeNames } from "./color-schemes";
33

44
export type InputConfig = {
55
type: "custom:plotly-graph-card";
66
hours_to_show?: number;
77
refresh_interval?: number; // in seconds
8-
color_scheme?: ColorSchemeNames;
8+
color_scheme?: ColorSchemeNames | ColorSchemeArray | number;
99
title?: string;
1010
entities: (Partial<Plotly.PlotData> & {
1111
entity: string;

0 commit comments

Comments
 (0)