venn.src.js 71.4 KB
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 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765
/**
 * @license Highcharts JS v7.2.0 (2019-09-03)
 *
 * (c) 2017-2019 Highsoft AS
 * Authors: Jon Arild Nygard
 *
 * License: www.highcharts.com/license
 */
'use strict';
(function (factory) {
    if (typeof module === 'object' && module.exports) {
        factory['default'] = factory;
        module.exports = factory;
    } else if (typeof define === 'function' && define.amd) {
        define('highcharts/modules/venn', ['highcharts'], function (Highcharts) {
            factory(Highcharts);
            factory.Highcharts = Highcharts;
            return factory;
        });
    } else {
        factory(typeof Highcharts !== 'undefined' ? Highcharts : undefined);
    }
}(function (Highcharts) {
    var _modules = Highcharts ? Highcharts._modules : {};
    function _registerModule(obj, path, args, fn) {
        if (!obj.hasOwnProperty(path)) {
            obj[path] = fn.apply(null, args);
        }
    }
    _registerModule(_modules, 'mixins/draw-point.js', [], function () {
        /* *
         *
         *  !!!!!!! SOURCE GETS TRANSPILED BY TYPESCRIPT. EDIT TS FILE ONLY. !!!!!!!
         *
         * */
        var isFn = function (x) {
            return typeof x === 'function';
        };
        /* eslint-disable no-invalid-this, valid-jsdoc */
        /**
         * Handles the drawing of a component.
         * Can be used for any type of component that reserves the graphic property, and
         * provides a shouldDraw on its context.
         *
         * @private
         * @function draw
         * @param {DrawPointParams} params
         *        Parameters.
         *
         * @todo add type checking.
         * @todo export this function to enable usage
         */
        var draw = function draw(params) {
            var component = this, graphic = component.graphic, animatableAttribs = params.animatableAttribs, onComplete = params.onComplete, css = params.css, renderer = params.renderer;
            if (component.shouldDraw()) {
                if (!graphic) {
                    component.graphic = graphic =
                        renderer[params.shapeType](params.shapeArgs)
                            .add(params.group);
                }
                graphic
                    .css(css)
                    .attr(params.attribs)
                    .animate(animatableAttribs, params.isNew ? false : undefined, onComplete);
            }
            else if (graphic) {
                var destroy = function () {
                    component.graphic = graphic = graphic.destroy();
                    if (isFn(onComplete)) {
                        onComplete();
                    }
                };
                // animate only runs complete callback if something was animated.
                if (Object.keys(animatableAttribs).length) {
                    graphic.animate(animatableAttribs, undefined, function () {
                        destroy();
                    });
                }
                else {
                    destroy();
                }
            }
        };
        /**
         * An extended version of draw customized for points.
         * It calls additional methods that is expected when rendering a point.
         *
         * @param {Highcharts.Dictionary<any>} params Parameters
         */
        var drawPoint = function drawPoint(params) {
            var point = this, attribs = params.attribs = params.attribs || {};
            // Assigning class in dot notation does go well in IE8
            // eslint-disable-next-line dot-notation
            attribs['class'] = point.getClassName();
            // Call draw to render component
            draw.call(point, params);
        };

        return drawPoint;
    });
    _registerModule(_modules, 'mixins/geometry.js', [], function () {
        /* *
         *
         *  !!!!!!! SOURCE GETS TRANSPILED BY TYPESCRIPT. EDIT TS FILE ONLY. !!!!!!!
         *
         * */
        /**
         * Calculates the center between a list of points.
         * @private
         * @param {Array<Highcharts.PositionObject>} points
         *        A list of points to calculate the center of.
         * @return {Highcharts.PositionObject}
         *         Calculated center
         */
        var getCenterOfPoints = function getCenterOfPoints(points) {
            var sum = points.reduce(function (sum, point) {
                sum.x += point.x;
                sum.y += point.y;
                return sum;
            }, { x: 0, y: 0 });
            return {
                x: sum.x / points.length,
                y: sum.y / points.length
            };
        };
        /**
         * Calculates the distance between two points based on their x and y
         * coordinates.
         * @private
         * @param {Highcharts.PositionObject} p1
         *        The x and y coordinates of the first point.
         * @param {Highcharts.PositionObject} p2
         *        The x and y coordinates of the second point.
         * @return {number}
         *         Returns the distance between the points.
         */
        var getDistanceBetweenPoints = function getDistanceBetweenPoints(p1, p2) {
            return Math.sqrt(Math.pow(p2.x - p1.x, 2) + Math.pow(p2.y - p1.y, 2));
        };
        /**
         * Calculates the angle between two points.
         * @todo add unit tests.
         * @private
         * @param {Highcharts.PositionObject} p1 The first point.
         * @param {Highcharts.PositionObject} p2 The second point.
         * @return {number} Returns the angle in radians.
         */
        var getAngleBetweenPoints = function getAngleBetweenPoints(p1, p2) {
            return Math.atan2(p2.x - p1.x, p2.y - p1.y);
        };
        var geometry = {
            getAngleBetweenPoints: getAngleBetweenPoints,
            getCenterOfPoints: getCenterOfPoints,
            getDistanceBetweenPoints: getDistanceBetweenPoints
        };

        return geometry;
    });
    _registerModule(_modules, 'mixins/geometry-circles.js', [_modules['mixins/geometry.js']], function (geometry) {
        /* *
         *
         *  !!!!!!! SOURCE GETS TRANSPILED BY TYPESCRIPT. EDIT TS FILE ONLY. !!!!!!!
         *
         * */
        var getAngleBetweenPoints = geometry.getAngleBetweenPoints, getCenterOfPoints = geometry.getCenterOfPoints, getDistanceBetweenPoints = geometry.getDistanceBetweenPoints;
        /**
         * @private
         * @param {number} x
         *        Number to round
         * @param {number} decimals
         *        Number of decimals to round to
         * @return {number}
         *         Rounded number
         */
        var round = function round(x, decimals) {
            var a = Math.pow(10, decimals);
            return Math.round(x * a) / a;
        };
        /**
         * Calculates the area of a circle based on its radius.
         * @private
         * @param {number} r
         *        The radius of the circle.
         * @return {number}
         *         Returns the area of the circle.
         */
        var getAreaOfCircle = function (r) {
            if (r <= 0) {
                throw new Error('radius of circle must be a positive number.');
            }
            return Math.PI * r * r;
        };
        /**
         * Calculates the area of a circular segment based on the radius of the circle
         * and the height of the segment.
         * See http://mathworld.wolfram.com/CircularSegment.html
         * @private
         * @param {number} r
         *        The radius of the circle.
         * @param {number} h
         *        The height of the circular segment.
         * @return {number}
         *         Returns the area of the circular segment.
         */
        var getCircularSegmentArea = function getCircularSegmentArea(r, h) {
            return r * r * Math.acos(1 - h / r) - (r - h) * Math.sqrt(h * (2 * r - h));
        };
        /**
         * Calculates the area of overlap between two circles based on their radiuses
         * and the distance between them.
         * See http://mathworld.wolfram.com/Circle-CircleIntersection.html
         * @private
         * @param {number} r1
         *        Radius of the first circle.
         * @param {number} r2
         *        Radius of the second circle.
         * @param {number} d
         *        The distance between the two circles.
         * @return {number}
         *         Returns the area of overlap between the two circles.
         */
        var getOverlapBetweenCircles = function getOverlapBetweenCircles(r1, r2, d) {
            var overlap = 0;
            // If the distance is larger than the sum of the radiuses then the circles
            // does not overlap.
            if (d < r1 + r2) {
                if (d <= Math.abs(r2 - r1)) {
                    // If the circles are completely overlapping, then the overlap
                    // equals the area of the smallest circle.
                    overlap = getAreaOfCircle(r1 < r2 ? r1 : r2);
                }
                else {
                    // Height of first triangle segment.
                    var d1 = (r1 * r1 - r2 * r2 + d * d) / (2 * d), 
                    // Height of second triangle segment.
                    d2 = d - d1;
                    overlap = (getCircularSegmentArea(r1, r1 - d1) +
                        getCircularSegmentArea(r2, r2 - d2));
                }
                // Round the result to two decimals.
                overlap = round(overlap, 14);
            }
            return overlap;
        };
        /**
         * Calculates the intersection points of two circles.
         *
         * NOTE: does not handle floating errors well.
         * @private
         * @param {Highcharts.CircleObject} c1
         *        The first circle.
         * @param {Highcharts.CircleObject} c2
         *        The second sircle.
         * @return {Array<Highcharts.PositionObject>}
         *         Returns the resulting intersection points.
         */
        var getCircleCircleIntersection = function getCircleCircleIntersection(c1, c2) {
            var d = getDistanceBetweenPoints(c1, c2), r1 = c1.r, r2 = c2.r, points = [];
            if (d < r1 + r2 && d > Math.abs(r1 - r2)) {
                // If the circles are overlapping, but not completely overlapping, then
                // it exists intersecting points.
                var r1Square = r1 * r1, r2Square = r2 * r2, 
                // d^2 - r^2 + R^2 / 2d
                x = (r1Square - r2Square + d * d) / (2 * d), 
                // y^2 = R^2 - x^2
                y = Math.sqrt(r1Square - x * x), x1 = c1.x, x2 = c2.x, y1 = c1.y, y2 = c2.y, x0 = x1 + x * (x2 - x1) / d, y0 = y1 + x * (y2 - y1) / d, rx = -(y2 - y1) * (y / d), ry = -(x2 - x1) * (y / d);
                points = [
                    { x: round(x0 + rx, 14), y: round(y0 - ry, 14) },
                    { x: round(x0 - rx, 14), y: round(y0 + ry, 14) }
                ];
            }
            return points;
        };
        /**
         * Calculates all the intersection points for between a list of circles.
         * @private
         * @param {Array<Highcharts.CircleObject>} circles
         *        The circles to calculate the points from.
         * @return {Array<Highcharts.GeometryObject>}
         *         Returns a list of intersection points.
         */
        var getCirclesIntersectionPoints = function getIntersectionPoints(circles) {
            return circles.reduce(function (points, c1, i, arr) {
                var additional = arr.slice(i + 1)
                    .reduce(function (points, c2, j) {
                    var indexes = [i, j + i + 1];
                    return points.concat(getCircleCircleIntersection(c1, c2)
                        .map(function (p) {
                        p.indexes = indexes;
                        return p;
                    }));
                }, []);
                return points.concat(additional);
            }, []);
        };
        /**
         * Tests wether a point lies within a given circle.
         * @private
         * @param {Highcharts.PositionObject} point
         *        The point to test for.
         * @param {Highcharts.CircleObject} circle
         *        The circle to test if the point is within.
         * @return {boolean}
         *         Returns true if the point is inside, false if outside.
         */
        var isPointInsideCircle = function isPointInsideCircle(point, circle) {
            return getDistanceBetweenPoints(point, circle) <= circle.r + 1e-10;
        };
        /**
         * Tests wether a point lies within a set of circles.
         * @private
         * @param {Highcharts.PositionObject} point
         *        The point to test.
         * @param {Array<Highcharts.CircleObject>} circles
         *        The list of circles to test against.
         * @return {boolean}
         *         Returns true if the point is inside all the circles, false if not.
         */
        var isPointInsideAllCircles = function isPointInsideAllCircles(point, circles) {
            return !circles.some(function (circle) {
                return !isPointInsideCircle(point, circle);
            });
        };
        /**
         * Tests wether a point lies outside a set of circles.
         *
         * TODO: add unit tests.
         * @private
         * @param {Highcharts.PositionObject} point
         *        The point to test.
         * @param {Array<Highcharts.CircleObject>} circles
         *        The list of circles to test against.
         * @return {boolean}
         *         Returns true if the point is outside all the circles, false if not.
         */
        var isPointOutsideAllCircles = function isPointOutsideAllCircles(point, circles) {
            return !circles.some(function (circle) {
                return isPointInsideCircle(point, circle);
            });
        };
        /**
         * Calculate the path for the area of overlap between a set of circles.
         * @todo handle cases with only 1 or 0 arcs.
         * @private
         * @param {Array<Highcharts.CircleObject>} circles
         *        List of circles to calculate area of.
         * @return {Highcharts.GeometryIntersectionObject|undefined}
         *         Returns the path for the area of overlap. Returns an empty string if
         *         there are no intersection between all the circles.
         */
        var getAreaOfIntersectionBetweenCircles = function getAreaOfIntersectionBetweenCircles(circles) {
            var intersectionPoints = (getCirclesIntersectionPoints(circles)
                .filter(function (p) {
                return isPointInsideAllCircles(p, circles);
            })), result;
            if (intersectionPoints.length > 1) {
                // Calculate the center of the intersection points.
                var center = getCenterOfPoints(intersectionPoints);
                intersectionPoints = intersectionPoints
                    // Calculate the angle between the center and the points.
                    .map(function (p) {
                    p.angle = getAngleBetweenPoints(center, p);
                    return p;
                })
                    // Sort the points by the angle to the center.
                    .sort(function (a, b) {
                    return b.angle - a.angle;
                });
                var startPoint = intersectionPoints[intersectionPoints.length - 1];
                var arcs = intersectionPoints
                    .reduce(function (data, p1) {
                    var startPoint = data.startPoint, midPoint = getCenterOfPoints([startPoint, p1]);
                    // Calculate the arc from the intersection points and their
                    // circles.
                    var arc = p1.indexes
                        // Filter out circles that are not included in both
                        // intersection points.
                        .filter(function (index) {
                        return startPoint.indexes.indexOf(index) > -1;
                    })
                        // Iterate the circles of the intersection points and
                        // calculate arcs.
                        .reduce(function (arc, index) {
                        var circle = circles[index], angle1 = getAngleBetweenPoints(circle, p1), angle2 = getAngleBetweenPoints(circle, startPoint), angleDiff = angle2 - angle1 +
                            (angle2 < angle1 ? 2 * Math.PI : 0), angle = angle2 - angleDiff / 2, width = getDistanceBetweenPoints(midPoint, {
                            x: circle.x + circle.r * Math.sin(angle),
                            y: circle.y + circle.r * Math.cos(angle)
                        }), r = circle.r;
                        // Width can sometimes become to large due to floating
                        // point errors
                        if (width > r * 2) {
                            width = r * 2;
                        }
                        // Get the arc with the smallest width.
                        if (!arc || arc.width > width) {
                            arc = {
                                r: r,
                                largeArc: width > r ? 1 : 0,
                                width: width,
                                x: p1.x,
                                y: p1.y
                            };
                        }
                        // Return the chosen arc.
                        return arc;
                    }, null);
                    // If we find an arc then add it to the list and update p2.
                    if (arc) {
                        var r = arc.r;
                        data.arcs.push(['A', r, r, 0, arc.largeArc, 1, arc.x, arc.y]);
                        data.startPoint = p1;
                    }
                    return data;
                }, {
                    startPoint: startPoint,
                    arcs: []
                }).arcs;
                if (arcs.length === 0) {
                    // empty
                }
                else if (arcs.length === 1) {
                    // empty
                }
                else {
                    arcs.unshift(['M', startPoint.x, startPoint.y]);
                    result = {
                        center: center,
                        d: arcs
                    };
                }
            }
            return result;
        };
        var geometryCircles = {
            getAreaOfCircle: getAreaOfCircle,
            getAreaOfIntersectionBetweenCircles: getAreaOfIntersectionBetweenCircles,
            getCircleCircleIntersection: getCircleCircleIntersection,
            getCirclesIntersectionPoints: getCirclesIntersectionPoints,
            getCircularSegmentArea: getCircularSegmentArea,
            getOverlapBetweenCircles: getOverlapBetweenCircles,
            isPointInsideCircle: isPointInsideCircle,
            isPointInsideAllCircles: isPointInsideAllCircles,
            isPointOutsideAllCircles: isPointOutsideAllCircles,
            round: round
        };

        return geometryCircles;
    });
    _registerModule(_modules, 'mixins/nelder-mead.js', [], function () {
        /* *
         *
         *  !!!!!!! SOURCE GETS TRANSPILED BY TYPESCRIPT. EDIT TS FILE ONLY. !!!!!!!
         *
         * */
        /* eslint-disable valid-jsdoc */
        var getCentroid = function (simplex) {
            var arr = simplex.slice(0, -1), length = arr.length, result = [], sum = function (data, point) {
                data.sum += point[data.i];
                return data;
            };
            for (var i = 0; i < length; i++) {
                result[i] = arr.reduce(sum, { sum: 0, i: i }).sum / length;
            }
            return result;
        };
        /**
         * Finds an optimal position for a given point.
         * @todo add unit tests.
         * @todo add constraints to optimize the algorithm.
         * @private
         * @param {Highcharts.NelderMeadTestFunction} fn
         *        The function to test a point.
         * @param {Highcharts.NelderMeadPointArray} initial
         *        The initial point to optimize.
         * @return {Highcharts.NelderMeadPointArray}
         *         Returns the opimized position of a point.
         */
        var nelderMead = function nelderMead(fn, initial) {
            var maxIterations = 100, sortByFx = function (a, b) {
                return a.fx - b.fx;
            }, pRef = 1, // Reflection parameter
            pExp = 2, // Expansion parameter
            pCon = -0.5, // Contraction parameter
            pOCon = pCon * pRef, // Outwards contraction parameter
            pShrink = 0.5; // Shrink parameter
            /**
             * @private
             */
            var weightedSum = function weightedSum(weight1, v1, weight2, v2) {
                return v1.map(function (x, i) {
                    return weight1 * x + weight2 * v2[i];
                });
            };
            /**
             * @private
             */
            var getSimplex = function getSimplex(initial) {
                var n = initial.length, simplex = new Array(n + 1);
                // Initial point to the simplex.
                simplex[0] = initial;
                simplex[0].fx = fn(initial);
                // Create a set of extra points based on the initial.
                for (var i = 0; i < n; ++i) {
                    var point = initial.slice();
                    point[i] = point[i] ? point[i] * 1.05 : 0.001;
                    point.fx = fn(point);
                    simplex[i + 1] = point;
                }
                return simplex;
            };
            var updateSimplex = function (simplex, point) {
                point.fx = fn(point);
                simplex[simplex.length - 1] = point;
                return simplex;
            };
            var shrinkSimplex = function (simplex) {
                var best = simplex[0];
                return simplex.map(function (point) {
                    var p = weightedSum(1 - pShrink, best, pShrink, point);
                    p.fx = fn(p);
                    return p;
                });
            };
            var getPoint = function (centroid, worst, a, b) {
                var point = weightedSum(a, centroid, b, worst);
                point.fx = fn(point);
                return point;
            };
            // Create a simplex
            var simplex = getSimplex(initial);
            // Iterate from 0 to max iterations
            for (var i = 0; i < maxIterations; i++) {
                // Sort the simplex
                simplex.sort(sortByFx);
                // Create a centroid from the simplex
                var worst = simplex[simplex.length - 1];
                var centroid = getCentroid(simplex);
                // Calculate the reflected point.
                var reflected = getPoint(centroid, worst, 1 + pRef, -pRef);
                if (reflected.fx < simplex[0].fx) {
                    // If reflected point is the best, then possibly expand.
                    var expanded = getPoint(centroid, worst, 1 + pExp, -pExp);
                    simplex = updateSimplex(simplex, (expanded.fx < reflected.fx) ? expanded : reflected);
                }
                else if (reflected.fx >= simplex[simplex.length - 2].fx) {
                    // If the reflected point is worse than the second worse, then
                    // contract.
                    var contracted;
                    if (reflected.fx > worst.fx) {
                        // If the reflected is worse than the worst point, do a
                        // contraction
                        contracted = getPoint(centroid, worst, 1 + pCon, -pCon);
                        if (contracted.fx < worst.fx) {
                            simplex = updateSimplex(simplex, contracted);
                        }
                        else {
                            simplex = shrinkSimplex(simplex);
                        }
                    }
                    else {
                        // Otherwise do an outwards contraction
                        contracted = getPoint(centroid, worst, 1 - pOCon, pOCon);
                        if (contracted.fx < reflected.fx) {
                            simplex = updateSimplex(simplex, contracted);
                        }
                        else {
                            simplex = shrinkSimplex(simplex);
                        }
                    }
                }
                else {
                    simplex = updateSimplex(simplex, reflected);
                }
            }
            return simplex[0];
        };
        var content = {
            getCentroid: getCentroid,
            nelderMead: nelderMead
        };

        return content;
    });
    _registerModule(_modules, 'modules/venn.src.js', [_modules['mixins/draw-point.js'], _modules['mixins/geometry.js'], _modules['mixins/geometry-circles.js'], _modules['mixins/nelder-mead.js'], _modules['parts/Globals.js'], _modules['parts/Utilities.js']], function (draw, geometry, geometryCircles, NelderMeadModule, H, U) {
        /* *
         * Experimental Highcharts module which enables visualization of a Venn Diagram.
         *
         * (c) 2016-2019 Highsoft AS
         *
         * Authors: Jon Arild Nygard
         *
         * Layout algorithm by Ben Frederickson:
         * https://www.benfrederickson.com/better-venn-diagrams/
         *
         * License: www.highcharts.com/license
         */



        // TODO: replace with individual imports
        var nelderMead = NelderMeadModule.nelderMead;


        var isArray = U.isArray,
            isNumber = U.isNumber,
            isObject = U.isObject,
            isString = U.isString;


        var addEvent = H.addEvent,
            color = H.Color,
            extend = H.extend,
            getAreaOfCircle = geometryCircles.getAreaOfCircle,
            getAreaOfIntersectionBetweenCircles =
                geometryCircles.getAreaOfIntersectionBetweenCircles,
            getCircleCircleIntersection = geometryCircles.getCircleCircleIntersection,
            getCenterOfPoints = geometry.getCenterOfPoints,
            getDistanceBetweenPoints = geometry.getDistanceBetweenPoints,
            getOverlapBetweenCirclesByDistance =
                geometryCircles.getOverlapBetweenCircles,
            isPointInsideAllCircles = geometryCircles.isPointInsideAllCircles,
            isPointInsideCircle = geometryCircles.isPointInsideCircle,
            isPointOutsideAllCircles = geometryCircles.isPointOutsideAllCircles,
            merge = H.merge,
            seriesType = H.seriesType,
            seriesTypes = H.seriesTypes;

        var objectValues = function objectValues(obj) {
            return Object.keys(obj).map(function (x) {
                return obj[x];
            });
        };

        /**
         * Calculates the area of overlap between a list of circles.
         * @private
         * @todo add support for calculating overlap between more than 2 circles.
         * @param {Array<object>} circles List of circles with their given positions.
         * @return {number} Returns the area of overlap between all the circles.
         */
        var getOverlapBetweenCircles = function getOverlapBetweenCircles(circles) {
            var overlap = 0;

            // When there is only two circles we can find the overlap by using their
            // radiuses and the distance between them.
            if (circles.length === 2) {
                var circle1 = circles[0];
                var circle2 = circles[1];

                overlap = getOverlapBetweenCirclesByDistance(
                    circle1.r,
                    circle2.r,
                    getDistanceBetweenPoints(circle1, circle2)
                );
            }

            return overlap;
        };

        /**
         * Calculates the difference between the desired overlap and the actual overlap
         * between two circles.
         * @private
         * @param {object} mapOfIdToCircle Map from id to circle.
         * @param {Array<object>} relations List of relations to calculate the loss of.
         * @return {number} Returns the loss between positions of the circles for the
         * given relations.
         */
        var loss = function loss(mapOfIdToCircle, relations) {
            var precision = 10e10;

            // Iterate all the relations and calculate their individual loss.
            return relations.reduce(function (totalLoss, relation) {
                var loss = 0;

                if (relation.sets.length > 1) {
                    var wantedOverlap = relation.value;
                    // Calculate the actual overlap between the sets.
                    var actualOverlap = getOverlapBetweenCircles(
                        // Get the circles for the given sets.
                        relation.sets.map(function (set) {
                            return mapOfIdToCircle[set];
                        })
                    );

                    var diff = wantedOverlap - actualOverlap;

                    loss = Math.round((diff * diff) * precision) / precision;
                }

                // Add calculated loss to the sum.
                return totalLoss + loss;
            }, 0);
        };

        /**
         * Finds the root of a given function. The root is the input value needed for
         * a function to return 0.
         *
         * See https://en.wikipedia.org/wiki/Bisection_method#Algorithm
         *
         * TODO: Add unit tests.
         *
         * @param {function} f The function to find the root of.
         * @param {number} a The lowest number in the search range.
         * @param {number} b The highest number in the search range.
         * @param {number} [tolerance=1e-10] The allowed difference between the returned
         * value and root.
         * @param {number} [maxIterations=100] The maximum iterations allowed.
         */
        var bisect = function bisect(f, a, b, tolerance, maxIterations) {
            var fA = f(a),
                fB = f(b),
                nMax = maxIterations || 100,
                tol = tolerance || 1e-10,
                delta = b - a,
                n = 1,
                x, fX;

            if (a >= b) {
                throw new Error('a must be smaller than b.');
            } else if (fA * fB > 0) {
                throw new Error('f(a) and f(b) must have opposite signs.');
            }

            if (fA === 0) {
                x = a;
            } else if (fB === 0) {
                x = b;
            } else {
                while (n++ <= nMax && fX !== 0 && delta > tol) {
                    delta = (b - a) / 2;
                    x = a + delta;
                    fX = f(x);

                    // Update low and high for next search interval.
                    if (fA * fX > 0) {
                        a = x;
                    } else {
                        b = x;
                    }
                }
            }

            return x;
        };

        /**
         * Uses the bisection method to make a best guess of the ideal distance between
         * two circles too get the desired overlap.
         * Currently there is no known formula to calculate the distance from the area
         * of overlap, which makes the bisection method preferred.
         * @private
         * @param {number} r1 Radius of the first circle.
         * @param {number} r2 Radiues of the second circle.
         * @param {number} overlap The wanted overlap between the two circles.
         * @return {number} Returns the distance needed to get the wanted overlap
         * between the two circles.
         */
        var getDistanceBetweenCirclesByOverlap =
        function getDistanceBetweenCirclesByOverlap(r1, r2, overlap) {
            var maxDistance = r1 + r2,
                distance;

            if (overlap <= 0) {
                // If overlap is below or equal to zero, then there is no overlap.
                distance = maxDistance;
            } else if (getAreaOfCircle(r1 < r2 ? r1 : r2) <= overlap) {
                // When area of overlap is larger than the area of the smallest circle,
                // then it is completely overlapping.
                distance = 0;
            } else {
                distance = bisect(function (x) {
                    var actualOverlap = getOverlapBetweenCirclesByDistance(r1, r2, x);

                    // Return the differance between wanted and actual overlap.
                    return overlap - actualOverlap;
                }, 0, maxDistance);
            }
            return distance;
        };

        var isSet = function (x) {
            return isArray(x.sets) && x.sets.length === 1;
        };

        /**
         * Calculates a margin for a point based on the iternal and external circles.
         * The margin describes if the point is well placed within the internal circles,
         * and away from the external
         * @private
         * @todo add unit tests.
         * @param {object} point The point to evaluate.
         * @param {Array<object>} internal The internal circles.
         * @param {Array<object>} external The external circles.
         * @return {number} Returns the margin.
         */
        var getMarginFromCircles =
        function getMarginFromCircles(point, internal, external) {
            var margin = internal.reduce(function (margin, circle) {
                var m = circle.r - getDistanceBetweenPoints(point, circle);

                return (m <= margin) ? m : margin;
            }, Number.MAX_VALUE);

            margin = external.reduce(function (margin, circle) {
                var m = getDistanceBetweenPoints(point, circle) - circle.r;

                return (m <= margin) ? m : margin;
            }, margin);

            return margin;
        };

        /**
         * Finds the optimal label position by looking for a position that has a low
         * distance from the internal circles, and as large possible distane to the
         * external circles.
         * @private
         * @todo Optimize the intial position.
         * @todo Add unit tests.
         * @param {Array<object>} internal Internal circles.
         * @param {Array<object>} external External circles.
         * @return {object} Returns the found position.
         */
        var getLabelPosition = function getLabelPosition(internal, external) {
            // Get the best label position within the internal circles.
            var best = internal.reduce(function (best, circle) {
                var d = circle.r / 2;

                // Give a set of points with the circle to evaluate as the best label
                // position.
                return [
                    { x: circle.x, y: circle.y },
                    { x: circle.x + d, y: circle.y },
                    { x: circle.x - d, y: circle.y },
                    { x: circle.x, y: circle.y + d },
                    { x: circle.x, y: circle.y - d }
                ]
                // Iterate the given points and return the one with the largest margin.
                    .reduce(function (best, point) {
                        var margin = getMarginFromCircles(point, internal, external);

                        // If the margin better than the current best, then update best.
                        if (best.margin < margin) {
                            best.point = point;
                            best.margin = margin;
                        }
                        return best;
                    }, best);
            }, {
                point: undefined,
                margin: -Number.MAX_VALUE
            }).point;

            // Use nelder mead to optimize the initial label position.
            var optimal = nelderMead(
                function (p) {
                    return -(
                        getMarginFromCircles({ x: p[0], y: p[1] }, internal, external)
                    );
                },
                [best.x, best.y]
            );

            // Update best to be the point which was found to have the best margin.
            best = {
                x: optimal[0],
                y: optimal[1]
            };

            if (!(
                isPointInsideAllCircles(best, internal) &&
                isPointOutsideAllCircles(best, external)
            )) {
                // If point was either outside one of the internal, or inside one of the
                // external, then it was invalid and should use a fallback.
                best = getCenterOfPoints(internal);
            }

            // Return the best point.
            return best;
        };

        /**
         * Finds the available width for a label, by taking the label position and
         * finding the largest distance, which is inside all internal circles, and
         * outside all external circles.
         *
         * @private
         * @param {object} pos The x and y coordinate of the label.
         * @param {Array<object>} internal Internal circles.
         * @param {Array<object>} external External circles.
         * @return {number} Returns available width for the label.
         */
        var getLabelWidth = function getLabelWidth(pos, internal, external) {
            var radius = internal.reduce(function (min, circle) {
                    return Math.min(circle.r, min);
                }, Infinity),
                // Filter out external circles that are completely overlapping.
                filteredExternals = external.filter(function (circle) {
                    return !isPointInsideCircle(pos, circle);
                });

            var findDistance = function (maxDistance, direction) {
                return bisect(function (x) {
                    var testPos = {
                            x: pos.x + (direction * x),
                            y: pos.y
                        },
                        isValid = (
                            isPointInsideAllCircles(testPos, internal) &&
                            isPointOutsideAllCircles(testPos, filteredExternals)
                        );

                    // If the position is valid, then we want to move towards the max
                    // distance. If not, then we want to  away from the max distance.
                    return -(maxDistance - x) + (isValid ? 0 : Number.MAX_VALUE);
                }, 0, maxDistance);
            };

            // Find the smallest distance of left and right.
            return Math.min(findDistance(radius, -1), findDistance(radius, 1)) * 2;
        };

        /**
         * Calulates data label values for a list of relations.
         * @private
         * @todo add unit tests
         * @todo NOTE: may be better suited as a part of the layout function.
         * @param {Array<object>} relations The list of relations.
         * @return {object} Returns a map from id to the data label values.
         */
        var getLabelValues = function getLabelValues(relations) {
            var singleSets = relations.filter(isSet);

            return relations.reduce(function (map, relation) {
                if (relation.value) {
                    var sets = relation.sets,
                        id = sets.join(),
                        // Create a list of internal and external circles.
                        data = singleSets.reduce(function (data, set) {
                            // If the set exists in this relation, then it is internal,
                            // otherwise it will be external.
                            var isInternal = sets.indexOf(set.sets[0]) > -1,
                                property = isInternal ? 'internal' : 'external';

                            // Add the circle to the list.
                            data[property].push(set.circle);
                            return data;
                        }, {
                            internal: [],
                            external: []
                        }),
                        // Calulate the label position.
                        position = getLabelPosition(
                            data.internal,
                            data.external
                        ),
                        // Calculate the label width
                        width = getLabelWidth(position, data.internal, data.external);

                    map[id] = {
                        position: position,
                        width: width
                    };
                }
                return map;
            }, {});
        };

        /**
         * Takes an array of relations and adds the properties `totalOverlap` and
         * `overlapping` to each set. The property `totalOverlap` is the sum of value
         * for each relation where this set is included. The property `overlapping` is
         * a map of how much this set is overlapping another set.
         * NOTE: This algorithm ignores relations consisting of more than 2 sets.
         * @private
         * @param {Array<object>} relations The list of relations that should be sorted.
         * @return {Array<object>} Returns the modified input relations with added
         * properties `totalOverlap` and `overlapping`.
         */
        var addOverlapToSets = function addOverlapToSets(relations) {
            // Calculate the amount of overlap per set.
            var mapOfIdToProps = relations
                // Filter out relations consisting of 2 sets.
                .filter(function (relation) {
                    return relation.sets.length === 2;
                })
                // Sum up the amount of overlap for each set.
                .reduce(function (map, relation) {
                    var sets = relation.sets;

                    sets.forEach(function (set, i, arr) {
                        if (!isObject(map[set])) {
                            map[set] = {
                                overlapping: {},
                                totalOverlap: 0
                            };
                        }
                        map[set].totalOverlap += relation.value;
                        map[set].overlapping[arr[1 - i]] = relation.value;
                    });
                    return map;
                }, {});

            relations
                // Filter out single sets
                .filter(isSet)
                // Extend the set with the calculated properties.
                .forEach(function (set) {
                    var properties = mapOfIdToProps[set.sets[0]];

                    extend(set, properties);
                });

            // Returns the modified relations.
            return relations;
        };

        /**
         * Takes two sets and finds the one with the largest total overlap.
         * @private
         * @param {object} a The first set to compare.
         * @param {object} b The second set to compare.
         * @return {number} Returns 0 if a and b are equal, <0 if a is greater, >0 if b
         * is greater.
         */
        var sortByTotalOverlap = function sortByTotalOverlap(a, b) {
            return b.totalOverlap - a.totalOverlap;
        };

        /**
         * Uses a greedy approach to position all the sets. Works well with a small
         * number of sets, and are in these cases a good choice aesthetically.
         * @private
         * @param {Array<object>} relations List of the overlap between two or more
         * sets, or the size of a single set.
         * @return {Array<object>} List of circles and their calculated positions.
         */
        var layoutGreedyVenn = function layoutGreedyVenn(relations) {
            var positionedSets = [],
                mapOfIdToCircles = {};

            // Define a circle for each set.
            relations
                .filter(function (relation) {
                    return relation.sets.length === 1;
                }).forEach(function (relation) {
                    mapOfIdToCircles[relation.sets[0]] = relation.circle = {
                        x: Number.MAX_VALUE,
                        y: Number.MAX_VALUE,
                        r: Math.sqrt(relation.value / Math.PI)
                    };
                });

            /**
             * Takes a set and updates the position, and add the set to the list of
             * positioned sets.
             * @private
             * @param {object} set The set to add to its final position.
             * @param {object} coordinates The coordinates to position the set at.
             */
            var positionSet = function positionSet(set, coordinates) {
                var circle = set.circle;

                circle.x = coordinates.x;
                circle.y = coordinates.y;
                positionedSets.push(set);
            };

            // Find overlap between sets. Ignore relations with more then 2 sets.
            addOverlapToSets(relations);

            // Sort sets by the sum of their size from large to small.
            var sortedByOverlap = relations
                .filter(isSet)
                .sort(sortByTotalOverlap);

            // Position the most overlapped set at 0,0.
            positionSet(sortedByOverlap.shift(), { x: 0, y: 0 });

            var relationsWithTwoSets = relations.filter(function (x) {
                return x.sets.length === 2;
            });

            // Iterate and position the remaining sets.
            sortedByOverlap.forEach(function (set) {
                var circle = set.circle,
                    radius = circle.r,
                    overlapping = set.overlapping;

                var bestPosition = positionedSets
                    .reduce(function (best, positionedSet, i) {
                        var positionedCircle = positionedSet.circle,
                            overlap = overlapping[positionedSet.sets[0]];

                        // Calculate the distance between the sets to get the correct
                        // overlap
                        var distance = getDistanceBetweenCirclesByOverlap(
                            radius,
                            positionedCircle.r,
                            overlap
                        );

                        // Create a list of possible coordinates calculated from
                        // distance.
                        var possibleCoordinates = [
                            { x: positionedCircle.x + distance, y: positionedCircle.y },
                            { x: positionedCircle.x - distance, y: positionedCircle.y },
                            { x: positionedCircle.x, y: positionedCircle.y + distance },
                            { x: positionedCircle.x, y: positionedCircle.y - distance }
                        ];

                        // If there are more circles overlapping, then add the
                        // intersection points as possible positions.
                        positionedSets.slice(i + 1).forEach(function (positionedSet2) {
                            var positionedCircle2 = positionedSet2.circle,
                                overlap2 = overlapping[positionedSet2.sets[0]],
                                distance2 = getDistanceBetweenCirclesByOverlap(
                                    radius,
                                    positionedCircle2.r,
                                    overlap2
                                );

                            // Add intersections to list of coordinates.
                            possibleCoordinates = possibleCoordinates.concat(
                                getCircleCircleIntersection({
                                    x: positionedCircle.x,
                                    y: positionedCircle.y,
                                    r: distance
                                }, {
                                    x: positionedCircle2.x,
                                    y: positionedCircle2.y,
                                    r: distance2
                                })
                            );
                        });

                        // Iterate all suggested coordinates and find the best one.
                        possibleCoordinates.forEach(function (coordinates) {
                            circle.x = coordinates.x;
                            circle.y = coordinates.y;

                            // Calculate loss for the suggested coordinates.
                            var currentLoss = loss(
                                mapOfIdToCircles, relationsWithTwoSets
                            );

                            // If the loss is better, then use these new coordinates.
                            if (currentLoss < best.loss) {
                                best.loss = currentLoss;
                                best.coordinates = coordinates;
                            }
                        });

                        // Return resulting coordinates.
                        return best;
                    }, {
                        loss: Number.MAX_VALUE,
                        coordinates: undefined
                    });

                // Add the set to its final position.
                positionSet(set, bestPosition.coordinates);
            });

            // Return the positions of each set.
            return mapOfIdToCircles;
        };

        /**
         * Calculates the positions of all the sets in the venn diagram.
         * @private
         * @todo Add support for constrained MDS.
         * @param {Array<object>} relations List of the overlap between two or more sets, or the
         * size of a single set.
         * @return {Arrat<object>} List of circles and their calculated positions.
         */
        var layout = function (relations) {
            var mapOfIdToShape = {};

            // Calculate best initial positions by using greedy layout.
            if (relations.length > 0) {
                mapOfIdToShape = layoutGreedyVenn(relations);

                relations
                    .filter(function (x) {
                        return !isSet(x);
                    })
                    .forEach(function (relation) {
                        var sets = relation.sets,
                            id = sets.join(),
                            circles = sets.map(function (set) {
                                return mapOfIdToShape[set];
                            });

                        // Add intersection shape to map
                        mapOfIdToShape[id] =
                            getAreaOfIntersectionBetweenCircles(circles);
                    });
            }
            return mapOfIdToShape;
        };

        var isValidRelation = function (x) {
            var map = {};

            return (
                isObject(x) &&
                (isNumber(x.value) && x.value > -1) &&
                (isArray(x.sets) && x.sets.length > 0) &&
                !x.sets.some(function (set) {
                    var invalid = false;

                    if (!map[set] && isString(set)) {
                        map[set] = true;
                    } else {
                        invalid = true;
                    }
                    return invalid;
                })
            );
        };

        var isValidSet = function (x) {
            return (isValidRelation(x) && isSet(x) && x.value > 0);
        };

        /**
         * Prepares the venn data so that it is usable for the layout function. Filter
         * out sets, or intersections that includes sets, that are missing in the data
         * or has (value < 1). Adds missing relations between sets in the data as
         * value = 0.
         * @private
         * @param {Array<object>} data The raw input data.
         * @return {Array<object>} Returns an array of valid venn data.
         */
        var processVennData = function processVennData(data) {
            var d = isArray(data) ? data : [];

            var validSets = d
                .reduce(function (arr, x) {
                    // Check if x is a valid set, and that it is not an duplicate.
                    if (isValidSet(x) && arr.indexOf(x.sets[0]) === -1) {
                        arr.push(x.sets[0]);
                    }
                    return arr;
                }, [])
                .sort();

            var mapOfIdToRelation = d.reduce(function (mapOfIdToRelation, relation) {
                if (isValidRelation(relation) && !relation.sets.some(function (set) {
                    return validSets.indexOf(set) === -1;
                })) {
                    mapOfIdToRelation[relation.sets.sort().join()] = relation;
                }
                return mapOfIdToRelation;
            }, {});

            validSets.reduce(function (combinations, set, i, arr) {
                var remaining = arr.slice(i + 1);

                remaining.forEach(function (set2) {
                    combinations.push(set + ',' + set2);
                });
                return combinations;
            }, []).forEach(function (combination) {
                if (!mapOfIdToRelation[combination]) {
                    var obj = {
                        sets: combination.split(','),
                        value: 0
                    };

                    mapOfIdToRelation[combination] = obj;
                }
            });

            // Transform map into array.
            return objectValues(mapOfIdToRelation);
        };

        /**
         * Calculates the proper scale to fit the cloud inside the plotting area.
         * @private
         * @todo add unit test
         * @param {number} targetWidth  Width of target area.
         * @param {number} targetHeight Height of target area.
         * @param {object} field The playing field.
         * @param {Highcharts.Series} series Series object.
         * @return {object} Returns the value to scale the playing field up to the size
         * of the target area, and center of x and y.
         */
        var getScale = function getScale(targetWidth, targetHeight, field) {
            var height = field.bottom - field.top, // top is smaller than bottom
                width = field.right - field.left,
                scaleX = width > 0 ? 1 / width * targetWidth : 1,
                scaleY = height > 0 ? 1 / height * targetHeight : 1,
                adjustX = (field.right + field.left) / 2,
                adjustY = (field.top + field.bottom) / 2,
                scale = Math.min(scaleX, scaleY);

            return {
                scale: scale,
                centerX: targetWidth / 2 - adjustX * scale,
                centerY: targetHeight / 2 - adjustY * scale
            };
        };

        /**
         * If a circle is outside a give field, then the boundaries of the field is
         * adjusted accordingly. Modifies the field object which is passed as the first
         * parameter.
         * @private
         * @todo NOTE: Copied from wordcloud, can probably be unified.
         * @param {object} field The bounding box of a playing field.
         * @param {object} placement The bounding box for a placed point.
         * @return {object} Returns a modified field object.
         */
        var updateFieldBoundaries = function updateFieldBoundaries(field, circle) {
            var left = circle.x - circle.r,
                right = circle.x + circle.r,
                bottom = circle.y + circle.r,
                top = circle.y - circle.r;

            // TODO improve type checking.
            if (!isNumber(field.left) || field.left > left) {
                field.left = left;
            }
            if (!isNumber(field.right) || field.right < right) {
                field.right = right;
            }
            if (!isNumber(field.top) || field.top > top) {
                field.top = top;
            }
            if (!isNumber(field.bottom) || field.bottom < bottom) {
                field.bottom = bottom;
            }
            return field;
        };

        /**
         * A Venn diagram displays all possible logical relations between a collection
         * of different sets. The sets are represented by circles, and the relation
         * between the sets are displayed by the overlap or lack of overlap between
         * them. The venn diagram is a special case of Euler diagrams, which can also
         * be displayed by this series type.
         *
         * @sample {highcharts} highcharts/demo/venn-diagram/
         *         Venn diagram
         * @sample {highcharts} highcharts/demo/euler-diagram/
         *         Euler diagram
         *
         * @extends      plotOptions.scatter
         * @excluding    connectEnds, connectNulls, cropThreshold, dragDrop,
         *               findNearestPointBy, getExtremesFromAll, jitter, label, linecap,
         *               lineWidth, linkedTo, marker, negativeColor, pointInterval,
         *               pointIntervalUnit, pointPlacement, pointStart, softThreshold,
         *               stacking, steps, threshold, xAxis, yAxis, zoneAxis, zones
         * @product      highcharts
         * @optionparent plotOptions.venn
         */
        var vennOptions = {
            borderColor: '#cccccc',
            borderDashStyle: 'solid',
            borderWidth: 1,
            brighten: 0,
            clip: false,
            colorByPoint: true,
            dataLabels: {
                /** @ignore-option */
                enabled: true,
                /** @ignore-option */
                verticalAlign: 'middle',
                /** @ignore-option */
                formatter: function () {
                    return this.point.name;
                }
            },
            /**
             * @ignore-option
             * @private
             */
            inactiveOtherPoints: true,
            marker: false,
            opacity: 0.75,
            showInLegend: false,
            states: {
                /**
                 * @excluding halo
                 */
                hover: {
                    opacity: 1,
                    borderColor: '#333333'
                },
                /**
                 * @excluding halo
                 */
                select: {
                    color: '#cccccc',
                    borderColor: '#000000',
                    animation: false
                }
            },
            tooltip: {
                pointFormat: '{point.name}: {point.value}'
            }
        };

        var vennSeries = {
            isCartesian: false,
            axisTypes: [],
            directTouch: true,
            pointArrayMap: ['value'],
            translate: function () {

                var chart = this.chart;

                this.processedXData = this.xData;
                this.generatePoints();

                // Process the data before passing it into the layout function.
                var relations = processVennData(this.options.data);

                // Calculate the positions of each circle.
                var mapOfIdToShape = layout(relations);

                // Calculate positions of each data label
                var mapOfIdToLabelValues = getLabelValues(relations);

                // Calculate the scale, and center of the plot area.
                var field = Object.keys(mapOfIdToShape)
                        .filter(function (key) {
                            var shape = mapOfIdToShape[key];

                            return shape && isNumber(shape.r);
                        })
                        .reduce(function (field, key) {
                            return updateFieldBoundaries(field, mapOfIdToShape[key]);
                        }, { top: 0, bottom: 0, left: 0, right: 0 }),
                    scaling = getScale(chart.plotWidth, chart.plotHeight, field),
                    scale = scaling.scale,
                    centerX = scaling.centerX,
                    centerY = scaling.centerY;

                // Iterate all points and calculate and draw their graphics.
                this.points.forEach(function (point) {
                    var sets = isArray(point.sets) ? point.sets : [],
                        id = sets.join(),
                        shape = mapOfIdToShape[id],
                        shapeArgs,
                        dataLabelValues = mapOfIdToLabelValues[id] || {},
                        dataLabelWidth = dataLabelValues.width,
                        dataLabelPosition = dataLabelValues.position,
                        dlOptions = point.options && point.options.dataLabels;

                    if (shape) {
                        if (shape.r) {
                            shapeArgs = {
                                x: centerX + shape.x * scale,
                                y: centerY + shape.y * scale,
                                r: shape.r * scale
                            };
                        } else if (shape.d) {
                            // TODO: find a better way to handle scaling of a path.
                            var d = shape.d.reduce(function (path, arr) {
                                if (arr[0] === 'M') {
                                    arr[1] = centerX + arr[1] * scale;
                                    arr[2] = centerY + arr[2] * scale;
                                } else if (arr[0] === 'A') {
                                    arr[1] = arr[1] * scale;
                                    arr[2] = arr[2] * scale;
                                    arr[6] = centerX + arr[6] * scale;
                                    arr[7] = centerY + arr[7] * scale;
                                }
                                return path.concat(arr);
                            }, [])
                                .join(' ');

                            shapeArgs = {
                                d: d
                            };
                        }

                        // Scale the position for the data label.
                        if (dataLabelPosition) {
                            dataLabelPosition.x = centerX + dataLabelPosition.x * scale;
                            dataLabelPosition.y = centerY + dataLabelPosition.y * scale;
                        } else {
                            dataLabelPosition = {};
                        }

                        if (isNumber(dataLabelWidth)) {
                            dataLabelWidth = Math.round(dataLabelWidth * scale);
                        }
                    }

                    point.shapeArgs = shapeArgs;

                    // Placement for the data labels
                    if (dataLabelPosition && shapeArgs) {
                        point.plotX = dataLabelPosition.x;
                        point.plotY = dataLabelPosition.y;
                    }

                    // Add width for the data label
                    if (dataLabelWidth && shapeArgs) {
                        point.dlOptions = merge(
                            true,
                            {
                                style: {
                                    width: dataLabelWidth
                                }
                            },
                            isObject(dlOptions) && dlOptions
                        );
                    }

                    // Set name for usage in tooltip and in data label.
                    point.name = point.options.name || sets.join('∩');
                });
            },
            /**
             * Draw the graphics for each point.
             * @private
             */
            drawPoints: function () {
                var series = this,
                    // Series properties
                    chart = series.chart,
                    group = series.group,
                    points = series.points || [],
                    // Chart properties
                    renderer = chart.renderer;

                // Iterate all points and calculate and draw their graphics.
                points.forEach(function (point) {
                    var attribs = {
                            zIndex: isArray(point.sets) ? point.sets.length : 0
                        },
                        shapeArgs = point.shapeArgs;

                    // Add point attribs
                    if (!chart.styledMode) {
                        extend(attribs, series.pointAttribs(point, point.state));
                    }
                    // Draw the point graphic.
                    point.draw({
                        isNew: !point.graphic,
                        animatableAttribs: shapeArgs,
                        attribs: attribs,
                        group: group,
                        renderer: renderer,
                        shapeType: shapeArgs && shapeArgs.d ? 'path' : 'circle'
                    });
                });

            },
            /**
             * Calculates the style attributes for a point. The attributes can vary
             * depending on the state of the point.
             * @private
             * @param {object} point The point which will get the resulting attributes.
             * @param {string} state The state of the point.
             * @return {object} Returns the calculated attributes.
             */
            pointAttribs: function (point, state) {
                var series = this,
                    seriesOptions = series.options || {},
                    pointOptions = point && point.options || {},
                    stateOptions = (state && seriesOptions.states[state]) || {},
                    options = merge(
                        seriesOptions,
                        { color: point && point.color },
                        pointOptions,
                        stateOptions
                    );

                // Return resulting values for the attributes.
                return {
                    'fill': color(options.color)
                        .setOpacity(options.opacity)
                        .brighten(options.brightness)
                        .get(),
                    'stroke': options.borderColor,
                    'stroke-width': options.borderWidth,
                    'dashstyle': options.borderDashStyle
                };
            },
            animate: function (init) {
                if (!init) {
                    var series = this,
                        animOptions = H.animObject(series.options.animation);

                    series.points.forEach(function (point) {
                        var args = point.shapeArgs;

                        if (point.graphic && args) {
                            var attr = {},
                                animate = {};

                            if (args.d) {
                                // If shape is a path, then animate opacity.
                                attr.opacity = 0.001;
                            } else {
                                // If shape is a circle, then animate radius.
                                attr.r = 0;
                                animate.r = args.r;
                            }

                            point.graphic
                                .attr(attr)
                                .animate(animate, animOptions);

                            // If shape is path, then fade it in after the circles
                            // animation
                            if (args.d) {
                                setTimeout(function () {
                                    if (point && point.graphic) {
                                        point.graphic.animate({
                                            opacity: 1
                                        });
                                    }
                                }, animOptions.duration);
                            }
                        }
                    }, series);
                    series.animate = null;
                }
            },
            utils: {
                addOverlapToSets: addOverlapToSets,
                geometry: geometry,
                geometryCircles: geometryCircles,
                getLabelWidth: getLabelWidth,
                getMarginFromCircles: getMarginFromCircles,
                getDistanceBetweenCirclesByOverlap: getDistanceBetweenCirclesByOverlap,
                layoutGreedyVenn: layoutGreedyVenn,
                loss: loss,
                nelderMead: NelderMeadModule,
                processVennData: processVennData,
                sortByTotalOverlap: sortByTotalOverlap
            }
        };

        var vennPoint = {
            draw: draw,
            shouldDraw: function () {
                var point = this;

                // Only draw points with single sets.
                return !!point.shapeArgs;
            },
            isValid: function () {
                return isNumber(this.value);
            }
        };

        /**
         * A `venn` series. If the [type](#series.venn.type) option is
         * not specified, it is inherited from [chart.type](#chart.type).
         *
         * @extends   series,plotOptions.venn
         * @excluding connectEnds, connectNulls, cropThreshold, dataParser, dataURL,
         *            findNearestPointBy, getExtremesFromAll, label, linecap, lineWidth,
         *            linkedTo, marker, negativeColor, pointInterval, pointIntervalUnit,
         *            pointPlacement, pointStart, softThreshold, stack, stacking, steps,
         *            threshold, xAxis, yAxis, zoneAxis, zones
         * @product   highcharts
         * @apioption series.venn
         */

        /**
         * @type      {Array<*>}
         * @extends   series.scatter.data
         * @excluding marker, x, y
         * @product   highcharts
         * @apioption series.venn.data
         */

        /**
         * The name of the point. Used in data labels and tooltip. If name is not
         * defined then it will default to the joined values in
         * [sets](#series.venn.sets).
         *
         * @sample {highcharts} highcharts/demo/venn-diagram/
         *         Venn diagram
         * @sample {highcharts} highcharts/demo/euler-diagram/
         *         Euler diagram
         *
         * @type      {number}
         * @since     7.0.0
         * @product   highcharts
         * @apioption series.venn.data.name
         */

        /**
         * The value of the point, resulting in a relative area of the circle, or area
         * of overlap between two sets in the venn or euler diagram.
         *
         * @sample {highcharts} highcharts/demo/venn-diagram/
         *         Venn diagram
         * @sample {highcharts} highcharts/demo/euler-diagram/
         *         Euler diagram
         *
         * @type      {number}
         * @since     7.0.0
         * @product   highcharts
         * @apioption series.venn.data.value
         */

        /**
         * The set or sets the options will be applied to. If a single entry is defined,
         * then it will create a new set. If more than one entry is defined, then it
         * will define the overlap between the sets in the array.
         *
         * @sample {highcharts} highcharts/demo/venn-diagram/
         *         Venn diagram
         * @sample {highcharts} highcharts/demo/euler-diagram/
         *         Euler diagram
         *
         * @type      {Array<string>}
         * @since     7.0.0
         * @product   highcharts
         * @apioption series.venn.data.sets
         */

        /**
         * @excluding halo
         * @apioption series.venn.states.hover
         */

        /**
         * @excluding halo
         * @apioption series.venn.states.select
         */

        /**
         * @private
         * @class
         * @name Highcharts.seriesTypes.venn
         *
         * @augments Highcharts.Series
         */
        seriesType('venn', 'scatter', vennOptions, vennSeries, vennPoint);

        // Modify final series options.
        addEvent(seriesTypes.venn, 'afterSetOptions', function (e) {
            var options = e.options,
                states = options.states;

            if (this instanceof seriesTypes.venn) {
                // Explicitly disable all halo options.
                Object.keys(states).forEach(function (state) {
                    states[state].halo = false;
                });
            }
        });

    });
    _registerModule(_modules, 'masters/modules/venn.src.js', [], function () {


    });
}));