|
9 | 9 | LinearScale, |
10 | 10 | PointElement, |
11 | 11 | } from 'chart.js'; |
12 | | - import { graphInspectorState } from '../3d-inspector/View3DUtility'; |
13 | 12 | import RecordingInspector from '../3d-inspector/RecordingInspector.svelte'; |
14 | | - import { element } from 'svelte/internal'; |
15 | 13 |
|
16 | 14 | export let data: { x: number[]; y: number[]; z: number[] }; |
17 | 15 |
|
|
33 | 31 | } |
34 | 32 | } |
35 | 33 |
|
36 | | - const lengthOfGraphsInPixels = 134; |
37 | | - const ratio = (1 / data.x.length) * lengthOfGraphsInPixels; |
38 | | - // let dataPointInFocusIndex = 0; // 10 to 144 |
39 | | - // let inspectorActive = false; |
40 | | - // let inspectedDataPoint = writable({ x: 0, y: 0, z: 0 }); |
41 | | - let componentElement: HTMLDivElement; |
42 | | - const inspectorMarginInPixels = 5; |
| 34 | + let htmlElement: HTMLDivElement; |
| 35 | + const inspectorMarginPx = 5; |
43 | 36 |
|
44 | 37 |
|
45 | 38 | function recomputeModalParams(){ |
46 | | - const rect = componentElement.getBoundingClientRect() |
| 39 | + const rect = htmlElement.getBoundingClientRect() |
47 | 40 | modalSize = generateSizeOfInspector(rect) |
48 | 41 | modalPosition = generatePositionOfInspector(rect, modalSize) |
49 | 42 | } |
50 | 43 |
|
51 | | - /** |
| 44 | + /** |
52 | 45 | * Positions in regards to size of modal following these rules: |
53 | 46 | * - It prefers center-aligning with element in focus (The recording). Moves into view if circle |
54 | 47 | * escapes the left or right side. |
|
57 | 50 | */ |
58 | 51 | function generatePositionOfInspector(rect: DOMRect, size: number): {x: number, y: number} { |
59 | 52 | const rectCenterX = (rect.left + rect.right) / 2; |
60 | | - let x = 0; |
61 | | - let y = 0; |
62 | | -
|
63 | | - // Calculate x value |
64 | | - if (rectCenterX < size / 2) { |
65 | | - // What the hell is this if structure |
66 | | - } else if (window.innerWidth - rectCenterX < size / 2) |
67 | | - x = window.innerWidth - size; |
68 | | - else x = rectCenterX - size / 2; |
69 | | -
|
70 | | - // Calculate y value |
71 | | - if (window.innerHeight - rect.bottom - 15 < size) { |
72 | | - y = rect.top - inspectorMarginInPixels - size; |
73 | | - } else { |
74 | | - y = rect.bottom + inspectorMarginInPixels; |
75 | | - } |
| 53 | + let x = rectCenterX - size / 2; |
| 54 | +
|
| 55 | + x = Math.max(0, x) |
| 56 | + x = Math.min(x, window.innerWidth - size) |
| 57 | +
|
| 58 | + const showAboveGraph = window.innerHeight - rect.bottom - 15 < size |
| 59 | + const y = showAboveGraph ? rect.top - inspectorMarginPx - size : rect.bottom + inspectorMarginPx; |
76 | 60 |
|
77 | 61 | return { x, y }; |
78 | 62 | } |
79 | 63 |
|
80 | 64 | function generateSizeOfInspector(rect: DOMRect): number { |
81 | | - return (window.innerHeight - rect.height) / 2 - inspectorMarginInPixels; |
| 65 | + return (window.innerHeight - rect.height) / 2 - inspectorMarginPx; |
82 | 66 | } |
83 | 67 |
|
84 | 68 | function getConfig(): ChartConfiguration<keyof ChartTypeRegistry, {x: number, y: number}[], string> { |
|
163 | 147 | } |
164 | 148 | verticalLineX = args.event.x ?? NaN |
165 | 149 | if (args.event.native != null){ |
166 | | - if (isNaN(hoverIndex)){ |
| 150 | + // only recompute modal params if modal is closed (i.e. this event opens the modal) |
| 151 | + if (isNaN(hoverIndex)){ |
167 | 152 | recomputeModalParams() |
168 | 153 | } |
169 | 154 | hoverIndex = chart.getElementsAtEventForMode(args.event.native, 'nearest', {}, true)[0].index |
|
191 | 176 | }; |
192 | 177 | } |
193 | 178 |
|
194 | | - // function computeInspectorPosition |
195 | | -
|
196 | | - // function openInspector(): void { |
197 | | -
|
198 | | - // } |
199 | | -
|
200 | | - // function closeInspector() { |
201 | | -
|
202 | | - // } |
203 | | - // $ : { |
204 | | - // graphInspectorState.update(s => { |
205 | | - // s.isOpen = !isNaN(hoverIndex) |
206 | | - // s.dataPoint = {x: data.x[hoverIndex], y: data.y[hoverIndex], z: data.z[hoverIndex]} |
207 | | - // return s |
208 | | - // }) |
209 | | - // } |
210 | | -
|
211 | | -
|
212 | 179 | let canvas: HTMLCanvasElement; |
213 | 180 | onMount(() => { |
214 | 181 | Chart.register([LinearScale, LineController, PointElement, LineElement]); |
|
221 | 188 | } |
222 | 189 | }); |
223 | 190 |
|
224 | | -
|
225 | 191 | </script> |
226 | 192 |
|
227 | 193 | <div |
228 | | - bind:this={componentElement} |
| 194 | + bind:this={htmlElement} |
229 | 195 | class="h-full w-full relative" |
230 | 196 | > |
231 | 197 | <div class="z-1 h-full w-full absolute"> |
|
0 commit comments