|
1 | 1 | <script lang="ts"> |
2 | | - import { run } from 'svelte/legacy'; |
3 | | -
|
4 | 2 | import { browser } from '$app/environment'; |
5 | 3 | import { onMount } from 'svelte'; |
6 | 4 | import { commas } from '$lib/utils.ts'; |
7 | 5 | import { fade } from 'svelte/transition'; |
8 | 6 | import { getTimePreference } from '$lib/prefUtils.ts'; |
9 | 7 | import type { StockCounts } from '$lib/lttstore/lttstore_types.ts'; |
10 | 8 | import { typed } from '$lib'; |
| 9 | + import UplotSvelte from 'uplot-svelte'; |
| 10 | + import uPlot from "uplot"; |
| 11 | + import 'uplot/dist/uPlot.min.css'; |
| 12 | + import { stockGaps, timeFormat, stockColors } from './ProductStockHistoryGraph.svelte'; |
11 | 13 |
|
12 | 14 | let { |
13 | 15 | productName = typed<string | undefined>(), |
14 | | - stockHistory = typed< |
| 16 | + stockHistory: stockHistoryRaw = typed< |
15 | 17 | { |
16 | 18 | handle: string; |
17 | 19 | id: number; |
|
22 | 24 | chartUpdateNumber = typed<number>(1) |
23 | 25 | } = $props(); |
24 | 26 |
|
25 | | - let chart = $state(); |
| 27 | + let stockHistory: { |
| 28 | + handle: string; |
| 29 | + id: number; |
| 30 | + timestamp: number; |
| 31 | + stock: StockCounts; |
| 32 | + }[] = $derived(stockHistoryRaw |
| 33 | + .map(h => ({ |
| 34 | + ...h, |
| 35 | + stock: JSON.parse(h.stock) |
| 36 | + })) |
| 37 | + .toSorted((a, b) => a.timestamp - b.timestamp) |
| 38 | + ); |
| 39 | +
|
| 40 | + let someStock = $derived( |
| 41 | + Object.keys(stockHistory).length >= 1 |
| 42 | + ? stockHistory |
| 43 | + .map((h) => h.stock) |
| 44 | + .reduce((p, c) => { |
| 45 | + return { |
| 46 | + ...c, |
| 47 | + ...p |
| 48 | + }; |
| 49 | + }, {}) |
| 50 | + : {} |
| 51 | + ); |
26 | 52 |
|
27 | 53 | let onlyTotalCheck = $state(false); |
| 54 | + // show only the total for items where the stock is just the default + the total |
| 55 | + let onlyTotal = $derived(Object.keys(someStock).length <= 2 || onlyTotalCheck); |
| 56 | +
|
| 57 | + let data = $derived.by(() => { |
| 58 | + const allTimestamps = new Set<number>(stockHistory.map(h => h.timestamp)); |
| 59 | + return [ |
| 60 | + [...allTimestamps].map(t => Math.round(t/1e3)), // uPlot expects epoch in seconds |
| 61 | + ...(onlyTotal ? ["total"] : Object.keys(someStock)) |
| 62 | + .map(k => stockHistory.map((h, i, a) => { |
| 63 | + if(i > 0) { |
| 64 | + const previous = a[i-1]; |
| 65 | + const previousStock = previous.stock[k] |
| 66 | + const currentStock = h.stock[k]; |
| 67 | + if(previousStock === null || currentStock === null) return null; |
| 68 | + const timeDiff = h.timestamp - previous.timestamp; |
| 69 | + const stockDiff = previousStock - currentStock; |
| 70 | + const rate = stockDiff / (timeDiff / (60 * 60e3)); |
| 71 | + if(rate < 0) return null; |
| 72 | + return rate; |
| 73 | + } else { |
| 74 | + return null; |
| 75 | + } |
| 76 | + })) |
| 77 | + ] |
| 78 | + }); |
28 | 79 |
|
29 | | - function getSeries() { |
30 | | - if(!onlyTotal) { |
31 | | - return Object.keys(someStock).map(k => { |
32 | | - return { |
33 | | - name: k, |
34 | | - data: stockHistory.map((h, i, a) => { |
35 | | - if(i > 0) { |
36 | | - const previous = a[i-1]; |
37 | | - const previousStock = JSON.parse(previous.stock)[k] |
38 | | - const timeDiff = h.timestamp - previous.timestamp; |
39 | | - const stockDiff = previousStock - JSON.parse(h.stock)[k] |
40 | | - return { |
41 | | - x: h.timestamp, |
42 | | - y: stockDiff / (timeDiff / (60 * 60e3)) |
43 | | - } |
44 | | - } else { |
45 | | - return { |
46 | | - x: h.timestamp, |
47 | | - y: -1 |
48 | | - } |
49 | | - } |
50 | | - }).filter(h => h.y >= 0) |
51 | | - } |
52 | | - }) |
53 | | - } else { |
54 | | - return [{ |
55 | | - name: "total", |
56 | | - data: stockHistory.map((h, i, a) => { |
57 | | - if(i > 0) { |
58 | | - const previous = a[i-1]; |
59 | | - const previousStock = JSON.parse(previous.stock)["total"] |
60 | | - const timeDiff = h.timestamp - previous.timestamp; |
61 | | - const stockDiff = previousStock - JSON.parse(h.stock)["total"] |
62 | | - return { |
63 | | - x: h.timestamp, |
64 | | - y: stockDiff / (timeDiff / (60 * 60e3)) |
65 | | - } |
66 | | - } else { |
67 | | - return { |
68 | | - x: h.timestamp, |
69 | | - y: -1 |
70 | | - } |
71 | | - } |
72 | | - }).filter(h => h.y >= 0) |
73 | | - }] |
74 | | - } |
75 | | - } |
76 | 80 |
|
77 | | - const options = $state({ |
78 | | - chart: { |
| 81 | + const options: uPlot.Options = $derived({ |
| 82 | + title: productName ? 'Move Rate History - ' + productName : 'Move Rate History', |
| 83 | + id: productName + "-move-rate-" + chartUpdateNumber, |
| 84 | + height: browser ? document.documentElement.clientHeight / 1.5 : 740, |
| 85 | + width: browser ? document.documentElement.clientWidth / 1.3 : 1490, |
| 86 | + series: [ |
| 87 | + { |
| 88 | + label: "Time", |
| 89 | + // value: timeFormat, |
| 90 | + }, |
| 91 | + ...Object.keys(someStock).map((k, i) => ({ |
| 92 | + show: true, |
| 93 | + gaps: stockGaps, |
| 94 | + label: k, |
| 95 | + value: (_, rawValue: number | null) => rawValue === null ? "" : commas(Math.round(rawValue))!, |
| 96 | + stroke: stockColors[i % stockColors.length], |
| 97 | + points: { |
| 98 | + show: false |
| 99 | + } |
| 100 | + }) satisfies uPlot.Series) |
| 101 | + ], |
| 102 | + axes: [ |
| 103 | + { |
| 104 | + stroke: "rgba(255, 255, 255, 0.5)", |
| 105 | + grid: {stroke: "rgba(255, 255, 255, 0.025)"}, |
| 106 | + // values: ((self, splits) => splits.map(s => new Date(s))) satisfies uPlot.Axis.Values |
| 107 | +
|
| 108 | + }, |
| 109 | + { |
| 110 | + stroke: "rgba(255, 255, 255, 0.5)", |
| 111 | + grid: {stroke: "rgba(255, 255, 255, 0.025)"}, |
| 112 | + } |
| 113 | + ] |
| 114 | + /*chart: { |
79 | 115 | type: 'area', |
80 | 116 | stacked: false, |
81 | 117 | height: browser ? document.documentElement.clientHeight / 1.5 : undefined, |
|
91 | 127 | enabled: false |
92 | 128 | } |
93 | 129 | }, |
94 | | - series: {}, |
95 | 130 | dataLabels: { |
96 | 131 | enabled: false |
97 | 132 | }, |
98 | 133 | markers: { |
99 | 134 | size: 0 |
100 | 135 | }, |
101 | | - title: { |
102 | | - text: productName ? 'Move Rate History - ' + productName : 'Move Rate History', |
103 | | - align: 'left' |
104 | | - }, |
105 | 136 | fill: { |
106 | 137 | type: 'gradient', |
107 | 138 | gradient: { |
|
151 | 182 | }, |
152 | 183 | grid: { |
153 | 184 | borderColor: '#535A6C' |
154 | | - } |
| 185 | + }*/ |
155 | 186 | }); |
156 | 187 |
|
157 | | - let chartDiv: HTMLDivElement = $state(); |
158 | | -
|
159 | | - let ApexCharts; |
160 | 188 | let mounted = $state(false); |
161 | 189 | onMount(async () => { |
162 | | - options.series = getSeries(); |
163 | | - mounted = true; |
164 | | - ApexCharts = (await import('apexcharts')).default; |
165 | | - chart = new ApexCharts(chartDiv, options); |
166 | | - chart.render(); |
167 | | -
|
168 | | - // console.log({options}) |
169 | | - }); |
170 | | -
|
171 | | - // let style = browser ? : undefined; |
172 | | - let someStock = $derived( |
173 | | - Object.keys(stockHistory).length >= 1 |
174 | | - ? stockHistory |
175 | | - .map((h) => JSON.parse(h.stock ?? '{}') as StockCounts) |
176 | | - .reduce((p, c) => { |
177 | | - return { |
178 | | - ...c, |
179 | | - ...p |
180 | | - }; |
181 | | - }, {}) |
182 | | - : {} |
183 | | - ); |
184 | | - // show only the total for items where the stock is just the default + the total |
185 | | - let onlyTotal = $derived(Object.keys(someStock).length <= 2 || onlyTotalCheck); |
186 | | - run(() => { |
187 | | - console.debug({ onlyTotal, length: Object.keys(someStock).length, someStock, stockHistory }); |
188 | | - }); |
189 | | - run(() => { |
190 | | - onlyTotal; |
191 | | - chartUpdateNumber; |
192 | | - options.series = getSeries(); |
193 | | - // console.debug("Series:", options.series) |
194 | | - if (chart) chart.updateSeries(options.series); |
| 190 | + setTimeout(() => mounted = true, 1); |
195 | 191 | }); |
196 | 192 | </script> |
197 | 193 |
|
198 | 194 | <div style="min-height: 69vh"> |
199 | 195 | {#if mounted} |
200 | | - <div bind:this={chartDiv} in:fade></div> |
| 196 | + <div in:fade> |
| 197 | + <UplotSvelte {data} {options} onCreate={() => {}} onDelete={() => {}}/> |
| 198 | + </div> |
201 | 199 | {/if} |
202 | 200 | </div> |
203 | 201 | {#if Object.keys(someStock).length > 2} |
|
0 commit comments