schangxiang@126.com
2025-09-17 c9c91ad3e3ac218d81c86809c93a35622b51a686
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
/**
 * echarts图表类:维恩图
 *
 * @desc echarts基于Canvas,纯Javascript图表库,提供直观,生动,可交互,可个性化定制的数据统计图表。
 * @author Loutongbing (娄同兵, loutongbing@126.com)
 */
 
define(function (require) {
    var ChartBase = require('./base');
    // 图形依赖
 
    var TextShape = require('zrender/shape/Text');
    var CircleShape = require('zrender/shape/Circle');
    var PathShape = require('zrender/shape/Path');
 
    var ecConfig = require('../config');
    // 维恩图默认参数
    ecConfig.venn = {
        zlevel: 0,                  // 一级层叠
        z: 1,                       // 二级层叠
        calculable: false
    };
 
    var ecData = require('../util/ecData');
    var zrUtil = require('zrender/tool/util');
 
    /**
     * 构造函数
     * @param {Object} messageCenter echart消息中心
     * @param {ZRender} zr zrender实例
     * @param {Object} series 数据
     * @param {Object} component 组件
     * @constructor
     * @exports Venn
     */
    function Venn(ecTheme, messageCenter, zr, option, myChart) {
        // 图表基类
        ChartBase.call(this, ecTheme, messageCenter, zr, option, myChart);
 
        this.refresh(option);
    }
    
    Venn.prototype = {
        type : ecConfig.CHART_TYPE_VENN,
        /**
         * 绘制图形
         */
        _buildShape : function () {
            this.selectedMap = {};
            this._symbol = this.option.symbolList;
            this._queryTarget;
            this._dropBoxList = [];
            this._vennDataCounter = 0;
            var series = this.series;
            var legend = this.component.legend;
            
            for (var i = 0; i < series.length; i++) {
                if (series[i].type === ecConfig.CHART_TYPE_VENN) {
                    series[i] = this.reformOption(series[i]);
                    var serieName = series[i].name || '';
 
                    // 系列图例开关
                    this.selectedMap[serieName] = 
                        legend ? legend.isSelected(serieName) : true;
                    if (!this.selectedMap[serieName]) {
                        continue;
                    }
 
                    this._buildVenn(i);
                }
            }
            
            this.addShapeList();
        },
        /**
         * 构建单个
         *
         * @param {Object} data 数据
         */
        _buildVenn : function (seriesIndex) {
            var r0;
            var r1;
            var serie = this.series[seriesIndex];
            var data = serie.data;
            if (data[0].value > data[1].value) {
                r0 = this.zr.getHeight() / 3;
                r1 = r0 * Math.sqrt(data[1].value) / Math.sqrt(data[0].value);
            }
            else {
                r1 = this.zr.getHeight() / 3;
                r0 = r1 * Math.sqrt(data[0].value) / Math.sqrt(data[1].value);
            }
 
            var x0 = this.zr.getWidth() / 2 - r0;
            // 估值 两个圆心的距离与两圆半径均值之比等于交集与两集合均值的开方之比。
            // 公共距离(coincideLengthAnchor)/ 公共数值开方 = 半径平均值 / 各自数值均值的开方
            var coincideLengthAnchor = ((r0 + r1) / 2) * Math.sqrt(data[2].value) / Math.sqrt((data[0].value + data[1].value) / 2);
            // 如果两者没有公共面积,则圆心距就为两圆半径之和
            var coincideLength = r0 + r1;
            if (data[2].value !== 0) {
                coincideLength = this._getCoincideLength(
                    data[0].value,
                    data[1].value,
                    data[2].value,
                    r0, r1,
                    coincideLengthAnchor,
                    Math.abs(r0 - r1),
                    r0 + r1
                );
            }
 
            var x1 = x0 +  coincideLength;
            var y = this.zr.getHeight() / 2;
            this._buildItem(
                seriesIndex, 0, data[0],
                x0,
                y,
                r0
            );
            this._buildItem(
                seriesIndex, 1, data[1],
                x1,
                y,
                r1
            );
            // 包含关系与无交集关系均不画公共部分
            if (
                data[2].value !== 0
                && data[2].value !== data[0].value
                && data[2].value !== data[1].value
            ) {
                var xLeft = (r0 * r0 - r1 * r1) / (2 * coincideLength) + coincideLength / 2;
                var xRight = coincideLength / 2 - (r0 * r0 - r1 * r1) / (2 * coincideLength);
                var h = Math.sqrt(r0 * r0 - xLeft * xLeft);
                // 判断大小圆弧
                var rightLargeArcFlag = 0;
                var leftLargeArcFlag = 0;
                if (
                    data[0].value > data[1].value
                    && x1 < x0 + xLeft
                ) {
                    leftLargeArcFlag = 1;
                }
                if (
                    data[0].value < data[1].value
                    && x1 < x0 + xRight
                ) {
                    rightLargeArcFlag = 1;
                }
                this._buildCoincideItem(
                    seriesIndex,
                    2,
                    data[2],
                    x0 + xLeft,
                    y - h,
                    y + h,
                    r0,
                    r1,
                    rightLargeArcFlag,
                    leftLargeArcFlag
                );
            }
        },
        /**
         * 逼近算法得到两圆的间距
         * @param {number} value0 第一个圆的原始数值
         * @param {number} value1 第二个圆的原始数值
         * @param {number} value3 公共部分的原始数值
         * @param {number} r0 第一个圆的半径
         * @param {number} r1 第二个圆的半径
         * @param {number} coincideLengthAnchor 锚定
         * @param {number} coincideLengthAnchorMin 下限
         * @param {number} coincideLengthAnchorMax 上限
         * @return {Node}
        */
        _getCoincideLength: function (
            value0,
            value1,
            value2,
            r0,
            r1,
            coincideLengthAnchor,
            coincideLengthAnchorMin,
            coincideLengthAnchorMax
        ) {
            // 计算
            var x = (r0 * r0 - r1 * r1) / (2 * coincideLengthAnchor) + coincideLengthAnchor / 2;
            var y = coincideLengthAnchor / 2 - (r0 * r0 - r1 * r1) / (2 * coincideLengthAnchor);
            // 夹角
            var alfa = Math.acos(x / r0);
            var beta = Math.acos(y / r1);
            // 第一个圆的面积
            var area0 = r0 * r0 * Math.PI;
            // 计算的公共面积 (思路是扇形减三角形)
            var area2 = alfa * r0 * r0 - x * r0 * Math.sin(alfa) + beta * r1 * r1 - y * r1 * Math.sin(beta);
            var scaleAnchor = area2 / area0;
            var scale = value2 / value0;
            var approximateValue = Math.abs(scaleAnchor / scale);
            if (approximateValue > 0.999 && approximateValue < 1.001) {
                return coincideLengthAnchor;
            }
            // 若是公共面积比较小,使距离减小一些,让公共面积增大
            else if (approximateValue <= 0.999) {
                coincideLengthAnchorMax = coincideLengthAnchor;
                // 二分法计算新的步调
                coincideLengthAnchor = (coincideLengthAnchor + coincideLengthAnchorMin) / 2;
                return this._getCoincideLength(value0, value1, value2, r0, r1,
                    coincideLengthAnchor, coincideLengthAnchorMin, coincideLengthAnchorMax);
            }
            // 若是公共面积比较大,使距离增大一些,让公共面积减小
            else {
                coincideLengthAnchorMin = coincideLengthAnchor;
                coincideLengthAnchor = (coincideLengthAnchor + coincideLengthAnchorMax) / 2;
                return this._getCoincideLength(value0, value1, value2, r0, r1,
                    coincideLengthAnchor, coincideLengthAnchorMin, coincideLengthAnchorMax);
            }
        },
 
        /**
         * 构建单个圆及指标
         */
        _buildItem : function (
            seriesIndex, dataIndex, dataItem,
            x, y, r
        ) {
            var series = this.series;
            var serie = series[seriesIndex];
 
            var circle = this.getCircle(
                seriesIndex,
                dataIndex,
                dataItem,
                x, y, r
            );
            ecData.pack(
                circle,
                serie, seriesIndex,
                dataItem, dataIndex,
                dataItem.name
            );
            this.shapeList.push(circle);
 
            if (serie.itemStyle.normal.label.show) {
                // 文本标签
                var label = this.getLabel(
                    seriesIndex,
                    dataIndex,
                    dataItem,
                    x, y, r
                );
                ecData.pack(
                    label,
                    serie, seriesIndex,
                    serie.data[dataIndex], dataIndex,
                    serie.data[dataIndex].name
                );
                this.shapeList.push(label);
            }
        },
 
        _buildCoincideItem : function (
            seriesIndex, dataIndex, dataItem,
            x, y0, y1, r0, r1, rightLargeArcFlag, leftLargeArcFlag
        ) {
            var series = this.series;
            var serie = series[seriesIndex];
            var queryTarget = [dataItem, serie];
 
            // 多级控制
            var normal = this.deepMerge(
                queryTarget,
                'itemStyle.normal'
            ) || {};
            var emphasis = this.deepMerge(
                queryTarget,
                'itemStyle.emphasis'
            ) || {};
            var normalColor = normal.color || this.zr.getColor(dataIndex);
            var emphasisColor = emphasis.color || this.zr.getColor(dataIndex);
 
            var path = 'M' + x + ',' + y0
                       + 'A' + r0 + ',' + r0 + ',0,' + rightLargeArcFlag + ',1,' + x + ',' + y1
                       + 'A' + r1 + ',' + r1 + ',0,' + leftLargeArcFlag + ',1,' + x + ',' + y0;
            var style = {
                color: normalColor,
                // path: rx ry x-axis-rotation large-arc-flag sweep-flag x y
                path: path
            };
 
            var shape = {
                zlevel: serie.zlevel,
                z: serie.z,
                style: style,
                highlightStyle: {
                    color: emphasisColor,
                    lineWidth: emphasis.borderWidth,
                    strokeColor: emphasis.borderColor
                }
            };
            shape = new PathShape(shape);
            if (shape.buildPathArray) {
                shape.style.pathArray = shape.buildPathArray(style.path);
            }
            ecData.pack(
                shape,
                series[seriesIndex], 0,
                dataItem, dataIndex,
                dataItem.name
            );
            this.shapeList.push(shape);
        },
        /**
         * 构建圆形
         */
        getCircle : function (
            seriesIndex,
            dataIndex,
            dataItem,
            x, y, r
        ) {
            var serie = this.series[seriesIndex];
            var queryTarget = [dataItem, serie];
 
            // 多级控制
            var normal = this.deepMerge(
                queryTarget,
                'itemStyle.normal'
            ) || {};
            var emphasis = this.deepMerge(
                queryTarget,
                'itemStyle.emphasis'
            ) || {};
            var normalColor = normal.color || this.zr.getColor(dataIndex);
            var emphasisColor = emphasis.color || this.zr.getColor(dataIndex);
 
            var circle = {
                zlevel: serie.zlevel,
                z: serie.z,
                clickable: true,
                style: {
                    x: x,
                    y: y,
                    r: r,
                    brushType: 'fill',
                    opacity: 1,
                    color: normalColor
                },
                highlightStyle: {
                    color: emphasisColor,
                    lineWidth: emphasis.borderWidth,
                    strokeColor: emphasis.borderColor
                }
            };
 
            if (this.deepQuery([dataItem, serie, this.option], 'calculable')) {
                this.setCalculable(circle);
                circle.draggable = true;
            }
            return new CircleShape(circle);
 
        },
 
        /**
         * 需要显示则会有返回构建好的shape,否则返回undefined
         */
        getLabel: function (
            seriesIndex,
            dataIndex,
            dataItem,
            x, y, r
        ) {
            var serie = this.series[seriesIndex];
            var itemStyle = serie.itemStyle;
            var queryTarget = [dataItem, serie];
 
            // 多级控制
            var normal = this.deepMerge(
                queryTarget,
                'itemStyle.normal'
            ) || {};
            var status = 'normal';
            // label配置
            var labelControl = itemStyle[status].label;
            var textStyle = labelControl.textStyle || {};
            var text = this.getLabelText(dataIndex, dataItem, status);
            var textFont = this.getFont(textStyle);
            var textColor = normal.color || this.zr.getColor(dataIndex);
            // 求出label的纵坐标
            var textSize = textStyle.fontSize || 12;
 
            var textShape = {
                zlevel: serie.zlevel,
                z: serie.z,
                style: {
                    x: x,
                    y: y - r - textSize,
                    color: textStyle.color || textColor,
                    text: text,
                    textFont: textFont,
                    textAlign: 'center'
                }
            };
 
            return new TextShape(textShape);
        },
 
        /**
         * 根据lable.format计算label text
         */
        getLabelText : function (dataIndex, dataItem, status) {
            var series = this.series;
            var serie = series[0];
            var formatter = this.deepQuery(
                [dataItem, serie],
                'itemStyle.' + status + '.label.formatter'
            );
 
            if (formatter) {
                if (typeof formatter == 'function') {
                    return formatter(
                        serie.name,
                        dataItem.name,
                        dataItem.value
                    );
                }
                else if (typeof formatter == 'string') {
                    formatter = formatter.replace('{a}','{a0}')
                                         .replace('{b}','{b0}')
                                         .replace('{c}','{c0}');
                    formatter = formatter.replace('{a0}', serie.name)
                                         .replace('{b0}', dataItem.name)
                                         .replace('{c0}', dataItem.value);
 
                    return formatter;
                }
            }
            else {
                return dataItem.name;
            }
        },
 
        /**
         * 刷新
         */
        refresh : function (newOption) {
            if (newOption) {
                this.option = newOption;
                this.series = newOption.series;
            }
 
            this._buildShape();
        }
    };
 
    zrUtil.inherits(Venn, ChartBase);
 
    // 图表注册
    require('../chart').define('venn', Venn);
 
    return Venn;
});