|
17 | 17 | // https://svelte.dev/repl/09711e43a1264ba18945d7db7cab9335?version=3.38.2
|
18 | 18 | // https://codepen.io/simeydotme/pen/rrOEmO/
|
19 | 19 |
|
20 |
| - import { getContext, tick } from 'svelte'; |
| 20 | + import { tick } from 'svelte'; |
21 | 21 | import type { SVGAttributes } from 'svelte/elements';
|
22 | 22 | import type { spring as springStore, tweened as tweenedStore } from 'svelte/motion';
|
23 | 23 | import { arc as d3arc } from 'd3-shape';
|
24 | 24 | import { scaleLinear } from 'd3-scale';
|
25 | 25 | import { min, max } from 'd3-array';
|
26 | 26 |
|
| 27 | + import { chartContext } from './ChartContext.svelte'; |
27 | 28 | import { motionStore } from '$lib/stores/motionStore.js';
|
28 | 29 | import { degreesToRadians } from '$lib/utils/math.js';
|
29 | 30 |
|
|
80 | 81 |
|
81 | 82 | export let track: boolean | SVGAttributes<SVGPathElement> = false;
|
82 | 83 |
|
83 |
| - const { yRange } = getContext('LayerCake'); |
| 84 | + const { yRange } = chartContext(); |
84 | 85 |
|
85 | 86 | $: scale = scaleLinear().domain(domain).range(range);
|
86 | 87 |
|
|
102 | 103 | }
|
103 | 104 | }
|
104 | 105 |
|
105 |
| - $: _outerRadius = getOuterRadius(outerRadius, max($yRange) / 2); |
| 106 | + $: _outerRadius = getOuterRadius(outerRadius, (max<number>($yRange) ?? 0) / 2); |
106 | 107 |
|
107 | 108 | function getInnerRadius(innerRadius: number | undefined, outerRadius: number) {
|
108 | 109 | if (innerRadius == null) {
|
|
129 | 130 | .startAngle(startAngle ?? degreesToRadians(range[0]))
|
130 | 131 | .endAngle(endAngle ?? degreesToRadians(scale($tweened_value)))
|
131 | 132 | .cornerRadius(cornerRadius)
|
132 |
| - .padAngle(padAngle); |
| 133 | + .padAngle(padAngle) as Function; |
133 | 134 | // .padRadius(padRadius);
|
134 | 135 |
|
135 | 136 | $: trackArc = d3arc()
|
|
138 | 139 | .startAngle(startAngle ?? degreesToRadians(range[0]))
|
139 | 140 | .endAngle(endAngle ?? degreesToRadians(range[1]))
|
140 | 141 | .cornerRadius(cornerRadius)
|
141 |
| - .padAngle(padAngle); |
| 142 | + .padAngle(padAngle) as Function; |
142 | 143 | // .padRadius(padRadius);
|
143 | 144 |
|
| 145 | + // @ts-expect-error |
144 | 146 | $: trackArcCentroid = trackArc.centroid();
|
145 | 147 | // $: console.log(trackArcCentroid)
|
146 | 148 |
|
147 |
| - let trackArcEl; |
148 |
| - $: boundingBox = trackArc && trackArcEl ? trackArcEl.getBBox() : {}; |
149 |
| - // $: console.log(boundingBox) |
| 149 | + let trackArcEl: SVGPathElement | undefined = undefined; |
| 150 | + $: boundingBox = trackArcEl ? trackArcEl.getBBox() : ({} as DOMRect); |
150 | 151 |
|
151 |
| - $: labelArcCenterOffset = { |
152 |
| - x: outerRadius - boundingBox.width / 2, |
153 |
| - // x: 0, |
154 |
| - y: (outerRadius - boundingBox.height / 2) * -1, |
155 |
| - }; |
| 152 | + // $: labelArcCenterOffset = { |
| 153 | + // x: outerRadius - boundingBox.width / 2, |
| 154 | + // // x: 0, |
| 155 | + // y: (outerRadius - boundingBox.height / 2) * -1, |
| 156 | + // }; |
156 | 157 | // $: console.log(labelArcCenterOffset)
|
157 | 158 |
|
158 |
| - $: labelArcBottomOffset = { |
159 |
| - // x: outerRadius - boundingBox.width / 2, |
160 |
| - x: outerRadius - boundingBox.width / 2, |
161 |
| - // x: 0, |
162 |
| - // y: (outerRadius - boundingBox.height) * -1 |
163 |
| - y: (outerRadius - boundingBox.height) * -1, |
164 |
| - }; |
| 159 | + // $: labelArcBottomOffset = { |
| 160 | + // // x: outerRadius - boundingBox.width / 2, |
| 161 | + // x: outerRadius - boundingBox.width / 2, |
| 162 | + // // x: 0, |
| 163 | + // // y: (outerRadius - boundingBox.height) * -1 |
| 164 | + // y: (outerRadius - boundingBox.height) * -1, |
| 165 | + // }; |
165 | 166 | // $: console.log(labelArcBottomOffset)
|
166 | 167 |
|
167 | 168 | /**
|
168 | 169 | * Offset arc from center
|
169 | 170 | */
|
170 | 171 | export let offset = 0;
|
171 |
| - $: angle = (startAngle + endAngle) / 2; |
| 172 | + $: angle = ((startAngle ?? 0) + (endAngle ?? 0)) / 2; |
172 | 173 | $: xOffset = Math.sin(angle) * offset;
|
173 | 174 | $: yOffset = -Math.cos(angle) * offset;
|
174 | 175 | </script>
|
175 | 176 |
|
176 | 177 | {#if track}
|
177 |
| - <path d={trackArc()} class="track" bind:this={trackArcEl} {...track} /> |
| 178 | + <path |
| 179 | + d={trackArc()} |
| 180 | + class="track" |
| 181 | + bind:this={trackArcEl} |
| 182 | + {...typeof track === 'object' ? track : null} |
| 183 | + /> |
178 | 184 | {/if}
|
179 | 185 |
|
| 186 | +<!-- svelte-ignore a11y-no-static-element-interactions --> |
180 | 187 | <path
|
181 | 188 | d={arc()}
|
182 | 189 | transform="translate({xOffset}, {yOffset})"
|
|
0 commit comments