all files / smithchart/legend/ legendrender.js

98.51% Statements 265/269
91.55% Branches 130/142
100% Functions 14/14
98.51% Lines 265/269
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   128×   152×   152× 152× 152× 152× 152× 152× 152× 152× 152× 152× 152× 152× 152× 152×   152× 152× 152× 152× 152× 152× 152× 152× 152× 152× 152× 152× 152× 128×   125×     125× 125×   128× 128×       24× 15×       24× 24×     152× 191×             191× 191× 191× 191× 191× 191× 191× 191× 143×     191× 161× 161× 161× 161× 161× 161×     152×   152× 152×   152× 152×   152×   152×   191× 191× 191× 191× 191× 191× 191×   152× 152× 152× 152× 152× 152× 152× 152× 152× 152× 152× 152× 152× 152× 152× 152× 152× 152× 152× 152× 152× 152× 152× 152× 148×   119× 119×       21× 21×   148× 24×     21× 21×         124×   117× 117×               152× 124× 135× 135×       152× 152× 152× 152× 152× 152× 22× 22×   152× 191×   143×   143× 143× 143×     48×   15× 15×   48× 48× 48× 48×     152× 152×   152× 152×   22× 22× 22× 22× 22× 22× 22× 22× 22×     20× 20×   22×       22×     22× 22×   22× 22×   191× 191× 191× 191×       191× 191× 191× 191×             191× 169× 168× 168× 168× 168× 168× 168×   168× 168× 168× 168×     191× 191× 191×   168× 168× 168× 168× 168× 168× 168× 168× 168× 168× 168× 168× 168× 168×   146× 146× 146×             16×         16× 16× 16×   12× 12× 12×     10×                 168×   412×          
define(["require", "exports", "../../smithchart/utils/helper", "../../smithchart/utils/utils", "../model/constant"], function (require, exports, helper_1, utils_1, constant_1) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    var SmithchartLegend = (function () {
        function SmithchartLegend() {
            this.legendSeries = [];
        }
        SmithchartLegend.prototype.renderLegend = function (smithchart) {
            this._drawLegend(smithchart);
        };
        SmithchartLegend.prototype.calculateLegendBounds = function (smithchart) {
            this.legendSeries = [];
            var padding = 10;
            var legend = smithchart.legendSettings;
            var legendSizeHeight = legend.height;
            var legendSizeWidth = legend.width;
            var itemPadding = legend.itemPadding > 0 ? legend.itemPadding : 0;
            var position = legend.position.toLowerCase();
            var font = legend.title.textStyle;
            var width = 0;
            var height = 0;
            var legendItemWidth = 0;
            var legendItemHeight = 0;
            var legendHeight = 0;
            var svgObjectWidth = smithchart.availableSize.width - ((smithchart.elementSpacing * 4) - (legend.border.width * 2)
                + (smithchart.border.width * 2));
            var rowCount = legend.rowCount;
            var columnCount = legend.columnCount;
            var titleSize = helper_1.measureText(smithchart.legendSettings['title']['text'], font, smithchart.themeStyle.legendTitleFont);
            var maxRowWidth = 0;
            var totalRowHeight = 0;
            var curRowWidth = 0;
            var curRowHeight = 0;
            var allowItems;
            var itemsCountRow = 0;
            var length = smithchart.series.length;
            var legendBounds;
            Eif (smithchart.legendSettings.visible && length !== 0) {
                if (position === 'bottom' || position === 'top' || position === 'custom') {
                    if ((rowCount && columnCount) && (rowCount <= columnCount)) {
                        rowCount = length / columnCount;
                    }
                    else Iif (rowCount == null && columnCount != null) {
                        rowCount = length / columnCount;
                    }
                    else Eif (rowCount == null && columnCount == null) {
                        rowCount = 1;
                    }
                    Eif (rowCount) {
                        allowItems = Math.ceil(length / rowCount);
                    }
                }
                else {
                    if ((rowCount && columnCount) && (rowCount <= columnCount)) {
                        columnCount = length / rowCount;
                    }
                    else if (rowCount != null && columnCount == null) {
                        columnCount = length / rowCount;
                    }
                    else if (rowCount == null && columnCount == null) {
                        columnCount = 1;
                    }
                    Eif (columnCount) {
                        allowItems = columnCount;
                    }
                }
                for (var i = 0; i < length; i++) {
                    this.legendSeries.push({
                        text: smithchart.series[i]['name'] ? smithchart.series[i]['name'] : 'series' + i,
                        seriesIndex: i,
                        shape: smithchart.legendSettings.shape,
                        fill: smithchart.series[i].fill || smithchart.seriesColors[i % smithchart.seriesColors.length],
                        bounds: null
                    });
                    var legendsize = this._getLegendSize(smithchart, this.legendSeries[i]);
                    legendItemWidth = Math.max(legendsize['width'], legendItemWidth);
                    legendItemHeight = Math.max(legendsize['height'], legendItemHeight);
                    this.legendSeries[i]['bounds'] = { width: legendItemWidth, height: legendItemHeight };
                    itemsCountRow = itemsCountRow + 1;
                    curRowWidth = curRowWidth + legendItemWidth + itemPadding;
                    curRowHeight = Math.max(legendItemHeight, curRowHeight);
                    if (position === 'top' || position === 'bottom' || position === 'custom') {
                        if (curRowWidth > svgObjectWidth) {
                            curRowWidth -= legendsize.width + itemPadding;
                            maxRowWidth = Math.max(maxRowWidth, curRowWidth);
                            curRowWidth = legendsize.width + itemPadding;
                            totalRowHeight = totalRowHeight + curRowHeight + itemPadding;
                        }
                    }
                    if (itemsCountRow === allowItems || i === length - 1) {
                        maxRowWidth = Math.max(maxRowWidth, curRowWidth);
                        totalRowHeight = totalRowHeight + curRowHeight + itemPadding;
                        legendHeight = totalRowHeight;
                        itemsCountRow = 0;
                        curRowHeight = 0;
                        curRowWidth = 0;
                    }
                }
                width = (titleSize.width) > maxRowWidth - itemPadding ? (titleSize.width + padding * 2 + itemPadding) :
                    maxRowWidth + padding * 2 - (smithchart.border.width * 2);
                height = legendHeight + smithchart.elementSpacing;
                legendBounds = { x: 0, y: 0, width: width, height: height };
            }
            this.legendActualBounds = legendBounds;
            if (legendSizeWidth != null) {
                this.legendActualBounds.width = legendSizeWidth;
            }
            if (legendSizeHeight != null) {
                this.legendActualBounds.height = legendSizeHeight;
            }
            return this.legendActualBounds;
        };
        SmithchartLegend.prototype._getLegendSize = function (smithchart, series) {
            var legend = smithchart.legendSettings;
            var symbolWidth = legend.itemStyle.width;
            var symbolHeight = legend.itemStyle.height;
            var textSize = helper_1.measureText(series.text, legend.textStyle, smithchart.themeStyle.legendLabelFont);
            var width = symbolWidth + textSize.width + legend.shapePadding;
            var height = Math.max(symbolHeight, textSize.height);
            return { width: width, height: height };
        };
        SmithchartLegend.prototype._drawLegend = function (smithchart) {
            var legend = smithchart.legendSettings;
            var legendPosition = legend.position.toLowerCase();
            var alignment = legend.alignment;
            var legendBounds = this.legendActualBounds;
            var maxWidth = 0;
            var startX;
            var startY;
            var titleFont = smithchart.title.font ? smithchart.title.font : smithchart.title.textStyle;
            var smithchartTitleHeight = helper_1.measureText(smithchart.title.text, titleFont, smithchart.themeStyle.legendLabelFont).height;
            var smithchartSubtitleHeight = helper_1.measureText(smithchart.title.subtitle.text, smithchart.title.subtitle.textStyle, smithchart.themeStyle.legendLabelFont).height;
            var elementSpacing = smithchart.elementSpacing;
            var offset = smithchartTitleHeight + smithchartSubtitleHeight + elementSpacing + smithchart.margin.top;
            var itemPadding = legend.itemPadding > 0 ? legend.itemPadding : 0;
            var svgObjectWidth = smithchart.availableSize.width;
            var svgObjectHeight = smithchart.availableSize.height;
            var legendBorder = legend.border.width;
            var legendWidth = 0;
            var titleSize = helper_1.measureText(legend['title']['text'], legend.title.textStyle, smithchart.themeStyle.legendLabelFont);
            var legendTitleHeight = titleSize.height;
            var borderSize = smithchart.border.width;
            var svgWidth = svgObjectWidth - ((borderSize * 2));
            var svgHeight = svgObjectHeight - ((borderSize * 2));
            legendBounds.height += legendTitleHeight;
            if (legendPosition !== 'custom') {
                switch (legendPosition) {
                    case 'bottom':
                        legendBounds.y = svgHeight - (legendBounds.height + (legendBorder) + elementSpacing);
                        break;
                    case 'top':
                        legendBounds.y = borderSize + offset;
                        break;
                    case 'right':
                        legendBounds.x = svgWidth - legendBounds.width - (elementSpacing * 2);
                        break;
                    case 'left':
                        legendBounds.x = borderSize + (elementSpacing * 2);
                        break;
                }
                if (legendPosition === 'left' || legendPosition === 'right') {
                    switch (alignment) {
                        case 'Center':
                            legendBounds.y = (svgHeight / 2) - ((legendBounds.height + legendBorder * 2) / 2) + (elementSpacing / 2);
                            break;
                        case 'Near':
                            legendBounds.y = borderSize + (elementSpacing * 2) + offset;
                            break;
                        case 'Far':
                            legendBounds.y = svgHeight - (legendBounds.height + (legendBorder)) - (elementSpacing * 2);
                            break;
                    }
                }
                else {
                    switch (alignment) {
                        case 'Center':
                            legendBounds.x = (svgWidth / 2) - ((legendBounds.width + legendBorder * 2) / 2) + (elementSpacing / 2);
                            break;
                        case 'Near':
                            legendBounds.x = borderSize + (elementSpacing * 2);
                            break;
                        case 'Far':
                            legendBounds.x = svgWidth - (legendBounds.width + (legendBorder)) - (elementSpacing * 2);
                            break;
                    }
                }
            }
            else {
                legendBounds.y = (legend.location.y < svgHeight) ? legend.location.y : 0;
                legendBounds.x = (legend.location.x < svgWidth) ? legend.location.x : 0;
            }
            if (legendPosition === 'bottom' || legendPosition === 'top') {
                for (var i = 0; i < this.legendSeries.length; i++) {
                    legendWidth += this.legendSeries[i].bounds.width + itemPadding;
                    if (legendWidth > svgWidth) {
                        legendBounds.x = (svgWidth / 2) - ((legendBounds.width + legendBorder * 2) / 2) + (elementSpacing / 2);
                        break;
                    }
                }
            }
            var gLegendEle = smithchart.renderer.createGroup({ 'id': smithchart.element.id + '_legend_group' });
            smithchart.svgObject.appendChild(gLegendEle);
            this.legendItemGroup = smithchart.renderer.createGroup({ 'id': smithchart.element.id + 'legendItem_Group' });
            var currentX = startX = elementSpacing;
            var currentY = startY = elementSpacing;
            if (legend.title.text !== '' && legend.title.visible) {
                gLegendEle.appendChild(this.drawLegendTitle(smithchart, legend, legendBounds, gLegendEle));
                currentY = startY = elementSpacing + legendTitleHeight;
            }
            for (var k = 0; k < this.legendSeries.length; k++) {
                if ((legend.rowCount < legend.columnCount || legend.rowCount === legend.columnCount) &&
                    (legendPosition === 'top' || legendPosition === 'bottom' || legendPosition === 'custom')) {
                    if ((currentX + this.legendSeries[k]['bounds'].width) > legendBounds.width + startX) {
                        currentX = elementSpacing;
                        currentY += this.legendSeries[k]['bounds'].height + itemPadding;
                    }
                    this.legendGroup = this.drawLegendItem(smithchart, legend, this.legendSeries[k], k, currentX, (currentY));
                    gLegendEle.appendChild(this.legendGroup);
                    currentX += this.legendSeries[k]['bounds'].width + itemPadding;
                }
                else {
                    if (((currentY + this.legendSeries[k]['bounds'].height + itemPadding) +
                        legendTitleHeight + borderSize > legendBounds.height + startY)) {
                        currentY = startY;
                        currentX += maxWidth + (itemPadding);
                    }
                    this.legendGroup = this.drawLegendItem(smithchart, legend, this.legendSeries[k], k, (currentX), (currentY));
                    gLegendEle.appendChild(this.legendGroup);
                    currentY += this.legendSeries[k]['bounds'].height + itemPadding;
                    maxWidth = Math.max(maxWidth, this.legendSeries[k]['bounds'].width);
                }
            }
            gLegendEle.setAttribute('transform', 'translate(' + legendBounds.x.toString() + ',' + legendBounds.y.toString() + ')');
            this.drawLegendBorder(gLegendEle, smithchart, legend, legendBounds);
        };
        SmithchartLegend.prototype.drawLegendBorder = function (gLegendEle, smithchart, legend, legendBounds) {
            var borderRect = new helper_1.RectOption(smithchart.element.id + '_svg' + '_legendRect', 'none', legend.border, 1, new utils_1.SmithchartRect(0, 0, legendBounds.width, legendBounds.height));
            gLegendEle.appendChild(smithchart.renderer.drawRectangle(borderRect));
        };
        SmithchartLegend.prototype.drawLegendTitle = function (smithchart, legend, legendBounds, gLegendEle) {
            var elementSpacing = smithchart.elementSpacing;
            var titleSize = helper_1.measureText(legend.title.text, legend.title.textStyle, smithchart.themeStyle.legendLabelFont);
            var titleWidth = titleSize.width;
            var titleHeight = titleSize.height;
            var textAlignment = legend.title.textAlignment;
            var startX = 0;
            var legendBoundsWidth = legendBounds.width;
            var startY = elementSpacing + (titleHeight / 2);
            switch (textAlignment) {
                case 'Far':
                    startX = legendBoundsWidth - titleWidth - startX;
                    break;
                case 'Center':
                    startX = legendBoundsWidth / 2 - (titleWidth) / 2;
                    break;
            }
            Iif (startX < 0) {
                startX = 0;
                legendBoundsWidth = titleWidth;
            }
            Iif (legendBoundsWidth < titleWidth + startX) {
                legendBoundsWidth = titleWidth + startX;
            }
            var options = new helper_1.TextOption(smithchart.element.id + '_LegendTitleText', startX, startY, 'start', legend.title.text);
            var element = helper_1.renderTextElement(options, legend.title.textStyle, legend.title.textStyle.color ||
                smithchart.themeStyle.legendTitleFont.color, gLegendEle, smithchart.themeStyle.legendTitleFont);
            element.setAttribute('aria-label', legend.title.description || legend.title.text);
            return element;
        };
        SmithchartLegend.prototype.drawLegendItem = function (smithchart, legend, legendSeries, k, x, y) {
            var _this = this;
            var symbol = legend.itemStyle;
            var textHeight = helper_1.measureText(legendSeries['text'], legend.textStyle, smithchart.themeStyle.legendLabelFont).height;
            var location = {
                x: x + symbol['width'] / 2,
                y: (y + (textHeight > symbol['height'] ? textHeight : symbol['height']) / 2)
            };
            var legendGroup = smithchart.renderer.createGroup({ id: smithchart.element.id + '_svg' + '_Legend' + k.toString() });
            legendGroup['style']['cursor'] = legend.toggleVisibility ? 'pointer' : 'default';
            legendGroup.setAttribute('tabindex', k === 0 ? '0' : '');
            var legendEventArgs = {
                text: legendSeries['text'],
                fill: legendSeries['fill'],
                shape: legendSeries['shape'],
                name: constant_1.legendRender,
                cancel: false
            };
            var legendRenderSuccess = function (args) {
                if (!args.cancel) {
                    var shape = _this.drawLegendShape(smithchart, legendSeries, location.x, location.y, k, legend, args);
                    legendGroup.appendChild(shape);
                    var options = new helper_1.TextOption(smithchart.element.id + '_LegendItemText' + k.toString(), location.x + symbol['width'] / 2 + legend.shapePadding, location.y + textHeight / 4, 'start', args.text);
                    legend.textStyle.fontFamily = legend.textStyle.fontFamily || smithchart.themeStyle.legendLabelFont.fontFamily;
                    legend.textStyle.size = legend.textStyle.size || smithchart.themeStyle.legendLabelFont.size;
                    var element = helper_1.renderTextElement(options, legend.textStyle, legend.textStyle.color ||
                        smithchart.themeStyle.legendLabelFont.color, legendGroup, smithchart.themeStyle.legendLabelFont);
                    legendGroup.setAttribute('aria-label', legend.description || ('Show ' + options.text));
                    legendGroup.setAttribute('role', 'button');
                    legendGroup.appendChild(element);
                    _this.legendItemGroup.appendChild(legendGroup);
                }
            };
            legendRenderSuccess.bind(this);
            smithchart.trigger(constant_1.legendRender, legendEventArgs, legendRenderSuccess);
            return this.legendItemGroup;
        };
        SmithchartLegend.prototype.drawLegendShape = function (smithchart, legendSeries, locX, locY, index, legend, legendEventArgs) {
            var element;
            var circleOptions;
            var pathOptions;
            var path;
            var symbol = legend.itemStyle;
            var width = symbol['width'];
            var height = symbol['height'];
            var x = locX + (-width / 2);
            var border = { color: symbol.border.color, width: symbol.border.width };
            var opacity = 1;
            var fill = (smithchart.series[index].visibility === 'visible') ? legendEventArgs.fill : 'grey';
            var shape = legendEventArgs.shape.toLowerCase();
            var radius = Math.sqrt(height * height + width * width) / 2;
            switch (shape) {
                case 'circle':
                    circleOptions = new helper_1.CircleOption(smithchart.element.id + '_svg' + '_LegendItemShape' + index.toString(), fill, border, opacity, locX, locY, radius, null);
                    element = smithchart.renderer.drawCircle(circleOptions);
                    break;
                case 'rectangle':
                    path = 'M' + ' ' + x + ' ' + (locY + (-height / 2)) + ' ' +
                        'L' + ' ' + ((width / 2) + locX) + ' ' + (locY + (-height / 2)) + ' ' +
                        'L' + ' ' + (locX + (width / 2)) + ' ' + (locY + (height / 2)) + ' ' +
                        'L' + ' ' + x + ' ' + (locY + (height / 2)) + ' ' +
                        'L' + ' ' + x + ' ' + (locY + (-height / 2)) + ' z';
                    pathOptions = new helper_1.PathOption(smithchart.element.id + '_svg' + '_LegendItemShape' + index.toString(), fill, border.width, border.color, 1, '', path);
                    element = smithchart.renderer.drawPath(pathOptions);
                    break;
                case 'diamond':
                    path = 'M' + ' ' + x + ' ' + locY + ' ' +
                        'L' + ' ' + locX + ' ' + (locY + (-height / 2)) + ' ' +
                        'L' + ' ' + ((width / 2) + locX) + ' ' + locY + ' ' +
                        'L' + ' ' + locX + ' ' + (locY + (height / 2)) + ' ' +
                        'L' + ' ' + x + ' ' + locY + ' z';
                    pathOptions = new helper_1.PathOption(smithchart.element.id + '_svg' + '_LegendItemShape' + index.toString(), fill, border.width, border.color, 1, '', path);
                    element = smithchart.renderer.drawPath(pathOptions);
                    break;
                case 'pentagon': {
                    var eq = 72;
                    for (var j = 0; j <= 5; j++) {
                        var xValue = radius * Math.cos((Math.PI / 180) * (j * eq));
                        var yValue = radius * Math.sin((Math.PI / 180) * (j * eq));
                        if (j === 0) {
                            path = 'M' + ' ' + (xValue + locX) + ' ' + (locY + yValue) + ' ';
                        }
                        else {
                            path = path.concat('L' + ' ' + (locX + xValue) + ' ' + (locY + yValue) + ' ');
                        }
                    }
                    path = path.concat('Z');
                    pathOptions = new helper_1.PathOption(smithchart.element.id + '_svg' + '_LegendItemShape' + index.toString(), fill, border.width, border.color, 1, '', path);
                    element = smithchart.renderer.drawPath(pathOptions);
                    break;
                }
                case 'triangle':
                    path = 'M' + ' ' + x + ' ' + ((height / 2) + locY) + ' ' +
                        'L' + ' ' + locX + ' ' + (locY + (-height / 2)) + ' ' +
                        'L' + ' ' + (locX + (width / 2)) + ' ' + (locY + (height / 2)) + ' ' +
                        'L' + ' ' + x + ' ' + (locY + (height / 2)) + ' Z';
                    pathOptions = new helper_1.PathOption(smithchart.element.id + '_svg' + '_LegendItemShape' + index.toString(), fill, border.width, border.color, 1, '', path);
                    element = smithchart.renderer.drawPath(pathOptions);
                    break;
            }
            return element;
        };
        SmithchartLegend.prototype.getModuleName = function () {
            return 'SmithchartLegend';
        };
        SmithchartLegend.prototype.destroy = function () {
        };
        return SmithchartLegend;
    }());
    exports.SmithchartLegend = SmithchartLegend;
});