@m3ui-vue/m3ui-vue 1.3.7 → 1.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/API.md +1097 -26
- package/dist/{MButton-D5eJiDza.js → MButton-DQMTIlaV.js} +2 -2
- package/dist/{MButton-D5eJiDza.js.map → MButton-DQMTIlaV.js.map} +1 -1
- package/dist/{MDialog-WqB0jewp.js → MDialog-cdgapGfZ.js} +2 -2
- package/dist/{MDialog-WqB0jewp.js.map → MDialog-cdgapGfZ.js.map} +1 -1
- package/dist/{MListItem-Cf-C2Ru-.js → MListItem-ZkSGIIrb.js} +2 -2
- package/dist/{MListItem-Cf-C2Ru-.js.map → MListItem-ZkSGIIrb.js.map} +1 -1
- package/dist/{MLoadingIndicator-D4GzEZoH.js → MLoadingIndicator-U6_oGx8z.js} +2 -2
- package/dist/{MLoadingIndicator-D4GzEZoH.js.map → MLoadingIndicator-U6_oGx8z.js.map} +1 -1
- package/dist/{MSeekBar-B19xoWE3.js → MSeekBar-DTZVizZg.js} +2 -2
- package/dist/{MSeekBar-B19xoWE3.js.map → MSeekBar-DTZVizZg.js.map} +1 -1
- package/dist/{MSlider-9RoiuebS.js → MSlider-CqJkEYRZ.js} +2 -2
- package/dist/{MSlider-9RoiuebS.js.map → MSlider-CqJkEYRZ.js.map} +1 -1
- package/dist/{MSpinner-BNF8MN-L.js → MSpinner-Btgc-0nb.js} +2 -2
- package/dist/{MSpinner-BNF8MN-L.js.map → MSpinner-Btgc-0nb.js.map} +1 -1
- package/dist/{MTextField-CuG0T3qh.js → MTextField-B6M0gJEO.js} +2 -2
- package/dist/{MTextField-CuG0T3qh.js.map → MTextField-B6M0gJEO.js.map} +1 -1
- package/dist/{MTooltip-Bv7yxngk.js → MTooltip-Dspji4XG.js} +4 -4
- package/dist/{MTooltip-Bv7yxngk.js.map → MTooltip-Dspji4XG.js.map} +1 -1
- package/dist/api.json +1 -1
- package/dist/code-editor.js +1 -1
- package/dist/components/MAudioPlayer.vue.d.ts +2 -2
- package/dist/components/MAvailabilityCalendar.vue.d.ts +83 -0
- package/dist/components/MBadge.vue.d.ts +1 -1
- package/dist/components/MBeforeAfter.vue.d.ts +61 -0
- package/dist/components/MBodyMap.vue.d.ts +70 -0
- package/dist/components/MCandlestickChart.vue.d.ts +82 -0
- package/dist/components/MCertificate.vue.d.ts +76 -0
- package/dist/components/MCreditCardInput.vue.d.ts +75 -0
- package/dist/components/MFlow.vue.d.ts +13 -0
- package/dist/components/MMortgageCalculator.vue.d.ts +83 -0
- package/dist/components/MOdontogram.vue.d.ts +86 -0
- package/dist/components/MOrderBook.vue.d.ts +89 -0
- package/dist/components/MOrderTicket.vue.d.ts +91 -0
- package/dist/components/MPosKeypad.vue.d.ts +84 -0
- package/dist/components/MProductConfigurator.vue.d.ts +136 -0
- package/dist/components/MQuiz.vue.d.ts +102 -0
- package/dist/components/MReceipt.vue.d.ts +93 -0
- package/dist/components/MSeatMap.vue.d.ts +131 -0
- package/dist/components/MSeekBar.vue.d.ts +1 -1
- package/dist/components/MSlider.vue.d.ts +2 -2
- package/dist/components/MSplitter.vue.d.ts +1 -1
- package/dist/components/MTicket.vue.d.ts +83 -0
- package/dist/components/MTour.vue.d.ts +52 -2
- package/dist/components/MVideoPlayer.vue.d.ts +1 -1
- package/dist/components/MVitalsChart.vue.d.ts +73 -0
- package/dist/composables/_printElement.d.ts +9 -0
- package/dist/composables/cardBrand.d.ts +25 -0
- package/dist/composables/flowPaths.d.ts +11 -3
- package/dist/composables/mortgage.d.ts +37 -0
- package/dist/composables/useFlow.d.ts +4 -0
- package/dist/composables/useLocale.d.ts +131 -0
- package/dist/draw.js +2 -2
- package/dist/flow.d.ts +1 -1
- package/dist/flow.js +526 -434
- package/dist/flow.js.map +1 -1
- package/dist/{flowPaths-DzW-aiET.js → flowPaths-CcWOKY4Z.js} +5 -5
- package/dist/flowPaths-CcWOKY4Z.js.map +1 -0
- package/dist/gantt.js +2 -2
- package/dist/index.d.ts +35 -0
- package/dist/locales/index.js +924 -7
- package/dist/locales/index.js.map +1 -1
- package/dist/m3ui-vue.css +1 -1
- package/dist/m3ui.js +8393 -3613
- package/dist/m3ui.js.map +1 -1
- package/dist/map.js +5 -5
- package/dist/media.js +4 -4
- package/dist/pdf.js +4 -4
- package/dist/rich-text-editor.js +4 -4
- package/dist/styles.css +1 -1
- package/dist/{useLocale-D2ucaY-l.js → useLocale-DLKJIRFi.js} +133 -2
- package/dist/useLocale-DLKJIRFi.js.map +1 -0
- package/package.json +1 -1
- package/src/__tests__/MAvailabilityCalendar.test.ts +85 -0
- package/src/__tests__/MBeforeAfter.test.ts +79 -0
- package/src/__tests__/MBodyMap.test.ts +60 -0
- package/src/__tests__/MCandlestickChart.test.ts +66 -0
- package/src/__tests__/MCertificate.test.ts +32 -0
- package/src/__tests__/MCreditCardInput.test.ts +122 -0
- package/src/__tests__/MFlow.test.ts +69 -0
- package/src/__tests__/MMortgageCalculator.test.ts +49 -0
- package/src/__tests__/MOdontogram.test.ts +93 -0
- package/src/__tests__/MOrderBook.test.ts +66 -0
- package/src/__tests__/MOrderTicket.test.ts +68 -0
- package/src/__tests__/MPosKeypad.test.ts +116 -0
- package/src/__tests__/MProductConfigurator.test.ts +73 -0
- package/src/__tests__/MQuiz.test.ts +104 -0
- package/src/__tests__/MReceipt.test.ts +52 -0
- package/src/__tests__/MSeatMap.test.ts +76 -0
- package/src/__tests__/MTicket.test.ts +38 -0
- package/src/__tests__/MTour.test.ts +102 -0
- package/src/__tests__/MVitalsChart.test.ts +49 -0
- package/src/__tests__/mortgage.test.ts +25 -0
- package/src/__tests__/ssr.test.ts +11 -0
- package/src/components/MAvailabilityCalendar.vue +460 -0
- package/src/components/MBeforeAfter.vue +248 -0
- package/src/components/MBodyMap.vue +266 -0
- package/src/components/MCandlestickChart.vue +479 -0
- package/src/components/MCertificate.vue +351 -0
- package/src/components/MCreditCardInput.vue +351 -0
- package/src/components/MFlow.vue +117 -17
- package/src/components/MMortgageCalculator.vue +258 -0
- package/src/components/MOdontogram.vue +462 -0
- package/src/components/MOrderBook.vue +287 -0
- package/src/components/MOrderTicket.vue +217 -0
- package/src/components/MPosKeypad.vue +406 -0
- package/src/components/MProductConfigurator.vue +412 -0
- package/src/components/MQuiz.vue +531 -0
- package/src/components/MReceipt.vue +249 -0
- package/src/components/MSeatMap.vue +431 -0
- package/src/components/MTicket.vue +209 -0
- package/src/components/MTour.vue +145 -27
- package/src/components/MVitalsChart.vue +303 -0
- package/src/composables/_printElement.ts +36 -0
- package/src/composables/cardBrand.ts +104 -0
- package/src/composables/flowPaths.ts +16 -7
- package/src/composables/mortgage.ts +70 -0
- package/src/composables/useFlow.ts +4 -0
- package/src/composables/useLocale.ts +277 -0
- package/src/flow.ts +1 -1
- package/src/index.ts +35 -0
- package/src/locales/de.ts +131 -0
- package/src/locales/es.ts +131 -0
- package/src/locales/fr.ts +131 -0
- package/src/locales/ja.ts +131 -0
- package/src/locales/ko.ts +131 -0
- package/src/locales/pt.ts +131 -0
- package/src/locales/zh.ts +131 -0
- package/dist/flowPaths-DzW-aiET.js.map +0 -1
- package/dist/useLocale-D2ucaY-l.js.map +0 -1
|
@@ -0,0 +1,479 @@
|
|
|
1
|
+
<script lang="ts">
|
|
2
|
+
/** One period of an `MCandlestickChart`. */
|
|
3
|
+
export interface Candle {
|
|
4
|
+
/** Start of the period (Date, ISO string or epoch ms) */
|
|
5
|
+
time: Date | string | number
|
|
6
|
+
open: number
|
|
7
|
+
high: number
|
|
8
|
+
low: number
|
|
9
|
+
close: number
|
|
10
|
+
volume?: number
|
|
11
|
+
}
|
|
12
|
+
</script>
|
|
13
|
+
|
|
14
|
+
<script setup lang="ts">
|
|
15
|
+
/**
|
|
16
|
+
* A candlestick (OHLC) chart for prices: stocks, crypto, commodities, an exchange rate. Drawn in plain SVG with no
|
|
17
|
+
* chart library: candles, volume bars, moving averages, the last-price line and a crosshair with the candle's
|
|
18
|
+
* open / high / low / close. Drag (or swipe) to pan through history, Ctrl + wheel (or a trackpad / two-finger
|
|
19
|
+
* pinch) to zoom; the arrow keys move the crosshair. Appending candles keeps the chart pinned to the latest one
|
|
20
|
+
* while it's in view, so it can be fed live.
|
|
21
|
+
*
|
|
22
|
+
* @slot legend Replaces the O/H/L/C legend. Slot props: `{ candle, change, changePercent }`
|
|
23
|
+
* @event hover The candle under the crosshair, or null when it leaves
|
|
24
|
+
*/
|
|
25
|
+
import { computed, onBeforeUnmount, onMounted, ref, watch } from 'vue'
|
|
26
|
+
import { useLocale } from '../composables/useLocale'
|
|
27
|
+
|
|
28
|
+
const locale = useLocale()
|
|
29
|
+
|
|
30
|
+
const props = withDefaults(
|
|
31
|
+
defineProps<{
|
|
32
|
+
/** Candles, oldest first */
|
|
33
|
+
data: Candle[]
|
|
34
|
+
/** Height in px */
|
|
35
|
+
height?: number
|
|
36
|
+
/** Volume bars under the candles (when the data has volume) */
|
|
37
|
+
volume?: boolean
|
|
38
|
+
/** Periods of simple moving averages to draw over the candles, e.g. [20, 50] */
|
|
39
|
+
movingAverages?: number[]
|
|
40
|
+
/** Candles visible at first (the latest ones) */
|
|
41
|
+
visible?: number
|
|
42
|
+
/** Color of rising candles (any CSS color) */
|
|
43
|
+
upColor?: string
|
|
44
|
+
/** Color of falling candles (any CSS color) */
|
|
45
|
+
downColor?: string
|
|
46
|
+
/** Decimals of prices */
|
|
47
|
+
decimals?: number
|
|
48
|
+
/** ISO 4217 code to format prices as money */
|
|
49
|
+
currency?: string
|
|
50
|
+
/** Draws the dashed line at the last close */
|
|
51
|
+
lastPrice?: boolean
|
|
52
|
+
}>(),
|
|
53
|
+
{
|
|
54
|
+
height: 320,
|
|
55
|
+
volume: true,
|
|
56
|
+
movingAverages: () => [],
|
|
57
|
+
visible: 60,
|
|
58
|
+
upColor: 'var(--color-success)',
|
|
59
|
+
downColor: 'var(--color-error)',
|
|
60
|
+
decimals: 2,
|
|
61
|
+
lastPrice: true,
|
|
62
|
+
},
|
|
63
|
+
)
|
|
64
|
+
|
|
65
|
+
const emit = defineEmits<{ hover: [Candle | null] }>()
|
|
66
|
+
|
|
67
|
+
const AXIS_W = 64
|
|
68
|
+
const AXIS_H = 24
|
|
69
|
+
const RIGHT_PAD = 3 // empty candle slots after the last one
|
|
70
|
+
const MA_COLORS = ['var(--color-tertiary)', 'var(--color-primary)', 'var(--color-secondary)']
|
|
71
|
+
|
|
72
|
+
const rootEl = ref<HTMLElement | null>(null)
|
|
73
|
+
const width = ref(600)
|
|
74
|
+
let ro: ResizeObserver | null = null
|
|
75
|
+
onMounted(() => {
|
|
76
|
+
if (!rootEl.value) return
|
|
77
|
+
width.value = rootEl.value.clientWidth || 600
|
|
78
|
+
if (typeof ResizeObserver !== 'undefined') {
|
|
79
|
+
ro = new ResizeObserver(([e]) => (width.value = e!.contentRect.width || width.value))
|
|
80
|
+
ro.observe(rootEl.value)
|
|
81
|
+
}
|
|
82
|
+
})
|
|
83
|
+
onBeforeUnmount(() => ro?.disconnect())
|
|
84
|
+
|
|
85
|
+
const plotW = computed(() => Math.max(50, width.value - AXIS_W))
|
|
86
|
+
const plotH = computed(() => props.height - AXIS_H)
|
|
87
|
+
const hasVolume = computed(() => props.volume && props.data.some((c) => c.volume !== undefined))
|
|
88
|
+
const priceH = computed(() => (hasVolume.value ? plotH.value * 0.78 : plotH.value))
|
|
89
|
+
const volTop = computed(() => plotH.value * 0.8)
|
|
90
|
+
|
|
91
|
+
// ── visible window: `count` slots ending at `right` (a fractional candle index) ──
|
|
92
|
+
const n = computed(() => props.data.length)
|
|
93
|
+
const count = ref(Math.max(5, props.visible))
|
|
94
|
+
const right = ref(n.value - 0.5 + RIGHT_PAD)
|
|
95
|
+
|
|
96
|
+
const minCount = 5
|
|
97
|
+
const maxCount = computed(() => Math.max(minCount, n.value + RIGHT_PAD * 2))
|
|
98
|
+
|
|
99
|
+
function clampView() {
|
|
100
|
+
count.value = Math.min(maxCount.value, Math.max(minCount, count.value))
|
|
101
|
+
const hi = n.value - 0.5 + RIGHT_PAD
|
|
102
|
+
const lo = Math.min(hi, count.value - 0.5 - RIGHT_PAD)
|
|
103
|
+
right.value = Math.min(hi, Math.max(lo, right.value))
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
// Live data: stay pinned to the latest candle when it was in view
|
|
107
|
+
watch(n, (now, before) => {
|
|
108
|
+
if (right.value >= before - 0.5) right.value += now - before
|
|
109
|
+
clampView()
|
|
110
|
+
})
|
|
111
|
+
|
|
112
|
+
const left = computed(() => right.value - count.value)
|
|
113
|
+
const cw = computed(() => plotW.value / count.value)
|
|
114
|
+
const x = (i: number) => (i + 0.5 - left.value) * cw.value
|
|
115
|
+
|
|
116
|
+
const range = computed(() => {
|
|
117
|
+
const a = Math.max(0, Math.floor(left.value))
|
|
118
|
+
const b = Math.min(n.value - 1, Math.ceil(right.value))
|
|
119
|
+
return [a, b] as const
|
|
120
|
+
})
|
|
121
|
+
const visibleCandles = computed(() => {
|
|
122
|
+
const [a, b] = range.value
|
|
123
|
+
return props.data.slice(a, b + 1).map((c, k) => ({ c, i: a + k }))
|
|
124
|
+
})
|
|
125
|
+
|
|
126
|
+
const yDomain = computed(() => {
|
|
127
|
+
let lo = Infinity
|
|
128
|
+
let hi = -Infinity
|
|
129
|
+
for (const { c } of visibleCandles.value) {
|
|
130
|
+
lo = Math.min(lo, c.low)
|
|
131
|
+
hi = Math.max(hi, c.high)
|
|
132
|
+
}
|
|
133
|
+
if (!isFinite(lo)) return [0, 1] as const
|
|
134
|
+
if (lo === hi) return [lo - 1, hi + 1] as const
|
|
135
|
+
const pad = (hi - lo) * 0.08
|
|
136
|
+
return [lo - pad, hi + pad] as const
|
|
137
|
+
})
|
|
138
|
+
// A band at the top is kept free for the legend
|
|
139
|
+
const TOP = 24
|
|
140
|
+
const y = (p: number) => {
|
|
141
|
+
const [lo, hi] = yDomain.value
|
|
142
|
+
return priceH.value - ((p - lo) / (hi - lo)) * (priceH.value - TOP)
|
|
143
|
+
}
|
|
144
|
+
const maxVol = computed(() => Math.max(1, ...visibleCandles.value.map(({ c }) => c.volume ?? 0)))
|
|
145
|
+
const yVol = (v: number) => plotH.value - (v / maxVol.value) * (plotH.value - volTop.value)
|
|
146
|
+
|
|
147
|
+
// ── shapes, batched into one path per color ──
|
|
148
|
+
const shapes = computed(() => {
|
|
149
|
+
const bodyW = Math.max(1, cw.value * 0.7)
|
|
150
|
+
let up = ''
|
|
151
|
+
let down = ''
|
|
152
|
+
let volUp = ''
|
|
153
|
+
let volDown = ''
|
|
154
|
+
for (const { c, i } of visibleCandles.value) {
|
|
155
|
+
const cx = Math.round(x(i)) + 0.5
|
|
156
|
+
const yo = y(c.open)
|
|
157
|
+
const yc = y(c.close)
|
|
158
|
+
const top = Math.min(yo, yc)
|
|
159
|
+
const h = Math.max(1, Math.abs(yo - yc))
|
|
160
|
+
const wick = `M${cx} ${y(c.high)}V${y(c.low)}`
|
|
161
|
+
const body = `M${cx - bodyW / 2} ${top}h${bodyW}v${h}h${-bodyW}Z`
|
|
162
|
+
if (c.close >= c.open) up += wick + body
|
|
163
|
+
else down += wick + body
|
|
164
|
+
if (hasVolume.value && c.volume !== undefined) {
|
|
165
|
+
const vb = `M${cx - bodyW / 2} ${yVol(c.volume)}H${cx + bodyW / 2}V${plotH.value}H${cx - bodyW / 2}Z`
|
|
166
|
+
if (c.close >= c.open) volUp += vb
|
|
167
|
+
else volDown += vb
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
return { up, down, volUp, volDown }
|
|
171
|
+
})
|
|
172
|
+
|
|
173
|
+
/** Simple moving averages over the whole series, drawn for the visible part */
|
|
174
|
+
const maLines = computed(() =>
|
|
175
|
+
props.movingAverages.map((period, k) => {
|
|
176
|
+
const [a, b] = range.value
|
|
177
|
+
let d = ''
|
|
178
|
+
let sum = 0
|
|
179
|
+
for (let i = 0; i < n.value && i <= b; i++) {
|
|
180
|
+
sum += props.data[i]!.close
|
|
181
|
+
if (i >= period) sum -= props.data[i - period]!.close
|
|
182
|
+
if (i >= period - 1 && i >= a) d += `${d ? 'L' : 'M'}${x(i).toFixed(1)} ${y(sum / period).toFixed(1)}`
|
|
183
|
+
}
|
|
184
|
+
return { period, d, color: MA_COLORS[k % MA_COLORS.length]! }
|
|
185
|
+
}),
|
|
186
|
+
)
|
|
187
|
+
|
|
188
|
+
// ── axes ──
|
|
189
|
+
function niceStep(span: number, target: number) {
|
|
190
|
+
const raw = span / target
|
|
191
|
+
const mag = 10 ** Math.floor(Math.log10(raw))
|
|
192
|
+
const f = raw / mag
|
|
193
|
+
return (f <= 1 ? 1 : f <= 2 ? 2 : f <= 2.5 ? 2.5 : f <= 5 ? 5 : 10) * mag
|
|
194
|
+
}
|
|
195
|
+
const priceTicks = computed(() => {
|
|
196
|
+
const [lo, hi] = yDomain.value
|
|
197
|
+
const step = niceStep(hi - lo, Math.max(2, Math.floor(priceH.value / 50)))
|
|
198
|
+
const out: number[] = []
|
|
199
|
+
// Labels too close to the edges would be cut
|
|
200
|
+
for (let p = Math.ceil(lo / step) * step; p <= hi; p += step) if (y(p) > 8 && y(p) < priceH.value - 4) out.push(p)
|
|
201
|
+
return out
|
|
202
|
+
})
|
|
203
|
+
|
|
204
|
+
const priceFmt = computed(
|
|
205
|
+
() =>
|
|
206
|
+
new Intl.NumberFormat(locale.lang, {
|
|
207
|
+
...(props.currency ? { style: 'currency', currency: props.currency } : {}),
|
|
208
|
+
minimumFractionDigits: props.decimals,
|
|
209
|
+
maximumFractionDigits: props.decimals,
|
|
210
|
+
}),
|
|
211
|
+
)
|
|
212
|
+
const price = (p: number) => priceFmt.value.format(p)
|
|
213
|
+
const compact = (v: number) => new Intl.NumberFormat(locale.lang, { notation: 'compact', maximumFractionDigits: 2 }).format(v)
|
|
214
|
+
|
|
215
|
+
const toMs = (t: Date | string | number) => (t instanceof Date ? t.getTime() : typeof t === 'number' ? t : Date.parse(t))
|
|
216
|
+
|
|
217
|
+
/** Median period length, to pick the time format */
|
|
218
|
+
const period = computed(() => {
|
|
219
|
+
if (n.value < 2) return 86_400_000
|
|
220
|
+
return Math.abs(toMs(props.data[Math.min(n.value - 1, 1)]!.time) - toMs(props.data[0]!.time))
|
|
221
|
+
})
|
|
222
|
+
const timeFmt = computed(() =>
|
|
223
|
+
new Intl.DateTimeFormat(
|
|
224
|
+
locale.lang,
|
|
225
|
+
period.value < 86_400_000 ? { hour: '2-digit', minute: '2-digit' } : { day: 'numeric', month: 'short' },
|
|
226
|
+
),
|
|
227
|
+
)
|
|
228
|
+
const fullTimeFmt = computed(() =>
|
|
229
|
+
new Intl.DateTimeFormat(locale.lang, period.value < 86_400_000 ? { dateStyle: 'medium', timeStyle: 'short' } : { dateStyle: 'medium' }),
|
|
230
|
+
)
|
|
231
|
+
|
|
232
|
+
const timeTicks = computed(() => {
|
|
233
|
+
const every = niceStep(90 / cw.value, 1)
|
|
234
|
+
const step = Math.max(1, Math.round(every))
|
|
235
|
+
const [a, b] = range.value
|
|
236
|
+
const out: { i: number; label: string }[] = []
|
|
237
|
+
for (let i = Math.ceil(a / step) * step; i <= b; i += step) out.push({ i, label: timeFmt.value.format(toMs(props.data[i]!.time)) })
|
|
238
|
+
return out
|
|
239
|
+
})
|
|
240
|
+
|
|
241
|
+
const last = computed(() => props.data[n.value - 1])
|
|
242
|
+
const lastUp = computed(() => {
|
|
243
|
+
const l = last.value
|
|
244
|
+
const prev = props.data[n.value - 2]
|
|
245
|
+
return l ? l.close >= (prev ? prev.close : l.open) : true
|
|
246
|
+
})
|
|
247
|
+
|
|
248
|
+
// ── crosshair ──
|
|
249
|
+
const hoverIdx = ref<number | null>(null)
|
|
250
|
+
const hoverY = ref<number | null>(null)
|
|
251
|
+
const shown = computed(() => (hoverIdx.value !== null ? props.data[hoverIdx.value] : last.value))
|
|
252
|
+
const shownIdx = computed(() => hoverIdx.value ?? n.value - 1)
|
|
253
|
+
const change = computed(() => {
|
|
254
|
+
const c = shown.value
|
|
255
|
+
if (!c) return 0
|
|
256
|
+
const prev = props.data[shownIdx.value - 1]
|
|
257
|
+
return c.close - (prev ? prev.close : c.open)
|
|
258
|
+
})
|
|
259
|
+
const changePercent = computed(() => {
|
|
260
|
+
const c = shown.value
|
|
261
|
+
if (!c) return 0
|
|
262
|
+
const base = c.close - change.value
|
|
263
|
+
return base ? (change.value / base) * 100 : 0
|
|
264
|
+
})
|
|
265
|
+
|
|
266
|
+
function priceAt(py: number) {
|
|
267
|
+
const [lo, hi] = yDomain.value
|
|
268
|
+
return lo + ((priceH.value - py) / (priceH.value - TOP)) * (hi - lo)
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
function setHover(i: number | null, py: number | null = null) {
|
|
272
|
+
const clamped = i === null ? null : Math.max(0, Math.min(n.value - 1, i))
|
|
273
|
+
if (clamped !== hoverIdx.value) emit('hover', clamped === null ? null : props.data[clamped]!)
|
|
274
|
+
hoverIdx.value = clamped
|
|
275
|
+
hoverY.value = py
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
function local(e: { clientX: number; clientY: number }) {
|
|
279
|
+
const r = rootEl.value!.getBoundingClientRect()
|
|
280
|
+
return { px: e.clientX - r.left, py: e.clientY - r.top }
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
// ── gestures ──
|
|
284
|
+
const pointers = new Map<number, { x: number; y: number }>()
|
|
285
|
+
let drag: { x: number; right: number } | null = null
|
|
286
|
+
let pinch: { dist: number; count: number; center: number } | null = null
|
|
287
|
+
|
|
288
|
+
function onPointerDown(e: PointerEvent) {
|
|
289
|
+
if (e.button > 0) return
|
|
290
|
+
const { px, py } = local(e)
|
|
291
|
+
pointers.set(e.pointerId, { x: px, y: py })
|
|
292
|
+
try { (e.currentTarget as Element).setPointerCapture(e.pointerId) } catch { /* synthetic */ }
|
|
293
|
+
if (pointers.size === 1) {
|
|
294
|
+
drag = { x: px, right: right.value }
|
|
295
|
+
if (e.pointerType !== 'mouse') setHover(Math.round(px / cw.value + left.value - 0.5), py < priceH.value ? py : null)
|
|
296
|
+
} else if (pointers.size === 2) {
|
|
297
|
+
const [a, b] = [...pointers.values()]
|
|
298
|
+
pinch = { dist: Math.abs(a!.x - b!.x) || 1, count: count.value, center: (a!.x + b!.x) / 2 }
|
|
299
|
+
drag = null
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
function onPointerMove(e: PointerEvent) {
|
|
304
|
+
const { px, py } = local(e)
|
|
305
|
+
if (pointers.has(e.pointerId)) pointers.set(e.pointerId, { x: px, y: py })
|
|
306
|
+
if (pinch && pointers.size === 2) {
|
|
307
|
+
const [a, b] = [...pointers.values()]
|
|
308
|
+
zoomAround(pinch.center, pinch.count * (pinch.dist / (Math.abs(a!.x - b!.x) || 1)))
|
|
309
|
+
return
|
|
310
|
+
}
|
|
311
|
+
if (drag && pointers.has(e.pointerId)) {
|
|
312
|
+
const moved = px - drag.x
|
|
313
|
+
if (Math.abs(moved) > 3 || e.pointerType === 'mouse') {
|
|
314
|
+
right.value = drag.right - moved / cw.value
|
|
315
|
+
clampView()
|
|
316
|
+
}
|
|
317
|
+
if (e.pointerType !== 'mouse') return
|
|
318
|
+
}
|
|
319
|
+
if (px > plotW.value || py > plotH.value) return setHover(null)
|
|
320
|
+
setHover(Math.round(px / cw.value + left.value - 0.5), py < priceH.value ? py : null)
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
function onPointerUp(e: PointerEvent) {
|
|
324
|
+
pointers.delete(e.pointerId)
|
|
325
|
+
if (pointers.size < 2) pinch = null
|
|
326
|
+
if (pointers.size === 0) drag = null
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
function zoomAround(px: number, nextCount: number) {
|
|
330
|
+
const anchor = left.value + px / cw.value // candle index under px stays put
|
|
331
|
+
const before = count.value
|
|
332
|
+
count.value = nextCount
|
|
333
|
+
clampView()
|
|
334
|
+
if (count.value === before) return
|
|
335
|
+
right.value = anchor + (plotW.value - px) / (plotW.value / count.value)
|
|
336
|
+
clampView()
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
function onWheel(e: WheelEvent) {
|
|
340
|
+
// Plain vertical wheel scrolls the page; Ctrl/⌘ + wheel (and trackpad pinch) zooms, horizontal wheel pans
|
|
341
|
+
if (e.ctrlKey || e.metaKey) {
|
|
342
|
+
e.preventDefault()
|
|
343
|
+
zoomAround(local(e).px, count.value * Math.exp(e.deltaY * 0.01))
|
|
344
|
+
} else if (Math.abs(e.deltaX) > Math.abs(e.deltaY)) {
|
|
345
|
+
e.preventDefault()
|
|
346
|
+
right.value += e.deltaX / cw.value
|
|
347
|
+
clampView()
|
|
348
|
+
}
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
function onKeydown(e: KeyboardEvent) {
|
|
352
|
+
const cur = hoverIdx.value ?? n.value - 1
|
|
353
|
+
if (e.key === 'ArrowLeft' || e.key === 'ArrowRight') {
|
|
354
|
+
const next = Math.max(0, Math.min(n.value - 1, cur + (e.key === 'ArrowLeft' ? -1 : 1)))
|
|
355
|
+
setHover(next)
|
|
356
|
+
// Scroll the window to keep the crosshair in view
|
|
357
|
+
if (next < left.value + 0.5) right.value = next - 0.5 + count.value
|
|
358
|
+
else if (next > right.value - 0.5) right.value = next + 0.5
|
|
359
|
+
clampView()
|
|
360
|
+
} else if (e.key === '+' || e.key === '=') zoomAround(x(cur), count.value / 1.25)
|
|
361
|
+
else if (e.key === '-') zoomAround(x(cur), count.value * 1.25)
|
|
362
|
+
else if (e.key === 'Home') setHover(0)
|
|
363
|
+
else if (e.key === 'End') setHover(n.value - 1)
|
|
364
|
+
else if (e.key === 'Escape') setHover(null)
|
|
365
|
+
else return
|
|
366
|
+
e.preventDefault()
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
onMounted(() => rootEl.value?.addEventListener('wheel', onWheel, { passive: false }))
|
|
370
|
+
onBeforeUnmount(() => rootEl.value?.removeEventListener('wheel', onWheel))
|
|
371
|
+
|
|
372
|
+
const ariaLabel = computed(() => {
|
|
373
|
+
const c = shown.value
|
|
374
|
+
if (!c) return ''
|
|
375
|
+
return `${fullTimeFmt.value.format(toMs(c.time))}: ${locale.chartOpen} ${price(c.open)}, ${locale.chartHigh} ${price(c.high)}, ${locale.chartLow} ${price(c.low)}, ${locale.chartClose} ${price(c.close)}`
|
|
376
|
+
})
|
|
377
|
+
|
|
378
|
+
defineExpose({
|
|
379
|
+
/** Shows the latest candles again */
|
|
380
|
+
reset() {
|
|
381
|
+
count.value = Math.max(minCount, props.visible)
|
|
382
|
+
right.value = n.value - 0.5 + RIGHT_PAD
|
|
383
|
+
clampView()
|
|
384
|
+
},
|
|
385
|
+
})
|
|
386
|
+
</script>
|
|
387
|
+
|
|
388
|
+
<template>
|
|
389
|
+
<div
|
|
390
|
+
ref="rootEl"
|
|
391
|
+
class="m3-candles relative touch-pan-y select-none text-on-surface outline-none"
|
|
392
|
+
:style="{ height: `${height}px` }"
|
|
393
|
+
tabindex="0"
|
|
394
|
+
role="img"
|
|
395
|
+
:aria-label="ariaLabel"
|
|
396
|
+
@pointerdown="onPointerDown"
|
|
397
|
+
@pointermove="onPointerMove"
|
|
398
|
+
@pointerup="onPointerUp"
|
|
399
|
+
@pointercancel="onPointerUp"
|
|
400
|
+
@pointerleave="(e) => e.pointerType === 'mouse' && setHover(null)"
|
|
401
|
+
@keydown="onKeydown"
|
|
402
|
+
>
|
|
403
|
+
<svg :width="width" :height="height" class="block overflow-hidden" aria-hidden="true">
|
|
404
|
+
<!-- grid -->
|
|
405
|
+
<g class="text-outline-variant" stroke="currentColor" stroke-opacity="0.5">
|
|
406
|
+
<line v-for="p in priceTicks" :key="p" x1="0" :x2="plotW" :y1="Math.round(y(p)) + 0.5" :y2="Math.round(y(p)) + 0.5" />
|
|
407
|
+
</g>
|
|
408
|
+
<line :x1="plotW + 0.5" :x2="plotW + 0.5" y1="0" :y2="plotH" class="text-outline-variant" stroke="currentColor" />
|
|
409
|
+
<line x1="0" :x2="width" :y1="plotH + 0.5" :y2="plotH + 0.5" class="text-outline-variant" stroke="currentColor" />
|
|
410
|
+
|
|
411
|
+
<svg :width="plotW" :height="plotH" class="overflow-hidden">
|
|
412
|
+
<path v-if="shapes.volUp" :d="shapes.volUp" :fill="upColor" fill-opacity="0.3" />
|
|
413
|
+
<path v-if="shapes.volDown" :d="shapes.volDown" :fill="downColor" fill-opacity="0.3" />
|
|
414
|
+
<path :d="shapes.up" :fill="upColor" :stroke="upColor" stroke-width="1" data-candles="up" />
|
|
415
|
+
<path :d="shapes.down" :fill="downColor" :stroke="downColor" stroke-width="1" data-candles="down" />
|
|
416
|
+
<path v-for="m in maLines" :key="m.period" :d="m.d" fill="none" :stroke="m.color" stroke-width="1.5" :data-ma="m.period" />
|
|
417
|
+
<line
|
|
418
|
+
v-if="lastPrice && last"
|
|
419
|
+
x1="0"
|
|
420
|
+
:x2="plotW"
|
|
421
|
+
:y1="y(last.close)"
|
|
422
|
+
:y2="y(last.close)"
|
|
423
|
+
:stroke="lastUp ? upColor : downColor"
|
|
424
|
+
stroke-dasharray="3 3"
|
|
425
|
+
/>
|
|
426
|
+
<!-- crosshair -->
|
|
427
|
+
<g v-if="hoverIdx !== null" class="text-on-surface-variant" stroke="currentColor" stroke-dasharray="4 4" stroke-opacity="0.7">
|
|
428
|
+
<line :x1="x(hoverIdx)" :x2="x(hoverIdx)" y1="0" :y2="plotH" />
|
|
429
|
+
<line v-if="hoverY !== null" x1="0" :x2="plotW" :y1="hoverY" :y2="hoverY" />
|
|
430
|
+
</g>
|
|
431
|
+
</svg>
|
|
432
|
+
|
|
433
|
+
<!-- price axis -->
|
|
434
|
+
<g class="fill-on-surface-variant text-[11px] tabular-nums">
|
|
435
|
+
<text v-for="p in priceTicks" :key="p" :x="plotW + 6" :y="y(p)" dominant-baseline="middle">{{ price(p) }}</text>
|
|
436
|
+
</g>
|
|
437
|
+
<g v-if="lastPrice && last">
|
|
438
|
+
<rect :x="plotW + 1" :y="y(last.close) - 9" :width="AXIS_W - 1" height="18" rx="3" :fill="lastUp ? upColor : downColor" />
|
|
439
|
+
<text :x="plotW + 6" :y="y(last.close)" dominant-baseline="middle" class="fill-white text-[11px] font-medium tabular-nums" data-last-price>{{ price(last.close) }}</text>
|
|
440
|
+
</g>
|
|
441
|
+
<g v-if="hoverY !== null">
|
|
442
|
+
<rect :x="plotW + 1" :y="hoverY - 9" :width="AXIS_W - 1" height="18" rx="3" class="fill-inverse-surface" />
|
|
443
|
+
<text :x="plotW + 6" :y="hoverY" dominant-baseline="middle" class="fill-inverse-on-surface text-[11px] tabular-nums">{{ price(priceAt(hoverY)) }}</text>
|
|
444
|
+
</g>
|
|
445
|
+
|
|
446
|
+
<!-- time axis -->
|
|
447
|
+
<g class="fill-on-surface-variant text-[11px]">
|
|
448
|
+
<text v-for="t in timeTicks" :key="t.i" :x="x(t.i)" :y="plotH + 16" text-anchor="middle">{{ t.label }}</text>
|
|
449
|
+
</g>
|
|
450
|
+
<g v-if="hoverIdx !== null">
|
|
451
|
+
<rect :x="Math.min(plotW - 110, Math.max(0, x(hoverIdx) - 55))" :y="plotH + 2" width="110" height="20" rx="3" class="fill-inverse-surface" />
|
|
452
|
+
<text :x="Math.min(plotW - 55, Math.max(55, x(hoverIdx)))" :y="plotH + 16" text-anchor="middle" class="fill-inverse-on-surface text-[11px]">{{ fullTimeFmt.format(toMs(data[hoverIdx]!.time)) }}</text>
|
|
453
|
+
</g>
|
|
454
|
+
</svg>
|
|
455
|
+
|
|
456
|
+
<!-- legend -->
|
|
457
|
+
<div class="pointer-events-none absolute left-2 top-1 flex flex-wrap items-baseline gap-x-3 text-label-medium tabular-nums" data-legend>
|
|
458
|
+
<slot name="legend" :candle="shown" :change="change" :change-percent="changePercent">
|
|
459
|
+
<template v-if="shown">
|
|
460
|
+
<span><span class="text-on-surface-variant">{{ locale.chartOpen }}</span> {{ price(shown.open) }}</span>
|
|
461
|
+
<span><span class="text-on-surface-variant">{{ locale.chartHigh }}</span> {{ price(shown.high) }}</span>
|
|
462
|
+
<span><span class="text-on-surface-variant">{{ locale.chartLow }}</span> {{ price(shown.low) }}</span>
|
|
463
|
+
<span><span class="text-on-surface-variant">{{ locale.chartClose }}</span> {{ price(shown.close) }}</span>
|
|
464
|
+
<span :style="{ color: change >= 0 ? upColor : downColor }">{{ change >= 0 ? '+' : '' }}{{ changePercent.toFixed(2) }}%</span>
|
|
465
|
+
<span v-if="hasVolume && shown.volume !== undefined"><span class="text-on-surface-variant">{{ locale.chartVolume }}</span> {{ compact(shown.volume) }}</span>
|
|
466
|
+
</template>
|
|
467
|
+
</slot>
|
|
468
|
+
<span v-for="m in maLines" :key="m.period" :style="{ color: m.color }">MA {{ m.period }}</span>
|
|
469
|
+
</div>
|
|
470
|
+
</div>
|
|
471
|
+
</template>
|
|
472
|
+
|
|
473
|
+
<style>
|
|
474
|
+
.m3-candles:focus-visible {
|
|
475
|
+
outline: 3px solid var(--color-secondary);
|
|
476
|
+
outline-offset: 2px;
|
|
477
|
+
border-radius: 4px;
|
|
478
|
+
}
|
|
479
|
+
</style>
|