2023-10-04 18:59:46 +00:00
|
|
|
class CustomChart extends HTMLElement {
|
2023-10-05 16:38:25 +00:00
|
|
|
constructor () {
|
2023-10-04 18:59:46 +00:00
|
|
|
super();
|
|
|
|
this.attachShadow({ mode: "open" });
|
|
|
|
this.shadowRoot.innerHTML = `
|
|
|
|
<style>
|
2023-10-05 16:38:25 +00:00
|
|
|
* {
|
|
|
|
box-sizing: border-box;
|
|
|
|
font-family: monospace;
|
|
|
|
}
|
|
|
|
figure {
|
|
|
|
margin: 0;
|
|
|
|
}
|
2023-10-04 18:59:46 +00:00
|
|
|
div {
|
|
|
|
max-width: 400px;
|
|
|
|
aspect-ratio: 1 / 1;
|
|
|
|
}
|
2023-10-05 16:38:25 +00:00
|
|
|
figcaption {
|
|
|
|
text-align: center;
|
|
|
|
margin-top: 10px;
|
|
|
|
display: flex;
|
|
|
|
flex-direction: column;
|
|
|
|
}
|
2023-10-04 18:59:46 +00:00
|
|
|
</style>
|
2023-10-05 16:38:25 +00:00
|
|
|
<style id="responsive-style" media="not all">
|
|
|
|
figure {
|
|
|
|
display: flex;
|
|
|
|
align-items: center;
|
|
|
|
flex-direction: row;
|
|
|
|
flex-wrap: nowrap;
|
|
|
|
justify-content: flex-start;
|
|
|
|
}
|
|
|
|
div {
|
|
|
|
max-height: 1lh;
|
|
|
|
}
|
|
|
|
figcaption {
|
|
|
|
margin: 0;
|
|
|
|
margin-left: 10px;
|
|
|
|
display: flex;
|
|
|
|
flex-direction: row;
|
|
|
|
gap: 1ch;
|
|
|
|
font-size: small;
|
|
|
|
}
|
|
|
|
</style>
|
|
|
|
<figure>
|
|
|
|
<div>
|
|
|
|
<canvas>
|
|
|
|
</div>
|
|
|
|
<figcaption></figcaption>
|
|
|
|
</figure>
|
2023-10-04 18:59:46 +00:00
|
|
|
`;
|
2023-10-05 16:38:25 +00:00
|
|
|
this.responsiveStyle = this.shadowRoot.querySelector("#responsive-style");
|
2023-10-04 18:59:46 +00:00
|
|
|
this.canvas = this.shadowRoot.querySelector("canvas");
|
2023-10-05 16:38:25 +00:00
|
|
|
this.caption = this.shadowRoot.querySelector("figcaption");
|
2023-10-04 18:59:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
set data (data) {
|
2023-10-05 16:38:25 +00:00
|
|
|
if (data.chart?.options?.plugins?.title.display) {
|
|
|
|
data.chart.options.plugins.title.display = false;
|
|
|
|
for (let line of data.chart.options.plugins.title.text) {
|
|
|
|
this.caption.innerHTML += `<span>${line}</span>`
|
|
|
|
}
|
|
|
|
}
|
2023-10-04 18:59:46 +00:00
|
|
|
this.canvas.role = "img";
|
|
|
|
this.canvas.ariaLabel = data.ariaLabel;
|
|
|
|
createChart(this.canvas, data.chart);
|
2023-10-05 16:38:25 +00:00
|
|
|
|
|
|
|
|
2023-10-04 18:59:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
get data () {
|
|
|
|
return null;
|
|
|
|
}
|
2023-10-05 16:38:25 +00:00
|
|
|
|
|
|
|
set responsive (breakpoint) {
|
|
|
|
this.breakpoint = breakpoint;
|
|
|
|
if (breakpoint) {
|
|
|
|
this.responsiveStyle.media = `screen and (width <= ${breakpoint})`;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
this.responsiveStyle.media = "not all";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
get responsive () {
|
|
|
|
return this.breakpoint;
|
|
|
|
}
|
2023-10-04 18:59:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function createChart (ctx, data) {
|
|
|
|
return new Chart(ctx, data);
|
|
|
|
}
|
|
|
|
|
|
|
|
customElements.define("custom-chart", CustomChart);
|