Skip to content

Commit e135ce6

Browse files
Add JSON support for element attributes
1 parent a19a93e commit e135ce6

File tree

3 files changed

+46
-17
lines changed

3 files changed

+46
-17
lines changed

demo/index.html

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,26 +17,41 @@
1717
}
1818
</style>
1919
<body>
20-
<trend-line width="100" height="100" margin="20"></trend-line>
21-
<trend-line width="100" height="100" margin="20"></trend-line>
20+
21+
<trend-line
22+
data="[[0, 1], [1, 2], [2, 4], [3, 8]]"
23+
x-interval="[0, 3]"
24+
y-interval="[1, 8]"
25+
width="100"
26+
height="100"
27+
margin="20"
28+
></trend-line>
29+
30+
<trend-line
31+
id="js"
32+
width="100"
33+
height="100"
34+
margin="20"
35+
></trend-line>
36+
2237
<button>Update</button>
2338
</body>
2439
<script>
2540
const N = 10;
26-
const elements = document.querySelectorAll('trend-line');
27-
update(elements);
41+
const element = document.querySelector("#js");
42+
update(element);
2843

29-
function update(elements) {
30-
for (const el of elements) {
31-
el.data = getRandomData(N);
32-
el.xInterval = [0, 10];
33-
el.yInterval = [0, 1];
34-
}
44+
function update(element) {
45+
element.data = getRandomData(N);
46+
/*
47+
element.xInterval and element.yInterval default to
48+
the ranges observed in data.
49+
*/
3550
}
3651

3752
const button = document.querySelector('button');
3853
button.addEventListener('click', event => {
39-
update(elements);
54+
update(element);
4055
});
4156

4257
function getRandomData (length) {

readme.md

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,13 @@ Use the custom element:
3333

3434
```html
3535
<body>
36-
<trend-line width="100" height="100" margin="20"></trend-line>
36+
<trend-line
37+
x-interval="[0, 10]"
38+
y_interval="[0, 1]"
39+
width="100"
40+
height="100"
41+
margin="20"
42+
></trend-line>
3743
</body>
3844
```
3945

@@ -46,7 +52,7 @@ Set the data to plot:
4652
</script>
4753
```
4854

49-
Please have a look at the [demo](https://pp41i.csb.app/).
55+
Please have a look at the [demo](https://github.com/nextbitlabs/trend-line/blob/main/demo/index.html) in the repo, or the live [demo](https://pp41i.csb.app/).
5056

5157
## License
5258

src/index.js

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,28 @@
1-
import {define} from 'https://cdn.skypack.dev/hybrids@6';
1+
import {define, property} from 'https://cdn.skypack.dev/hybrids@6';
22
import {render} from './render.js';
33
import * as layout from './layout.js';
44
import * as path from './path.js';
55
import * as arrow from './arrow.js';
66

77
export const TrendLine = {
8-
data: [],
8+
data: property(parseJSON),
9+
xInterval: property(parseJSON),
10+
yInterval: property(parseJSON),
911
margin: 20,
1012
width: 100,
1113
height: 100,
12-
xInterval: [],
13-
yInterval: [],
1414
render,
1515
...layout,
1616
...path,
1717
...arrow,
1818
};
1919

2020
define('trend-line', TrendLine);
21+
22+
// Custom property transform for JSON support
23+
// from element attribute.
24+
function parseJSON(value = []) {
25+
return typeof value === 'string'
26+
? JSON.parse(value)
27+
: value;
28+
}

0 commit comments

Comments
 (0)