all files / circular-gauge/legend/ legend.js

99.08% Statements 431/435
91.6% Branches 240/262
100% Functions 34/34
99.08% Lines 431/435
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   49× 49× 49× 49× 49× 49× 49× 49× 49× 49× 49× 49× 49× 49×   49×     49×   49×   45×   60× 60× 60× 60× 82× 480× 480× 474× 474×         60× 60×   60× 60× 60× 32× 32×     28× 28×   60× 60×   60×     59× 59×   60×   60× 60× 60× 60× 60× 12× 12× 12×   48× 15× 15× 15×   33× 19× 19× 19×     14× 14× 14×     54× 54× 54× 54× 54× 54× 54× 54× 54× 54× 54× 54× 54× 54× 54× 54× 54× 402× 402× 401×     401× 401× 401× 401× 401×   401× 401×   401×   402×   54× 21×     33×     54× 54×   21× 21× 21× 21× 21× 21× 21× 13×       21× 21× 21× 21×   21×               21× 21× 21× 21× 21× 21× 21×   21× 21× 21× 21× 21× 21× 21× 21× 21×   21× 21× 21×   24× 24× 24×   24× 24× 24× 24×   401× 401× 401× 401× 401×               401× 401×   401× 401× 401× 401×   401×   401×   401× 401×               401× 201× 60×   60× 60× 60× 60×     141× 141×       200× 200× 57×   57×     143× 143×   200×   401× 401×   200× 152×     48×     17× 17× 17× 17× 17× 17× 34× 34× 14× 14× 14× 12× 12× 12×                         12×         17×   16×     66× 17× 17× 20× 20× 20×     17× 17× 17× 17× 17× 17× 17×       13× 13× 10× 10× 10×         12× 30× 30× 12×               401× 201×   401×   54× 54×       54× 54× 54× 54× 54× 54× 54× 54× 54× 54× 54× 54× 54× 54× 54×   108× 108× 108× 108×     100×     54× 54× 55× 55× 54×     54×   60× 60× 60× 60× 28×     32×   60× 60× 60×               60× 60× 60× 60× 60× 60× 60× 60× 60× 60× 474× 474×       474× 474× 474× 474× 474× 474× 474× 473× 473× 473× 249× 249× 249× 32× 32× 32× 32× 32× 32×   249×     224× 224× 58× 58× 28×   58× 58× 58×         60× 32× 32× 32× 32× 32× 31×       28× 28× 28×   60× 60×   60× 60×           60× 60× 60× 60× 60×   60× 60× 60× 60× 60× 60×       32× 32× 32× 44× 44× 44× 14× 14× 14×     32× 32× 10×   32× 32× 32×   1667×   49× 49× 49× 49× 49× 49× 49× 49× 49× 49× 49× 49× 49×     12× 12× 12×     474× 474× 474× 474× 474× 474× 474× 474× 474× 474× 474× 474×        
define(["require", "exports", "../utils/helper-common", "../utils/helper-legend", "@syncfusion/ej2-base"], function (require, exports, helper_common_1, helper_legend_1, ej2_base_1) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    var Legend = (function () {
        function Legend(gauge) {
            this.legendRegions = [];
            this.rowCount = 0;
            this.pageButtonSize = 8;
            this.pageXCollections = [];
            this.maxColumns = 0;
            this.maxWidth = 0;
            this.currentPage = 1;
            this.pagingRegions = [];
            this.position = 'Auto';
            this.gauge = gauge;
            this.toggledIndexes = [];
            this.legend = this.gauge.legendSettings;
            this.legendID = this.gauge.element.id + '_gauge_legend';
            this.addEventListener();
        }
        Legend.prototype.addEventListener = function () {
            Iif (this.gauge.isDestroyed) {
                return;
            }
            this.gauge.on('click', this.click, this);
        };
        Legend.prototype.removeEventListener = function () {
            if (this.gauge.isDestroyed) {
                return;
            }
            this.gauge.off('click', this.click);
        };
        Legend.prototype.getLegendOptions = function (axes) {
            this.legendCollection = [];
            var range;
            var text = '';
            for (var i = 0; i < axes.length; i++) {
                for (var j = 0; j < axes[i].ranges.length; j++) {
                    range = axes[i].ranges[j];
                    if (!ej2_base_1.isNullOrUndefined(range.start) && !ej2_base_1.isNullOrUndefined(range.end) && (range.start !== range.end)) {
                        text = range.legendText ? range.legendText : range.start + ' - ' + range.end;
                        this.legendCollection.push(new LegendOptions(text, text, range.color, this.legend.shape, this.legend.visible, this.legend.border, this.legend.shapeBorder, this.legend.shapeWidth, this.legend.shapeHeight, j, i));
                    }
                }
            }
        };
        Legend.prototype.calculateLegendBounds = function (rect, availableSize) {
            var legend = this.legend;
            this.position = (legend.position !== 'Auto') ? legend.position :
                (availableSize.width > availableSize.height ? 'Right' : 'Bottom');
            this.legendBounds = new helper_common_1.Rect(rect.x, rect.y, 0, 0);
            this.isVertical = (this.position === 'Left' || this.position === 'Right');
            if (this.isVertical) {
                this.legendBounds.height = helper_common_1.stringToNumber(legend.height, availableSize.height - (rect.y - this.gauge.margin.top)) || rect.height;
                this.legendBounds.width = helper_common_1.stringToNumber(legend.width || '20%', availableSize.width);
            }
            else {
                this.legendBounds.width = helper_common_1.stringToNumber(legend.width, availableSize.width) || rect.width;
                this.legendBounds.height = helper_common_1.stringToNumber(legend.height || '20%', availableSize.height);
            }
            this.getLegendBounds(availableSize, this.legendBounds, legend);
            this.getLocation(this.position, legend.alignment, this.legendBounds, rect, availableSize);
        };
        Legend.prototype.alignLegend = function (start, size, legendSize, alignment) {
            switch (alignment) {
                case 'Far':
                    start = (size - legendSize) - start;
                    break;
                case 'Center':
                    start = ((size - legendSize) / 2);
                    break;
            }
            return start;
        };
        Legend.prototype.getLocation = function (position, alignment, legendBounds, rect, availableSize) {
            var padding = this.legend.border.width;
            var legendHeight = legendBounds.height + padding + this.legend.margin.top + this.legend.margin.bottom;
            var legendWidth = legendBounds.width + padding + this.legend.margin.left + this.legend.margin.right;
            var marginBottom = this.gauge.margin.bottom;
            if (position === 'Bottom') {
                legendBounds.x = this.alignLegend(legendBounds.x, availableSize.width, legendBounds.width, alignment);
                legendBounds.y = rect.y + (rect.height - legendHeight) + padding + this.legend.margin.top;
                this.subtractThickness(rect, 0, 0, 0, legendHeight);
            }
            else if (position === 'Top') {
                legendBounds.x = this.alignLegend(legendBounds.x, availableSize.width, legendBounds.width, alignment);
                legendBounds.y = rect.y + padding + this.legend.margin.top;
                this.subtractThickness(rect, 0, 0, legendHeight, 0);
            }
            else if (position === 'Right') {
                legendBounds.x = rect.x + (rect.width - legendBounds.width) + this.legend.margin.right;
                legendBounds.y = rect.y + this.alignLegend(0, availableSize.height - (rect.y + marginBottom), legendBounds.height, alignment);
                this.subtractThickness(rect, 0, legendWidth, 0, 0);
            }
            else {
                legendBounds.x = legendBounds.x + this.legend.margin.left;
                legendBounds.y = rect.y + this.alignLegend(0, availableSize.height - (rect.y + marginBottom), legendBounds.height, alignment);
                this.subtractThickness(rect, legendWidth, 0, 0, 0);
            }
        };
        Legend.prototype.renderLegend = function (legend, legendBounds) {
            var firstLegend = this.findFirstLegendPosition(this.legendCollection);
            var padding = legend.padding;
            this.legendRegions = [];
            this.maxItemHeight = Math.max(this.legendCollection[0].textSize.height, legend.shapeHeight);
            var legendGroup = this.gauge.renderer.createGroup({ id: this.legendID + '_g' });
            var legendTranslateGroup = this.createLegendElements(legendBounds, legendGroup, legend, this.legendID);
            Eif (firstLegend !== this.legendCollection.length) {
                this.totalPages = 0;
                var legendAxisGroup = void 0;
                var start = new helper_common_1.GaugeLocation((!this.gauge.enableRtl) ? legendBounds.x + padding + (legend.shapeWidth / 2) : (!this.isVertical) ? legendBounds.width + legendBounds.x - (padding) - (legend.shapeWidth) : legendBounds.x + this.maxWidth - padding - legend.shapeWidth / 2, legendBounds.y + padding + this.maxItemHeight / 2);
                var textOptions = new helper_common_1.TextOption('', start.x, start.y, 'start');
                var textPadding = (2 * legend.shapePadding) + (2 * padding) + legend.shapeWidth;
                var count = 0;
                this.pageXCollections = [];
                this.legendCollection[firstLegend].location = start;
                var previousLegend = this.legendCollection[firstLegend];
                for (var _i = 0, _a = this.legendCollection; _i < _a.length; _i++) {
                    var legendOption = _a[_i];
                    if (legendOption.render && legendOption.text !== '') {
                        legendAxisGroup = this.gauge.renderer.createGroup({
                            id: this.legendID + '_g_' + count
                        });
                        this.getRenderPoint(legendOption, start, textPadding, previousLegend, legendBounds, count, firstLegend);
                        this.renderSymbol(legendOption, legendAxisGroup, legendOption.axisIndex, legendOption.rangeIndex);
                        this.renderText(legendOption, legendAxisGroup, textOptions, legendOption.axisIndex, legendOption.rangeIndex);
                        Eif (legendAxisGroup) {
                            legendAxisGroup.style.cursor = (!legend.toggleVisibility) ? 'auto' : 'pointer';
                        }
                        Eif (legendTranslateGroup) {
                            legendTranslateGroup.appendChild(legendAxisGroup);
                        }
                        previousLegend = legendOption;
                    }
                    count++;
                }
                if (this.isPaging) {
                    this.renderPagingElements(legendBounds, textOptions, legendGroup);
                }
                else {
                    this.totalPages = 1;
                }
            }
            this.appendChildElement(this.gauge.svgObject, legendGroup);
            this.setStyles(this.toggledIndexes);
        };
        Legend.prototype.renderPagingElements = function (bounds, textOption, legendGroup) {
            var paginggroup = this.gauge.renderer.createGroup({ id: this.legendID + '_navigation' });
            this.pagingRegions = [];
            legendGroup.appendChild(paginggroup);
            var grayColor = this.gauge.themeStyle.labelColor;
            var legend = this.gauge.legendSettings;
            var padding = 8;
            if (!this.isVertical) {
                this.totalPages = Math.ceil(this.totalPages / Math.max(1, this.rowCount - 1));
            }
            else {
                this.totalPages = Math.ceil(this.totalPages / this.maxColumns);
            }
            var symbolOption = new helper_common_1.PathOption(this.legendID + '_pageup', 'transparent', 5, grayColor, 1, '', '');
            var iconSize = this.pageButtonSize;
            Eif (paginggroup) {
                paginggroup.style.cursor = 'pointer';
            }
            var textStyle = {
                size: legend.textStyle.size || this.gauge.themeStyle.fontSize,
                color: legend.textStyle.color || this.gauge.themeStyle.labelColor,
                fontFamily: legend.textStyle.fontFamily || this.gauge.themeStyle.labelFontFamily,
                fontWeight: legend.textStyle.fontWeight || this.gauge.themeStyle.fontWeight,
                fontStyle: legend.textStyle.fontStyle,
                opacity: legend.textStyle.opacity
            };
            this.clipPathHeight = (this.rowCount - 1) * (this.maxItemHeight + legend.padding);
            this.clipRect.setAttribute('height', this.clipPathHeight.toString());
            var x = bounds.x + iconSize / 2;
            var y = bounds.y + this.clipPathHeight + ((bounds.height - this.clipPathHeight) / 2);
            var size = helper_common_1.measureText(this.totalPages + '/' + this.totalPages, textStyle);
            helper_common_1.appendPath(helper_common_1.calculateShapes({ x: x, y: y }, 'LeftArrow', new helper_common_1.Size(iconSize, iconSize), '', symbolOption), paginggroup, this.gauge, 'Path');
            this.pagingRegions.push(new helper_common_1.Rect(!this.gauge.enableRtl ?
                x + bounds.width - (2 * (iconSize + padding) + padding + size.width) - iconSize * 0.5 : x, y - iconSize * 0.5, iconSize, iconSize));
            textOption.x = x + (iconSize / 2) + padding;
            textOption.y = y + (size.height / 4);
            textOption.id = this.legendID + '_pagenumber';
            textOption.text = !this.gauge.enableRtl ? '1/' + this.totalPages : this.totalPages + '/1';
            var pageTextElement = helper_common_1.textElement(textOption, textStyle, grayColor, paginggroup);
            x = (textOption.x + padding + (iconSize / 2) + size.width);
            symbolOption.id = this.legendID + '_pagedown';
            helper_common_1.appendPath(helper_common_1.calculateShapes({ x: x, y: y }, 'RightArrow', new helper_common_1.Size(iconSize, iconSize), '', symbolOption), paginggroup, this.gauge, 'Path');
            this.pagingRegions.push(new helper_common_1.Rect(!this.gauge.enableRtl ?
                x + (bounds.width - (2 * (iconSize + padding) + padding + size.width) - iconSize * 0.5) : x, y - iconSize * 0.5, iconSize, iconSize));
            var translateX = (this.gauge.enableRtl) ? legend.border.width + (iconSize / 2) : bounds.width - (2 * (iconSize + padding) + padding + size.width);
            paginggroup.setAttribute('transform', 'translate(' + translateX + ', ' + 0 + ')');
            this.translatePage(pageTextElement, this.currentPage - 1, this.currentPage);
        };
        Legend.prototype.translatePage = function (pagingText, page, pageNumber) {
            var size = (this.clipPathHeight) * page;
            var translate = 'translate(0,-' + size + ')';
            if (this.isVertical) {
                var pageX = this.pageXCollections[page * this.maxColumns];
                size = (!this.gauge.enableRtl) ? pageX - this.legendBounds.x : (this.legendBounds.x + this.maxWidth) - pageX;
                size = size < 0 ? 0 : size;
                translate = ((!this.gauge.enableRtl) ? 'translate(-' : 'translate(') + size + ',0)';
            }
            this.legendTranslateGroup.setAttribute('transform', translate);
            pagingText.textContent = !this.gauge.enableRtl ? (pageNumber) + '/' + this.totalPages : this.totalPages + '/' + pageNumber;
            this.currentPage = pageNumber;
            return size;
        };
        Legend.prototype.renderText = function (legendOption, group, textOptions, axisIndex, rangeIndex) {
            var legend = this.gauge.legendSettings;
            var hiddenColor = '#D3D3D3';
            textOptions.id = this.legendID + '_Axis_' + axisIndex + '_text_' + rangeIndex;
            var fontcolor = legendOption.visible ? legend.textStyle.color || this.gauge.themeStyle.labelColor : hiddenColor;
            var textStyle = {
                size: legend.textStyle.size || this.gauge.themeStyle.fontSize,
                color: fontcolor,
                fontFamily: legend.textStyle.fontFamily || this.gauge.themeStyle.labelFontFamily,
                fontWeight: legend.textStyle.fontWeight || this.gauge.themeStyle.fontWeight,
                fontStyle: legend.textStyle.fontStyle,
                opacity: legend.textStyle.opacity
            };
            textOptions.text = legendOption.text;
            textOptions.x = this.gauge.enableRtl ? (legendOption.location.x - (helper_common_1.measureText(legendOption.text, textStyle).width +
                legend.shapeWidth / 2 + legend.shapePadding)) : (legendOption.location.x + (legend.shapeWidth / 2) + legend.shapePadding);
            textOptions.y = legendOption.location.y + this.maxItemHeight / 4;
            var legendTextElement = helper_common_1.textElement(textOptions, textStyle, fontcolor, group, '');
            legendTextElement.setAttribute('aria-label', textOptions.text);
            legendTextElement.setAttribute('role', 'region');
        };
        Legend.prototype.renderSymbol = function (legendOption, group, axisIndex, rangeIndex) {
            legendOption.fill = legendOption.fill ? legendOption.fill :
                this.gauge.axes[axisIndex].ranges[rangeIndex].rangeColor;
            helper_common_1.appendPath(helper_common_1.calculateShapes(legendOption.location, legendOption.shape, new helper_common_1.Size(legendOption.shapeWidth, legendOption.shapeHeight), '', new helper_common_1.PathOption(this.legendID + '_Axis_' + axisIndex + '_Shape_' + rangeIndex, legendOption.fill, legendOption.shapeBorder.width, legendOption.shapeBorder.color, null, legendOption.shapeBorder.dashArray, '', '')), group, this.gauge, legendOption.shape === 'Circle' ? 'Ellipse' : 'Path');
        };
        Legend.prototype.getRenderPoint = function (legendOption, start, textPadding, prevLegend, rect, count, firstLegend) {
            var padding = this.legend.padding;
            var textStyle = {
                size: this.legend.textStyle.size || this.gauge.themeStyle.fontSize,
                color: this.legend.textStyle.color || this.gauge.themeStyle.labelColor,
                fontFamily: this.legend.textStyle.fontFamily || this.gauge.themeStyle.labelFontFamily,
                fontWeight: this.legend.textStyle.fontWeight || this.gauge.themeStyle.fontWeight,
                fontStyle: this.legend.textStyle.fontStyle,
                opacity: this.legend.textStyle.opacity
            };
            if (this.isVertical) {
                if (count === firstLegend || (prevLegend.location.y + (this.maxItemHeight * 1.5) + (padding * 2) > rect.y + rect.height)) {
                    legendOption.location.x = prevLegend.location.x + ((count === firstLegend) ? 0 : (!this.gauge.enableRtl) ?
                        this.maxColumnWidth : -this.maxColumnWidth - (4 * this.legend.shapePadding) / 3);
                    legendOption.location.y = start.y;
                    var textStartLoc = (this.legend.shapeWidth / 2) + padding;
                    this.pageXCollections.push(legendOption.location.x + ((!this.gauge.enableRtl) ? -textStartLoc : textStartLoc));
                    this.totalPages++;
                }
                else {
                    legendOption.location.x = prevLegend.location.x;
                    legendOption.location.y = prevLegend.location.y + this.maxItemHeight + padding;
                }
            }
            else {
                var previousBound = (prevLegend.location.x + ((!this.gauge.enableRtl) ? prevLegend.textSize.width + textPadding : -prevLegend.textSize.width - textPadding));
                if (this.isWithinBounds(previousBound, (legendOption.textSize.width + textPadding) - padding, rect, this.legend.shapeWidth / 2)) {
                    legendOption.location.y = (count === firstLegend) ? prevLegend.location.y :
                        prevLegend.location.y + this.maxItemHeight + padding;
                    legendOption.location.x = start.x;
                }
                else {
                    legendOption.location.y = prevLegend.location.y;
                    legendOption.location.x = (count === firstLegend) ? prevLegend.location.x : previousBound;
                }
                this.totalPages = this.totalRowCount;
            }
            var availablewidth = this.getAvailWidth(legendOption.location.x, this.legendBounds.width);
            legendOption.text = helper_legend_1.textTrim(+availablewidth.toFixed(4), legendOption.text, textStyle);
        };
        Legend.prototype.isWithinBounds = function (previousBound, textWidth, legendBounds, shapeWidth) {
            if (!this.gauge.enableRtl) {
                return (previousBound + textWidth) > (legendBounds.x + legendBounds.width + shapeWidth);
            }
            else {
                return (previousBound - textWidth) < (legendBounds.x - shapeWidth);
            }
        };
        Legend.prototype.click = function (event) {
            var targetId = event.target.id;
            var legendItemsId = ['_text_', '_Shape_'];
            var index;
            var toggledIndex = -1;
            Eif (targetId.indexOf(this.legendID) > -1) {
                for (var _i = 0, legendItemsId_1 = legendItemsId; _i < legendItemsId_1.length; _i++) {
                    var id = legendItemsId_1[_i];
                    if (targetId.indexOf(id) > -1) {
                        var axisIndex = parseInt(targetId.split(this.legendID + '_Axis_')[1].split(id)[0], 10);
                        var rangeIndex = parseInt(targetId.split(this.legendID + '_Axis_')[1].split(id)[1], 10);
                        if (this.gauge.legendSettings.toggleVisibility && !isNaN(rangeIndex)) {
                            var legendOption = this.legendByIndex(axisIndex, rangeIndex, this.legendCollection);
                            index = new Index(axisIndex, rangeIndex, !legendOption.render);
                            if (this.toggledIndexes.length === 0) {
                                this.toggledIndexes.push(index);
                            }
                            else {
                                for (var i = 0; i < this.toggledIndexes.length; i++) {
                                    if (this.toggledIndexes[i].axisIndex === index.axisIndex &&
                                        this.toggledIndexes[i].rangeIndex === index.rangeIndex) {
                                        toggledIndex = i;
                                        break;
                                    }
                                    else {
                                        toggledIndex = -1;
                                    }
                                }
                                if (toggledIndex === -1) {
                                    this.toggledIndexes.push(index);
                                }
                                else {
                                    this.toggledIndexes[toggledIndex].isToggled =
                                        !this.toggledIndexes[toggledIndex].isToggled;
                                }
                            }
                            this.setStyles(this.toggledIndexes);
                        }
                    }
                }
            }
            if (targetId.indexOf(this.legendID + '_pageup') > -1) {
                this.changePage(event, !this.gauge.enableRtl ? true : false);
            }
            else if (targetId.indexOf(this.legendID + '_pagedown') > -1) {
                this.changePage(event, !this.gauge.enableRtl ? false : true);
            }
        };
        Legend.prototype.setStyles = function (toggledIndexes) {
            for (var i = 0; i < toggledIndexes.length; i++) {
                var count = 0;
                for (var j = 0; j < toggledIndexes[i].rangeIndex; j++) {
                    var rangeStart = this.gauge.axes[toggledIndexes[i].axisIndex].ranges[j].start;
                    var rangeEnd = this.gauge.axes[toggledIndexes[i].axisIndex].ranges[j].end;
                    if (rangeStart === rangeEnd) {
                        count++;
                    }
                }
                var rangeID = this.gauge.element.id + '_Axis_' + toggledIndexes[i].axisIndex + '_Range_' + toggledIndexes[i].rangeIndex;
                var shapeID = this.legendID + '_Axis_' + toggledIndexes[i].axisIndex + '_Shape_' + toggledIndexes[i].rangeIndex;
                var textID = this.legendID + '_Axis_' + toggledIndexes[i].axisIndex + '_text_' + toggledIndexes[i].rangeIndex;
                var rangeElement = this.gauge.svgObject.querySelector('#' + rangeID);
                var shapeElement = this.gauge.svgObject.querySelector('#' + shapeID);
                var textElement_1 = this.gauge.svgObject.querySelector('#' + textID);
                if (toggledIndexes[i].isToggled) {
                    Eif (!ej2_base_1.isNullOrUndefined(rangeElement)) {
                        rangeElement.style.visibility = 'visible';
                        shapeElement.setAttribute('fill', this.legendCollection[toggledIndexes[i].rangeIndex - count].fill);
                        textElement_1.setAttribute('fill', this.legend.textStyle.color || this.gauge.themeStyle.labelColor);
                    }
                }
                else {
                    var hiddenColor = '#D3D3D3';
                    if (!ej2_base_1.isNullOrUndefined(rangeElement)) {
                        rangeElement.style.visibility = 'hidden';
                        shapeElement.setAttribute('fill', hiddenColor);
                        textElement_1.setAttribute('fill', hiddenColor);
                    }
                }
            }
        };
        Legend.prototype.legendByIndex = function (axisIndex, rangeIndex, legendCollections) {
            for (var _i = 0, legendCollections_1 = legendCollections; _i < legendCollections_1.length; _i++) {
                var legend = legendCollections_1[_i];
                if (legend.axisIndex === axisIndex && legend.rangeIndex === rangeIndex) {
                    return legend;
                }
            }
            return null;
        };
        Legend.prototype.changePage = function (event, pageUp) {
            var pageText = document.getElementById(this.legendID + '_pagenumber');
            var page = parseInt(pageText.textContent.split('/')[!this.gauge.enableRtl ? 0 : 1], 10);
            if (pageUp && page > 1) {
                this.translatePage(pageText, (page - 2), (page - 1));
            }
            else Eif (!pageUp && page < this.totalPages) {
                this.translatePage(pageText, page, (page + 1));
            }
        };
        Legend.prototype.getAvailWidth = function (tx, width) {
            if (this.isVertical) {
                width = this.maxWidth;
            }
            return width - ((this.legend.padding * 2) + this.legend.shapeWidth + this.legend.shapePadding);
        };
        Legend.prototype.createLegendElements = function (legendBounds, legendGroup, legend, id) {
            var padding = legend.padding;
            var borderStyle = {
                color: legend.border.color || this.gauge.themeStyle.legendBorderColor || '',
                width: legend.border.width || this.gauge.themeStyle.legendBorderWidth || 1, dashArray: legend.border.dashArray
            };
            var options = new helper_common_1.RectOption(id + '_element', legend.background, borderStyle, legend.opacity, legendBounds);
            options.width = this.isVertical ? this.maxWidth : legendBounds.width;
            legendGroup ? legendGroup.appendChild(this.gauge.renderer.drawRectangle(options)) : this.gauge.renderer.drawRectangle(options);
            var legendItemsGroup = this.gauge.renderer.createGroup({ id: id + '_collections' });
            legendGroup.appendChild(legendItemsGroup);
            this.legendTranslateGroup = this.gauge.renderer.createGroup({ id: id + '_translate_g' });
            legendItemsGroup.appendChild(this.legendTranslateGroup);
            var clippath = this.gauge.renderer.createClipPath({ id: id + '_clipPath' });
            options.id += '_clipPath_rect';
            options.width = this.isVertical ? options.width - padding : options.width;
            this.clipRect = this.gauge.renderer.drawRectangle(options);
            clippath.appendChild(this.clipRect);
            this.appendChildElement(this.gauge.svgObject, clippath);
            legendItemsGroup.style.cssText = 'clip-path:url(#' + clippath.id + ')';
            return this.legendTranslateGroup;
        };
        Legend.prototype.appendChildElement = function (parent, childElement) {
            var existChild = parent.querySelector('#' + childElement.id);
            var element = (existChild || helper_common_1.getElement(childElement.id));
            var child = childElement;
            if (existChild) {
                parent.replaceChild(child, element);
            }
            else {
                parent.appendChild(child);
            }
        };
        Legend.prototype.findFirstLegendPosition = function (legendCollection) {
            var count = 0;
            for (var _i = 0, legendCollection_1 = legendCollection; _i < legendCollection_1.length; _i++) {
                var legend = legendCollection_1[_i];
                if (legend.render && legend.text !== '') {
                    break;
                }
                count++;
            }
            return count;
        };
        Legend.prototype.getLegendBounds = function (availableSize, legendBounds, legend) {
            var extraWidth = 0;
            var extraHeight = 0;
            var padding = legend.padding;
            if (!this.isVertical) {
                extraHeight = !legend.height ? ((availableSize.height / 100) * 5) : 0;
            }
            else {
                extraWidth = !legend.width ? ((availableSize.width / 100) * 5) : 0;
            }
            legendBounds.width += extraWidth;
            legendBounds.height += extraHeight;
            var textStyle = {
                size: legend.textStyle.size || this.gauge.themeStyle.fontSize,
                color: this.legend.textStyle.color || this.gauge.themeStyle.labelColor,
                fontFamily: legend.textStyle.fontFamily || this.gauge.themeStyle.labelFontFamily,
                fontWeight: legend.textStyle.fontWeight || this.gauge.themeStyle.fontWeight,
                fontStyle: legend.textStyle.fontStyle,
                opacity: legend.textStyle.opacity
            };
            var maximumWidth = 0;
            var rowWidth = 0;
            var rowCount = 0;
            var columnWidth = [];
            var columnHeight = 0;
            var legendWidth = 0;
            this.maxItemHeight = Math.max(helper_common_1.measureText('MeasureText', textStyle).height, legend.shapeHeight);
            var legendEventArgs;
            var render = false;
            for (var _i = 0, _a = this.legendCollection; _i < _a.length; _i++) {
                var legendOption = _a[_i];
                legendEventArgs = {
                    fill: legendOption.fill, text: legendOption.text, shape: legendOption.shape,
                    name: 'legendRender', cancel: false
                };
                this.gauge.trigger('legendRender', legendEventArgs);
                legendOption.render = !legendEventArgs.cancel;
                legendOption.text = legendEventArgs.text;
                legendOption.fill = legendEventArgs.fill;
                legendOption.shape = legendEventArgs.shape;
                legendOption.textSize = helper_common_1.measureText(legendOption.text, textStyle);
                if (legendOption.render && legendOption.text !== '') {
                    render = true;
                    legendWidth = legend.shapeWidth + (2 * legend.shapePadding) + legendOption.textSize.width + (2 * padding);
                    if (this.isVertical) {
                        ++rowCount;
                        columnHeight = (rowCount * (this.maxItemHeight + padding)) + padding;
                        if ((rowCount * (this.maxItemHeight + padding)) + padding > legendBounds.height) {
                            columnHeight = Math.max(columnHeight, (rowCount * (this.maxItemHeight + padding)) + padding);
                            rowWidth = rowWidth + maximumWidth;
                            columnWidth.push(maximumWidth);
                            this.totalPages = Math.max(rowCount, this.totalPages || 1);
                            maximumWidth = 0;
                            rowCount = 1;
                        }
                        maximumWidth = Math.max(legendWidth, maximumWidth);
                    }
                    else {
                        rowWidth = rowWidth + legendWidth;
                        if (legendBounds.width < (padding + rowWidth)) {
                            maximumWidth = Math.max(maximumWidth, (rowWidth + padding - legendWidth));
                            if (rowCount === 0 && (legendWidth !== rowWidth)) {
                                rowCount = 1;
                            }
                            rowWidth = legendWidth;
                            rowCount++;
                            columnHeight = (rowCount * (this.maxItemHeight + padding)) + padding;
                        }
                    }
                }
            }
            if (this.isVertical) {
                rowWidth = rowWidth + maximumWidth;
                this.isPaging = legendBounds.width < (rowWidth + padding);
                columnHeight = Math.max(columnHeight, ((this.totalPages || 1) * (this.maxItemHeight + padding)) + padding);
                this.isPaging = this.isPaging && (this.totalPages > 1);
                if (columnWidth[columnWidth.length - 1] !== maximumWidth) {
                    columnWidth.push(maximumWidth);
                }
            }
            else {
                this.isPaging = legendBounds.height < columnHeight;
                this.totalPages = this.totalRowCount = rowCount;
                columnHeight = Math.max(columnHeight, (this.maxItemHeight + padding) + padding);
            }
            this.maxColumns = 0;
            var width = this.isVertical ? this.getMaxColumn(columnWidth, legendBounds.width, padding, rowWidth + padding) :
                Math.max(rowWidth + padding, maximumWidth);
            Eif (render) {
                this.setBounds(width, columnHeight, legend, legendBounds);
            }
            else {
                this.setBounds(0, 0, legend, legendBounds);
            }
        };
        Legend.prototype.subtractThickness = function (rect, left, right, top, bottom) {
            rect.x += left;
            rect.y += top;
            rect.width -= left + right;
            rect.height -= top + bottom;
            return rect;
        };
        Legend.prototype.setBounds = function (computedWidth, computedHeight, legend, legendBounds) {
            computedWidth = computedWidth < legendBounds.width ? computedWidth : legendBounds.width;
            computedHeight = computedHeight < legendBounds.height ? computedHeight : legendBounds.height;
            legendBounds.width = !legend.width ? computedWidth : legendBounds.width;
            legendBounds.height = !legend.height ? computedHeight : legendBounds.height;
            this.rowCount = Math.max(1, Math.ceil((legendBounds.height - legend.padding) / (this.maxItemHeight + legend.padding)));
            Iif (this.rowCount === 1 && (legend.position === 'Bottom' || legend.position === 'Top') && (!ej2_base_1.isNullOrUndefined(legend.width) && legend.width.indexOf('%') > -1)) {
                legendBounds.width = computedWidth;
            }
        };
        Legend.prototype.getMaxColumn = function (columns, width, padding, rowWidth) {
            var maxPageColumn = padding;
            this.maxColumnWidth = Math.max.apply(null, columns);
            for (var i = 0; i < columns.length; i++) {
                maxPageColumn += this.maxColumnWidth;
                this.maxColumns++;
                if (maxPageColumn + padding > width) {
                    maxPageColumn -= this.maxColumnWidth;
                    this.maxColumns--;
                    break;
                }
            }
            this.isPaging = (maxPageColumn < rowWidth) && (this.totalPages > 1);
            if (maxPageColumn === padding) {
                maxPageColumn = width;
            }
            this.maxColumns = Math.max(1, this.maxColumns);
            this.maxWidth = maxPageColumn;
            return maxPageColumn;
        };
        Legend.prototype.getModuleName = function () {
            return 'Legend';
        };
        Legend.prototype.destroy = function () {
            this.legendCollection = [];
            this.legendRenderingCollections = [];
            this.legendRegions = [];
            this.titleRect = null;
            this.pageXCollections = [];
            this.clipRect = null;
            this.legendTranslateGroup = null;
            this.legend = null;
            this.pagingRegions = [];
            this.toggledIndexes = [];
            this.legendBounds = null;
            this.removeEventListener();
            this.gauge = null;
        };
        return Legend;
    }());
    exports.Legend = Legend;
    var Index = (function () {
        function Index(axisIndex, rangeIndex, isToggled) {
            this.axisIndex = axisIndex;
            this.rangeIndex = rangeIndex;
            this.isToggled = isToggled;
        }
        return Index;
    }());
    exports.Index = Index;
    var LegendOptions = (function () {
        function LegendOptions(text, originalText, fill, shape, visible, border, shapeBorder, shapeWidth, shapeHeight, rangeIndex, axisIndex) {
            this.location = { x: 0, y: 0 };
            this.text = text;
            this.originalText = originalText;
            this.fill = fill;
            this.shape = shape;
            this.visible = visible;
            this.border = border;
            this.shapeBorder = shapeBorder;
            this.shapeWidth = shapeWidth;
            this.shapeHeight = shapeHeight;
            this.rangeIndex = rangeIndex;
            this.axisIndex = axisIndex;
        }
        return LegendOptions;
    }());
    exports.LegendOptions = LegendOptions;
});