|
|
@@ -2,163 +2,144 @@
|
|
|
<canvas
|
|
|
class="triangle-canvas"
|
|
|
:style="{ width: canvasWidth + 'px', height: canvasHeight + 'px' }"
|
|
|
- @click="onCanvasReady"
|
|
|
></canvas>
|
|
|
</template>
|
|
|
|
|
|
<script setup lang="ts">
|
|
|
import { ref, onMounted, watch, nextTick } from 'vue'
|
|
|
+import type { TriangleResult } from '@/utils/calculator'
|
|
|
|
|
|
const props = defineProps<{
|
|
|
- chartData: Record<string, number>
|
|
|
+ chartData: TriangleResult
|
|
|
}>()
|
|
|
|
|
|
const canvasWidth = ref(320)
|
|
|
const canvasHeight = ref(400)
|
|
|
|
|
|
-// Position definitions for Classic A 5-row layout
|
|
|
-interface Position {
|
|
|
- key: string
|
|
|
- label: string
|
|
|
- x: number // percentage 0-100
|
|
|
- y: number // percentage 0-100
|
|
|
- isPrimary: boolean
|
|
|
+const ZONE_COLORS: Record<string, string> = {
|
|
|
+ left: 'rgba(59, 130, 246, 0.08)',
|
|
|
+ mid: 'rgba(16, 185, 129, 0.08)',
|
|
|
+ right: 'rgba(245, 158, 11, 0.08)',
|
|
|
+ father: 'rgba(139, 92, 246, 0.08)',
|
|
|
+ mother: 'rgba(236, 72, 153, 0.08)',
|
|
|
+ main: 'rgba(212, 168, 71, 0.15)'
|
|
|
}
|
|
|
|
|
|
-const positions: Position[] = [
|
|
|
- { key: 'A', label: 'A', x: 10, y: 5, isPrimary: true },
|
|
|
- { key: 'B', label: 'B', x: 30, y: 5, isPrimary: false },
|
|
|
- { key: 'C', label: 'C', x: 50, y: 5, isPrimary: false },
|
|
|
- { key: 'D', label: 'D', x: 70, y: 5, isPrimary: false },
|
|
|
- { key: 'E', label: 'E', x: 90, y: 5, isPrimary: true },
|
|
|
- { key: 'F', label: 'F', x: 10, y: 25, isPrimary: true },
|
|
|
- { key: 'G', label: 'G', x: 30, y: 25, isPrimary: false },
|
|
|
- { key: 'N', label: 'N', x: 50, y: 25, isPrimary: false },
|
|
|
- { key: 'H', label: 'H', x: 70, y: 25, isPrimary: false },
|
|
|
- { key: 'I', label: 'I', x: 90, y: 25, isPrimary: false },
|
|
|
- { key: 'J', label: 'J', x: 20, y: 45, isPrimary: false },
|
|
|
- { key: 'M', label: 'M', x: 50, y: 45, isPrimary: false },
|
|
|
- { key: 'L', label: 'L', x: 80, y: 45, isPrimary: false },
|
|
|
- { key: 'I2', label: 'I', x: 35, y: 65, isPrimary: false },
|
|
|
- { key: 'K', label: 'K', x: 65, y: 65, isPrimary: false },
|
|
|
- { key: 'O', label: 'O', x: 50, y: 85, isPrimary: true },
|
|
|
+function getNumColor(num: number): string {
|
|
|
+ const map: Record<number, string> = {
|
|
|
+ 1: '#3B82F6', 2: '#10B981', 3: '#F59E0B',
|
|
|
+ 4: '#8B5CF6', 5: '#EF4444', 6: '#EC4899',
|
|
|
+ 7: '#6366F1', 8: '#D97706', 9: '#14B8A6',
|
|
|
+ 0: '#9CA3AF'
|
|
|
+ }
|
|
|
+ return map[num] || '#9CA3AF'
|
|
|
+}
|
|
|
+
|
|
|
+type PosDef = { key: string; x: number; y: number; zone: string; label: string }
|
|
|
+const positions: PosDef[] = [
|
|
|
+ { key: 'A', x: 10, y: 5, zone: 'left', label: 'A' },
|
|
|
+ { key: 'B', x: 30, y: 5, zone: 'mid', label: 'B' },
|
|
|
+ { key: 'C', x: 50, y: 5, zone: 'right', label: 'C' },
|
|
|
+ { key: 'D', x: 70, y: 5, zone: 'right', label: 'D' },
|
|
|
+ { key: 'E', x: 90, y: 5, zone: 'right', label: 'E' },
|
|
|
+ { key: 'F', x: 10, y: 30, zone: 'father', label: 'F' },
|
|
|
+ { key: 'G', x: 30, y: 30, zone: 'left', label: 'G' },
|
|
|
+ { key: 'H', x: 70, y: 30, zone: 'right', label: 'H' },
|
|
|
+ { key: 'I', x: 90, y: 30, zone: 'mother', label: 'I' },
|
|
|
+ { key: 'J', x: 10, y: 55, zone: 'father', label: 'J' },
|
|
|
+ { key: 'K', x: 50, y: 55, zone: 'left', label: 'K' },
|
|
|
+ { key: 'L', x: 90, y: 55, zone: 'mother', label: 'L' },
|
|
|
+ { key: 'M', x: 10, y: 75, zone: 'left', label: 'M' },
|
|
|
+ { key: 'N', x: 50, y: 75, zone: 'right', label: 'N' },
|
|
|
+ { key: 'O', x: 50, y: 90, zone: 'main', label: 'O' },
|
|
|
]
|
|
|
|
|
|
-// Line connections (parent-child in calculation)
|
|
|
const lines: [string, string][] = [
|
|
|
['A', 'F'], ['B', 'F'], ['B', 'G'], ['C', 'G'],
|
|
|
- ['C', 'N'], ['D', 'N'], ['D', 'H'], ['E', 'H'],
|
|
|
- ['G', 'J'], ['F', 'J'], ['G', 'M'], ['N', 'M'],
|
|
|
- ['N', 'L'], ['H', 'L'], ['I', 'L'],
|
|
|
- ['J', 'I2'], ['M', 'I2'], ['M', 'K'], ['L', 'K'],
|
|
|
- ['I2', 'O'], ['K', 'O']
|
|
|
+ ['C', 'H'], ['D', 'H'], ['D', 'I'], ['E', 'I'],
|
|
|
+ ['F', 'J'], ['G', 'J'], ['G', 'K'], ['H', 'K'],
|
|
|
+ ['H', 'L'], ['I', 'L'],
|
|
|
+ ['J', 'M'], ['K', 'M'], ['K', 'N'], ['L', 'N'],
|
|
|
+ ['M', 'O'], ['N', 'O']
|
|
|
]
|
|
|
|
|
|
-function getColor(num: number): string {
|
|
|
- if (num === 0) return '#9CA3AF'
|
|
|
- if (num % 2 === 0) return '#10B981'
|
|
|
- return '#3B82F6'
|
|
|
-}
|
|
|
-
|
|
|
-function getPrimaryColor(key: string): string {
|
|
|
- if (['A', 'E', 'F', 'O'].includes(key)) return '#D4A847'
|
|
|
- return getColor(0)
|
|
|
-}
|
|
|
-
|
|
|
-function drawTriangle(ctx: CanvasRenderingContext2D, width: number, height: number) {
|
|
|
- const dpr = uni.getSystemInfoSync().pixelRatio
|
|
|
- ctx.scale(dpr, dpr)
|
|
|
-
|
|
|
+function drawChart(ctx: CanvasRenderingContext2D, w: number, h: number) {
|
|
|
const padX = 20
|
|
|
- const padY = 30
|
|
|
- const drawW = width - padX * 2
|
|
|
- const drawH = height - padY * 2
|
|
|
+ const padY = 25
|
|
|
+ const drawW = w - padX * 2
|
|
|
+ const drawH = h - padY * 2
|
|
|
+
|
|
|
+ ctx.clearRect(0, 0, w, h)
|
|
|
|
|
|
- ctx.clearRect(0, 0, width * dpr, height * dpr)
|
|
|
+ const posMap = new Map(positions.map(p => [
|
|
|
+ p.key,
|
|
|
+ { x: padX + p.x / 100 * drawW, y: padY + p.y / 100 * drawH, zone: p.zone, label: p.label }
|
|
|
+ ]))
|
|
|
|
|
|
- // Draw connection lines
|
|
|
ctx.strokeStyle = '#CBD5E1'
|
|
|
ctx.lineWidth = 1.5
|
|
|
for (const [from, to] of lines) {
|
|
|
- const p1 = positions.find(p => p.key === from)
|
|
|
- const p2 = positions.find(p => p.key === to)
|
|
|
+ const p1 = posMap.get(from); const p2 = posMap.get(to)
|
|
|
if (!p1 || !p2) continue
|
|
|
ctx.beginPath()
|
|
|
- ctx.moveTo(padX + p1.x / 100 * drawW, padY + p1.y / 100 * drawH)
|
|
|
- ctx.lineTo(padX + p2.x / 100 * drawW, padY + p2.y / 100 * drawH)
|
|
|
+ ctx.moveTo(p1.x, p1.y)
|
|
|
+ ctx.lineTo(p2.x, p2.y)
|
|
|
ctx.stroke()
|
|
|
}
|
|
|
|
|
|
- // Draw position circles
|
|
|
- for (const pos of positions) {
|
|
|
- const cx = padX + pos.x / 100 * drawW
|
|
|
- const cy = padY + pos.y / 100 * drawH
|
|
|
- const num = props.chartData[pos.key === 'I2' ? 'I_pos2' : pos.key] ?? 0
|
|
|
- const radius = pos.isPrimary ? 18 : 14
|
|
|
+ for (const [key, pos] of posMap) {
|
|
|
+ const num = (props.chartData as any)[key] ?? null
|
|
|
+ const radius = key === 'O' ? 22 : 16
|
|
|
+ const isMain = key === 'O'
|
|
|
|
|
|
- // Circle background
|
|
|
ctx.beginPath()
|
|
|
- ctx.arc(cx, cy, radius, 0, Math.PI * 2)
|
|
|
- ctx.fillStyle = pos.isPrimary ? '#D4A847' : '#FFFFFF'
|
|
|
+ ctx.arc(pos.x, pos.y, radius, 0, Math.PI * 2)
|
|
|
+ ctx.fillStyle = isMain ? '#D4A847' : '#fff'
|
|
|
ctx.fill()
|
|
|
- ctx.strokeStyle = pos.isPrimary ? '#B8860B' : '#CBD5E1'
|
|
|
- ctx.lineWidth = pos.isPrimary ? 2.5 : 1.5
|
|
|
+ ctx.strokeStyle = isMain ? '#B8860B' : (num !== null ? getNumColor(num) : '#E5E7EB')
|
|
|
+ ctx.lineWidth = isMain ? 3 : 2
|
|
|
ctx.stroke()
|
|
|
|
|
|
- // Number text
|
|
|
- ctx.fillStyle = pos.isPrimary ? '#FFFFFF' : '#1F2937'
|
|
|
- ctx.font = `bold ${pos.isPrimary ? '16' : '14'}px sans-serif`
|
|
|
- ctx.textAlign = 'center'
|
|
|
- ctx.textBaseline = 'middle'
|
|
|
- ctx.fillText(String(num), cx, cy);
|
|
|
+ if (num !== null) {
|
|
|
+ ctx.fillStyle = isMain ? '#fff' : '#1F2937'
|
|
|
+ ctx.font = `bold ${isMain ? '18' : '15'}px sans-serif`
|
|
|
+ ctx.textAlign = 'center'
|
|
|
+ ctx.textBaseline = 'middle'
|
|
|
+ ctx.fillText(String(num), pos.x, pos.y)
|
|
|
+ }
|
|
|
|
|
|
- // Label text below
|
|
|
- ctx.fillStyle = '#6B7280'
|
|
|
+ ctx.fillStyle = '#9CA3AF'
|
|
|
ctx.font = '11px sans-serif'
|
|
|
- ctx.fillText(pos.label, cx, cy + radius + 14)
|
|
|
+ ctx.textAlign = 'center'
|
|
|
+ ctx.textBaseline = 'top'
|
|
|
+ ctx.fillText(pos.label, pos.x, pos.y + radius + 4)
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-onMounted(async () => {
|
|
|
- await nextTick()
|
|
|
+function render() {
|
|
|
const sysInfo = uni.getSystemInfoSync()
|
|
|
canvasWidth.value = Math.min(sysInfo.windowWidth - 32, 360)
|
|
|
- canvasHeight.value = canvasWidth.value * 1.25
|
|
|
-
|
|
|
- const query = uni.createSelectorQuery()
|
|
|
- query.select('.triangle-canvas').fields({ node: true, size: true })
|
|
|
- .exec((res: any) => {
|
|
|
- if (!res[0]) return
|
|
|
- const canvas = res[0].node
|
|
|
- const ctx = canvas.getContext('2d')
|
|
|
- const dpr = uni.getSystemInfoSync().pixelRatio
|
|
|
- canvas.width = canvasWidth.value * dpr
|
|
|
- canvas.height = canvasHeight.value * dpr
|
|
|
- ctx.scale(dpr, dpr)
|
|
|
- drawTriangle(ctx, canvasWidth.value, canvasHeight.value)
|
|
|
- })
|
|
|
-})
|
|
|
-
|
|
|
-watch(() => props.chartData, () => {
|
|
|
+ canvasHeight.value = Math.min(canvasWidth.value * 1.25, 420)
|
|
|
+
|
|
|
nextTick(() => {
|
|
|
const query = uni.createSelectorQuery()
|
|
|
- query.select('.triangle-canvas').fields({ node: true, size: true })
|
|
|
- .exec((res: any) => {
|
|
|
- if (!res[0]) return
|
|
|
- const canvas = res[0].node
|
|
|
- const ctx = canvas.getContext('2d')
|
|
|
- const dpr = uni.getSystemInfoSync().pixelRatio
|
|
|
+ query.select('.triangle-canvas')
|
|
|
+ .fields({ node: true, size: true }, (res: any) => {
|
|
|
+ if (!res?.node) return
|
|
|
+ const canvas = res.node as HTMLCanvasElement
|
|
|
+ const ctx = canvas.getContext('2d') as CanvasRenderingContext2D
|
|
|
+ const dpr = sysInfo.pixelRatio
|
|
|
canvas.width = canvasWidth.value * dpr
|
|
|
canvas.height = canvasHeight.value * dpr
|
|
|
ctx.scale(dpr, dpr)
|
|
|
- drawTriangle(ctx, canvasWidth.value, canvasHeight.value)
|
|
|
+ drawChart(ctx, canvasWidth.value, canvasHeight.value)
|
|
|
})
|
|
|
+ .exec()
|
|
|
})
|
|
|
-}, { deep: true })
|
|
|
-
|
|
|
-function onCanvasReady() {
|
|
|
- // Re-draw on click (sometimes needed for WebView canvas)
|
|
|
}
|
|
|
+
|
|
|
+onMounted(() => render())
|
|
|
+watch(() => props.chartData, () => render(), { deep: true })
|
|
|
</script>
|
|
|
|
|
|
<style scoped>
|
|
|
@@ -168,4 +149,4 @@ function onCanvasReady() {
|
|
|
background: #FFFFFF;
|
|
|
border-radius: 12px;
|
|
|
}
|
|
|
-</style>
|
|
|
+</style>
|