File size: 27,096 Bytes
e134901
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
/*
REQUIREMENTS_BEGIN
{
    "chart_type": "Grouped Scatterplot",
    "chart_name": "grouped_scatterplot_05",
    "required_fields": ["x", "y", "y2", "group"],
    "required_fields_type": [["categorical"], ["numerical"], ["numerical"], ["categorical"]],
    "required_fields_range": [[8, 150], [0, "inf"], [0, "inf"], [2, 6]],
    "required_fields_icons": ["group"],
    "required_other_icons": [],
    "required_fields_colors": [],
    "required_other_colors": ["primary"],
    "hierarchy": ["group"],
    "supported_effects": ["shadow", "radius_corner"],
    "min_height": 750,
    "min_width": 750,
    "background": "no",
    "icon_mark": "replace",
    "icon_label": "none",
    "has_x_axis": "yes",
    "has_y_axis": "yes"
}
REQUIREMENTS_END
*/

function makeChart(containerSelector, data) {
    // Extract data
    const jsonData = data;
    const chartData = jsonData.data.data;
    const variables = jsonData.variables;
    const typography = jsonData.typography;
    const dataColumns = chartUtils.schema.columns(jsonData);
    const images = jsonData.images || {};
    const colors = jsonData.colors;
    const colorResolver = chartUtils.color.resolver(jsonData);
    const effects = variables.effects || {};
    
    // 强制启用阴影效果和网格线,确保它们始终显示
    const hasShadow = true; // 将其设置为true,不再依赖effects.shadow
    const hasGridlines = true;
    
    // Clear container
    d3.select(containerSelector).html("");
    
    // Get field names
    const xField = chartUtils.schema.columnField(dataColumns, 0);
    const yField = chartUtils.schema.columnField(dataColumns, 1);
    const y2Field = chartUtils.schema.columnField(dataColumns, 2);
    const groupField = chartUtils.schema.channel(jsonData, "group", { fallbackKey: "" }).key;
    // Set dimensions and margins
    const width = variables.width;
    const height = variables.height;
    const margin = { top: 50, right: 25, bottom: 50, left: 50 };
    
    // 设置标记大小和圆角效果(移到前面定义)
    const squareSize = 24; // 正方形的尺寸
    const cornerRadius = effects.radius_corner ? 3 : 0; // 圆角半径,根据效果设置
    
    // Create SVG
    const svg = d3.select(containerSelector)
        .append("svg")
        .attr("width", width)
        .attr("height", height)
        .attr("xmlns", "http://www.w3.org/2000/svg")
        .attr("xmlns:xlink", "http://www.w3.org/1999/xlink");
    
    // Create chart area
    const chartWidth = width - margin.left - margin.right;
    const chartHeight = height - margin.top - margin.bottom;
    
    const g = svg.append("g")
        .attr("transform", `translate(${margin.left}, ${margin.top})`);
    
    // 获取分组列表(只在一个地方定义,避免重复)
    const groups = [...new Set(chartData.map(d => d[groupField]))];
    
    // 创建定义区域,用于存放图标和滤镜
    const defs = svg.append("defs");
        
    // 预先为每个分组定义图标,避免重复
    groups.forEach((group, i) => {
        // 如果有图标,添加到defs中以便复用
        if (images.field && images.field[group]) {
            defs.append("image")
                .attr("id", `icon-${i}`)
                .attr("xlink:href", images.field[group])
                .attr("width", squareSize)
                .attr("height", squareSize)
                .attr("x", -squareSize/2)
                .attr("y", -squareSize/2);
        }
    });
    
    // Create scales
    const xExtent = d3.extent(chartData, d => +d[yField]);
    const yExtent = d3.extent(chartData, d => +d[y2Field]);
    
    // 检查数据是否包含负值或0值
    function hasNegativeOrZeroValues(data, field) {
        return data.some(d => d[field] <= 0);
    }
    
    // 判断数据分布是否不均匀
    function isDistributionUneven(data, field) {
        const values = data.map(d => d[field]);
        const extent = d3.extent(values);
        const range = extent[1] - extent[0];
        const median = d3.median(values);
        const q1 = d3.quantile(values.sort(d3.ascending), 0.25);
        const q3 = d3.quantile(values.sort(d3.ascending), 0.75);
        const iqr = q3 - q1;
        
        // 不均匀分布的判断标准
        return range > iqr * 3 || Math.abs(median - (extent[0] + extent[1])/2) > range * 0.2;
    }
    
    // 为X轴创建合适的比例尺
    const xHasNegativeOrZero = hasNegativeOrZeroValues(chartData, yField);
    const xIsUneven = isDistributionUneven(chartData, yField);
    
    const xScale = (!xHasNegativeOrZero && xIsUneven) 
        ? d3.scaleLog()
            .domain([Math.max(xExtent[0] * 0.9, 0.1), xExtent[1] * 1.1])
            .range([0, chartWidth])
        : d3.scaleLinear()
            .domain([xExtent[0] - (xExtent[1] - xExtent[0]) * 0.1, xExtent[1] + (xExtent[1] - xExtent[0]) * 0.1])
            .range([0, chartWidth]);
            
    // 为Y轴创建合适的比例尺
    const yHasNegativeOrZero = hasNegativeOrZeroValues(chartData, y2Field);
    const yIsUneven = isDistributionUneven(chartData, y2Field);
    
    const yScale = (!yHasNegativeOrZero && yIsUneven)
        ? d3.scaleLog()
            .domain([Math.max(yExtent[0] * 0.9, 0.1), yExtent[1] * 1.1])
            .range([chartHeight, 0])
        : d3.scaleLinear()
            .domain([yExtent[0] - (yExtent[1] - yExtent[0]) * 0.1, yExtent[1] + (yExtent[1] - yExtent[0]) * 0.1])
            .range([chartHeight, 0]);
    
    // 添加网格线 - 白色半透明网格线,直接控制网格线数量
    if (hasGridlines) {
        // 定义固定的网格线数量,无论是线性还是对数刻度
        const xGridCount = 8; // X轴固定8条网格线
        const yGridCount = 6; // Y轴固定6条网格线
        
        // 手动计算网格线位置,确保在对数刻度下也有固定数量
        function generateGridPositions(scale, count, domain) {
            const min = domain[0];
            const max = domain[1];
            const isLog = scale.constructor.name.includes('Log');
            
            if (isLog) {
                // 对数刻度下创建均匀分布的网格线
                const logMin = Math.log10(Math.max(min, 0.1)); // 保护性处理,避免负数或0
                const logMax = Math.log10(max);
                const step = (logMax - logMin) / (count - 1);
                
                return Array.from({length: count}, (_, i) => {
                    return Math.pow(10, logMin + step * i);
                });
            } else {
                // 线性刻度下创建均匀分布的网格线
                const step = (max - min) / (count - 1);
                return Array.from({length: count}, (_, i) => min + step * i);
            }
        }
        
        // 生成网格线位置
        const xGridPositions = generateGridPositions(
            xScale, 
            xGridCount, 
            [xExtent[0] - (xExtent[1] - xExtent[0]) * 0.1, xExtent[1] + (xExtent[1] - xExtent[0]) * 0.1]
        );
        
        const yGridPositions = generateGridPositions(
            yScale, 
            yGridCount, 
            [yExtent[0] - (yExtent[1] - yExtent[0]) * 0.1, yExtent[1] + (yExtent[1] - yExtent[0]) * 0.1]
        );
        
        // 添加垂直网格线,使用固定数量的位置
        g.append("g")
            .attr("class", "grid x-grid")
            .attr("transform", `translate(0, ${chartHeight})`)
            .call(d3.axisBottom(xScale)
                .tickSize(-chartHeight)
                .tickFormat("")
                .tickValues(xGridPositions) // 使用固定数量的位置
            )
            .selectAll("line")
            .style("stroke", "white")
            .style("stroke-width", 1.0)
            .style("stroke-opacity", 0.5)
            .style("stroke-dasharray", "none");

        // 添加水平网格线,使用固定数量的位置
        g.append("g")
            .attr("class", "grid y-grid")
            .call(d3.axisLeft(yScale)
                .tickSize(-chartWidth)
                .tickFormat("")
                .tickValues(yGridPositions) // 使用固定数量的位置
            )
            .selectAll("line")
            .style("stroke", "white")
            .style("stroke-width", 1.0)
            .style("stroke-opacity", 0.5)
            .style("stroke-dasharray", "none");

        // 隐藏网格线轴的路径
        g.selectAll(".grid path")
            .style("stroke", "none");
            
        // 确保网格线显示在数据点下方
        g.selectAll(".grid").lower();
    }
    
    // Create axes with自定义格式化 - 限制tick数量,但确保显示一定数量
    const xAxis = d3.axisBottom(xScale)
        .tickSize(0)
        .tickPadding(10)
        .ticks((!xHasNegativeOrZero && xIsUneven) ? 5 : 6) // 对数刻度使用较少的刻度
        .tickFormat(d => chartUtils.format.autoText(+(d)));
        
    const yAxis = d3.axisLeft(yScale)
        .tickSize(0)
        .tickPadding(10)
        .ticks((!yHasNegativeOrZero && yIsUneven) ? 5 : 6) // 对数刻度使用较少的刻度
        .tickFormat(d => chartUtils.format.autoText(+(d)));
    
    // Add X axis
    const xAxisGroup = g.append("g")
        .attr("class", "axis x-axis")
        .attr("transform", `translate(0, ${chartHeight})`)
        .call(xAxis)

    xAxisGroup
        .selectAll("path")
        .style("stroke", colorResolver.text({ fallback: "#333333" }).value)
        .style("stroke-width", 1)
        .style("opacity", 0.5)

    xAxisGroup
        .selectAll("text")
        .style("color", colorResolver.text({ fallback: "#333333" }).value)
        
    // Add Y axis
    const yAxisGroup = g.append("g")
        .attr("class", "axis y-axis")
        .call(yAxis)
        .style("color", colorResolver.text({ fallback: "#333333" }).value)
        .style("fill", "black");

    yAxisGroup
        .selectAll("path")
        .style("stroke", colorResolver.text({ fallback: "#333333" }).value)
        .style("stroke-width", 1)
        .style("opacity", 0.5)

    yAxisGroup
        .selectAll("text")
        .style("color", colorResolver.text({ fallback: "#333333" }).value)
        
    // 对于对数刻度,更智能地过滤刻度标签,确保至少显示关键点
    if (!xHasNegativeOrZero && xIsUneven) {
        // 获取所有刻度值
        const ticks = xAxisGroup.selectAll(".tick").data();
        const visibleTicks = new Set();
        
        // 确保显示范围端点
        if (ticks.length > 0) {
            visibleTicks.add(ticks[0]);
            visibleTicks.add(ticks[ticks.length - 1]);
        }
        
        // 添加10的整数幂刻度点
        ticks.forEach(d => {
            const log10 = Math.log10(d);
            if (Math.abs(log10 - Math.round(log10)) < 0.01) {
                visibleTicks.add(d);
            }
        });
        
        // 如果可见刻度太少,添加一些中间值
        if (visibleTicks.size < 3 && ticks.length > 3) {
            // 添加中间点
            const middleIndex = Math.floor(ticks.length / 2);
            visibleTicks.add(ticks[middleIndex]);
        }
        
        // 隐藏不需要的刻度标签
        xAxisGroup.selectAll(".tick text").each(function(d) {
            if (!visibleTicks.has(d)) {
                d3.select(this).style("display", "none");
            }
        });
    }
    
    if (!yHasNegativeOrZero && yIsUneven) {
        // 获取所有刻度值
        const ticks = yAxisGroup.selectAll(".tick").data();
        const visibleTicks = new Set();
        
        // 确保显示范围端点
        if (ticks.length > 0) {
            visibleTicks.add(ticks[0]);
            visibleTicks.add(ticks[ticks.length - 1]);
        }
        
        // 添加10的整数幂刻度点
        ticks.forEach(d => {
            const log10 = Math.log10(d);
            if (Math.abs(log10 - Math.round(log10)) < 0.01) {
                visibleTicks.add(d);
            }
        });
        
        // 如果可见刻度太少,添加一些中间值
        if (visibleTicks.size < 3 && ticks.length > 3) {
            // 添加中间点
            const middleIndex = Math.floor(ticks.length / 2);
            visibleTicks.add(ticks[middleIndex]);
        }
        
        // 隐藏不需要的刻度标签
        yAxisGroup.selectAll(".tick text").each(function(d) {
            if (!visibleTicks.has(d)) {
                d3.select(this).style("display", "none");
            }
        });
    }
    
    // Add axis titles
    g.append("text")
        .attr("class", "axis-title")
        .attr("x", chartWidth)
        .attr("y", chartHeight + margin.bottom / 2 + 15)
        .attr("text-anchor", "end")
        .attr("font-size", 13)
        .text(yField);
        
    g.append("text")
        .attr("class", "axis-title")
        .attr("transform", "rotate(-90)")
        .attr("x", -margin.top)
        .attr("y", -margin.left / 2 - 10)
        .attr("text-anchor", "end")
        .attr("font-size", 13)
        .text(y2Field);
    
    // Create tooltip
    const tooltip = d3.select("body").append("div")
        .attr("class", "tooltip")
        .style("opacity", 0);
    
    // 计算标签边界的辅助函数(移到外层作用域)
    function calculateLabelBounds(pointX, pointY, position, labelWidth, labelHeight) {
        let labelX1, labelY1;
        
        if (position.priority === 1) { // right
            labelX1 = pointX + 20;
            labelY1 = pointY - labelHeight/2;
        } else if (position.priority === 2) { // top
            labelX1 = pointX - labelWidth / 2;
            labelY1 = pointY - 20 - labelHeight;
        } else if (position.priority === 3) { // left
            labelX1 = pointX - 20 - labelWidth;
            labelY1 = pointY - labelHeight/2;
        } else if (position.priority === 4) { // bottom
            labelX1 = pointX - labelWidth / 2;
            labelY1 = pointY + 20;
        } else if (position.priority === 5) { // top-right
            labelX1 = pointX + 15;
            labelY1 = pointY - 15 - labelHeight;
        } else if (position.priority === 6) { // top-left
            labelX1 = pointX - 15 - labelWidth;
            labelY1 = pointY - 15 - labelHeight;
        } else if (position.priority === 7) { // bottom-left
            labelX1 = pointX - 15 - labelWidth;
            labelY1 = pointY + 20;
        } else { // bottom-right
            labelX1 = pointX + 15;
            labelY1 = pointY + 20;
        }
        
        return {
            x1: labelX1,
            y1: labelY1,
            x2: labelX1 + labelWidth,
            y2: labelY1 + labelHeight
        };
    }
    
    // Helper function to find optimal label position
    function findOptimalPosition(d, allPoints, currentPositions = {}) {
        const positions = [
            { x: 20, y: 4, anchor: "start", priority: 1 },         // right
            { x: 0, y: -20, anchor: "middle", priority: 2 },       // top
            { x: -20, y: 4, anchor: "end", priority: 3 },          // left
            { x: 0, y: 28, anchor: "middle", priority: 4 },        // bottom
            { x: 20, y: -20, anchor: "start", priority: 5 },       // top-right
            { x: -20, y: -20, anchor: "end", priority: 6 },        // top-left
            { x: -20, y: 28, anchor: "end", priority: 7 },         // bottom-left
            { x: 20, y: 28, anchor: "start", priority: 8 }         // bottom-right
        ];

        const pointX = xScale(+d[yField]);
        const pointY = yScale(+d[y2Field]);

        // 如果已经有位置分配,直接返回
        if (currentPositions[d[xField]]) {
            return currentPositions[d[xField]];
        }

        const textBBox = chartUtils.text.measure(g, d[xField], {
            fontFamily: typography.label.font_family,
            fontSize: 10,
        });

        const labelWidth = textBBox.width;
        const labelHeight = textBBox.height;

        // 贪心算法:按优先级顺序尝试每个位置,选择第一个没有重叠的位置
        for (const pos of positions) {
            let hasOverlap = false;

            // 计算标签边界
            let labelX1, labelY1, labelX2, labelY2;

            if (pos.priority === 1) { // right
                labelX1 = pointX + 20;
                labelY1 = pointY - labelHeight / 2;
            } else if (pos.priority === 2) { // top
                labelX1 = pointX - labelWidth / 2;
                labelY1 = pointY - 20 - labelHeight;
            } else if (pos.priority === 3) { // left
                labelX1 = pointX - 20 - labelWidth;
                labelY1 = pointY - labelHeight / 2;
            } else if (pos.priority === 4) { // bottom
                labelX1 = pointX - labelWidth / 2;
                labelY1 = pointY + 20;
            } else if (pos.priority === 5) { // top-right
                labelX1 = pointX + 15;
                labelY1 = pointY - 15 - labelHeight;
            } else if (pos.priority === 6) { // top-left
                labelX1 = pointX - 15 - labelWidth;
                labelY1 = pointY - 15 - labelHeight;
            } else if (pos.priority === 7) { // bottom-left
                labelX1 = pointX - 15 - labelWidth;
                labelY1 = pointY + 15;
            } else { // bottom-right
                labelX1 = pointX + 15;
                labelY1 = pointY + 15;
            }

            labelX2 = labelX1 + labelWidth;
            labelY2 = labelY1 + labelHeight;

            // 检查边界约束
            if (labelX1 < 0 || labelX2 > chartWidth || labelY1 < 0 || labelY2 > chartHeight) {
                continue;
            }

            // 检查与其他点及其标签的重叠
            for (const p of allPoints) {
                if (p === d) continue;

                const pX = xScale(p[yField]);
                const pY = yScale(p[y2Field]);

                // 检查与点的重叠
                const pointRadius = circleRadius;
                const dx = labelX1 + labelWidth/2 - pX;
                const dy = labelY1 + labelHeight/2 - pY;
                const distance = Math.sqrt(dx * dx + dy * dy);
                    
                if (distance < pointRadius + Math.sqrt(labelWidth * labelWidth + labelHeight * labelHeight) / 2) {
                    hasOverlap = true;
                    break;
                }

                // 检查与其他标签的重叠
                const pPos = currentPositions[p[xField]];
                if (pPos) {
                    const otherBBox = chartUtils.text.measure(g, p[xField], {
                        fontFamily: typography.label.font_family,
                        fontSize: 10,
                    });

                    let otherX1, otherY1;
                    if (pPos.anchor === "start") {
                        otherX1 = pX + pPos.x;
                        otherY1 = pY + pPos.y - otherBBox.height/2;
                    } else if (pPos.anchor === "middle") {
                        otherX1 = pX + pPos.x - otherBBox.width/2;
                        otherY1 = pY + pPos.y;
                    } else {
                        otherX1 = pX + pPos.x - otherBBox.width;
                        otherY1 = pY + pPos.y - otherBBox.height/2;
                    }

                    if (labelX1 < otherX1 + otherBBox.width && labelX2 > otherX1 &&
                        labelY1 < otherY1 + otherBBox.height && labelY2 > otherY1) {
                        hasOverlap = true;
                        break;
                    }
                }
            }

            if (!hasOverlap) {
                return { ...pos, canShow: true };
            }
        }

        // 如果所有位置都有重叠,返回优先级最高的位置,但标记为不显示
        return { ...positions[0], canShow: false };
    }
    
    // Determine circle size based on number of data points
    const numPoints = chartData.length;
    const circleRadius = numPoints <= 15 ? 15 : Math.max(10, 15 - (numPoints - 15) / 20);
    
    // 添加数据点
    const points = g.selectAll(".data-point")
        .data(chartData)
        .enter()
        .append("g")
        .attr("class", "data-point")
        .attr("transform", d => `translate(${xScale(+d[yField])}, ${yScale(+d[y2Field])})`);
    
    // Add white circular background
    points.append("circle")
        .attr("r", circleRadius)
        .attr("fill", d => colorResolver.field(d[groupField], 0, { fallbackKey: "primary" }).value)
        .attr("stroke", d => colorResolver.field(d[groupField], 0, { fallbackKey: "primary" }).value)
        .attr("stroke-width", 8);
        
    // Add icon images
    points.append("image")
        .attr("xlink:href", d => images.field[d[groupField]])
        .attr("width", circleRadius * 2)
        .attr("height", circleRadius * 2)
        .attr("x", -circleRadius)
        .attr("y", -circleRadius);
        
    // Calculate optimal positions for all labels
    let labelPositions = {};
    chartData.forEach(d => {
        labelPositions[d[xField]] = findOptimalPosition(d, chartData, labelPositions);
    });
    
    // Add labels with optimized positions, only showing non-overlapping ones
    points.append("text")
        .attr("class", "data-label")
        .attr("x", d => labelPositions[d[xField]].x)
        .attr("y", d => labelPositions[d[xField]].y)
        .attr("text-anchor", d => labelPositions[d[xField]].anchor)
        .style("font-family", typography.label.font_family)
        .style("font-size", 10)
        .style("font-weight", typography.label.font_weight)
        .style("opacity", d => labelPositions[d[xField]].canShow ? 1 : 0)
        .text(d => d[xField]);
    
    // Add interactivity
    points
        .on("mouseover", function(event, d) {
            const markGroup = d3.select(this).select(".mark-group");
            
            // 判断是使用图标还是圆形,分别处理放大效果
            if (markGroup.select("use").size() > 0) {
                markGroup.select("use")
                    .transition()
                    .duration(200)
                    .attr("transform", "scale(1.2)");
            } else if (markGroup.select("circle").size() > 0) {
                markGroup.select("circle")
                    .transition()
                    .duration(200)
                    .attr("r", squareSize * 0.6);
            }
                
            d3.select(this).select(".data-label")
                .style("font-weight", "bold");
                
            // 格式化工具提示中的数值
            const yValueFormatted = chartUtils.format.autoText(+(d[yField]));
            const y2ValueFormatted = chartUtils.format.autoText(+(d[y2Field]));
                
            tooltip.transition()
                .duration(200)
                .style("opacity", 0.9);
            tooltip.html(`<strong>${d[xField]}</strong><br/>${yField}: ${yValueFormatted}<br/>${y2Field}: ${y2ValueFormatted}`)
                .style("left", (event.pageX + 10) + "px")
                .style("top", (event.pageY - 20) + "px");
        })
        .on("mouseout", function() {
            const markGroup = d3.select(this).select(".mark-group");
            
            // 判断是使用图标还是圆形,分别处理还原效果
            if (markGroup.select("use").size() > 0) {
                markGroup.select("use")
                    .transition()
                    .duration(200)
                    .attr("transform", "scale(1)");
            } else if (markGroup.select("circle").size() > 0) {
                markGroup.select("circle")
                    .transition()
                    .duration(200)
                    .attr("r", squareSize/2);
            }
                
            d3.select(this).select(".data-label")
                .style("font-weight", "normal");
                
            tooltip.transition()
                .duration(500)
                .style("opacity", 0);
        });
    
    // 图例配置参数
    const legendFontSize = parseFloat(typography.label?.font_size || 12);
    const legendFontWeight = typography.label?.font_weight || "normal";
    const legendFontFamily = typography.label?.font_family || "Arial";
    const legendColor = colorResolver.text({ fallback: "#333333" }).value;
    const legendSquareSize = 20; // 图例方块尺寸
    const legendItemPadding = 5; // 方块与文本间距
    const legendColumnPadding = 20; // 图例项间距
    
    // 创建图例组,标题放在左侧
    if (groups.length > 0) {
        // 创建图例容器
        const legendTitleText = groupField;
        const titleWidth = chartUtils.text.measure(svg, legendTitleText, {
            fontSize: legendFontSize + 1,
            fontFamily: legendFontFamily,
            fontWeight: "bold",
        }).width;
        const legendDraw = chartUtils.legend.draw(svg, groups, {
            color: group => colorScale(group),
            colorResolver,
            colors,
        }, {
            x: margin.left + titleWidth + 15,
            y: legendY,
            markerShape: "rect",
            markerSize: legendSquareSize,
            labelGap: legendItemPadding,
            itemGap: legendColumnPadding,
            fontSize: legendFontSize,
            fontFamily: legendFontFamily,
            fontWeight: legendFontWeight,
            textColor: legendColor,
        });
        const legendBgWidth = titleWidth + 15 + legendDraw.width + 20;
        const legendBgHeight = legendSquareSize + 20;
        const legendBgX = margin.left - 10;
        const legendBgY = legendY - legendBgHeight / 2;
        const legendGroup = svg.insert("g", ".chart-legend").attr("class", "chart-legend");
        legendGroup.append("rect")
            .attr("class", "legend-background")
            .attr("x", legendBgX)
            .attr("y", legendBgY)
            .attr("width", legendBgWidth)
            .attr("height", legendBgHeight)
            .attr("fill", "white")
            .attr("stroke", "#ddd")
            .attr("rx", 4)
            .attr("ry", 4);
        legendGroup.append("text")
            .attr("class", "legend-title")
            .attr("x", margin.left)
            .attr("y", legendY)
            .attr("dominant-baseline", "middle")
            .style("font-family", legendFontFamily)
            .style("font-size", `${legendFontSize + 1}px`)
            .style("font-weight", "bold")
            .style("fill", legendColor)
            .text(legendTitleText);
    }
    
    // 统一处理轴标签格式化
    function applyFormattedAxisLabels(axisGroup) {
        axisGroup.selectAll(".tick text")
            .each(function(d) {
                const formattedValue = chartUtils.format.autoText(+(d));
                d3.select(this).text(formattedValue);
            });
    }
    
    // 修改轴标签格式
    applyFormattedAxisLabels(xAxisGroup);
    applyFormattedAxisLabels(yAxisGroup);
    
    return svg.node();
}