jvs-ppt-ui 0.0.30 → 0.0.32

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.
@@ -1,833 +1,1242 @@
1
- <script setup lang="ts">
2
- import { computed } from 'vue'
3
- import type { SlideElement } from '../composables/useSlides'
4
-
5
- const props = defineProps<{
6
- element: SlideElement
7
- }>()
8
-
9
- const chartType = computed(() => {
10
- let t = (props.element.chartType as string) || 'bar'
11
- if (t === 'doughnutChart' || t === 'ring') t = 'donut'
12
- if (t === 'pieChart' || t === 'pie3DChart') t = 'pie'
13
- if (t === 'barChart' || t === 'bar3DChart') t = 'bar'
14
- if (t === 'lineChart' || t === 'line3DChart') t = 'line'
15
- if (t === 'areaChart' || t === 'area3DChart') t = 'area'
16
- return t
17
- })
18
-
19
- const chartColors = computed(() => {
20
- if (props.element.style?.chartColors && Array.isArray(props.element.style.chartColors) && props.element.style.chartColors.length > 0) {
21
- const valid = props.element.style.chartColors.filter((c: any) => c && typeof c === 'string')
22
- if (valid.length > 0) return valid
23
- }
24
- if (props.element.themeColors && Array.isArray(props.element.themeColors) && props.element.themeColors.length > 0) {
25
- const valid = props.element.themeColors.filter((c: any) => c && typeof c === 'string')
26
- if (valid.length > 0) return valid
27
- }
28
- if ((props.element as any).colors && Array.isArray((props.element as any).colors)) {
29
- const valid = (props.element as any).colors.filter((c: any) => c && typeof c === 'string')
30
- if (valid.length > 0) return valid
31
- }
32
- return ['#4f46e5', '#06b6d4', '#f59e0b', '#10b981', '#ec4899', '#8b5cf6', '#ef4444', '#14b8a6']
33
- })
34
-
35
- const textColor = computed(() => props.element.style?.color || props.element.style?.textColor || props.element.textColor || props.element.labelColor || '#64748b')
36
- const fontFamily = computed(() => props.element.style?.fontFamily || props.element.fontFamily || 'system-ui, sans-serif')
37
- const gridColor = computed(() => props.element.style?.gridColor || props.element.gridColor || '#e2e8f0')
38
-
39
- const showAxes = computed(() => props.element.showAxes !== false)
40
- const showGridlines = computed(() => props.element.showGridlines !== false)
41
- const showLegend = computed(() => {
42
- if (props.element.showLegend !== undefined) return Boolean(props.element.showLegend)
43
- if ((props.element as any).options?.showLegend !== undefined) return Boolean((props.element as any).options.showLegend)
44
- return true
45
- })
46
- const showPercent = computed(() => {
47
- if ((props.element as any).showPercent !== undefined) return Boolean((props.element as any).showPercent)
48
- if ((props.element as any).options?.showPercent !== undefined) return Boolean((props.element as any).options.showPercent)
49
- return true
50
- })
51
- const legendPosition = computed(() => props.element.legendPosition || 'bottom')
52
-
53
- const isLegendTop = computed(() => legendPosition.value.startsWith('top'))
54
- const isLegendBottom = computed(() => legendPosition.value.startsWith('bottom'))
55
- const isLegendLeft = computed(() => legendPosition.value.startsWith('left'))
56
- const isLegendRight = computed(() => legendPosition.value.startsWith('right'))
57
-
58
- const defaultData = {
59
- labels: ['一月', '二月', '三月', '四月', '五月'],
60
- datasets: [
61
- { label: '系列1', data: [30, 45, 55, 60, 80] },
62
- { label: '系列2', data: [20, 30, 40, 45, 65] }
63
- ]
64
- }
65
-
66
- const defaultPieData = {
67
- labels: ['分类A', '分类B', '分类C', '分类D'],
68
- datasets: [
69
- { label: '占比', data: [35, 25, 20, 20] }
70
- ]
71
- }
72
-
73
- const defaultRadarData = {
74
- labels: ['指标A', '指标B', '指标C', '指标D', '指标E'],
75
- datasets: [
76
- { label: '系列1', data: [80, 70, 90, 60, 85] },
77
- { label: '系列2', data: [60, 80, 75, 80, 70] }
78
- ]
79
- }
80
-
81
- const data = computed(() => {
82
- const d = props.element.data
83
- if (!d) {
84
- if (chartType.value === 'pie' || chartType.value === 'donut') return defaultPieData
85
- if (chartType.value === 'radar') return defaultRadarData
86
- return defaultData
87
- }
88
- if (!d.datasets || d.datasets.length === 0) {
89
- if ((d as any).series && Array.isArray((d as any).series)) {
90
- const legends = (d as any).legends || []
91
- const datasets = ((d as any).series as any[]).map((s: any, idx: number) => ({
92
- label: legends[idx] || `系列${idx + 1}`,
93
- data: Array.isArray(s) ? s : [s]
94
- }))
95
- return {
96
- ...d,
97
- datasets
98
- }
99
- }
100
- }
101
- return d
102
- })
103
-
104
- const labels = computed(() => data.value.labels || [])
105
- const datasets = computed(() => data.value.datasets || [])
106
-
107
- // Find max value in all datasets for scaling
108
- const maxVal = computed(() => {
109
- const allNums = datasets.value.flatMap(d => d.data)
110
- const max = Math.max(...allNums, 10)
111
- return Math.ceil(max * 1.15) // 15% padding
112
- })
113
-
114
- // Ticks and Grid lines for Cartesian charts
115
- const gridTicks = computed(() => {
116
- const max = maxVal.value
117
- return [0, 0.25, 0.5, 0.75, 1.0].map(p => ({
118
- val: Math.round(p * max),
119
- pct: p
120
- }))
121
- })
122
-
123
- // Dimensions
124
- const width = computed(() => {
125
- if (typeof props.element.width === 'number' && props.element.width > 0) return props.element.width
126
- const wStr = props.element.style?.width
127
- if (wStr && typeof wStr === 'string' && !wStr.endsWith('%')) {
128
- const val = parseFloat(wStr)
129
- if (!isNaN(val) && val > 0) return val
130
- }
131
- return 400
132
- })
133
- const height = computed(() => {
134
- if (typeof props.element.height === 'number' && props.element.height > 0) return props.element.height
135
- const hStr = props.element.style?.height
136
- if (hStr && typeof hStr === 'string' && !hStr.endsWith('%')) {
137
- const val = parseFloat(hStr)
138
- if (!isNaN(val) && val > 0) return val
139
- }
140
- return 300
141
- })
142
-
143
- // Layout margins for Cartesian charts
144
- const padding = computed(() => {
145
- let top = 40
146
- let bottom = 40
147
- let left = 45
148
- let right = 25
149
-
150
- if (chartType.value === 'horizontal-bar') {
151
- left = 65
152
- right = 35
153
- }
154
-
155
- // Adjust for legend position
156
- if (showLegend.value) {
157
- if (isLegendTop.value) {
158
- top += 25
159
- } else if (isLegendBottom.value) {
160
- bottom += 25
161
- } else if (isLegendLeft.value) {
162
- left += 95
163
- } else if (isLegendRight.value) {
164
- right += 95
165
- }
166
- }
167
-
168
- return { top, right, bottom, left }
169
- })
170
-
171
- const chartWidth = computed(() => width.value - padding.value.left - padding.value.right)
172
- const chartHeight = computed(() => height.value - padding.value.top - padding.value.bottom)
173
-
174
- // Math helpers for circular charts (Pie, Donut, Radar)
175
- const cx = computed(() => {
176
- const baseCx = width.value / 2
177
- if (showLegend.value) {
178
- if (isLegendLeft.value) {
179
- return width.value * 0.58
180
- } else if (isLegendRight.value) {
181
- return width.value * 0.42
182
- }
183
- }
184
- return baseCx
185
- })
186
-
187
- const cy = computed(() => {
188
- const baseCy = showLegend.value ? (height.value * 0.42) : (height.value / 2)
189
- if (showLegend.value) {
190
- if (isLegendTop.value) {
191
- return height.value * 0.52
192
- } else if (isLegendBottom.value) {
193
- return height.value * 0.42
194
- }
195
- }
196
- return baseCy
197
- })
198
-
199
- const radius = computed(() => {
200
- let w = width.value
201
- let h = height.value
202
- if (showLegend.value) {
203
- if (isLegendLeft.value || isLegendRight.value) {
204
- w -= 100
205
- } else if (isLegendTop.value || isLegendBottom.value) {
206
- h -= 40
207
- }
208
- return Math.min(w * 0.4, h * 0.3) || 80
209
- }
210
- return Math.min(w * 0.45, h * 0.45) || 80
211
- })
212
-
213
- const innerRadius = computed(() => {
214
- const hole = parseFloat((props.element as any).holeSize || (props.element as any).options?.holeSize)
215
- const ratio = (!isNaN(hole) && hole > 0 && hole < 100) ? (hole / 100) : 0.72
216
- return radius.value * ratio
217
- })
218
-
219
- // Pie & Donut Slice generator
220
- interface Slice {
221
- d: string
222
- color: string
223
- label: string
224
- value: number
225
- percent: string
226
- labelX: number
227
- labelY: number
228
- }
229
-
230
- const pieSlices = computed<Slice[]>(() => {
231
- const vals = datasets.value[0]?.data || []
232
- const total = vals.reduce((a, b) => a + b, 0) || 1
233
- let startAngle = 0
234
- const isDonut = chartType.value === 'donut'
235
- const innerRadiusVal = innerRadius.value
236
-
237
- return vals.map((val, i) => {
238
- const pct = val / total
239
- const angle = pct * 360
240
- const endAngle = startAngle + angle
241
- const pctText = (pct * 100).toFixed(0) + '%'
242
-
243
- let dPath = ''
244
- const rad1 = (startAngle - 90) * Math.PI / 180
245
- const rad2 = (endAngle - 90) * Math.PI / 180
246
-
247
- if (pct >= 0.999) {
248
- if (isDonut) {
249
- dPath = `M ${cx.value} ${cy.value - radius.value} A ${radius.value} ${radius.value} 0 1 1 ${cx.value - 0.01} ${cy.value - radius.value} ` +
250
- `L ${cx.value - 0.01} ${cy.value - innerRadiusVal} A ${innerRadiusVal} ${innerRadiusVal} 0 1 0 ${cx.value} ${cy.value - innerRadiusVal} Z`
251
- } else {
252
- dPath = `M ${cx.value} ${cy.value - radius.value} A ${radius.value} ${radius.value} 0 1 1 ${cx.value - 0.01} ${cy.value - radius.value} Z`
253
- }
254
- } else {
255
- const xo1 = cx.value + radius.value * Math.cos(rad1)
256
- const yo1 = cy.value + radius.value * Math.sin(rad1)
257
- const xo2 = cx.value + radius.value * Math.cos(rad2)
258
- const yo2 = cy.value + radius.value * Math.sin(rad2)
259
- const largeArc = angle > 180 ? 1 : 0
260
-
261
- if (isDonut) {
262
- const xi1 = cx.value + innerRadiusVal * Math.cos(rad1)
263
- const yi1 = cy.value + innerRadiusVal * Math.sin(rad1)
264
- const xi2 = cx.value + innerRadiusVal * Math.cos(rad2)
265
- const yi2 = cy.value + innerRadiusVal * Math.sin(rad2)
266
- dPath = `M ${xo1} ${yo1} A ${radius.value} ${radius.value} 0 ${largeArc} 1 ${xo2} ${yo2} L ${xi2} ${yi2} A ${innerRadiusVal} ${innerRadiusVal} 0 ${largeArc} 0 ${xi1} ${yi1} Z`
267
- } else {
268
- dPath = `M ${cx.value} ${cy.value} L ${xo1} ${yo1} A ${radius.value} ${radius.value} 0 ${largeArc} 1 ${xo2} ${yo2} Z`
269
- }
270
- }
271
-
272
- const midAngle = (startAngle + endAngle) / 2
273
- const midRad = (midAngle - 90) * Math.PI / 180
274
- const labelR = isDonut ? (radius.value + innerRadiusVal) / 2 : radius.value * 0.65
275
- const lx = cx.value + labelR * Math.cos(midRad)
276
- const ly = cy.value + labelR * Math.sin(midRad)
277
-
278
- const slice: Slice = {
279
- d: dPath,
280
- color: chartColors.value[i % chartColors.value.length],
281
- label: labels.value[i] || `分类${i + 1}`,
282
- value: val,
283
- percent: pctText,
284
- labelX: lx,
285
- labelY: ly,
286
- }
287
- startAngle = endAngle
288
- return slice
289
- })
290
- })
291
-
292
- interface LegendItem {
293
- label: string
294
- color: string
295
- percent?: string
296
- x: number
297
- y: number
298
- }
299
-
300
- const legendItems = computed<LegendItem[]>(() => {
301
- if (!showLegend.value) return []
302
-
303
- let rawItems: { label: string; color: string; percent?: string }[] = []
304
- if (chartType.value === 'pie' || chartType.value === 'donut') {
305
- rawItems = pieSlices.value.slice(0, 4).map(slice => ({
306
- label: slice.label,
307
- color: slice.color,
308
- percent: slice.percent
309
- }))
310
- } else {
311
- rawItems = datasets.value.slice(0, 4).map((ds, j) => ({
312
- label: ds.label,
313
- color: chartColors.value[j % chartColors.value.length]
314
- }))
315
- }
316
-
317
- const pos = legendPosition.value
318
- const W = width.value
319
- const H = height.value
320
-
321
- const itemsCount = rawItems.length
322
-
323
- if (isLegendLeft.value || isLegendRight.value) {
324
- const stepY = 22
325
- const totalHeight = itemsCount * stepY
326
-
327
- let startX = 20
328
- if (isLegendLeft.value) {
329
- startX = 20
330
- } else {
331
- startX = W - 110
332
- }
333
-
334
- let startY = 40
335
- if (pos.endsWith('top')) {
336
- startY = 40
337
- } else if (pos.endsWith('bottom')) {
338
- startY = H - totalHeight - 20
339
- } else {
340
- startY = (H - totalHeight) / 2
341
- }
342
-
343
- return rawItems.map((item, idx) => ({
344
- ...item,
345
- x: startX,
346
- y: startY + idx * stepY
347
- }))
348
- } else {
349
- const stepX = 90
350
- const totalWidth = itemsCount * stepX
351
-
352
- let startY = 20
353
- if (isLegendTop.value) {
354
- startY = 20
355
- } else {
356
- startY = H - 25
357
- }
358
-
359
- let startX = 20
360
- if (pos.endsWith('left')) {
361
- startX = 20
362
- } else if (pos.endsWith('right')) {
363
- startX = W - totalWidth - 20
364
- } else {
365
- startX = (W - totalWidth) / 2
366
- }
367
-
368
- return rawItems.map((item, idx) => ({
369
- ...item,
370
- x: startX + idx * stepX,
371
- y: startY
372
- }))
373
- }
374
- })
375
-
376
- // Line & Area Paths generator
377
- interface LinePath {
378
- dLine: string
379
- dArea: string
380
- color: string
381
- points: { x: number; y: number; val: number }[]
382
- }
383
-
384
- const linePaths = computed<LinePath[]>(() => {
385
- const N = labels.value.length
386
- if (N === 0) return []
387
- const dx = N > 1 ? chartWidth.value / (N - 1) : chartWidth.value
388
- const max = maxVal.value
389
-
390
- return datasets.value.map((ds, j) => {
391
- const points = (ds.data || []).map((val, i) => {
392
- const x = padding.value.left + i * dx
393
- const y = padding.value.top + chartHeight.value - (val / max) * chartHeight.value
394
- return { x, y, val }
395
- })
396
-
397
- // 空数据系列会拼出 dLine="M "、dArea="M L ...",触发浏览器 <path> attribute d 控制台报错;整组跳过
398
- if (points.length === 0) return null
399
-
400
- const linePointsStr = points.map(p => `${p.x.toFixed(1)},${p.y.toFixed(1)}`).join(' L ')
401
- const dLine = `M ${linePointsStr}`
402
-
403
- // Area path closes at bottom of chart area
404
- const startX = points[0]?.x || padding.value.left
405
- const endX = points[points.length - 1]?.x || (padding.value.left + chartWidth.value)
406
- const bottomY = padding.value.top + chartHeight.value
407
- const dArea = `${dLine} L ${endX.toFixed(1)},${bottomY} L ${startX.toFixed(1)},${bottomY} Z`
408
-
409
- return {
410
- dLine,
411
- dArea,
412
- color: chartColors.value[j % chartColors.value.length],
413
- points
414
- }
415
- }).filter((lp): lp is LinePath => lp !== null)
416
- })
417
-
418
- // Radar Grid levels and axis vectors
419
- const radarAxis = computed(() => {
420
- const N = labels.value.length
421
- const angle = 360 / N
422
- return labels.value.map((label, i) => {
423
- const rad = (i * angle - 90) * Math.PI / 180
424
- return {
425
- label,
426
- lineX: cx.value + radius.value * Math.cos(rad),
427
- lineY: cy.value + radius.value * Math.sin(rad),
428
- labelX: cx.value + (radius.value + 18) * Math.cos(rad),
429
- labelY: cy.value + (radius.value + 18) * Math.sin(rad) + 4,
430
- align: Math.cos(rad) > 0.05 ? 'start' : Math.cos(rad) < -0.05 ? 'end' : 'middle'
431
- }
432
- })
433
- })
434
-
435
- const radarLevels = computed(() => {
436
- const N = labels.value.length
437
- const angle = 360 / N
438
- return [1, 2, 3, 4].map(level => {
439
- const r = radius.value * (level / 4)
440
- const points = Array.from({ length: N }).map((_, i) => {
441
- const rad = (i * angle - 90) * Math.PI / 180
442
- const x = cx.value + r * Math.cos(rad)
443
- const y = cy.value + r * Math.sin(rad)
444
- return `${x.toFixed(1)},${y.toFixed(1)}`
445
- }).join(' ')
446
- return points
447
- })
448
- })
449
-
450
- const radarPolygons = computed(() => {
451
- const N = labels.value.length
452
- const angle = 360 / N
453
- const max = maxVal.value
454
-
455
- return datasets.value.map((ds, j) => {
456
- const points = ds.data.map((val, i) => {
457
- const rad = (i * angle - 90) * Math.PI / 180
458
- const r_val = radius.value * (val / max)
459
- const x = cx.value + r_val * Math.cos(rad)
460
- const y = cy.value + r_val * Math.sin(rad)
461
- return `${x.toFixed(1)},${y.toFixed(1)}`
462
- }).join(' ')
463
-
464
- return {
465
- points,
466
- color: chartColors.value[j % chartColors.value.length]
467
- }
468
- })
469
- })
470
- </script>
471
-
472
- <template>
473
- <svg
474
- :viewBox="`0 0 ${width} ${height}`"
475
- :style="{ width: '100%', height: '100%', display: 'block', overflow: 'visible', userSelect: 'none', fontFamily: fontFamily, '--ppt-font-family': fontFamily }"
476
- >
477
- <!-- Background Grid & Ticks (Cartesian charts only) -->
478
- <g v-if="['bar', 'line', 'area', 'scatter'].includes(chartType) && showGridlines">
479
- <!-- Horizontal gridlines -->
480
- <line
481
- v-for="tick in gridTicks"
482
- :key="'grid-' + tick.val"
483
- :x1="padding.left"
484
- :y1="padding.top + chartHeight - tick.pct * chartHeight"
485
- :x2="padding.left + chartWidth"
486
- :y2="padding.top + chartHeight - tick.pct * chartHeight"
487
- :stroke="gridColor"
488
- stroke-width="1"
489
- :stroke-dasharray="tick.val === 0 ? '' : '4 4'"
490
- />
491
- <!-- Value ticks text labels -->
492
- <text
493
- v-for="tick in gridTicks"
494
- :key="'label-' + tick.val"
495
- :x="padding.left - 8"
496
- :y="padding.top + chartHeight - tick.pct * chartHeight + 4"
497
- text-anchor="end"
498
- font-size="10"
499
- font-weight="500"
500
- :fill="textColor"
501
- :font-family="fontFamily"
502
- >
503
- {{ tick.val }}
504
- </text>
505
- </g>
506
-
507
- <!-- 1. Column/Bar Chart -->
508
- <g v-if="chartType === 'bar'">
509
- <g v-for="(label, i) in labels" :key="'col-group-' + i">
510
- <!-- Render X axis label -->
511
- <text v-if="showAxes"
512
- :x="padding.left + i * (chartWidth / labels.length) + (chartWidth / labels.length) / 2"
513
- :y="padding.top + chartHeight + 16"
514
- text-anchor="middle"
515
- font-size="10"
516
- font-weight="500"
517
- :fill="textColor"
518
- :font-family="fontFamily"
519
- >
520
- {{ label }}
521
- </text>
522
-
523
- <!-- Render columns for each series -->
524
- <rect
525
- v-for="(ds, j) in datasets"
526
- :key="'bar-' + i + '-' + j"
527
- :x="padding.left + i * (chartWidth / labels.length) + ((chartWidth / labels.length) * 0.15) + (j * ((chartWidth / labels.length) * 0.7 / datasets.length))"
528
- :y="padding.top + chartHeight - ((ds.data[i] || 0) / maxVal) * chartHeight"
529
- :width="((chartWidth / labels.length) * 0.7 / datasets.length) * 0.9"
530
- :height="((ds.data[i] || 0) / maxVal) * chartHeight"
531
- :fill="chartColors[j % chartColors.length]"
532
- rx="2"
533
- />
534
- </g>
535
- </g>
536
-
537
- <!-- 2. Horizontal Bar Chart -->
538
- <g v-if="chartType === 'horizontal-bar'">
539
- <!-- Grid vertical lines -->
540
- <line
541
- v-for="tick in gridTicks"
542
- :key="'grid-v-' + tick.val"
543
- :x1="padding.left + tick.pct * chartWidth"
544
- :y1="padding.top"
545
- :x2="padding.left + tick.pct * chartWidth"
546
- :y2="padding.top + chartHeight"
547
- :stroke="gridColor"
548
- stroke-width="1"
549
- :stroke-dasharray="tick.val === 0 ? '' : '4 4'"
550
- />
551
- <!-- Value labels on X-axis -->
552
- <text
553
- v-for="tick in gridTicks"
554
- :key="'v-label-' + tick.val"
555
- :x="padding.left + tick.pct * chartWidth"
556
- :y="padding.top + chartHeight + 16"
557
- text-anchor="middle"
558
- font-size="10"
559
- font-weight="500"
560
- :fill="textColor"
561
- :font-family="fontFamily"
562
- >
563
- {{ tick.val }}
564
- </text>
565
-
566
- <g v-for="(label, i) in labels" :key="'row-group-' + i">
567
- <!-- Render Y axis labels -->
568
- <text v-if="showAxes"
569
- :x="padding.left - 8"
570
- :y="padding.top + i * (chartHeight / labels.length) + (chartHeight / labels.length) / 2 + 4"
571
- text-anchor="end"
572
- font-size="10"
573
- font-weight="500"
574
- :fill="textColor"
575
- :font-family="fontFamily"
576
- >
577
- {{ label }}
578
- </text>
579
-
580
- <!-- Render horizontal bars -->
581
- <rect
582
- v-for="(ds, j) in datasets"
583
- :key="'hbar-' + i + '-' + j"
584
- :x="padding.left"
585
- :y="padding.top + i * (chartHeight / labels.length) + ((chartHeight / labels.length) * 0.15) + (j * ((chartHeight / labels.length) * 0.7 / datasets.length))"
586
- :width="((ds.data[i] || 0) / maxVal) * chartWidth"
587
- :height="((chartHeight / labels.length) * 0.7 / datasets.length) * 0.9"
588
- :fill="chartColors[j % chartColors.length]"
589
- rx="2"
590
- />
591
- </g>
592
- </g>
593
-
594
- <!-- 3. Line Chart -->
595
- <g v-if="chartType === 'line'">
596
- <!-- X axis labels -->
597
- <text v-if="showAxes"
598
- v-for="(label, i) in labels"
599
- :key="'line-lbl-' + i"
600
- :x="padding.left + i * (chartWidth / (labels.length - 1 || 1))"
601
- :y="padding.top + chartHeight + 16"
602
- text-anchor="middle"
603
- font-size="10"
604
- font-weight="500"
605
- :fill="textColor"
606
- :font-family="fontFamily"
607
- >
608
- {{ label }}
609
- </text>
610
-
611
- <!-- Line paths and markers -->
612
- <g v-for="(lp, j) in linePaths" :key="'line-p-' + j">
613
- <path
614
- :d="lp.dLine"
615
- fill="none"
616
- :stroke="lp.color"
617
- stroke-width="3"
618
- stroke-linecap="round"
619
- stroke-linejoin="round"
620
- />
621
- <circle
622
- v-for="(pt, i) in lp.points"
623
- :key="'circle-' + i"
624
- :cx="pt.x"
625
- :cy="pt.y"
626
- r="4.5"
627
- fill="#ffffff"
628
- :stroke="lp.color"
629
- stroke-width="2.5"
630
- />
631
- </g>
632
- </g>
633
-
634
- <!-- 4. Area Chart -->
635
- <g v-if="chartType === 'area'">
636
- <!-- X axis labels -->
637
- <text v-if="showAxes"
638
- v-for="(label, i) in labels"
639
- :key="'area-lbl-' + i"
640
- :x="padding.left + i * (chartWidth / (labels.length - 1 || 1))"
641
- :y="padding.top + chartHeight + 16"
642
- text-anchor="middle"
643
- font-size="10"
644
- font-weight="500"
645
- :fill="textColor"
646
- :font-family="fontFamily"
647
- >
648
- {{ label }}
649
- </text>
650
-
651
- <!-- Area fill and line paths -->
652
- <g v-for="(lp, j) in linePaths" :key="'area-p-' + j">
653
- <path
654
- :d="lp.dArea"
655
- :fill="lp.color"
656
- fill-opacity="0.18"
657
- />
658
- <path
659
- :d="lp.dLine"
660
- fill="none"
661
- :stroke="lp.color"
662
- stroke-width="2.5"
663
- stroke-linecap="round"
664
- stroke-linejoin="round"
665
- />
666
- <circle
667
- v-for="(pt, i) in lp.points"
668
- :key="'acircle-' + i"
669
- :cx="pt.x"
670
- :cy="pt.y"
671
- r="3.5"
672
- :fill="lp.color"
673
- />
674
- </g>
675
- </g>
676
-
677
- <!-- 5. Scatter Chart -->
678
- <g v-if="chartType === 'scatter'">
679
- <!-- X axis labels -->
680
- <text v-if="showAxes"
681
- v-for="(label, i) in labels"
682
- :key="'scat-lbl-' + i"
683
- :x="padding.left + i * (chartWidth / (labels.length - 1 || 1))"
684
- :y="padding.top + chartHeight + 16"
685
- text-anchor="middle"
686
- font-size="10"
687
- font-weight="500"
688
- :fill="textColor"
689
- :font-family="fontFamily"
690
- >
691
- {{ label }}
692
- </text>
693
-
694
- <!-- Scatters -->
695
- <g v-for="(lp, j) in linePaths" :key="'scat-g-' + j">
696
- <circle
697
- v-for="(pt, i) in lp.points"
698
- :key="'pt-' + i"
699
- :cx="pt.x"
700
- :cy="pt.y"
701
- r="6.5"
702
- :fill="lp.color"
703
- fill-opacity="0.8"
704
- />
705
- </g>
706
- </g>
707
-
708
- <!-- 6. Pie Chart -->
709
- <g v-if="chartType === 'pie'">
710
- <g v-for="(slice, i) in pieSlices" :key="'pie-slice-g-' + i">
711
- <path
712
- :d="slice.d"
713
- :fill="slice.color"
714
- stroke="#ffffff"
715
- stroke-width="1.5"
716
- />
717
- <text
718
- v-if="showPercent && slice.percent && slice.value > 0"
719
- :x="slice.labelX"
720
- :y="slice.labelY"
721
- text-anchor="middle"
722
- dominant-baseline="central"
723
- fill="#ffffff"
724
- font-size="11"
725
- font-weight="bold"
726
- :font-family="fontFamily"
727
- style="pointer-events: none; user-select: none;"
728
- >
729
- {{ slice.percent }}
730
- </text>
731
- </g>
732
- </g>
733
-
734
- <!-- 7. Donut Chart -->
735
- <g v-if="chartType === 'donut'">
736
- <g v-for="(slice, i) in pieSlices" :key="'donut-slice-g-' + i">
737
- <path
738
- :d="slice.d"
739
- :fill="slice.color"
740
- stroke="#ffffff"
741
- stroke-width="1.5"
742
- />
743
- <text
744
- v-if="showPercent && slice.percent && slice.value > 0"
745
- :x="slice.labelX"
746
- :y="slice.labelY"
747
- text-anchor="middle"
748
- dominant-baseline="central"
749
- fill="#ffffff"
750
- font-size="11"
751
- font-weight="bold"
752
- :font-family="fontFamily"
753
- style="pointer-events: none; user-select: none;"
754
- >
755
- {{ slice.percent }}
756
- </text>
757
- </g>
758
- </g>
759
-
760
- <!-- 8. Radar Chart -->
761
- <g v-if="chartType === 'radar'">
762
- <!-- Concentric grid levels -->
763
- <polygon
764
- v-for="(lvlPoints, lvlIdx) in radarLevels"
765
- :key="'radar-lvl-' + lvlIdx"
766
- :points="lvlPoints"
767
- fill="none"
768
- :stroke="gridColor"
769
- stroke-width="1"
770
- />
771
-
772
- <!-- Radial axis lines -->
773
- <line
774
- v-for="(axis, i) in radarAxis"
775
- :key="'radar-axis-' + i"
776
- :x1="cx"
777
- :y1="cy"
778
- :x2="axis.lineX"
779
- :y2="axis.lineY"
780
- :stroke="gridColor"
781
- stroke-width="1"
782
- />
783
-
784
- <!-- Category Label Texts -->
785
- <text v-if="showAxes"
786
- v-for="(axis, i) in radarAxis"
787
- :key="'radar-txt-' + i"
788
- :x="axis.labelX"
789
- :y="axis.labelY"
790
- :text-anchor="axis.align"
791
- font-size="9"
792
- font-weight="500"
793
- :fill="textColor"
794
- :font-family="fontFamily"
795
- >
796
- {{ axis.label }}
797
- </text>
798
-
799
- <!-- Radar Filled Data Polygons -->
800
- <polygon
801
- v-for="(poly, j) in radarPolygons"
802
- :key="'radar-poly-' + j"
803
- :points="poly.points"
804
- :fill="poly.color"
805
- fill-opacity="0.25"
806
- :stroke="poly.color"
807
- stroke-width="2.5"
808
- stroke-linejoin="round"
809
- />
810
- </g>
811
-
812
- <!-- Unified Chart Legends for all chart types -->
813
- <g v-if="showLegend">
814
- <g
815
- v-for="(item, idx) in legendItems"
816
- :key="'legend-' + idx"
817
- :transform="`translate(${item.x}, ${item.y})`"
818
- >
819
- <rect x="0" y="-8" width="10" height="10" rx="2" :fill="item.color" />
820
- <text
821
- x="16"
822
- y="1"
823
- font-size="10"
824
- font-weight="600"
825
- :fill="textColor"
826
- :font-family="fontFamily"
827
- >
828
- {{ item.label }}{{ item.percent ? ` (${item.percent})` : '' }}
829
- </text>
830
- </g>
831
- </g>
832
- </svg>
833
- </template>
1
+ <script setup lang="ts">
2
+ import { computed } from 'vue'
3
+ import type { SlideElement } from '../composables/useSlides'
4
+
5
+ const props = defineProps<{
6
+ element: SlideElement
7
+ }>()
8
+
9
+ const chartType = computed(() => {
10
+ let t = (props.element.chartType as string) || 'bar'
11
+ if (t === 'doughnutChart' || t === 'ring') t = 'donut'
12
+ if (t === 'pieChart' || t === 'pie3DChart') t = 'pie'
13
+ if (t === 'barChart' || t === 'bar3DChart') t = 'bar'
14
+ if (t === 'lineChart' || t === 'line3DChart') t = 'line'
15
+ if (t === 'areaChart' || t === 'area3DChart') t = 'area'
16
+ return t
17
+ })
18
+
19
+ const isPieFamily = computed(() =>
20
+ ['pie', 'donut', 'rose-pie', 'rose-donut'].includes(chartType.value)
21
+ )
22
+ const isBarFamily = computed(() =>
23
+ ['bar', 'stacked-bar', 'percent-bar'].includes(chartType.value)
24
+ )
25
+ const isHBarFamily = computed(() =>
26
+ ['horizontal-bar', 'stacked-horizontal-bar'].includes(chartType.value)
27
+ )
28
+ const isLineFamily = computed(() =>
29
+ ['line', 'compare-line', 'step-line'].includes(chartType.value)
30
+ )
31
+ const isAreaFamily = computed(() =>
32
+ ['area', 'stacked-area'].includes(chartType.value)
33
+ )
34
+ const isVerticalCartesian = computed(() =>
35
+ isBarFamily.value || isLineFamily.value || isAreaFamily.value || chartType.value === 'scatter'
36
+ )
37
+ const isCartesian = computed(() =>
38
+ isVerticalCartesian.value || isHBarFamily.value
39
+ )
40
+
41
+ const chartColors = computed(() => {
42
+ if (props.element.style?.chartColors && Array.isArray(props.element.style.chartColors) && props.element.style.chartColors.length > 0) {
43
+ const valid = props.element.style.chartColors.filter((c: any) => c && typeof c === 'string')
44
+ if (valid.length > 0) return valid
45
+ }
46
+ if (props.element.themeColors && Array.isArray(props.element.themeColors) && props.element.themeColors.length > 0) {
47
+ const valid = props.element.themeColors.filter((c: any) => c && typeof c === 'string')
48
+ if (valid.length > 0) return valid
49
+ }
50
+ if ((props.element as any).colors && Array.isArray((props.element as any).colors)) {
51
+ const valid = (props.element as any).colors.filter((c: any) => c && typeof c === 'string')
52
+ if (valid.length > 0) return valid
53
+ }
54
+ return ['#4f46e5', '#06b6d4', '#f59e0b', '#10b981', '#ec4899', '#8b5cf6', '#ef4444', '#14b8a6']
55
+ })
56
+
57
+ const textColor = computed(() => props.element.style?.color || props.element.style?.textColor || props.element.textColor || props.element.labelColor || '#64748b')
58
+ const fontFamily = computed(() => props.element.style?.fontFamily || props.element.fontFamily || 'system-ui, sans-serif')
59
+ const gridColor = computed(() => props.element.style?.gridColor || props.element.gridColor || '#e2e8f0')
60
+
61
+ const showAxes = computed(() => props.element.showAxes !== false)
62
+ const showGridlines = computed(() => props.element.showGridlines !== false)
63
+ const showLegend = computed(() => {
64
+ if (props.element.showLegend !== undefined) return Boolean(props.element.showLegend)
65
+ if ((props.element as any).options?.showLegend !== undefined) return Boolean((props.element as any).options.showLegend)
66
+ return true
67
+ })
68
+ const showTitle = computed(() => Boolean((props.element as any).showTitle))
69
+ const chartTitleText = computed(() => (props.element as any).chartTitle || (props.element as any).title || '图表标题')
70
+
71
+ const showAxisTitles = computed(() => isCartesian.value && Boolean((props.element as any).showAxisTitles))
72
+ const xAxisTitleText = computed(() => (props.element as any).xAxisTitle || (isHBarFamily.value ? '数值' : '分类'))
73
+ const yAxisTitleText = computed(() => (props.element as any).yAxisTitle || (isHBarFamily.value ? '分类' : '数值'))
74
+
75
+ const showDataLabels = computed(() => {
76
+ if ((props.element as any).showDataLabels !== undefined) {
77
+ return Boolean((props.element as any).showDataLabels)
78
+ }
79
+ if (isPieFamily.value) {
80
+ if ((props.element as any).showPercent !== undefined) return Boolean((props.element as any).showPercent)
81
+ if ((props.element as any).options?.showPercent !== undefined) return Boolean((props.element as any).options.showPercent)
82
+ return true
83
+ }
84
+ return false
85
+ })
86
+
87
+ const showDataTable = computed(() => Boolean((props.element as any).showDataTable))
88
+ const showErrorBars = computed(() => isCartesian.value && Boolean((props.element as any).showErrorBars))
89
+
90
+ const legendPosition = computed(() => props.element.legendPosition || 'bottom')
91
+
92
+ const isLegendTop = computed(() => legendPosition.value.startsWith('top'))
93
+ const isLegendBottom = computed(() => legendPosition.value.startsWith('bottom'))
94
+ const isLegendLeft = computed(() => legendPosition.value.startsWith('left'))
95
+ const isLegendRight = computed(() => legendPosition.value.startsWith('right'))
96
+
97
+ const defaultData = {
98
+ labels: ['一月', '二月', '三月', '四月', '五月'],
99
+ datasets: [
100
+ { label: '系列1', data: [30, 45, 55, 60, 80] },
101
+ { label: '系列2', data: [20, 30, 40, 45, 65] }
102
+ ]
103
+ }
104
+
105
+ const defaultPieData = {
106
+ labels: ['分类A', '分类B', '分类C', '分类D'],
107
+ datasets: [
108
+ { label: '占比', data: [35, 25, 20, 20] }
109
+ ]
110
+ }
111
+
112
+ const defaultRadarData = {
113
+ labels: ['指标A', '指标B', '指标C', '指标D', '指标E'],
114
+ datasets: [
115
+ { label: '系列1', data: [80, 70, 90, 60, 85] },
116
+ { label: '系列2', data: [60, 80, 75, 80, 70] }
117
+ ]
118
+ }
119
+
120
+ const data = computed(() => {
121
+ const d = props.element.data
122
+ if (!d) {
123
+ if (isPieFamily.value) return defaultPieData
124
+ if (chartType.value === 'radar') return defaultRadarData
125
+ return defaultData
126
+ }
127
+ if (!d.datasets || d.datasets.length === 0) {
128
+ if ((d as any).series && Array.isArray((d as any).series)) {
129
+ const legends = (d as any).legends || []
130
+ const datasets = ((d as any).series as any[]).map((s: any, idx: number) => ({
131
+ label: legends[idx] || `系列${idx + 1}`,
132
+ data: Array.isArray(s) ? s : [s]
133
+ }))
134
+ return {
135
+ ...d,
136
+ datasets
137
+ }
138
+ }
139
+ }
140
+ return d
141
+ })
142
+
143
+ const labels = computed(() => data.value.labels || [])
144
+ const datasets = computed(() => data.value.datasets || [])
145
+
146
+ // Find max value in all datasets for scaling (supports stacked charts)
147
+ const maxVal = computed(() => {
148
+ if (chartType.value === 'percent-bar') return 100
149
+ if (['stacked-bar', 'stacked-horizontal-bar', 'stacked-area'].includes(chartType.value)) {
150
+ const colSums = labels.value.map((_, i) =>
151
+ datasets.value.reduce((acc, ds) => acc + Math.max(0, Number(ds.data?.[i]) || 0), 0)
152
+ )
153
+ const maxSum = Math.max(...colSums, 10)
154
+ return Math.ceil(maxSum * 1.15)
155
+ }
156
+ const allNums = datasets.value.flatMap(d => d.data)
157
+ const max = Math.max(...allNums, 10)
158
+ return Math.ceil(max * 1.15)
159
+ })
160
+
161
+ // Ticks and Grid lines for Cartesian charts
162
+ const gridTicks = computed(() => {
163
+ const isPct = chartType.value === 'percent-bar'
164
+ const max = maxVal.value
165
+ return [0, 0.25, 0.5, 0.75, 1.0].map(p => ({
166
+ val: isPct ? `${Math.round(p * 100)}%` : Math.round(p * max),
167
+ rawVal: Math.round(p * max),
168
+ pct: p
169
+ }))
170
+ })
171
+
172
+ // Dimensions
173
+ const width = computed(() => {
174
+ if (typeof props.element.width === 'number' && props.element.width > 0) return props.element.width
175
+ const wStr = props.element.style?.width
176
+ if (wStr && typeof wStr === 'string' && !wStr.endsWith('%')) {
177
+ const val = parseFloat(wStr)
178
+ if (!isNaN(val) && val > 0) return val
179
+ }
180
+ return 400
181
+ })
182
+ const height = computed(() => {
183
+ if (typeof props.element.height === 'number' && props.element.height > 0) return props.element.height
184
+ const hStr = props.element.style?.height
185
+ if (hStr && typeof hStr === 'string' && !hStr.endsWith('%')) {
186
+ const val = parseFloat(hStr)
187
+ if (!isNaN(val) && val > 0) return val
188
+ }
189
+ return 300
190
+ })
191
+
192
+ // Data table dimensions
193
+ const dataTableRowH = 16
194
+ const dataTableRowsCount = computed(() => {
195
+ if (!showDataTable.value) return 0
196
+ const seriesCount = isPieFamily.value ? 1 : Math.min(datasets.value.length, 3)
197
+ return seriesCount + 1
198
+ })
199
+ const dataTableHeight = computed(() => dataTableRowsCount.value * dataTableRowH)
200
+
201
+ // Layout margins for Cartesian charts
202
+ const padding = computed(() => {
203
+ let top = showTitle.value ? 52 : 36
204
+ let bottom = 38
205
+ let left = 45
206
+ let right = 25
207
+
208
+ if (isHBarFamily.value) {
209
+ left = 65
210
+ right = 35
211
+ }
212
+
213
+ if (showAxisTitles.value) {
214
+ left += 18
215
+ bottom += 18
216
+ }
217
+
218
+ if (showLegend.value) {
219
+ if (isLegendTop.value) {
220
+ top += 24
221
+ } else if (isLegendBottom.value) {
222
+ bottom += 24
223
+ } else if (isLegendLeft.value) {
224
+ left += 95
225
+ } else if (isLegendRight.value) {
226
+ right += 95
227
+ }
228
+ }
229
+
230
+ if (showDataTable.value) {
231
+ bottom += dataTableHeight.value + 12
232
+ }
233
+
234
+ return { top, right, bottom, left }
235
+ })
236
+
237
+ const chartWidth = computed(() => Math.max(60, width.value - padding.value.left - padding.value.right))
238
+ const chartHeight = computed(() => Math.max(50, height.value - padding.value.top - padding.value.bottom))
239
+
240
+ // Math helpers for circular charts (Pie, Donut, Rose, Radar)
241
+ const cx = computed(() => {
242
+ const baseCx = width.value / 2
243
+ if (showLegend.value) {
244
+ if (isLegendLeft.value) {
245
+ return width.value * 0.58
246
+ } else if (isLegendRight.value) {
247
+ return width.value * 0.42
248
+ }
249
+ }
250
+ return baseCx
251
+ })
252
+
253
+ const cy = computed(() => {
254
+ const topOffset = showTitle.value ? 26 : 0
255
+ const bottomOffset = showDataTable.value ? dataTableHeight.value + 10 : 0
256
+ const availH = height.value - topOffset - bottomOffset
257
+ let baseCy = topOffset + (showLegend.value ? availH * 0.44 : availH * 0.5)
258
+ if (showLegend.value) {
259
+ if (isLegendTop.value) {
260
+ baseCy = topOffset + availH * 0.56
261
+ } else if (isLegendBottom.value) {
262
+ baseCy = topOffset + availH * 0.43
263
+ }
264
+ }
265
+ return baseCy
266
+ })
267
+
268
+ const radius = computed(() => {
269
+ let w = width.value
270
+ let h = height.value - (showTitle.value ? 26 : 0) - (showDataTable.value ? dataTableHeight.value + 12 : 0)
271
+ if (showLegend.value) {
272
+ if (isLegendLeft.value || isLegendRight.value) {
273
+ w -= 100
274
+ } else if (isLegendTop.value || isLegendBottom.value) {
275
+ h -= 40
276
+ }
277
+ return Math.max(32, Math.min(w * 0.4, h * 0.32) || 75)
278
+ }
279
+ return Math.max(36, Math.min(w * 0.44, h * 0.42) || 80)
280
+ })
281
+
282
+ const innerRadius = computed(() => {
283
+ const hole = parseFloat((props.element as any).holeSize || (props.element as any).options?.holeSize)
284
+ const defaultHole = chartType.value === 'rose-donut' ? 0.36 : 0.72
285
+ const ratio = (!isNaN(hole) && hole > 0 && hole < 100) ? (hole / 100) : defaultHole
286
+ return radius.value * ratio
287
+ })
288
+
289
+ // Pie / Donut / Rose-Pie / Rose-Donut Slice generator
290
+ interface Slice {
291
+ d: string
292
+ color: string
293
+ label: string
294
+ value: number
295
+ percent: string
296
+ labelX: number
297
+ labelY: number
298
+ }
299
+
300
+ const pieSlices = computed<Slice[]>(() => {
301
+ const vals = (datasets.value[0]?.data || []).map(v => Math.max(0, Number(v) || 0))
302
+ const total = vals.reduce((a, b) => a + b, 0) || 1
303
+ const maxSliceVal = Math.max(...vals, 1)
304
+ let startAngle = 0
305
+ const isDonut = chartType.value === 'donut' || chartType.value === 'rose-donut'
306
+ const isRose = chartType.value === 'rose-pie' || chartType.value === 'rose-donut'
307
+ const innerRadiusVal = isDonut ? innerRadius.value : 0
308
+
309
+ return vals.map((val, i) => {
310
+ const pct = val / total
311
+ const angle = isRose ? (360 / (vals.length || 1)) : (pct * 360)
312
+ const endAngle = startAngle + angle
313
+ const pctText = (pct * 100).toFixed(0) + '%'
314
+
315
+ const outerR = isRose
316
+ ? innerRadiusVal + (radius.value - innerRadiusVal) * (0.28 + 0.72 * (val / maxSliceVal))
317
+ : radius.value
318
+
319
+ let dPath = ''
320
+ const rad1 = (startAngle - 90) * Math.PI / 180
321
+ const rad2 = (endAngle - 90) * Math.PI / 180
322
+
323
+ if (!isRose && pct >= 0.999) {
324
+ if (isDonut) {
325
+ dPath = `M ${cx.value} ${cy.value - outerR} A ${outerR} ${outerR} 0 1 1 ${cx.value - 0.01} ${cy.value - outerR} ` +
326
+ `L ${cx.value - 0.01} ${cy.value - innerRadiusVal} A ${innerRadiusVal} ${innerRadiusVal} 0 1 0 ${cx.value} ${cy.value - innerRadiusVal} Z`
327
+ } else {
328
+ dPath = `M ${cx.value} ${cy.value - outerR} A ${outerR} ${outerR} 0 1 1 ${cx.value - 0.01} ${cy.value - outerR} Z`
329
+ }
330
+ } else {
331
+ const xo1 = cx.value + outerR * Math.cos(rad1)
332
+ const yo1 = cy.value + outerR * Math.sin(rad1)
333
+ const xo2 = cx.value + outerR * Math.cos(rad2)
334
+ const yo2 = cy.value + outerR * Math.sin(rad2)
335
+ const largeArc = angle > 180 ? 1 : 0
336
+
337
+ if (isDonut) {
338
+ const xi1 = cx.value + innerRadiusVal * Math.cos(rad1)
339
+ const yi1 = cy.value + innerRadiusVal * Math.sin(rad1)
340
+ const xi2 = cx.value + innerRadiusVal * Math.cos(rad2)
341
+ const yi2 = cy.value + innerRadiusVal * Math.sin(rad2)
342
+ dPath = `M ${xo1} ${yo1} A ${outerR} ${outerR} 0 ${largeArc} 1 ${xo2} ${yo2} L ${xi2} ${yi2} A ${innerRadiusVal} ${innerRadiusVal} 0 ${largeArc} 0 ${xi1} ${yi1} Z`
343
+ } else {
344
+ dPath = `M ${cx.value} ${cy.value} L ${xo1} ${yo1} A ${outerR} ${outerR} 0 ${largeArc} 1 ${xo2} ${yo2} Z`
345
+ }
346
+ }
347
+
348
+ const midAngle = (startAngle + endAngle) / 2
349
+ const midRad = (midAngle - 90) * Math.PI / 180
350
+ const labelR = isDonut ? (outerR + innerRadiusVal) / 2 : outerR * 0.65
351
+ const lx = cx.value + labelR * Math.cos(midRad)
352
+ const ly = cy.value + labelR * Math.sin(midRad)
353
+
354
+ const slice: Slice = {
355
+ d: dPath,
356
+ color: chartColors.value[i % chartColors.value.length],
357
+ label: labels.value[i] || `分类${i + 1}`,
358
+ value: val,
359
+ percent: pctText,
360
+ labelX: lx,
361
+ labelY: ly,
362
+ }
363
+ startAngle = endAngle
364
+ return slice
365
+ })
366
+ })
367
+
368
+ // Stacked Bar & Percent Bar segments generator
369
+ const stackedBarGroups = computed(() => {
370
+ const N = labels.value.length || 1
371
+ const groupW = chartWidth.value / N
372
+ const barW = Math.min(38, groupW * 0.52)
373
+ const isPct = chartType.value === 'percent-bar'
374
+
375
+ return labels.value.map((label, i) => {
376
+ const colTotal = datasets.value.reduce((acc, ds) => acc + Math.max(0, Number(ds.data?.[i]) || 0), 0) || 1
377
+ let currentBottomY = padding.value.top + chartHeight.value
378
+
379
+ const segments = datasets.value.map((ds, j) => {
380
+ const val = Math.max(0, Number(ds.data?.[i]) || 0)
381
+ const ratio = isPct ? (val / colTotal) : (val / maxVal.value)
382
+ const h = ratio * chartHeight.value
383
+ const y = currentBottomY - h
384
+ currentBottomY = y
385
+ return {
386
+ x: padding.value.left + i * groupW + (groupW - barW) / 2,
387
+ y,
388
+ width: barW,
389
+ height: Math.max(0, h),
390
+ color: chartColors.value[j % chartColors.value.length],
391
+ val,
392
+ displayVal: isPct ? `${Math.round((val / colTotal) * 100)}%` : `${val}`
393
+ }
394
+ })
395
+
396
+ return {
397
+ label,
398
+ centerX: padding.value.left + i * groupW + groupW / 2,
399
+ topY: currentBottomY,
400
+ colTotal,
401
+ segments
402
+ }
403
+ })
404
+ })
405
+
406
+ // Stacked Horizontal Bar segments generator
407
+ const stackedHBarGroups = computed(() => {
408
+ const N = labels.value.length || 1
409
+ const rowH = chartHeight.value / N
410
+ const barH = Math.min(26, rowH * 0.55)
411
+
412
+ return labels.value.map((label, i) => {
413
+ let currentLeftX = padding.value.left
414
+ const segments = datasets.value.map((ds, j) => {
415
+ const val = Math.max(0, Number(ds.data?.[i]) || 0)
416
+ const w = (val / maxVal.value) * chartWidth.value
417
+ const x = currentLeftX
418
+ currentLeftX += w
419
+ return {
420
+ x,
421
+ y: padding.value.top + i * rowH + (rowH - barH) / 2,
422
+ width: Math.max(0, w),
423
+ height: barH,
424
+ color: chartColors.value[j % chartColors.value.length],
425
+ val
426
+ }
427
+ })
428
+ return {
429
+ label,
430
+ centerY: padding.value.top + i * rowH + rowH / 2,
431
+ endX: currentLeftX,
432
+ segments
433
+ }
434
+ })
435
+ })
436
+
437
+ interface LegendItem {
438
+ label: string
439
+ color: string
440
+ percent?: string
441
+ x: number
442
+ y: number
443
+ }
444
+
445
+ const legendItems = computed<LegendItem[]>(() => {
446
+ if (!showLegend.value) return []
447
+
448
+ let rawItems: { label: string; color: string; percent?: string }[] = []
449
+ if (isPieFamily.value) {
450
+ rawItems = pieSlices.value.slice(0, 6).map(slice => ({
451
+ label: slice.label,
452
+ color: slice.color,
453
+ percent: slice.percent
454
+ }))
455
+ } else {
456
+ rawItems = datasets.value.slice(0, 6).map((ds, j) => ({
457
+ label: ds.label,
458
+ color: chartColors.value[j % chartColors.value.length]
459
+ }))
460
+ }
461
+
462
+ const pos = legendPosition.value
463
+ const W = width.value
464
+ const H = height.value
465
+ const topTitleSpace = showTitle.value ? 26 : 0
466
+ const bottomTableSpace = showDataTable.value ? dataTableHeight.value + 10 : 0
467
+
468
+ const itemsCount = rawItems.length
469
+
470
+ if (isLegendLeft.value || isLegendRight.value) {
471
+ const stepY = 22
472
+ const totalHeight = itemsCount * stepY
473
+
474
+ let startX = isLegendLeft.value ? 20 : W - 110
475
+
476
+ let startY = 40 + topTitleSpace
477
+ if (pos.endsWith('top')) {
478
+ startY = 36 + topTitleSpace
479
+ } else if (pos.endsWith('bottom')) {
480
+ startY = H - totalHeight - 20 - bottomTableSpace
481
+ } else {
482
+ startY = (H - totalHeight) / 2
483
+ }
484
+
485
+ return rawItems.map((item, idx) => ({
486
+ ...item,
487
+ x: startX,
488
+ y: startY + idx * stepY
489
+ }))
490
+ } else {
491
+ const stepX = Math.min(90, Math.floor((W - 40) / Math.max(itemsCount, 1)))
492
+ const totalWidth = itemsCount * stepX
493
+
494
+ let startY = isLegendTop.value ? (20 + topTitleSpace) : (H - 18 - bottomTableSpace)
495
+
496
+ let startX = 20
497
+ if (pos.endsWith('left')) {
498
+ startX = 20
499
+ } else if (pos.endsWith('right')) {
500
+ startX = W - totalWidth - 20
501
+ } else {
502
+ startX = (W - totalWidth) / 2
503
+ }
504
+
505
+ return rawItems.map((item, idx) => ({
506
+ ...item,
507
+ x: startX + idx * stepX,
508
+ y: startY
509
+ }))
510
+ }
511
+ })
512
+
513
+ // Smooth curve helper
514
+ function buildSmoothPath(points: { x: number; y: number }[]): string {
515
+ if (points.length === 0) return ''
516
+ if (points.length === 1) return `M ${points[0].x.toFixed(1)},${points[0].y.toFixed(1)}`
517
+ let d = `M ${points[0].x.toFixed(1)},${points[0].y.toFixed(1)}`
518
+ for (let i = 0; i < points.length - 1; i++) {
519
+ const p0 = points[i]
520
+ const p1 = points[i + 1]
521
+ const midX = ((p0.x + p1.x) / 2).toFixed(1)
522
+ d += ` C ${midX},${p0.y.toFixed(1)} ${midX},${p1.y.toFixed(1)} ${p1.x.toFixed(1)},${p1.y.toFixed(1)}`
523
+ }
524
+ return d
525
+ }
526
+
527
+ // Line & Area Paths generator
528
+ interface LinePath {
529
+ dLine: string
530
+ dSmooth: string
531
+ dArea: string
532
+ color: string
533
+ points: { x: number; y: number; val: number }[]
534
+ }
535
+
536
+ const linePaths = computed<LinePath[]>(() => {
537
+ const N = labels.value.length
538
+ if (N === 0) return []
539
+ const dx = N > 1 ? chartWidth.value / (N - 1) : chartWidth.value
540
+ const max = maxVal.value
541
+ const isStackedArea = chartType.value === 'stacked-area'
542
+ const cumulativeVals = new Array(N).fill(0)
543
+
544
+ return datasets.value.map((ds, j) => {
545
+ const points = (ds.data || []).map((rawVal, i) => {
546
+ const val = Number(rawVal) || 0
547
+ cumulativeVals[i] += val
548
+ const plotVal = isStackedArea ? cumulativeVals[i] : val
549
+ const x = padding.value.left + i * dx
550
+ const y = padding.value.top + chartHeight.value - (plotVal / max) * chartHeight.value
551
+ return { x, y, val }
552
+ })
553
+
554
+ if (points.length === 0) return null
555
+
556
+ const linePointsStr = points.map(p => `${p.x.toFixed(1)},${p.y.toFixed(1)}`).join(' L ')
557
+ const dLine = `M ${linePointsStr}`
558
+ const dSmooth = buildSmoothPath(points)
559
+
560
+ const startX = points[0]?.x || padding.value.left
561
+ const endX = points[points.length - 1]?.x || (padding.value.left + chartWidth.value)
562
+ const bottomY = padding.value.top + chartHeight.value
563
+ const baseCurve = isStackedArea ? dSmooth : dLine
564
+ const dArea = `${baseCurve} L ${endX.toFixed(1)},${bottomY} L ${startX.toFixed(1)},${bottomY} Z`
565
+
566
+ return {
567
+ dLine,
568
+ dSmooth,
569
+ dArea,
570
+ color: chartColors.value[j % chartColors.value.length],
571
+ points
572
+ }
573
+ }).filter((lp): lp is LinePath => lp !== null)
574
+ })
575
+
576
+ // Radar Grid levels and axis vectors
577
+ const radarAxis = computed(() => {
578
+ const N = labels.value.length || 1
579
+ const angle = 360 / N
580
+ return labels.value.map((label, i) => {
581
+ const rad = (i * angle - 90) * Math.PI / 180
582
+ return {
583
+ label,
584
+ lineX: cx.value + radius.value * Math.cos(rad),
585
+ lineY: cy.value + radius.value * Math.sin(rad),
586
+ labelX: cx.value + (radius.value + 18) * Math.cos(rad),
587
+ labelY: cy.value + (radius.value + 18) * Math.sin(rad) + 4,
588
+ align: Math.cos(rad) > 0.05 ? 'start' : Math.cos(rad) < -0.05 ? 'end' : 'middle'
589
+ }
590
+ })
591
+ })
592
+
593
+ const radarLevels = computed(() => {
594
+ const N = labels.value.length
595
+ if (N === 0) return []
596
+ const angle = 360 / N
597
+ return [1, 2, 3, 4].map(level => {
598
+ const r = radius.value * (level / 4)
599
+ const points = Array.from({ length: N }).map((_, i) => {
600
+ const rad = (i * angle - 90) * Math.PI / 180
601
+ const x = cx.value + r * Math.cos(rad)
602
+ const y = cy.value + r * Math.sin(rad)
603
+ return `${x.toFixed(1)},${y.toFixed(1)}`
604
+ }).join(' ')
605
+ return points
606
+ })
607
+ })
608
+
609
+ const radarPolygons = computed(() => {
610
+ const N = labels.value.length || 1
611
+ const angle = 360 / N
612
+ const max = maxVal.value
613
+
614
+ return datasets.value.map((ds, j) => {
615
+ const vertexList = ds.data.map((val, i) => {
616
+ const rad = (i * angle - 90) * Math.PI / 180
617
+ const r_val = radius.value * (val / max)
618
+ const x = cx.value + r_val * Math.cos(rad)
619
+ const y = cy.value + r_val * Math.sin(rad)
620
+ return { x, y, val }
621
+ })
622
+ const points = vertexList.map(v => `${v.x.toFixed(1)},${v.y.toFixed(1)}`).join(' ')
623
+
624
+ return {
625
+ points,
626
+ vertices: vertexList,
627
+ color: chartColors.value[j % chartColors.value.length]
628
+ }
629
+ })
630
+ })
631
+ </script>
632
+
633
+ <template>
634
+ <svg
635
+ :viewBox="`0 0 ${width} ${height}`"
636
+ :style="{ width: '100%', height: '100%', display: 'block', overflow: 'visible', userSelect: 'none', fontFamily: fontFamily, '--ppt-font-family': fontFamily }"
637
+ >
638
+ <!-- Chart Title (All chart types) -->
639
+ <text
640
+ v-if="showTitle"
641
+ :x="width / 2"
642
+ y="22"
643
+ text-anchor="middle"
644
+ font-size="14"
645
+ font-weight="700"
646
+ :fill="textColor"
647
+ :font-family="fontFamily"
648
+ >
649
+ {{ chartTitleText }}
650
+ </text>
651
+
652
+ <!-- Background Gridlines (Cartesian vertical-Y charts) -->
653
+ <g v-if="isVerticalCartesian && showGridlines">
654
+ <line
655
+ v-for="tick in gridTicks"
656
+ :key="'grid-' + tick.pct"
657
+ :x1="padding.left"
658
+ :y1="padding.top + chartHeight - tick.pct * chartHeight"
659
+ :x2="padding.left + chartWidth"
660
+ :y2="padding.top + chartHeight - tick.pct * chartHeight"
661
+ :stroke="gridColor"
662
+ stroke-width="1"
663
+ :stroke-dasharray="tick.pct === 0 ? '' : '4 4'"
664
+ />
665
+ </g>
666
+
667
+ <!-- Y-Axis Ticks & Axis Baseline (Cartesian vertical-Y charts, controlled by showAxes) -->
668
+ <g v-if="isVerticalCartesian && showAxes">
669
+ <line
670
+ :x1="padding.left"
671
+ :y1="padding.top + chartHeight"
672
+ :x2="padding.left + chartWidth"
673
+ :y2="padding.top + chartHeight"
674
+ :stroke="gridColor"
675
+ stroke-width="1.2"
676
+ />
677
+ <text
678
+ v-for="tick in gridTicks"
679
+ :key="'label-' + tick.pct"
680
+ :x="padding.left - 8"
681
+ :y="padding.top + chartHeight - tick.pct * chartHeight + 4"
682
+ text-anchor="end"
683
+ font-size="10"
684
+ font-weight="500"
685
+ :fill="textColor"
686
+ :font-family="fontFamily"
687
+ >
688
+ {{ tick.val }}
689
+ </text>
690
+ </g>
691
+
692
+ <!-- Axis Titles (Cartesian charts only) -->
693
+ <g v-if="showAxisTitles">
694
+ <text
695
+ :x="padding.left + chartWidth / 2"
696
+ :y="padding.top + chartHeight + (showAxes ? 30 : 16)"
697
+ text-anchor="middle"
698
+ font-size="10"
699
+ font-weight="600"
700
+ :fill="textColor"
701
+ :font-family="fontFamily"
702
+ >
703
+ {{ xAxisTitleText }}
704
+ </text>
705
+ <text
706
+ :x="14"
707
+ :y="padding.top + chartHeight / 2"
708
+ text-anchor="middle"
709
+ font-size="10"
710
+ font-weight="600"
711
+ :fill="textColor"
712
+ :font-family="fontFamily"
713
+ :transform="`rotate(-90, 14, ${padding.top + chartHeight / 2})`"
714
+ >
715
+ {{ yAxisTitleText }}
716
+ </text>
717
+ </g>
718
+
719
+ <!-- 1A. Grouped Column/Bar Chart ('bar') -->
720
+ <g v-if="chartType === 'bar'">
721
+ <g v-for="(label, i) in labels" :key="'col-group-' + i">
722
+ <text v-if="showAxes"
723
+ :x="padding.left + i * (chartWidth / labels.length) + (chartWidth / labels.length) / 2"
724
+ :y="padding.top + chartHeight + 15"
725
+ text-anchor="middle"
726
+ font-size="10"
727
+ font-weight="500"
728
+ :fill="textColor"
729
+ :font-family="fontFamily"
730
+ >
731
+ {{ label }}
732
+ </text>
733
+
734
+ <g v-for="(ds, j) in datasets" :key="'bar-' + i + '-' + j">
735
+ <rect
736
+ :x="padding.left + i * (chartWidth / labels.length) + ((chartWidth / labels.length) * 0.15) + (j * ((chartWidth / labels.length) * 0.7 / datasets.length))"
737
+ :y="padding.top + chartHeight - ((ds.data[i] || 0) / maxVal) * chartHeight"
738
+ :width="((chartWidth / labels.length) * 0.7 / datasets.length) * 0.9"
739
+ :height="((ds.data[i] || 0) / maxVal) * chartHeight"
740
+ :fill="chartColors[j % chartColors.length]"
741
+ rx="2"
742
+ />
743
+ <g v-if="showErrorBars && (ds.data[i] || 0) > 0">
744
+ <line
745
+ :x1="padding.left + i * (chartWidth / labels.length) + ((chartWidth / labels.length) * 0.15) + (j * ((chartWidth / labels.length) * 0.7 / datasets.length)) + (((chartWidth / labels.length) * 0.7 / datasets.length) * 0.45)"
746
+ :y1="padding.top + chartHeight - ((ds.data[i] || 0) * 1.08 / maxVal) * chartHeight"
747
+ :x2="padding.left + i * (chartWidth / labels.length) + ((chartWidth / labels.length) * 0.15) + (j * ((chartWidth / labels.length) * 0.7 / datasets.length)) + (((chartWidth / labels.length) * 0.7 / datasets.length) * 0.45)"
748
+ :y2="padding.top + chartHeight - ((ds.data[i] || 0) * 0.92 / maxVal) * chartHeight"
749
+ stroke="#334155"
750
+ stroke-width="1.2"
751
+ />
752
+ </g>
753
+ <text
754
+ v-if="showDataLabels"
755
+ :x="padding.left + i * (chartWidth / labels.length) + ((chartWidth / labels.length) * 0.15) + (j * ((chartWidth / labels.length) * 0.7 / datasets.length)) + (((chartWidth / labels.length) * 0.7 / datasets.length) * 0.45)"
756
+ :y="padding.top + chartHeight - ((ds.data[i] || 0) / maxVal) * chartHeight - 5"
757
+ text-anchor="middle"
758
+ font-size="9"
759
+ font-weight="600"
760
+ :fill="textColor"
761
+ :font-family="fontFamily"
762
+ >
763
+ {{ ds.data[i] ?? 0 }}
764
+ </text>
765
+ </g>
766
+ </g>
767
+ </g>
768
+
769
+ <!-- 1B. Stacked Bar & Percent Bar ('stacked-bar' | 'percent-bar') -->
770
+ <g v-if="chartType === 'stacked-bar' || chartType === 'percent-bar'">
771
+ <g v-for="(grp, i) in stackedBarGroups" :key="'sbar-grp-' + i">
772
+ <text v-if="showAxes"
773
+ :x="grp.centerX"
774
+ :y="padding.top + chartHeight + 15"
775
+ text-anchor="middle"
776
+ font-size="10"
777
+ font-weight="500"
778
+ :fill="textColor"
779
+ :font-family="fontFamily"
780
+ >
781
+ {{ grp.label }}
782
+ </text>
783
+ <g v-for="(seg, j) in grp.segments" :key="'sbar-seg-' + i + '-' + j">
784
+ <rect
785
+ :x="seg.x"
786
+ :y="seg.y"
787
+ :width="seg.width"
788
+ :height="seg.height"
789
+ :fill="seg.color"
790
+ stroke="#ffffff"
791
+ stroke-width="0.8"
792
+ rx="1.5"
793
+ />
794
+ <text
795
+ v-if="showDataLabels && seg.height > 12"
796
+ :x="grp.centerX"
797
+ :y="seg.y + seg.height / 2 + 3"
798
+ text-anchor="middle"
799
+ font-size="8.5"
800
+ font-weight="600"
801
+ fill="#ffffff"
802
+ >
803
+ {{ seg.displayVal }}
804
+ </text>
805
+ </g>
806
+ </g>
807
+ </g>
808
+
809
+ <!-- 2. Horizontal Bar Family ('horizontal-bar' | 'stacked-horizontal-bar') -->
810
+ <g v-if="isHBarFamily">
811
+ <g v-if="showGridlines">
812
+ <line
813
+ v-for="tick in gridTicks"
814
+ :key="'grid-v-' + tick.pct"
815
+ :x1="padding.left + tick.pct * chartWidth"
816
+ :y1="padding.top"
817
+ :x2="padding.left + tick.pct * chartWidth"
818
+ :y2="padding.top + chartHeight"
819
+ :stroke="gridColor"
820
+ stroke-width="1"
821
+ :stroke-dasharray="tick.pct === 0 ? '' : '4 4'"
822
+ />
823
+ </g>
824
+ <g v-if="showAxes">
825
+ <text
826
+ v-for="tick in gridTicks"
827
+ :key="'v-label-' + tick.pct"
828
+ :x="padding.left + tick.pct * chartWidth"
829
+ :y="padding.top + chartHeight + 15"
830
+ text-anchor="middle"
831
+ font-size="10"
832
+ font-weight="500"
833
+ :fill="textColor"
834
+ :font-family="fontFamily"
835
+ >
836
+ {{ tick.val }}
837
+ </text>
838
+ </g>
839
+
840
+ <!-- Grouped Horizontal Bar -->
841
+ <template v-if="chartType === 'horizontal-bar'">
842
+ <g v-for="(label, i) in labels" :key="'row-group-' + i">
843
+ <text v-if="showAxes"
844
+ :x="padding.left - 8"
845
+ :y="padding.top + i * (chartHeight / labels.length) + (chartHeight / labels.length) / 2 + 4"
846
+ text-anchor="end"
847
+ font-size="10"
848
+ font-weight="500"
849
+ :fill="textColor"
850
+ :font-family="fontFamily"
851
+ >
852
+ {{ label }}
853
+ </text>
854
+
855
+ <g v-for="(ds, j) in datasets" :key="'hbar-' + i + '-' + j">
856
+ <rect
857
+ :x="padding.left"
858
+ :y="padding.top + i * (chartHeight / labels.length) + ((chartHeight / labels.length) * 0.15) + (j * ((chartHeight / labels.length) * 0.7 / datasets.length))"
859
+ :width="((ds.data[i] || 0) / maxVal) * chartWidth"
860
+ :height="((chartHeight / labels.length) * 0.7 / datasets.length) * 0.9"
861
+ :fill="chartColors[j % chartColors.length]"
862
+ rx="2"
863
+ />
864
+ <text
865
+ v-if="showDataLabels"
866
+ :x="padding.left + ((ds.data[i] || 0) / maxVal) * chartWidth + 5"
867
+ :y="padding.top + i * (chartHeight / labels.length) + ((chartHeight / labels.length) * 0.15) + (j * ((chartHeight / labels.length) * 0.7 / datasets.length)) + (((chartHeight / labels.length) * 0.7 / datasets.length) * 0.45) + 3"
868
+ text-anchor="start"
869
+ font-size="9"
870
+ font-weight="600"
871
+ :fill="textColor"
872
+ :font-family="fontFamily"
873
+ >
874
+ {{ ds.data[i] ?? 0 }}
875
+ </text>
876
+ </g>
877
+ </g>
878
+ </template>
879
+
880
+ <!-- Stacked Horizontal Bar -->
881
+ <template v-else>
882
+ <g v-for="(grp, i) in stackedHBarGroups" :key="'shbar-grp-' + i">
883
+ <text v-if="showAxes"
884
+ :x="padding.left - 8"
885
+ :y="grp.centerY + 4"
886
+ text-anchor="end"
887
+ font-size="10"
888
+ font-weight="500"
889
+ :fill="textColor"
890
+ :font-family="fontFamily"
891
+ >
892
+ {{ grp.label }}
893
+ </text>
894
+ <g v-for="(seg, j) in grp.segments" :key="'shbar-seg-' + i + '-' + j">
895
+ <rect
896
+ :x="seg.x"
897
+ :y="seg.y"
898
+ :width="seg.width"
899
+ :height="seg.height"
900
+ :fill="seg.color"
901
+ stroke="#ffffff"
902
+ stroke-width="0.8"
903
+ rx="1.5"
904
+ />
905
+ <text
906
+ v-if="showDataLabels && seg.width > 18"
907
+ :x="seg.x + seg.width / 2"
908
+ :y="grp.centerY + 3"
909
+ text-anchor="middle"
910
+ font-size="8.5"
911
+ font-weight="600"
912
+ fill="#ffffff"
913
+ >
914
+ {{ seg.val }}
915
+ </text>
916
+ </g>
917
+ </g>
918
+ </template>
919
+ </g>
920
+
921
+ <!-- 3. Line Chart Family ('line' | 'compare-line' | 'step-line') -->
922
+ <g v-if="isLineFamily">
923
+ <text v-if="showAxes"
924
+ v-for="(label, i) in labels"
925
+ :key="'line-lbl-' + i"
926
+ :x="padding.left + i * (chartWidth / (labels.length - 1 || 1))"
927
+ :y="padding.top + chartHeight + 15"
928
+ text-anchor="middle"
929
+ font-size="10"
930
+ font-weight="500"
931
+ :fill="textColor"
932
+ :font-family="fontFamily"
933
+ >
934
+ {{ label }}
935
+ </text>
936
+
937
+ <g v-for="(lp, j) in linePaths" :key="'line-p-' + j">
938
+ <path
939
+ :d="chartType === 'line' ? lp.dSmooth : lp.dLine"
940
+ fill="none"
941
+ :stroke="lp.color"
942
+ stroke-width="3"
943
+ stroke-linecap="round"
944
+ stroke-linejoin="round"
945
+ />
946
+ <g v-for="(pt, i) in lp.points" :key="'circle-' + i">
947
+ <line
948
+ v-if="showErrorBars"
949
+ :x1="pt.x"
950
+ :y1="pt.y - 8"
951
+ :x2="pt.x"
952
+ :y2="pt.y + 8"
953
+ stroke="#334155"
954
+ stroke-width="1.2"
955
+ />
956
+ <circle
957
+ v-if="chartType !== 'step-line'"
958
+ :cx="pt.x"
959
+ :cy="pt.y"
960
+ r="4.5"
961
+ fill="#ffffff"
962
+ :stroke="lp.color"
963
+ stroke-width="2.5"
964
+ />
965
+ <text
966
+ v-if="showDataLabels"
967
+ :x="pt.x"
968
+ :y="pt.y - 8"
969
+ text-anchor="middle"
970
+ font-size="9"
971
+ font-weight="600"
972
+ :fill="textColor"
973
+ :font-family="fontFamily"
974
+ >
975
+ {{ pt.val }}
976
+ </text>
977
+ </g>
978
+ </g>
979
+ </g>
980
+
981
+ <!-- 4. Area Chart Family ('area' | 'stacked-area') -->
982
+ <g v-if="isAreaFamily">
983
+ <text v-if="showAxes"
984
+ v-for="(label, i) in labels"
985
+ :key="'area-lbl-' + i"
986
+ :x="padding.left + i * (chartWidth / (labels.length - 1 || 1))"
987
+ :y="padding.top + chartHeight + 15"
988
+ text-anchor="middle"
989
+ font-size="10"
990
+ font-weight="500"
991
+ :fill="textColor"
992
+ :font-family="fontFamily"
993
+ >
994
+ {{ label }}
995
+ </text>
996
+
997
+ <g v-for="(lp, j) in linePaths" :key="'area-p-' + j">
998
+ <path
999
+ :d="lp.dArea"
1000
+ :fill="lp.color"
1001
+ :fill-opacity="chartType === 'stacked-area' ? 0.35 : 0.18"
1002
+ />
1003
+ <path
1004
+ :d="chartType === 'stacked-area' ? lp.dSmooth : lp.dLine"
1005
+ fill="none"
1006
+ :stroke="lp.color"
1007
+ stroke-width="2.5"
1008
+ stroke-linecap="round"
1009
+ stroke-linejoin="round"
1010
+ />
1011
+ <g v-for="(pt, i) in lp.points" :key="'acircle-' + i">
1012
+ <line
1013
+ v-if="showErrorBars"
1014
+ :x1="pt.x"
1015
+ :y1="pt.y - 8"
1016
+ :x2="pt.x"
1017
+ :y2="pt.y + 8"
1018
+ stroke="#334155"
1019
+ stroke-width="1.2"
1020
+ />
1021
+ <circle
1022
+ :cx="pt.x"
1023
+ :cy="pt.y"
1024
+ r="3.5"
1025
+ :fill="lp.color"
1026
+ />
1027
+ <text
1028
+ v-if="showDataLabels"
1029
+ :x="pt.x"
1030
+ :y="pt.y - 7"
1031
+ text-anchor="middle"
1032
+ font-size="9"
1033
+ font-weight="600"
1034
+ :fill="textColor"
1035
+ :font-family="fontFamily"
1036
+ >
1037
+ {{ pt.val }}
1038
+ </text>
1039
+ </g>
1040
+ </g>
1041
+ </g>
1042
+
1043
+ <!-- 5. Scatter Chart -->
1044
+ <g v-if="chartType === 'scatter'">
1045
+ <text v-if="showAxes"
1046
+ v-for="(label, i) in labels"
1047
+ :key="'scat-lbl-' + i"
1048
+ :x="padding.left + i * (chartWidth / (labels.length - 1 || 1))"
1049
+ :y="padding.top + chartHeight + 15"
1050
+ text-anchor="middle"
1051
+ font-size="10"
1052
+ font-weight="500"
1053
+ :fill="textColor"
1054
+ :font-family="fontFamily"
1055
+ >
1056
+ {{ label }}
1057
+ </text>
1058
+
1059
+ <g v-for="(lp, j) in linePaths" :key="'scat-g-' + j">
1060
+ <g v-for="(pt, i) in lp.points" :key="'pt-' + i">
1061
+ <line
1062
+ v-if="showErrorBars"
1063
+ :x1="pt.x"
1064
+ :y1="pt.y - 10"
1065
+ :x2="pt.x"
1066
+ :y2="pt.y + 10"
1067
+ stroke="#334155"
1068
+ stroke-width="1.2"
1069
+ />
1070
+ <circle
1071
+ :cx="pt.x"
1072
+ :cy="pt.y"
1073
+ r="6.5"
1074
+ :fill="lp.color"
1075
+ fill-opacity="0.8"
1076
+ />
1077
+ <text
1078
+ v-if="showDataLabels"
1079
+ :x="pt.x"
1080
+ :y="pt.y - 9"
1081
+ text-anchor="middle"
1082
+ font-size="9"
1083
+ font-weight="600"
1084
+ :fill="textColor"
1085
+ :font-family="fontFamily"
1086
+ >
1087
+ {{ pt.val }}
1088
+ </text>
1089
+ </g>
1090
+ </g>
1091
+ </g>
1092
+
1093
+ <!-- 6 & 7. Pie / Donut / Rose-Pie / Rose-Donut Chart -->
1094
+ <g v-if="isPieFamily">
1095
+ <g v-for="(slice, i) in pieSlices" :key="'pie-slice-g-' + i">
1096
+ <path
1097
+ :d="slice.d"
1098
+ :fill="slice.color"
1099
+ stroke="#ffffff"
1100
+ stroke-width="1.5"
1101
+ />
1102
+ <text
1103
+ v-if="showDataLabels && slice.percent && slice.value > 0"
1104
+ :x="slice.labelX"
1105
+ :y="slice.labelY"
1106
+ text-anchor="middle"
1107
+ dominant-baseline="central"
1108
+ fill="#ffffff"
1109
+ font-size="11"
1110
+ font-weight="bold"
1111
+ :font-family="fontFamily"
1112
+ style="pointer-events: none; user-select: none;"
1113
+ >
1114
+ {{ slice.percent }}
1115
+ </text>
1116
+ </g>
1117
+ </g>
1118
+
1119
+ <!-- 8. Radar Chart -->
1120
+ <g v-if="chartType === 'radar'">
1121
+ <g v-if="showGridlines">
1122
+ <polygon
1123
+ v-for="(lvlPoints, lvlIdx) in radarLevels"
1124
+ :key="'radar-lvl-' + lvlIdx"
1125
+ :points="lvlPoints"
1126
+ fill="none"
1127
+ :stroke="gridColor"
1128
+ stroke-width="1"
1129
+ />
1130
+ <line
1131
+ v-for="(axis, i) in radarAxis"
1132
+ :key="'radar-axis-' + i"
1133
+ :x1="cx"
1134
+ :y1="cy"
1135
+ :x2="axis.lineX"
1136
+ :y2="axis.lineY"
1137
+ :stroke="gridColor"
1138
+ stroke-width="1"
1139
+ />
1140
+ </g>
1141
+
1142
+ <text v-if="showAxes"
1143
+ v-for="(axis, i) in radarAxis"
1144
+ :key="'radar-txt-' + i"
1145
+ :x="axis.labelX"
1146
+ :y="axis.labelY"
1147
+ :text-anchor="axis.align"
1148
+ font-size="9"
1149
+ font-weight="500"
1150
+ :fill="textColor"
1151
+ :font-family="fontFamily"
1152
+ >
1153
+ {{ axis.label }}
1154
+ </text>
1155
+
1156
+ <g v-for="(poly, j) in radarPolygons" :key="'radar-poly-' + j">
1157
+ <polygon
1158
+ :points="poly.points"
1159
+ :fill="poly.color"
1160
+ fill-opacity="0.25"
1161
+ :stroke="poly.color"
1162
+ stroke-width="2.5"
1163
+ stroke-linejoin="round"
1164
+ />
1165
+ <template v-if="showDataLabels">
1166
+ <text
1167
+ v-for="(vtx, vi) in poly.vertices"
1168
+ :key="'radar-val-' + j + '-' + vi"
1169
+ :x="vtx.x"
1170
+ :y="vtx.y - 5"
1171
+ text-anchor="middle"
1172
+ font-size="9"
1173
+ font-weight="600"
1174
+ :fill="textColor"
1175
+ :font-family="fontFamily"
1176
+ >
1177
+ {{ vtx.val }}
1178
+ </text>
1179
+ </template>
1180
+ </g>
1181
+ </g>
1182
+
1183
+ <!-- Embedded Data Table (when showDataTable is enabled) -->
1184
+ <g v-if="showDataTable" :transform="`translate(${padding.left}, ${height - dataTableHeight - 4})`">
1185
+ <rect x="0" y="0" :width="chartWidth" :height="dataTableRowH" fill="#f8fafc" :stroke="gridColor" stroke-width="0.8" />
1186
+ <text
1187
+ v-for="(lbl, cIdx) in labels"
1188
+ :key="'dt-h-' + cIdx"
1189
+ :x="60 + (cIdx + 0.5) * ((chartWidth - 60) / (labels.length || 1))"
1190
+ :y="11"
1191
+ text-anchor="middle"
1192
+ font-size="9"
1193
+ font-weight="600"
1194
+ :fill="textColor"
1195
+ >
1196
+ {{ lbl }}
1197
+ </text>
1198
+ <g
1199
+ v-for="(ds, rIdx) in (isPieFamily ? datasets.slice(0, 1) : datasets.slice(0, 3))"
1200
+ :key="'dt-r-' + rIdx"
1201
+ :transform="`translate(0, ${(rIdx + 1) * dataTableRowH})`"
1202
+ >
1203
+ <rect x="0" y="0" :width="chartWidth" :height="dataTableRowH" fill="#ffffff" :stroke="gridColor" stroke-width="0.8" />
1204
+ <text x="6" y="11" font-size="9" font-weight="600" :fill="textColor">
1205
+ {{ ds.label || `系列${rIdx + 1}` }}
1206
+ </text>
1207
+ <text
1208
+ v-for="(lbl, cIdx) in labels"
1209
+ :key="'dt-c-' + rIdx + '-' + cIdx"
1210
+ :x="60 + (cIdx + 0.5) * ((chartWidth - 60) / (labels.length || 1))"
1211
+ :y="11"
1212
+ text-anchor="middle"
1213
+ font-size="9"
1214
+ :fill="textColor"
1215
+ >
1216
+ {{ ds.data[cIdx] ?? 0 }}
1217
+ </text>
1218
+ </g>
1219
+ </g>
1220
+
1221
+ <!-- Unified Chart Legends for all chart types -->
1222
+ <g v-if="showLegend">
1223
+ <g
1224
+ v-for="(item, idx) in legendItems"
1225
+ :key="'legend-' + idx"
1226
+ :transform="`translate(${item.x}, ${item.y})`"
1227
+ >
1228
+ <rect x="0" y="-8" width="10" height="10" rx="2" :fill="item.color" />
1229
+ <text
1230
+ x="16"
1231
+ y="1"
1232
+ font-size="10"
1233
+ font-weight="600"
1234
+ :fill="textColor"
1235
+ :font-family="fontFamily"
1236
+ >
1237
+ {{ item.label }}{{ item.percent ? ` (${item.percent})` : '' }}
1238
+ </text>
1239
+ </g>
1240
+ </g>
1241
+ </svg>
1242
+ </template>