Plot

/views/components/Plot.html (1.5 KB)

 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
{% import "{{{ .Package }}}/app/util" %}

{% func PlotAssets() %}
<script src="/assets/plot/d3.min.js" type="application/javascript" defer="defer"></script>
<script src="/assets/plot/plot.min.js" type="application/javascript" defer="defer"></script>
{% endfunc %}

{% func PlotCall(id string, call string) %}
function createPlot(div) {
function render() {
const plot = {%s call %};
div.innerHTML = "";
div.appendChild(plot);
}

window.addEventListener('resize', render);
render();

return () => {
window.removeEventListener('resize', render);
};
}

document.addEventListener("DOMContentLoaded", () => {
const div = document.querySelector("#{%s id %}");
const cancel = createPlot(div);
});
{% endfunc %}

{% func PlotHorizontalBar(id string, data []util.ValueMap, x string, y string, title string, marginLeft int) %}
<script type="module">
// files: ${d.files}\nsize: ${d.size.toLocaleString()} bytes
const data = {%s= util.ToJSON(data) %};

function horizontalBar(div) {
const height = {%d (len(data) * 24) + 48 %};
const width = div.clientWidth;
const color = { legend: false };
const marginLeft = {%d marginLeft %};
const marks = [Plot.barX(data, { x: "{%s x %}", fill: "{%s y %}", y: "{%s y %}", title: d => `{%s title %}`, marginLeft })];
return Plot.plot({ height, width, color, marks });
}

{%= PlotCall(id, "horizontalBar(div)") %}
</script>
{% endfunc %}