all files / circularchart3d/renderer/ dataLabel.js

97.13% Statements 237/244
90.91% Branches 170/187
100% Functions 29/29
97.05% Lines 230/237
18 statements, 7 functions, 29 branches Ignored     
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              11×                             11×                     52×                                                 143×   154× 154× 154× 154× 1442× 1442× 1442×   1442× 1442×       1442× 1442× 1442× 1437×     154×     40×         40×   40× 40× 40× 40×   60× 60× 60× 60× 60× 60× 60× 60× 60× 60× 60×     60× 60×             60×   1437×   1437× 1437× 1437× 1437× 1437× 1437× 1437× 1437× 1437× 1437× 392×   1437× 320×   1117× 1117×   1437× 1437× 1437×       1437× 1437× 1437× 1437× 1437× 1437× 1437× 1437× 10× 10× 10×   10× 10×                         1436× 1436× 319×   1436× 20×         20×   1436×   1436× 1436× 1436× 1436× 319×   64× 64× 64×   319× 191× 191×     128× 128×     1436× 1396× 1396× 1396× 1396× 59× 59× 39× 39× 39× 29×   10×           20×   59×   1396× 1396×   1396×   1436× 1436× 1436×   1437× 1437× 1437× 1437× 1437× 1437×   1177×     1442× 950× 950×       950×   1442×   319× 319× 319× 319× 319× 319× 319× 319× 319× 319× 319× 319× 191×     128×   319× 319× 319×   319× 319× 957×   319× 319× 319× 319×               319× 319×   1315×          
/* istanbul ignore next */ 
var __extends = (this && this.__extends) || (function () {
    var extendStatics = function (d, b) {
        extendStatics = Object.setPrototypeOf ||
            ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
            function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
        return extendStatics(d, b);
    };
    return function (d, b) {
        extendStatics(d, b);
        function __() { this.constructor = d; }
        d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
    };
})();
/* istanbul ignore next */ 
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
define(["require", "exports", "@syncfusion/ej2-base", "../../common/model/base", "@syncfusion/ej2-svg-base", "../../common/model/constants", "../../common/utils/helper"], function (require, exports, ej2_base_1, base_1, ej2_svg_base_1, constants_1, helper_1) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    var CircularChart3DDataLabelFont = (function (_super) {
        __extends(CircularChart3DDataLabelFont, _super);
        function CircularChart3DDataLabelFont() {
            return _super !== null && _super.apply(this, arguments) || this;
        }
        __decorate([
            ej2_base_1.Property('Normal')
        ], CircularChart3DDataLabelFont.prototype, "fontStyle", void 0);
        __decorate([
            ej2_base_1.Property('16px')
        ], CircularChart3DDataLabelFont.prototype, "size", void 0);
        __decorate([
            ej2_base_1.Property('Normal')
        ], CircularChart3DDataLabelFont.prototype, "fontWeight", void 0);
        __decorate([
            ej2_base_1.Property('')
        ], CircularChart3DDataLabelFont.prototype, "color", void 0);
        __decorate([
            ej2_base_1.Property('Segoe UI')
        ], CircularChart3DDataLabelFont.prototype, "fontFamily", void 0);
        __decorate([
            ej2_base_1.Property(1)
        ], CircularChart3DDataLabelFont.prototype, "opacity", void 0);
        return CircularChart3DDataLabelFont;
    }(ej2_base_1.ChildProperty));
    exports.CircularChart3DDataLabelFont = CircularChart3DDataLabelFont;
    var CircularChart3DConnector = (function (_super) {
        __extends(CircularChart3DConnector, _super);
        function CircularChart3DConnector() {
            return _super !== null && _super.apply(this, arguments) || this;
        }
        __decorate([
            ej2_base_1.Property(null)
        ], CircularChart3DConnector.prototype, "color", void 0);
        __decorate([
            ej2_base_1.Property(1)
        ], CircularChart3DConnector.prototype, "width", void 0);
        __decorate([
            ej2_base_1.Property(null)
        ], CircularChart3DConnector.prototype, "length", void 0);
        __decorate([
            ej2_base_1.Property('')
        ], CircularChart3DConnector.prototype, "dashArray", void 0);
        return CircularChart3DConnector;
    }(ej2_base_1.ChildProperty));
    exports.CircularChart3DConnector = CircularChart3DConnector;
    var CircularChart3DDataLabelSettings = (function (_super) {
        __extends(CircularChart3DDataLabelSettings, _super);
        function CircularChart3DDataLabelSettings() {
            return _super !== null && _super.apply(this, arguments) || this;
        }
        __decorate([
            ej2_base_1.Property(false)
        ], CircularChart3DDataLabelSettings.prototype, "visible", void 0);
        __decorate([
            ej2_base_1.Property(null)
        ], CircularChart3DDataLabelSettings.prototype, "name", void 0);
        __decorate([
            ej2_base_1.Property('transparent')
        ], CircularChart3DDataLabelSettings.prototype, "fill", void 0);
        __decorate([
            ej2_base_1.Property('Inside')
        ], CircularChart3DDataLabelSettings.prototype, "position", void 0);
        __decorate([
            ej2_base_1.Property(0)
        ], CircularChart3DDataLabelSettings.prototype, "angle", void 0);
        __decorate([
            ej2_base_1.Property(false)
        ], CircularChart3DDataLabelSettings.prototype, "enableRotation", void 0);
        __decorate([
            ej2_base_1.Complex({ width: null, color: null }, base_1.Border)
        ], CircularChart3DDataLabelSettings.prototype, "border", void 0);
        __decorate([
            ej2_base_1.Complex({ fontFamily: null, size: '12px', fontStyle: 'Normal', fontWeight: '400', color: null }, CircularChart3DDataLabelFont)
        ], CircularChart3DDataLabelSettings.prototype, "font", void 0);
        __decorate([
            ej2_base_1.Complex({}, CircularChart3DConnector)
        ], CircularChart3DDataLabelSettings.prototype, "connectorStyle", void 0);
        __decorate([
            ej2_base_1.Property(null)
        ], CircularChart3DDataLabelSettings.prototype, "template", void 0);
        __decorate([
            ej2_base_1.Property('')
        ], CircularChart3DDataLabelSettings.prototype, "format", void 0);
        return CircularChart3DDataLabelSettings;
    }(ej2_base_1.ChildProperty));
    exports.CircularChart3DDataLabelSettings = CircularChart3DDataLabelSettings;
    var CircularChartDataLabel3D = (function (_super) {
        __extends(CircularChartDataLabel3D, _super);
        function CircularChartDataLabel3D() {
            return _super !== null && _super.apply(this, arguments) || this;
        }
        CircularChartDataLabel3D.prototype.renderDataLabel = function (series, chart) {
            var templateId = chart.element.id + '-series-' + series.index + '-data-label-collections';
            series.labelBound = ej2_base_1.isNullOrUndefined(series.labelBound) ? new ej2_svg_base_1.Rect(Infinity, Infinity, -Infinity, -Infinity) : series.labelBound;
            series.dataLabelElement = ej2_base_1.createElement('div', { id: templateId });
            for (var i = 0; i < series.points.length; i++) {
                var point = series.points[i];
                var pointText = this.getDatalabelText(series.dataLabel.format, chart, point.text ? point.text : ej2_base_1.isNullOrUndefined(point.y) ? '' : point.y.toString());
                var border = { width: series.dataLabel.border.width, color: series.dataLabel.border.color,
                    dashArray: series.dataLabel.border.dashArray };
                var argsFont = (ej2_base_1.extend({}, ej2_base_1.getValue('properties', series.dataLabel.font), null, true));
                var argsData = {
                    cancel: false, name: constants_1.textRender, series: series, point: point,
                    text: pointText, border: border, color: series.dataLabel.fill, template: series.dataLabel.template, font: argsFont
                };
                chart.trigger(constants_1.textRender, argsData);
                point.argsData = argsData;
                if (point.visible && !argsData.cancel && !ej2_base_1.isNullOrUndefined(point.y)) {
                    this.draw3DDataLabel(series, point.index, point, chart);
                }
            }
            if (series.dataLabel.template) {
                helper_1.appendChildElement(false, document.getElementById(chart.element.id + '_Secondary_Element'), series.dataLabelElement, chart.redraw, false, 'x', 'y', null, '', false, false, null);
            }
        };
        CircularChartDataLabel3D.prototype.createDataLabelTemplate = function (parentElement, series, dataLabel, point, data, labelIndex, redraw, location, chart) {
            var childElement = this.createTemplate(ej2_base_1.createElement('div', {
                id: chart.element.id + '-series-' + series.index + '-data-label-' + labelIndex,
                styles: 'position: absolute;background-color:' + data.color + ';' +
                    helper_1.getFontStyle(dataLabel.font, chart.themeStyle.datalabelFont) + ';border:' + data.border.width + 'px solid ' + data.border.color + ';'
            }), data.template, chart, point, series, chart.element.id + '-data-label-');
            this.calculateTemplateLabelSize(parentElement, childElement, point, series, dataLabel, redraw, location);
        };
        CircularChartDataLabel3D.prototype.calculateTemplateLabelSize = function (parentElement, childElement, point, series, dataLabel, redraw, location, isReactCallback) {
            var elementRect = helper_1.measureElementRect(childElement, redraw, isReactCallback);
            childElement.style.left = (location.x - (elementRect.width / 2)) + 'px';
            childElement.style.top = (location.y - elementRect.height) + 'px';
            helper_1.appendChildElement(false, parentElement, childElement, redraw, true, 'left', 'top');
        };
        CircularChartDataLabel3D.prototype.createTemplate = function (childElement, content, chart, point, series, dataLabelId) {
            var templateFn = helper_1.getTemplateFunction(content);
            var templateElement;
            try {
                var tempObject = { chart: chart, series: series, point: point };
                var templateId = dataLabelId + '-template';
                var elementData = templateFn ? templateFn(tempObject, chart, templateId, dataLabelId) : [];
                Eif (elementData.length) {
                    templateElement = Array.prototype.slice.call(elementData);
                    var len = templateElement.length;
                    for (var i = 0; i < len; i++) {
                        childElement.appendChild(templateElement[i]);
                    }
                }
                var reactCallback = void 0;
                Iif (chart.isReact) {
                    chart.renderReactTemplates(reactCallback);
                }
            }
            catch (e) {
                return childElement;
            }
            return childElement;
        };
        CircularChartDataLabel3D.prototype.draw3DDataLabel = function (series, pointIndex, point, chart) {
            var connectorHeight = series.dataLabel.connectorStyle.length ?
                parseFloat(series.dataLabel.connectorStyle.length) : series.dataLabel.connectorStyle.length;
            var radius;
            var pointX;
            var pointY;
            var degree;
            var commonEventArgs = { data: null };
            var location = { x: 0, y: 0 };
            var seriesIndex = series.index;
            var center = point.symbolLocation.center;
            var dradius = point.symbolLocation.radius * series.coefficient;
            if (ej2_base_1.isNullOrUndefined(connectorHeight)) {
                connectorHeight = ej2_svg_base_1.measureText(point.argsData.text, series.dataLabel.font, chart.themeStyle.datalabelFont).height;
            }
            if (series != null && series.dataLabel.position !== 'Inside') {
                radius = point.symbolLocation.radius + connectorHeight;
            }
            else Eif (series != null) {
                radius = dradius + (point.symbolLocation.radius - dradius) / 2;
            }
            pointX = location.x = center.x + (parseFloat(point.argsData.font.size) / 3) + radius * Math.cos(point.symbolLocation.angle);
            pointY = location.y = center.y + (parseFloat(point.argsData.font.size) / 3) + radius * Math.sin(point.symbolLocation.angle);
            commonEventArgs.data = {
                text: point.argsData.text, location: { x: pointX, y: pointY },
                series: series, pointIndex: pointIndex, seriesIndex: seriesIndex
            };
            commonEventArgs.data.Text = commonEventArgs.data.text;
            var size = ej2_svg_base_1.measureText(point.argsData.text, series.dataLabel.font, chart.themeStyle.datalabelFont);
            pointX = location.x = commonEventArgs.data.location.x;
            pointY = location.y = commonEventArgs.data.location.y;
            var tag = (!series.dataLabel.template) ? 'text' : 'template';
            var saturationColor = this.getSaturatedColor(point, point.argsData.color, chart);
            var element = { width: size.width, height: size.height, fill: saturationColor, label: commonEventArgs.data, textAnchor: 'middle', tag: tag, font: point.argsData.font, angle: 0, id: chart.element.id + '-svg-data-label-text-' + pointIndex, child: chart.groupElement };
            if (chart.circularChartLegend3DModule && chart.legendSettings.visible && point.visible && series.dataLabel.position === 'Outside') {
                var rect = chart.circularChartLegend3DModule.legendBounds;
                var legendpadding = chart.legendSettings.border.width / 2;
                rect = new ej2_svg_base_1.Rect(rect.x - legendpadding, rect.y - legendpadding, rect.width +
                    (2 * legendpadding), rect.height + (2 * legendpadding));
                var labelRegion = new ej2_svg_base_1.Rect(element.label.location.x + (size.width / 2) + 20, element.label.location.y + 2.5, element.width, element.height);
                if (helper_1.isOverlap(labelRegion, rect)) {
                    Eif (chart.circularChartLegend3DModule.position === 'Right') {
                        element.width = rect.x - labelRegion.x;
                    }
                    else if (chart.circularChartLegend3DModule.position === 'Left') {
                        element.width = labelRegion.x - (rect.x + rect.width);
                        if (element.width < 0) {
                            element.width += labelRegion.width;
                            element.label.location.x = rect.x + rect.width - (size.width / 2) + 20;
                        }
                    }
                    Eif (labelRegion && element.width < labelRegion.width) {
                        element.label.text = helper_1.textTrim(element.width, element.label.text, series.dataLabel.font, chart.enableRtl, chart.themeStyle.datalabelFont);
                    }
                    Eif (element.label.text.length === 3 && element.label.text.indexOf('...') > -1) {
                        return;
                    }
                }
            }
            var connectorPoints;
            if (series.dataLabel.position !== 'Inside') {
                connectorPoints = this.updateConnectorLine(point, pointIndex, series, connectorHeight, chart);
            }
            if (series.dataLabel.template && series.dataLabel.position !== 'Inside') {
                var childElement = this.createTemplate(ej2_base_1.createElement('div', {
                    id: chart.element.id + '-series-data-label-' + 0,
                    styles: 'position: absolute;background-color:' + point.argsData.color + ';' +
                        helper_1.getFontStyle(point.argsData.font, chart.themeStyle.datalabelFont) + ';border:' + point.argsData.border.width + 'px solid ' + point.argsData.border.color + ';'
                }), point.argsData.template, chart, point, series, chart.element.id + '-data-label-');
                size = ej2_svg_base_1.measureText(childElement.textContent, series.dataLabel.font, chart.themeStyle.datalabelFont);
            }
            if (chart.circularChartLegend3DModule && chart.legendSettings.visible && (series.dataLabel.position === 'Outside')) {
                chart.visibleSeries[0].findMaxBounds(chart.visibleSeries[0].labelBound, { x: pointX, y: pointY, width: size.width, height: size.height });
            }
            var padding = 0;
            var heightPadding = 0;
            var textAngle = point.symbolLocation.angle;
            if (series.dataLabel.position !== 'Inside') {
                if ((textAngle > 1.5 && textAngle < 1.8) || (textAngle > 1.3 && textAngle < 1.5) ||
                    (textAngle > 4.5 && textAngle < 4.8) || (textAngle > 4.3 && textAngle < 4.5)) {
                    location.x = connectorPoints.x;
                    location.y = connectorPoints.y;
                    textAngle = connectorPoints.angle;
                }
                if (textAngle < (Math.PI / 2) || textAngle >= (Math.PI / 2) + Math.PI) {
                    padding = (size.width / 2) + 20;
                    heightPadding = 5 / 2;
                }
                else {
                    padding = -((size.width / 2) + (point.argsData.color !== 'transparent' || point.argsData.border.color ? 25 : 20));
                    heightPadding = 5 / 2;
                }
            }
            if (!point.argsData.template && commonEventArgs.data.text !== '') {
                var element_1 = { tag: 'dataLabel', series: series, point: point, pointIndex: pointIndex, id: chart.element.id + '-svg-' + seriesIndex + '-data-label-' + pointIndex, child: chart.groupElement };
                var angle = void 0;
                var transform = '';
                if (series.dataLabel.enableRotation) {
                    angle = degree = series.dataLabel.angle;
                    if (angle === 0) {
                        var toDegrees = function (angle) { return angle * (180 / Math.PI); };
                        var midAngle = toDegrees(point.symbolLocation.angle);
                        if (series.dataLabel.position === 'Outside') {
                            degree = 0;
                        }
                        else if (midAngle >= 90 && midAngle <= 260) {
                            degree = midAngle + 180;
                            location.x = location.x - (parseFloat(point.argsData.font.size) / 2);
                        }
                        else {
                            degree = midAngle;
                        }
                    }
                    else {
                        degree = (angle > 360) ? angle - 360 : (angle < -360) ? angle + 360 : angle;
                    }
                    transform = 'rotate(' + degree + ',' + (location.x) + ',' + (location.y) + ')';
                }
                element_1.transform = transform;
                var borderElement = chart.polygon.createTextElement(chart.vector.vector3D(pointX + padding, pointY + heightPadding, (point.symbolLocation.z) ?
                    point.symbolLocation.z : 0), element_1, 0, -size.height);
                chart.circular3DPolygon.push(borderElement);
            }
            element.angle = series.dataLabel.enableRotation ? series.dataLabel.angle !== 0 ? series.dataLabel.angle : degree : 0;
            var polygon = chart.polygon.createTextElement(chart.vector.vector3D(location.x + padding, location.y + heightPadding, -1), element, 0, -size.height);
            chart.circular3DPolygon.push(polygon);
        };
        CircularChartDataLabel3D.prototype.getSaturatedColor = function (point, color, chart) {
            var saturatedColor;
            saturatedColor = color === 'transparent' ? this.getLabelBackground(point, chart) : color;
            saturatedColor = (saturatedColor === 'transparent') ? ((chart.theme.indexOf('Dark') > -1 || chart.theme.indexOf('HighContrast') > -1) ? 'black' : 'white') : saturatedColor;
            var rgbValue = helper_1.convertHexToColor(helper_1.colorNameToHex(saturatedColor));
            var contrast = Math.round((rgbValue.r * 299 + rgbValue.g * 587 + rgbValue.b * 114) / 1000);
            return chart.theme === 'Bootstrap5' ? '#212529' : chart.theme === 'Bootstrap5Dark' ? '#DEE2E6' : contrast >= 128 ? 'black' : 'white';
        };
        CircularChartDataLabel3D.prototype.getLabelBackground = function (point, chart) {
            return point.argsData.series.dataLabel.position === 'Outside' ?
                chart.background || chart.themeStyle.background : !point.y ? chart.theme.indexOf('dark') ? 'white' : 'black' : point.color;
        };
        CircularChartDataLabel3D.prototype.getDatalabelText = function (labelFormat, chart, labelText) {
            if (Number(labelText)) {
                var customLabelFormat = labelFormat.match('{value}') !== null;
                var format = chart.intl.getNumberFormat({
                    format: customLabelFormat ? '' : labelFormat,
                    useGrouping: chart.useGroupingSeparator
                });
                labelText = customLabelFormat ? labelFormat.replace('{value}', format(parseFloat(labelText))) : format(parseFloat(labelText));
            }
            return labelText;
        };
        CircularChartDataLabel3D.prototype.updateConnectorLine = function (point, pointIndex, series, connectorHeight, chart) {
            var drawPoints = [];
            var symbolLocation = point.symbolLocation;
            var x = symbolLocation.center.x + Math.cos(symbolLocation.angle) * symbolLocation.radius;
            var y = symbolLocation.center.y + Math.sin(symbolLocation.angle) * symbolLocation.radius;
            drawPoints.push({ x: x, y: y });
            var labelRadiusFromOrigin = symbolLocation.radius + connectorHeight;
            var angle = symbolLocation.angle;
            x = symbolLocation.center.x + Math.cos(angle) * labelRadiusFromOrigin;
            y = symbolLocation.center.y + Math.sin(angle) * labelRadiusFromOrigin;
            drawPoints.push({ x: x, y: y });
            var padding;
            if (angle < (Math.PI / 2) || angle >= (Math.PI / 2) + Math.PI) {
                padding = 10;
            }
            else {
                padding = -10;
            }
            drawPoints.push({ x: x + padding, y: y });
            this.drawLineSegment(drawPoints, pointIndex, series, chart);
            return ({ x: x, y: y, angle: angle });
        };
        CircularChartDataLabel3D.prototype.drawLineSegment = function (drawpoints, pointIndex, series, chart) {
            var vectorPoints = [];
            for (var i = 0; i < drawpoints.length; i++) {
                vectorPoints.push(chart.vector.vector3D(drawpoints[i].x, drawpoints[i].y, 0));
            }
            var seriesIndex = series.index;
            var color = series.points[pointIndex].color;
            var stroke = series.dataLabel.connectorStyle.color ? series.dataLabel.connectorStyle.color : color;
            var line = {
                width: series.dataLabel.connectorStyle.width,
                stroke: stroke,
                child: chart.groupElement,
                tag: 'polyline',
                dashArray: series.dataLabel.connectorStyle.dashArray,
                id: chart.element.id + '-datalabel-series-' + seriesIndex + '-connector-' + pointIndex
            };
            var ploygon = chart.polygon.createPolyline(vectorPoints, line);
            chart.circular3DPolygon.push(ploygon);
        };
        CircularChartDataLabel3D.prototype.getModuleName = function () {
            return 'CircularChartDataLabel3D';
        };
        CircularChartDataLabel3D.prototype.destroy = function () {
        };
        return CircularChartDataLabel3D;
    }(ej2_base_1.ChildProperty));
    exports.CircularChartDataLabel3D = CircularChartDataLabel3D;
});