all files / sparkline/rendering/ sparkline-renderer.js

99.05% Statements 623/629
93.56% Branches 363/388
100% Functions 47/47
99.02% Lines 609/615
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   111×   139× 139×   134× 90× 90×     44×   134×   113× 113× 113× 113×       112×     30× 30× 30× 30× 30× 30× 30× 259× 253×   259× 259× 259× 259×   30×   14× 14× 14× 14× 14× 91× 91× 91× 91×   14×   158× 158× 158× 158× 158×               158× 158×   154× 45× 45× 66×   61×     45×   154× 154× 154×   158× 158×   61× 61× 61× 61× 61× 61× 61× 61× 61×   55× 11×   61× 13×   48×   61×   61×                 61×   64× 64×       64× 64×   64× 64× 64× 522× 64×   522×   64× 64× 64× 64×   11× 11× 11× 11× 11× 11× 11× 11× 11× 11× 11× 11× 11× 11× 11× 11× 11× 11×   11× 11× 77× 77× 77× 77× 77× 77× 77× 77× 77× 77×       77×   77×                 77× 77× 77× 77× 77× 77× 77× 77× 77× 77×   77× 77×       77× 77×   11×   77×   77×   76×   77×   76×           42× 35×       42× 42×       63× 63× 63× 63×       63× 63× 63× 10×   63× 63× 63× 63× 63× 63× 63× 584× 584× 584× 584× 584× 584× 584× 584× 584×       584× 584× 584× 584× 584× 584× 584× 584× 584× 584× 584× 583× 583× 583× 583× 583×       63× 63× 584×   63×         75× 75× 75×     75× 75× 75× 75× 75× 75× 75× 75× 75× 75× 75× 75× 75×       154× 154× 154× 154× 139×   15× 15×       15× 15× 15× 15× 15× 15× 15× 24×   15× 15× 134× 134× 134× 134× 134× 134× 134× 134× 134×             134× 134× 100× 100× 100× 100× 100× 100× 100× 100× 100× 100× 100× 80×   100×       15× 15× 134×   15×   1302× 1302× 323× 323× 323×   1302× 141× 141× 141×   1161× 139× 139× 139×   1302×   1297×   1302×     1302×   154× 154× 154× 154× 154× 84×   70× 70× 70×       70× 70× 70× 70× 70× 70× 70× 630× 70× 70× 70× 70× 70× 70× 70× 70× 630× 630× 630× 630×   630×   630×           630× 630× 630× 630× 630× 630× 630× 594× 594× 594× 594× 594× 594× 594× 594× 594× 594× 594×       70× 70× 630×   70×   630× 614×   16×         14×         16×   630× 630× 130×   630× 70×   560× 70×   630× 67×   563× 67×   630×     630×   48×     48× 48× 192× 192×   48×   134×   134× 134× 134× 134× 115× 115× 115× 1033× 1033×     115× 932×     932×   115× 115×   134×   134× 134× 134× 134× 134× 134× 207× 207× 207× 115×   92×   134×   134× 134× 134× 134× 134× 134× 134× 134× 134× 134× 16× 25×       11× 11× 11× 11×       118× 32×       114× 51×     112× 2040× 112× 112×   114× 1920× 112× 112× 112×           134× 134× 134× 134× 134× 134× 134× 134× 134× 134× 125× 125× 125× 125× 125× 125× 125× 125× 125×         125× 125×       125× 125× 125× 125× 125× 125× 125×   134× 134× 134× 134× 134× 134× 134× 134× 134× 134× 134× 61×   134× 134× 134× 134× 134× 134× 134× 134× 134× 1161×   76× 76×   1085× 51× 51×     1034× 1034×   1161× 45×   1116× 450× 450×   666× 609× 609× 609× 609× 609× 609× 65× 65× 65×   65×           544×   544× 544×     544× 544×           57× 57× 57×       1116× 1059× 1059×   1116× 1051×   1116× 1116× 1116× 1116×   134× 997×   134×   158× 158× 158× 56×                   56×     152×           152× 152×        
define(["require", "exports", "@syncfusion/ej2-base", "../utils/helper", "../utils/helper", "@syncfusion/ej2-data"], function (require, exports, ej2_base_1, helper_1, helper_2, ej2_data_1) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    var SparklineRenderer = (function () {
        function SparklineRenderer(sparkline) {
            this.sparkline = sparkline;
        }
        SparklineRenderer.prototype.processData = function () {
            var data = this.sparkline.dataSource;
            if (ej2_base_1.isNullOrUndefined(data) || !data.length) {
                return;
            }
            else if (!isNaN(this.sparkline.dataSource[0]) || this.sparkline.valueType === 'Numeric') {
                data = (this.sparkline.enableRtl) ? data.reverse() : data;
                this.sparkline.sparklineData = data;
            }
            else {
                this['process' + this.sparkline.valueType]();
            }
            this.axisCalculation();
        };
        SparklineRenderer.prototype.processDataManager = function () {
            var _this = this;
            var dataModule;
            var queryModule;
            if (this.sparkline.dataSource instanceof ej2_data_1.DataManager) {
                dataModule = this.sparkline.dataSource;
                queryModule = this.sparkline.query instanceof ej2_data_1.Query ? this.sparkline.query : new ej2_data_1.Query();
                var dataManager = dataModule.executeQuery(queryModule);
                dataManager.then(function (e) {
                    _this.sparkline.setProperties({ dataSource: e['result'] }, true);
                    _this.sparkline.sparklineData = _this.sparkline.dataSource;
                    _this.sparkline.processSparklineData();
                });
            }
            else {
                this.sparkline.processSparklineData();
            }
        };
        SparklineRenderer.prototype.processCategory = function (data, x, y) {
            var _this = this;
            Eif (data === void 0) { data = this.sparkline.dataSource; }
            Eif (x === void 0) { x = this.sparkline.xName; }
            Eif (y === void 0) { y = this.sparkline.yName; }
            var temp = [];
            var xValues = [];
            data.forEach(function (value) {
                if (xValues.indexOf(value[x]) === -1) {
                    xValues.push(value[x]);
                }
                var currentData = {};
                currentData[_this.sparkline.xName] = xValues.indexOf(value[x]);
                currentData[_this.sparkline.yName] = value[y];
                temp.push(currentData);
            });
            this.sparkline.sparklineData = temp;
        };
        SparklineRenderer.prototype.processDateTime = function (data, x, y) {
            Eif (data === void 0) { data = this.sparkline.dataSource; }
            Eif (x === void 0) { x = this.sparkline.xName; }
            Eif (y === void 0) { y = this.sparkline.yName; }
            var temp = [];
            data.forEach(function (value) {
                var currentData = {};
                currentData[x] = value[x].getTime();
                currentData[y] = value[y];
                temp.push(currentData);
            });
            this.sparkline.sparklineData = temp;
        };
        SparklineRenderer.prototype.renderSeries = function () {
            var _this = this;
            var spark = this.sparkline;
            this.clipId = spark.element.id + '_sparkline_clip_path';
            this.drawAxis();
            var argsData = {
                name: 'seriesRendering',
                cancel: false,
                lineWidth: spark.lineWidth,
                border: spark.border,
                fill: spark.fill,
                sparkline: spark
            };
            var seriesRenderingSuccess = function (args) {
                if (!_this.visiblePoints || args.cancel) {
                    return;
                }
                if (spark.type !== 'Pie' && spark.type !== 'WinLoss' && spark.rangeBandSettings.length) {
                    var group = _this.sparkline.renderer.createGroup({ id: _this.sparkline.element.id + '_sparkline_rangeband_g' });
                    for (var i = 0; i < spark.rangeBandSettings.length; i++) {
                        if ((spark.axisSettings.minY <= spark.rangeBandSettings[i].startRange) ||
                            (spark.axisSettings.maxY >= spark.rangeBandSettings[i].endRange)) {
                            _this.rangeBand(spark.rangeBandSettings[i], group, i);
                        }
                    }
                    _this.sparkline.svgObject.appendChild(group);
                }
                _this['render' + spark.type](_this.visiblePoints, args);
                _this.renderMarker(_this.visiblePoints);
                _this.renderLabel(_this.visiblePoints);
            };
            seriesRenderingSuccess.bind(this);
            spark.trigger('seriesRendering', argsData, seriesRenderingSuccess);
        };
        SparklineRenderer.prototype.rangeBand = function (rangeBandSettings, group, index) {
            var model = this.sparkline;
            var height = (model.availableSize.height) - model.padding.top * 2;
            var width = (model.availableSize.width) - model.padding.left * 2;
            var stValue = rangeBandSettings.startRange;
            var edValue = rangeBandSettings.endRange;
            var stHeight = (height - ((height / this.unitY) * (stValue - this.min))) + model.padding.top;
            var edHeight = (height - ((height / this.unitY) * (edValue - this.min))) + model.padding.top;
            var color = rangeBandSettings.color || this.sparkline.sparkTheme.rangeBandColor;
            if (edHeight > (height + model.padding.top)) {
                edHeight = (height + model.padding.top);
            }
            else if (edHeight < (0 + model.padding.top)) {
                edHeight = (0 + model.padding.top);
            }
            if (stHeight > (height + model.padding.top)) {
                stHeight = (height + model.padding.top);
            }
            else if (stHeight < (0 + model.padding.top)) {
                stHeight = (0 + model.padding.top);
            }
            var path = 'M ' + (model.padding.left) + ' ' + stHeight + ' L ' + (width + (model.padding.left)) + ' ' + stHeight +
                ' L ' + (width + (model.padding.left)) + ' ' + edHeight + ' L ' + (model.padding.left) + ' ' + edHeight + ' Z ';
            var pathOption = {
                'id': model.element.id + '_rangeBand_' + index,
                'fill': color,
                'opacity': rangeBandSettings.opacity,
                'stroke': 'transparent',
                'stroke-width': model.lineWidth,
                'd': path,
                'stroke-dasharray': ''
            };
            helper_1.drawPath(this.sparkline, pathOption, group);
        };
        SparklineRenderer.prototype.renderLine = function (points, args) {
            var spark = this.sparkline;
            var g = this.sparkline.renderer.createGroup({
                id: spark.element.id + '_sparkline_g',
                'clip-path': 'url(#' + this.clipId + ')'
            });
            var color = this.sparkline.fill;
            color = (this.sparkline.fill === '#00bdae' && this.sparkline.theme === 'Bootstrap4')
                ? this.sparkline.sparkTheme.axisLineColor : color;
            var pathOption = new helper_1.PathOption(spark.element.id + '_sparkline_line', 'transparent', args.lineWidth, color, spark.opacity);
            var d = '';
            for (var i = 0, len = points.length; i < len; i++) {
                if (i === 0) {
                    d = 'M ' + points[0].x + ' ' + points[i].y + ' ';
                }
                d += 'L ' + points[i].x + ' ' + points[i].y + ' ';
            }
            pathOption.d = d;
            pathOption['aria-label'] = 'Line series with' + points.length + 'data points';
            helper_1.drawPath(this.sparkline, pathOption, g);
            this.sparkline.svgObject.appendChild(g);
        };
        SparklineRenderer.prototype.renderPie = function (points, args) {
            var spark = this.sparkline;
            var height = spark.availableSize.height - (spark.padding.top + spark.padding.bottom);
            var width = spark.availableSize.width - (spark.padding.left + spark.padding.right);
            var area = (height <= width) ? height / 2 : width / 2;
            var X = spark.availableSize.width / 2;
            var Y = spark.availableSize.height / 2;
            var deg = 0;
            var stRad;
            var edRad;
            var stroke = args.border.color;
            var opacity = spark.opacity;
            var strokeWidth = args.border.width;
            var colors = (spark.palette.length) ? spark.palette : helper_1.getSeriesColor(this.sparkline.theme);
            var group = this.sparkline.renderer.createGroup({ id: spark.element.id + '_sparkline_g' });
            var low;
            var high;
            var locations = ej2_base_1.extend([], [], points);
            if (spark.highPointColor || spark.lowPointColor) {
                var pointsYvalues = locations.map(function (a) { return a.yVal; });
                low = Math.min.apply(null, pointsYvalues);
                high = Math.max.apply(null, pointsYvalues);
            }
            this.negativePointIndexes = [];
            for (var i = 0, stDeg = 90, edDeg = void 0, flag = void 0; i < points.length; i++) {
                stDeg += deg;
                deg = points[i]['degree'];
                deg = (deg === 360 ? deg - 0.001 : deg);
                edDeg = stDeg + deg;
                stRad = (stDeg - 90) * Math.PI / 180.0;
                edRad = (edDeg - 90) * Math.PI / 180.0;
                points[i]['stAng'] = stRad;
                points[i]['endAng'] = edRad;
                flag = (deg < 180) ? '0' : '1';
                var temp = points[i]['coordinates'] = {
                    sX: X + (area * Math.cos(stRad)), sY: Y +
                        (area * Math.sin(stRad)), eX: X + (area * Math.cos(edRad)), eY: Y + (area * Math.sin(edRad))
                };
                var pathArc = 'M ' + X + ' ' + Y + ' L ' + temp['eX'] + ' ' + temp['eY'] + ' A ' + area + ' ' +
                    area + ' 0 ' + flag + ',0 ' + temp['sX'] + ' ' + temp['sY'] + ' Z';
                var pathOption = {
                    'id': spark.element.id + '_sparkline_pie_' + i,
                    'opacity': opacity,
                    'fill': colors[i % colors.length],
                    'stroke': stroke,
                    'stroke-width': strokeWidth,
                    'd': pathArc,
                    'stroke-dasharray': ''
                };
                this.getPieSpecialPoint(points[i], spark, pathOption, i, high, low, points.length);
                var pointArgs = this.triggerPointRender('pointRendering', i, pathOption.fill, { color: stroke, width: strokeWidth });
                pathOption.fill = pointArgs.fill;
                pathOption.stroke = pointArgs.border.color;
                pathOption['stroke-width'] = pointArgs.border.width;
                Eif (!pointArgs.cancel) {
                    var element = helper_1.drawPath(this.sparkline, pathOption, group);
                    element.setAttribute('role', 'img');
                    element.setAttribute('aria-label', spark.dataSource[i][spark.xName] + ' : ' + points[i].yVal);
                    element.setAttribute('tabindex', i === 0 ? '0' : '-1');
                }
                var diffRadian = edRad - stRad;
                var mid = {
                    x: X + ((area / 2) * Math.cos(stRad + (diffRadian / 2))),
                    y: Y + ((area / 2) * Math.sin(stRad + (diffRadian / 2)))
                };
                points[i].location.x = mid.x;
                points[i].location.y = mid.y;
            }
            this.sparkline.svgObject.appendChild(group);
        };
        SparklineRenderer.prototype.getPieSpecialPoint = function (temp, spark, option, i, high, low, length) {
            if (temp.yVal < 0 && spark.negativePointColor) {
                option.fill = spark.negativePointColor;
                this.negativePointIndexes.push(i);
            }
            if (i === 0 && spark.startPointColor) {
                option.fill = spark.startPointColor;
                this.startPointIndex = i;
            }
            else if ((i === (length - 1)) && spark.endPointColor) {
                option.fill = spark.endPointColor;
                this.endPointIndex = i;
            }
            if (temp.yVal === high && spark.highPointColor) {
                option.fill = spark.highPointColor;
                this.highPointIndex = i;
            }
            else if (temp.yVal === low && spark.lowPointColor) {
                option.fill = spark.lowPointColor;
                this.lowPointIndex = i;
            }
        };
        SparklineRenderer.prototype.renderArea = function (points, args) {
            var spark = this.sparkline;
            var group = this.sparkline.renderer.createGroup({
                id: spark.element.id + '_sparkline_g',
                'clip-path': 'url(#' + this.clipId + ')'
            });
            var pathOption = new helper_1.PathOption(spark.element.id + '_sparkline_area', args.fill, 0, 'transparent', spark.opacity);
            var d = '';
            var str = '';
            for (var i = 0, len = points.length; i < len; i++) {
                if (i !== 0) {
                    str += 'L ' + points[i].x + ' ' + points[i].y + ' ';
                }
                else {
                    d = 'M ' + points[i].x + ' ' + this.axisHeight + ' ';
                    str = 'M ' + points[i].x + ' ' + points[i].y + ' ';
                }
                d += 'L ' + points[i].x + ' ' + points[i].y + ' ';
                if (i === (len - 1)) {
                    d += 'L ' + points[i].x + ' ' + this.axisHeight + ' Z';
                }
            }
            pathOption.d = d;
            pathOption['aria-label'] = 'Area series with' + points.length + 'data points';
            helper_1.drawPath(this.sparkline, pathOption, group);
            pathOption = new helper_1.PathOption(spark.element.id + '_sparkline_area_str', 'transparent', args.border.width, args.border.color, spark.opacity, '', str);
            helper_1.drawPath(this.sparkline, pathOption, group);
            this.sparkline.svgObject.appendChild(group);
        };
        SparklineRenderer.prototype.renderColumn = function (points, args) {
            var _this = this;
            var spark = this.sparkline;
            var locations = ej2_base_1.extend([], [], points);
            var group = this.sparkline.renderer.createGroup({
                id: spark.element.id + '_sparkline_g',
                'clip-path': 'url(#' + this.clipId + ')'
            });
            var lowPos;
            var highPos;
            if (this.sparkline.highPointColor || this.sparkline.lowPointColor) {
                var pointsYPos = locations.map(function (a) { return a.markerPosition; });
                highPos = Math.min.apply(null, pointsYPos);
                lowPos = Math.max.apply(null, pointsYPos);
            }
            var id = spark.element.id + '_sparkline_column_';
            var rectOptions = new helper_1.RectOption(id, '', args.border, spark.opacity, null);
            var temp;
            var len = points.length;
            this.negativePointIndexes = [];
            var colors = (spark.palette.length) ? spark.palette : helper_1.getSeriesColor(this.sparkline.theme);
            var _loop_1 = function (i) {
                temp = points[i];
                rectOptions.id = id + i;
                rectOptions.fill = spark.fill !== '#00bdae' ? spark.fill : colors[0];
                rectOptions.rect = new helper_1.Rect(temp.x, temp.y, temp.width, temp.height);
                this_1.getSpecialPoint(true, temp, spark, rectOptions, i, highPos, lowPos, len);
                temp.location.y = (temp.markerPosition <= this_1.axisHeight) ? temp.y : (temp.y + temp.height);
                temp.location.x = temp.x + (temp.width / 2);
                rectOptions.stroke = args.border.color ? (args.border.color) : rectOptions.fill;
                var pointArgs = {
                    name: 'pointRendering', cancel: false, pointIndex: i, fill: rectOptions.fill,
                    border: { color: rectOptions.stroke, width: args.border.width }
                };
                this_1.sparkline.trigger('pointRendering', pointArgs, function () {
                    temp = points[i];
                    rectOptions.id = id + i;
                    rectOptions.rect = new helper_1.Rect(temp.x, temp.y, temp.width, temp.height);
                    _this.getSpecialPoint(true, temp, spark, rectOptions, i, highPos, lowPos, len);
                    rectOptions.fill = pointArgs.fill;
                    rectOptions.stroke = pointArgs.border.color;
                    temp.location.y = (temp.markerPosition <= _this.axisHeight) ? temp.y : (temp.y + temp.height);
                    rectOptions['stroke-width'] = pointArgs.border.width;
                    temp.location.x = temp.x + (temp.width / 2);
                    if (!pointArgs.cancel) {
                        var element = helper_1.drawRectangle(spark, rectOptions, group);
                        element.setAttribute('role', 'img');
                        element.setAttribute('aria-label', spark.dataSource[i][spark.xName] + ' : ' + points[i].yVal);
                        element.setAttribute('tabindex', i === 0 ? '0' : '-1');
                        group.appendChild(element);
                    }
                });
            };
            var this_1 = this;
            for (var i = 0; i < len; i++) {
                _loop_1(i);
            }
            this.sparkline.svgObject.appendChild(group);
        };
        SparklineRenderer.prototype.renderWinLoss = function (points, args) {
            var spark = this.sparkline;
            var group = this.sparkline.renderer.createGroup({
                id: spark.element.id + '_sparkline_g',
                'clip-path': 'url(#' + this.clipId + ')'
            });
            var id = spark.element.id + '_sparkline_winloss_';
            var options = new helper_1.RectOption(id, '', args.border, spark.opacity, null);
            var temp;
            var len = points.length;
            var paletteLength = spark.palette.length;
            var colors = (spark.palette.length) ? spark.palette : helper_1.getSeriesColor(this.sparkline.theme);
            for (var i = 0; i < len; i++) {
                temp = points[i];
                options.id = id + i;
                options.fill = (paletteLength) ? spark.palette[i % paletteLength] : ((temp.yVal === this.axisValue) ?
                    (this.sparkline.tiePointColor || '#a216f3') : ((temp.yVal > this.axisValue) ? args.fill || colors[i % colors.length] :
                    (spark.negativePointColor || '#e20f07')));
                options.stroke = (args.border.color) ? (args.border.color) : options.fill;
                options.rect = new helper_1.Rect(temp.x, temp.y, temp.width, temp.height);
                temp.location.x = temp.x + (temp.width / 2);
                temp.location.y = (temp.yVal >= this.axisValue) ? (temp.y) : (temp.y + temp.height);
                var pointArgs = this.triggerPointRender('pointRendering', i, options.fill, { color: options.stroke, width: args.border.width });
                options.fill = pointArgs.fill;
                options.stroke = pointArgs.border.color;
                options['stroke-width'] = pointArgs.border.width;
                Eif (!pointArgs.cancel) {
                    var element = helper_1.drawRectangle(spark, options, group);
                    element.setAttribute('role', 'img');
                    element.setAttribute('aria-label', spark.dataSource[i][spark.xName] + ' : ' + points[i].yVal);
                    element.setAttribute('tabindex', i === 0 ? '0' : '-1');
                }
            }
            this.sparkline.svgObject.appendChild(group);
        };
        SparklineRenderer.prototype.renderMarker = function (points) {
            var _this = this;
            var spark = this.sparkline;
            var marker = spark.markerSettings;
            if ((spark.type === 'Pie' || spark.type === 'WinLoss' || !marker.visible.length)) {
                return;
            }
            var locations = ej2_base_1.extend([], [], points);
            var group = this.sparkline.renderer.createGroup({
                id: spark.element.id + '_sparkline_marker_g',
                'clip-path': 'url(#' + this.clipId + ')'
            });
            var temp;
            var id = spark.element.id + '_sparkline_marker_';
            var option = new helper_1.CircleOption('', marker.fill, marker.border, marker.opacity, 0, 0, marker.size / 2, '');
            var highPos;
            var lowPos;
            var visible = marker.visible.join();
            if ((visible.toLowerCase().indexOf('high') > -1) || (visible.toLowerCase().indexOf('low') > -1)) {
                var pointsYPos = locations.map(function (a) { return a.markerPosition; });
                highPos = Math.min.apply(null, pointsYPos);
                lowPos = Math.max.apply(null, pointsYPos);
            }
            this.negativePointIndexes = [];
            var _loop_2 = function (i, length_1) {
                temp = points[i];
                option.id = id + i;
                option.cx = temp.location.x;
                option.cy = temp.location.y;
                option.fill = marker.fill;
                var render = (visible.toLowerCase().indexOf('all') > -1);
                render = this_2.getSpecialPoint(render, temp, spark, option, i, highPos, lowPos, length_1, visible.toLowerCase());
                option.stroke = marker.border.color || option.fill;
                var markerArgs = {
                    name: 'markerRendering', cancel: false,
                    border: { color: option.stroke, width: marker.border.width },
                    fill: option.fill, pointIndex: i,
                    sparkline: this_2.sparkline,
                    x: option.cx, y: option.cy, size: marker.size
                };
                this_2.sparkline.trigger('markerRendering', markerArgs, function () {
                    if (render && !markerArgs.cancel) {
                        option.id = id + i;
                        option.cx = markerArgs.x;
                        option.cy = markerArgs.y;
                        option.fill = markerArgs.fill;
                        option.stroke = markerArgs.border.color;
                        option['stroke-width'] = markerArgs.border.width;
                        option.r = markerArgs.size / 2;
                        var element = helper_1.drawCircle(spark, option, group);
                        element.setAttribute('role', 'img');
                        element.setAttribute('aria-label', spark.dataSource[i][spark.xName] + ' : ' + points[i].yVal);
                        if ((_this.sparkline.type.indexOf('Line') > -1) || (_this.sparkline.type.indexOf('Area') > -1)) {
                            element.setAttribute('tabindex', i === 0 ? '0' : '-1');
                        }
                        group.appendChild(element);
                    }
                });
            };
            var this_2 = this;
            for (var i = 0, length_1 = points.length; i < length_1; i++) {
                _loop_2(i, length_1);
            }
            this.sparkline.svgObject.appendChild(group);
        };
        SparklineRenderer.prototype.getSpecialPoint = function (render, temp, spark, option, i, highPos, lowPos, length, visible) {
            if (visible === void 0) { visible = ''; }
            if (temp.markerPosition > this.axisHeight) {
                option.fill = spark.negativePointColor || option.fill;
                this.negativePointIndexes.push(i);
                render = render || (visible.indexOf('negative') > -1);
            }
            if (i === 0) {
                option.fill = spark.startPointColor || option.fill;
                this.startPointIndex = i;
                render = render || (visible.indexOf('start') > -1);
            }
            else if ((i === (length - 1))) {
                option.fill = spark.endPointColor || option.fill;
                this.endPointIndex = i;
                render = render || (visible.indexOf('end') > -1);
            }
            if (temp.markerPosition === highPos) {
                option.fill = spark.highPointColor || option.fill;
                this.highPointIndex = i;
                render = render || (visible.indexOf('high') > -1);
            }
            else if (temp.markerPosition === lowPos) {
                option.fill = spark.lowPointColor || option.fill;
                this.lowPointIndex = i;
                render = render || (visible.indexOf('low') > -1);
            }
            Iif (visible.indexOf('none') > -1) {
                render = false;
            }
            return render;
        };
        SparklineRenderer.prototype.renderLabel = function (points) {
            var _this = this;
            var spark = this.sparkline;
            var dataLabel = spark.dataLabelSettings;
            var color = dataLabel.textStyle.color || spark.sparkTheme.dataLabelColor;
            if ((spark.type === 'WinLoss' || !dataLabel.visible.length)) {
                return;
            }
            var locations = ej2_base_1.extend([], [], points);
            var id = spark.element.id + '_sparkline_label_';
            var group = this.sparkline.renderer.createGroup({
                id: spark.element.id + '_sparkline_label_g',
                style: 'pointer-events: none;'
            });
            group.setAttribute('aria-hidden', 'true');
            var g;
            var temp;
            var textId = id + 'text_';
            var rectId = id + 'rect_';
            var option = new helper_2.TextOption('', 0, 0, 'middle', '', 'middle');
            var labelStyle = dataLabel.textStyle;
            var pointsYPos = locations.map(function (a) { return a.markerPosition; });
            var highPos = Math.min.apply(null, pointsYPos);
            var lowPos = Math.max.apply(null, pointsYPos);
            var space = 1;
            var padding = (dataLabel.fill !== 'transparent' || dataLabel.border.width) ? 2 : 0;
            var size = helper_2.measureText('sparkline_measure_text', labelStyle, this.sparkline.sparkTheme.dataLabelFont);
            var rectOptions = new helper_1.RectOption('', dataLabel.fill, dataLabel.border, dataLabel.opacity, null);
            var edgeLabelOption;
            var _loop_3 = function (i, length_2) {
                temp = points[i];
                option.id = textId + i;
                option.x = temp.location.x + dataLabel.offset.x;
                option.y = ((spark.type === 'Pie') ? temp.location.y : ((temp.markerPosition > this_3.axisHeight) ? (temp.location.y +
                    (size.height / 2) + space + 2 + padding) : (temp.location.y - (size.height / 2) - space - padding))) + dataLabel.offset.y;
                option.text = (dataLabel.format !== '') ? this_3.formatter(dataLabel.format, this_3.sparkline.dataSource[i]) :
                    temp.yVal.toString();
                var labelArgs = {
                    name: 'dataLabelRendering', cancel: false,
                    border: dataLabel.border, fill: dataLabel.fill, pointIndex: i,
                    sparkline: this_3.sparkline,
                    x: option.x, y: option.y, text: option.text, color: color
                };
                this_3.sparkline.trigger('dataLabelRendering', labelArgs, function () {
                    size = helper_2.measureText(labelArgs.text, labelStyle, _this.sparkline.sparkTheme.dataLabelFont);
                    option.text = labelArgs.text;
                    var renderLabel = (dataLabel.visible.join().toLowerCase().indexOf('all') > -1);
                    renderLabel = _this.getLabelVisible(renderLabel, temp, i, dataLabel, length_2, highPos, lowPos);
                    edgeLabelOption = _this.arrangeLabelPosition(dataLabel.edgeLabelMode, renderLabel, labelArgs.x, i, length_2, size, padding);
                    if (renderLabel && !labelArgs.cancel && edgeLabelOption.render) {
                        rectOptions.id = rectId + i;
                        rectOptions.fill = labelArgs.fill;
                        rectOptions.stroke = labelArgs.border.color;
                        rectOptions['stroke-width'] = labelArgs.border.width;
                        option.y = labelArgs.y;
                        option.x = edgeLabelOption.x;
                        rectOptions.rect = new helper_1.Rect(option.x - ((size.width / 2) + padding), (option.y - padding - (size.height / 1.75)), size.width + (padding * 2), size.height + (padding * 2));
                        g = _this.sparkline.renderer.createGroup({ id: id + 'g' + i });
                        helper_1.drawRectangle(spark, rectOptions, g);
                        helper_2.renderTextElement(option, labelStyle, labelArgs.color, g, _this.sparkline.sparkTheme.dataLabelFont);
                        group.appendChild(g);
                    }
                });
            };
            var this_3 = this;
            for (var i = 0, length_2 = points.length; i < length_2; i++) {
                _loop_3(i, length_2);
            }
            this.sparkline.svgObject.appendChild(group);
        };
        SparklineRenderer.prototype.arrangeLabelPosition = function (edgeLabel, render, x, index, length, size, padding) {
            if (edgeLabel === 'None') {
                return { x: x, render: render };
            }
            if (index === 0 && ((x - (size.width / 2) - padding) <= 0)) {
                if (edgeLabel === 'Hide') {
                    render = false;
                }
                else {
                    x = this.sparkline.padding.left + padding + (size.width / 2);
                }
            }
            else if (index === length - 1 && ((x + (size.width / 2) + padding) >= this.sparkline.availableSize.width)) {
                if (edgeLabel === 'Hide') {
                    render = false;
                }
                else {
                    x -= (size.width / 2 + padding);
                }
            }
            return { x: x, render: render };
        };
        SparklineRenderer.prototype.getLabelVisible = function (render, temp, i, label, length, highPos, lowPos) {
            var labelVisible = label.visible.join().toLowerCase();
            if (temp.markerPosition > this.axisHeight) {
                render = render || (labelVisible.indexOf('negative') > -1);
            }
            if (i === 0) {
                render = render || (labelVisible.indexOf('start') > -1);
            }
            else if ((i === (length - 1))) {
                render = render || (labelVisible.indexOf('end') > -1);
            }
            if (temp.markerPosition === highPos) {
                render = render || (labelVisible.indexOf('high') > -1);
            }
            else if (temp.markerPosition === lowPos) {
                render = render || (labelVisible.indexOf('low') > -1);
            }
            Iif (label.visible.join().toLowerCase().indexOf('none') > -1) {
                render = false;
            }
            return render;
        };
        SparklineRenderer.prototype.formatter = function (format, data) {
            Iif (ej2_base_1.isNullOrUndefined(format)) {
                return null;
            }
            var keys = Object.keys(data);
            for (var _i = 0, keys_1 = keys; _i < keys_1.length; _i++) {
                var key = keys_1[_i];
                format = format.split('${' + key + '}').join(data[key]);
            }
            return format;
        };
        SparklineRenderer.prototype.axisCalculation = function () {
            this.findRanges(this.sparkline.sparklineData);
        };
        SparklineRenderer.prototype.getInterval = function (data, x) {
            var interval = 1;
            var x1 = data[0][x];
            var x2 = ej2_base_1.isNullOrUndefined(data[1]) ? undefined : data[1][x];
            if (!ej2_base_1.isNullOrUndefined(x1) && !ej2_base_1.isNullOrUndefined(x2)) {
                var temp = ej2_base_1.extend([], data);
                var validData_1 = [];
                temp.forEach(function (value) {
                    Eif (!ej2_base_1.isNullOrUndefined(value[x])) {
                        validData_1.push(value);
                    }
                });
                validData_1.sort(function (a, b) {
                    Iif (ej2_base_1.isNullOrUndefined(a[x]) || ej2_base_1.isNullOrUndefined(b[x])) {
                        return 0;
                    }
                    return a[x] - b[x];
                });
                validData_1 = (this.sparkline.enableRtl) ? validData_1.reverse() : validData_1;
                interval = validData_1[1][x] - validData_1[0][x];
            }
            return interval;
        };
        SparklineRenderer.prototype.getPaddingInterval = function (data, x, type, delta) {
            var interval = 1;
            var size = this.sparkline.availableSize.height;
            var intervalCount = interval * data.length;
            intervalCount = Math.max((size * (intervalCount / 100)), 1);
            var niceInterval = delta / intervalCount;
            for (var _i = 0, _a = this.sparkline.intervalDivs; _i < _a.length; _i++) {
                var intervalVal = _a[_i];
                var currentInterval = interval * intervalVal;
                if (intervalCount < (delta / currentInterval)) {
                    break;
                }
                niceInterval = currentInterval;
            }
            return niceInterval;
        };
        SparklineRenderer.prototype.findRanges = function (data) {
            var model = this.sparkline;
            var max;
            var min;
            var minX;
            var maxX;
            var maxPointsLength = data.length;
            var temp;
            var sumofValues = 0;
            var isNumericArray = Array.isArray(data) && typeof data[0] !== 'object';
            if (isNumericArray) {
                if (model.type === 'Pie') {
                    for (var i = 0; i < maxPointsLength; i++) {
                        sumofValues += Math.abs(data[i]);
                    }
                }
                else {
                    max = Math.max.apply(null, data);
                    min = Math.min.apply(null, data);
                    minX = 0;
                    maxX = maxPointsLength - 1;
                }
            }
            else {
                if (model.type === 'Pie') {
                    for (var i = 0; i < maxPointsLength; i++) {
                        sumofValues += Math.abs(data[i][model.yName]);
                    }
                }
                else {
                    if (ej2_base_1.isNullOrUndefined(data[0][model.xName])) {
                        var x_1 = data.map(function (z) { return z[model.yName]; });
                        max = Math.max.apply(null, x_1);
                        min = Math.min.apply(null, x_1);
                    }
                    else {
                        temp = ej2_base_1.extend([], data);
                        temp = temp.sort(function (a, b) { return a[model.yName] - b[model.yName]; });
                        max = temp[temp.length - 1][model.yName];
                        min = temp[0][model.yName];
                    }
                    if (!ej2_base_1.isNullOrUndefined(data[0][model.xName])) {
                        temp = temp.sort(function (a, b) { return a[model.xName] - b[model.xName]; });
                        temp = (this.sparkline.enableRtl) ? temp.reverse() : temp;
                        maxX = temp[temp.length - 1][model.xName];
                        minX = temp[0][model.xName];
                    }
                    else {
                        minX = 0;
                        maxX = maxPointsLength - 1;
                    }
                }
            }
            var y2;
            var height;
            var width;
            var x1 = 0;
            var y1;
            var padding = model.padding;
            var point;
            var axis = model.axisSettings;
            var value = axis.value;
            if (model.type !== 'Pie') {
                this.maxLength = maxPointsLength;
                height = model.availableSize.height - (padding.bottom + padding.top);
                width = model.availableSize.width - (padding.left + padding.right);
                maxX = ej2_base_1.isNullOrUndefined(axis.maxX) ? maxX : axis.maxX;
                minX = ej2_base_1.isNullOrUndefined(axis.minX) ? minX : axis.minX;
                max = ej2_base_1.isNullOrUndefined(axis.maxY) ? max : axis.maxY;
                min = ej2_base_1.isNullOrUndefined(axis.minY) ? min : axis.minY;
                var color = axis.lineSettings.color || this.sparkline.sparkTheme.axisLineColor;
                var eventArgs = {
                    name: 'axisRendering', cancel: false, sparkline: model,
                    maxX: maxX, minX: minX, maxY: max, minY: min, value: axis.value,
                    lineColor: color, lineWidth: axis.lineSettings.width
                };
                model.trigger('axisRendering', eventArgs);
                Iif (eventArgs.cancel) {
                    this.visiblePoints = [];
                    return;
                }
                maxX = eventArgs.maxX;
                minX = eventArgs.minX;
                max = eventArgs.maxY;
                min = eventArgs.minY;
                value = this.axisValue = eventArgs.value;
                this.axisColor = eventArgs.lineColor;
                this.axisWidth = eventArgs.lineWidth;
            }
            var unitX = maxX - minX;
            var unitY = max - min;
            unitX = (unitX === 0) ? 1 : unitX;
            unitY = (unitY === 0) ? 1 : unitY;
            this.unitX = unitX;
            this.unitY = unitY;
            this.min = min;
            x1 = 0;
            y1 = height - ((height / unitY) * (-min));
            y1 = (min < 0 && max <= 0) ? 0 : (min < 0 && max > 0) ? y1 : height;
            if (value >= min && value <= max) {
                y1 = height - Math.round(height * ((value - min) / this.unitY));
            }
            this.axisHeight = y1 + padding.top;
            var percent;
            var x;
            var y;
            var visiblePoints = [];
            var delta = max - min;
            var interval = this.getInterval(data, model.xName);
            var interVal = this.getPaddingInterval(data, model.xName, model.valueType, delta);
            for (var i = 0; i < maxPointsLength; i++) {
                if (ej2_base_1.isNullOrUndefined(data[i][model.xName]) && ej2_base_1.isNullOrUndefined(data[i][model.yName]) &&
                    ((data[i][model.yName]) !== 0) && isNumericArray) {
                    x = i;
                    y = data[i];
                }
                else if (ej2_base_1.isNullOrUndefined(data[i][model.xName])) {
                    x = i;
                    y = data[i][model.yName];
                }
                else {
                    x = data[i][model.xName];
                    y = data[i][model.yName];
                }
                if (ej2_base_1.isNullOrUndefined(x) || ej2_base_1.isNullOrUndefined(y)) {
                    continue;
                }
                if (model.type === 'Line' || model.type === 'Area') {
                    y2 = (min !== max && maxPointsLength !== 1) ? height - Math.round(height * ((y - min) / this.unitY)) : padding.top;
                    point = { x: (minX !== maxX) ? Math.round(width * ((x - minX) / this.unitX)) : width / 2, y: y2, markerPosition: y2 };
                }
                else if (model.type === 'Column' || model.type === 'WinLoss') {
                    var colWidth = width / (((maxX - minX) / interval) + 1);
                    var calSpace = 0.5;
                    var space = (calSpace * 2);
                    colWidth -= (space);
                    x1 = (((x - minX) / interval) * (colWidth + space)) + (space / 2);
                    if (model.type === 'WinLoss') {
                        var winLossFactor = 0.5;
                        var drawHeightFactor = 40;
                        y2 = (y > value) ? (height / 4) : (y < value) ? (height * winLossFactor) :
                            ((height * winLossFactor) - (height / drawHeightFactor));
                        point = {
                            x: x1, y: y2, height: (y !== value) ? (height / 4) : height / 20, width: colWidth,
                            markerPosition: (y2 > y1) ? (y1 + Math.abs(y2 - y1)) : y2
                        };
                    }
                    else {
                        if (i === 0 && model.rangePadding !== 'None') {
                            min -= model.rangePadding === 'Additional' ? (interVal + padding.top) : interVal;
                            max += model.rangePadding === 'Additional' ? (interVal + padding.top) : interVal;
                            unitX = maxX - minX;
                            unitY = max - min;
                            unitX = (unitX === 0) ? 1 : unitX;
                            unitY = (unitY === 0) ? 1 : unitY;
                            this.unitX = unitX;
                            this.unitY = unitY;
                            this.min = min;
                        }
                        var z = ((height / this.unitY) * (y - min));
                        var z1 = (y === min && y > value) ? ((maxPointsLength !== 1 && this.unitY !== 1) ?
                            (height / this.unitY) * (min / 2) : (z | 1)) :
                            (y === max && y < value && maxPointsLength !== 1 && this.unitY !== 1) ? (height / this.unitY) * (-max / 2) : z;
                        y2 = Math.abs(height - z1);
                        point = {
                            x: x1, y: (y2 > y1) ? y1 : y2, height: Math.abs(y2 - y1),
                            width: colWidth, markerPosition: (y2 > y1) ? (y1 + Math.abs(y2 - y1)) : y2
                        };
                    }
                }
                else Eif (model.type === 'Pie') {
                    percent = (Math.abs(y) / sumofValues) * 100;
                    point = {
                        percent: percent, degree: ((Math.abs(y) / sumofValues) * 360)
                    };
                }
                if (model.type !== 'Pie') {
                    point.x += padding.left;
                    point.y += padding.top;
                }
                if (model.type !== 'WinLoss') {
                    point.markerPosition += padding.top;
                }
                point.location = { x: point.x, y: point.y };
                point.xVal = x;
                point.yVal = y;
                visiblePoints.push(point);
            }
            visiblePoints.sort(function (a, b) {
                return a.x - b.x;
            });
            this.visiblePoints = visiblePoints;
        };
        SparklineRenderer.prototype.drawAxis = function () {
            var spark = this.sparkline;
            var height = this.axisHeight;
            if ((spark.type !== 'WinLoss') && (spark.type !== 'Pie') && spark.axisSettings.lineSettings.visible) {
                var xAxis = {
                    'id': spark.element.id + '_Sparkline_XAxis',
                    'x1': spark.padding.left, 'y1': height,
                    'x2': spark.availableSize.width - spark.padding.right, 'y2': height,
                    'stroke': this.axisColor,
                    'opacity': spark.axisSettings.lineSettings.opacity,
                    'stroke-dasharray': spark.axisSettings.lineSettings.dashArray,
                    'stroke-width': this.axisWidth,
                    'clip-path': 'url(#' + this.clipId + ')'
                };
                spark.svgObject.appendChild(spark.renderer.drawLine(xAxis));
            }
        };
        SparklineRenderer.prototype.triggerPointRender = function (name, i, fill, border) {
            var args = {
                name: name, cancel: false,
                border: border, fill: fill,
                sparkline: this.sparkline,
                pointIndex: i
            };
            this.sparkline.trigger(name, args);
            return args;
        };
        return SparklineRenderer;
    }());
    exports.SparklineRenderer = SparklineRenderer;
});