generated from productdevbookcom/vue-ts-bundle-template
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
App.vue
193 lines (179 loc) · 5.47 KB
/
App.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
<script setup lang="ts">
import { ref } from 'vue'
import type { AlignedData, Cursor, Options, Select, Series, UplotElement } from './components/uplot.vue'
import Uplot from './components/uplot.vue'
const options = ref<Options>({
series: [
{},
{ label: 's1', stroke: 'red' },
{ label: 's2', stroke: 'green' },
{ label: 's3', stroke: 'blue' },
],
})
const noZoomOptions = ref<Options>({
series: [
{},
{ label: 's1', stroke: 'red' },
{ label: 's2', stroke: 'green' },
{ label: 's3', stroke: 'blue' },
],
cursor: {
drag: { setScale: false },
},
})
const data = ref<AlignedData>(
[
[1654575670],
[10],
[10],
[10],
],
)
const zoom = ref<number[] | null[]>([null, null])
let i = 0
const limit = 80
function newData() {
Array.isArray(data.value[0]) && data.value[0].push(data.value[0][i] + 10000)
for (let j = 1; j < options.value.series.length; j++) {
if (!Array.isArray(data.value[j]))
data.value[j] = [] as number[]
(data.value[j] as number[])?.push((data.value[j][i] || 0) + (Math.random() - 0.5) * 10)
}
}
while (i < limit) {
newData()
i++
}
function addSeries() {
// console.log('addSeries')
const newSeries = { label: `s${options.value.series.length}`, stroke: `#${Math.floor(Math.random() * 16777215).toString(16)}` }
options.value.series.push(newSeries)
noZoomOptions.value.series.push(newSeries)
let foo = (Math.random() - 0.5) * 100
// @ts-expect-error don't know how to fix this
data.value.push(data.value[1].map(() => {
foo = foo + (Math.random() - 0.5) * 10
return foo
}))
}
setInterval(() => {
newData()
i++
if (i > limit) {
i = limit
for (let j = 0; j < options.value.series.length; j++)
(data.value[j] as number[])?.shift()
}
}, 1000)
const myseries = ref<Series[]>([])
const cursor = ref<Cursor>()
const select = ref<Select>()
const thePlot = ref(null as unknown as UplotElement)
const showDebug = ref(false)
const noResetScale = ref(false)
</script>
<template>
<div class="container mb-5">
<div class="row">
<div class="col-auto border">
<textarea id="" name="" class="resize">
Change the sizes of the textareaa and the plot to see the responsive behavior.
</textarea>
</div>
<div class="col border">
<Uplot :options="options" :data="data" />
</div>
</div>
<div class="row">
<div class="col-auto border">
<textarea id="" name="" class="resize">
This plot doesn't reset the scale (zoom) when the data changes.
</textarea>
<br>
<button class="btn btn-primary" @click="showDebug = !showDebug">
show debug
</button>
<br>
<button class="btn btn-primary" @click="noResetScale = !noResetScale">
toggle scale reset
</button>
<br>
{{ noResetScale ? 'no scale reset' : 'scale reset' }}
<div> zoom: {{ zoom }}</div>
<button class="btn btn-primary" @click="zoom = [(zoom[0] || 0) + 24 * 60 * 60, (zoom[1] || 0) - 24 * 60 * 60]">
zoom
</button>
</div>
<div class="col border">
<Uplot v-model:zoom="zoom" :options="options" :data="data" :no-reset-scale="noResetScale" :show-debug="showDebug">
<template #header="{ series, toggleShow }">
<div class="d-flex gap-3 flex-wrap">
<div v-for="s in series" :key="s.label" class="legend-item" @click="toggleShow(s)">
<div class="badge badge" :style="{ backgroundColor: s.show ? s.stroke || 'var(--bs-secondary)' : 'lightgrey' }">
{{ s.label }} : {{ s.value || '--' }} {{ s.show }}
</div>
</div>
</div>
</template>
<template #footer>
<span />
</template>
</Uplot>
</div>
</div>
<div class="row">
<div class="col-auto border">
<div v-for="series, index in myseries" :key="index" class="btn position-relative d-block m-1 mb-5" :class="{ 'btn-primary': series.show, 'btn-secondary': !series.show }" @click="thePlot.toggleShow(index)">
{{ series.label }}
<template v-if="series.value">
<div class="position-absolute top-100 end-0 border text-body bg-body text-nowrap p-1 rounded">
{{ series.value }}
</div>
</template>
</div>
</div>
<div class="col border" style="min-height: 400px;">
<Uplot
ref="thePlot"
v-model:series="myseries"
:options="noZoomOptions"
:data="data"
no-footer
@cursor="cursor = $event" @select="select = $event"
/>
</div>
</div>
<div class="row">
<div class="col">
<div>
cursor: {{ cursor }}
</div>
<div>
cursor to value: {{ thePlot?.uplot && thePlot.uplot.posToVal(cursor?.left || 0, 'x') }}
</div>
<div>
select: {{ select }}
</div>
</div>
</div>
<button class="btn btn-primary" @click="addSeries()">
Add series
</button>
<button class="btn btn-primary" @click="options.padding = [Math.random() * 50, Math.random() * 50, Math.random() * 50, Math.random() * 50]">
random padding
</button>
<div class="resize" style="height: 400px;">
<Uplot :options="options" :data="data" />
</div>
</div>
</template>
<style scoped>
.resize{
resize: both;
overflow: auto;
border: 1px red solid;
padding: 1em;
min-width: 200px;
min-height: 200px;
}
</style>