-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathPlotlyWidget.vue
61 lines (52 loc) · 1.22 KB
/
PlotlyWidget.vue
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
<script setup lang="ts">
import { newPlot, purge, relayout, type Layout } from "plotly.js-dist-min";
import { onBeforeUnmount, onMounted, ref } from "vue";
const props = defineProps<{
spec: { data: any; layout: any };
}>();
const container = ref<HTMLDivElement>();
function makeLayout(): Partial<Layout> {
return {
...props.spec.layout,
width: container.value?.clientWidth,
height: container.value?.clientHeight,
font: {
family: "GeistMono, monospace",
},
};
}
const resizeObserver = new ResizeObserver(() => {
if (container.value) {
relayout(container.value, makeLayout());
}
});
onMounted(() => {
if (container.value) {
resizeObserver.observe(container.value);
newPlot(container.value, props.spec.data, makeLayout());
}
});
onBeforeUnmount(() => {
if (container.value) {
resizeObserver.unobserve(container.value);
purge(container.value);
}
});
</script>
<template>
<div class="plotly-widget" ref="container"></div>
</template>
<style scoped>
.plotly-widget {
width: 100%;
}
/*
plotly "dark mode" fix
https://github.com/plotly/plotly.js/issues/2006
*/
@media (prefers-color-scheme: dark) {
.plotly-widget {
filter: invert(75%) hue-rotate(180deg);
}
}
</style>