all files / chart3d/series/ data-label.js

94.4% Statements 118/125
88.99% Branches 97/109
92.31% Functions 12/13
94.4% Lines 118/125
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   137×   264× 264× 264× 264× 1429× 1429× 1428×     264× 58×     1428× 1428× 1428× 1428× 1428× 1428× 1428× 1428× 1428× 1428× 1428× 54×     1374×   1428× 1428×           1428× 1428×         1428× 1428× 1428× 1428× 1068×               1068×   1428× 1428× 1428× 1428× 1428×               1428×                         1428× 1416×     1428× 1428× 1428× 1428× 1428× 425×       425× 425×       1428×   477× 477× 477×         477×   478× 478× 478× 478× 478× 478× 478×   478× 478× 478× 478× 478× 478× 478×   478×               459× 459×     478× 478× 478× 478× 478× 478× 19× 19× 19×     459× 459× 459×   478×                   477× 477× 477× 477× 477× 477× 477× 477× 477× 477× 477×     477× 477× 477× 477×               477×   4177×          
define(["require", "exports", "@syncfusion/ej2-svg-base", "../../common/model/constants", "@syncfusion/ej2-base", "../../common/utils/helper"], function (require, exports, ej2_svg_base_1, constants_1, ej2_base_1, helper_1) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    var DataLabel3D = (function () {
        function DataLabel3D(chart) {
            this.chart = chart;
        }
        DataLabel3D.prototype.render = function (series, chart, dataLabel) {
            var point;
            var templateId = chart.element.id + '-series-' + series.index + '-data-label-collections';
            series.dataLabelElement = ej2_base_1.createElement('div', { id: templateId });
            for (var i = 0; i < series.visiblePoints.length; i++) {
                point = series.visiblePoints[i];
                if (point.visible) {
                    this.draw3DDataLabel(series, point.index, point, chart, dataLabel);
                }
            }
            if (series.dataLabel.template) {
                helper_1.appendChildElement(false, document.getElementById(this.chart.element.id + '_Secondary_Element'), series.dataLabelElement, chart.redraw, false, 'x', 'y', null, '', false, false, null);
            }
        };
        DataLabel3D.prototype.draw3DDataLabel = function (series, pointIndex, point, chart, dataLabel) {
            var pointX;
            var pointY;
            var xOffset = 0;
            var yOffset = 0;
            var commonEventArgs = { data: null };
            var pointText = this.getLabelText(point, series, this.chart)[0];
            var size = ej2_svg_base_1.measureText(pointText, dataLabel.font, this.chart.themeStyle.datalabelFont);
            var location = chart.svg3DRenderer.transform3DToVisible(series, point.symbolLocations.x, point.symbolLocations.y, chart);
            pointY = location.y;
            pointX = location.x;
            if (series.dataLabel.position === 'Bottom') {
                pointY = location.y + yOffset;
            }
            else {
                pointY = location.y - yOffset;
            }
            pointX = location.x + xOffset;
            commonEventArgs.data = {
                text: pointText,
                location: { x: pointX, y: pointY },
                series: series,
                pointIndex: pointIndex
            };
            commonEventArgs.data.Text = commonEventArgs.data.text;
            var argsData = {
                cancel: false, series: series,
                point: point, text: pointText, border: dataLabel.border,
                color: dataLabel.fill, template: dataLabel.template, textStyle: dataLabel.font
            };
            chart.trigger(constants_1.textRender, argsData);
            this.fontBackground = series.dataLabel.position === 'Middle' ? argsData.color === 'transparent' ? point.color : argsData.color : argsData.color;
            commonEventArgs.data.text = argsData.text;
            if (!series.dataLabel.template && commonEventArgs.data.Text !== '' && !argsData.cancel) {
                var element_1 = {
                    tag: 'dataLabel',
                    series: series,
                    point: point,
                    pointIndex: pointIndex,
                    id: chart.svgObject.id + series.index + '-data-label' + pointIndex,
                    child: chart.chart3D
                };
                chart.graphics.addVisual(chart.polygon.createTextElement(chart.vector.vector3D(pointX, pointY, point.symbolLocations.z), element_1, 0, -size.height), chart);
            }
            var tag = !(series.dataLabel && series.dataLabel.template) ? 'text' : 'template';
            var backgroundColor = this.fontBackground === 'transparent' ? ((this.chart.theme.indexOf('Dark') > -1 || this.chart.theme === 'HighContrast') ? '#000000' : '#FFFFFF') : this.fontBackground;
            var rgbValue = helper_1.convertHexToColor(helper_1.colorNameToHex(backgroundColor));
            var contrast = Math.round((rgbValue.r * 299 + rgbValue.g * 587 + rgbValue.b * 114) / 1000);
            var font = {
                size: argsData.textStyle.size || this.chart.themeStyle.datalabelFont.size,
                fontWeight: argsData.textStyle.fontWeight || this.chart.themeStyle.datalabelFont.fontWeight,
                fontStyle: argsData.textStyle.fontStyle || chart.themeStyle.datalabelFont.fontStyle,
                fontFamily: argsData.textStyle.fontFamily || this.chart.themeStyle.datalabelFont.fontFamily,
                color: argsData.textStyle.color || (this.chart.theme === 'Bootstrap5' ? '#212529' : this.chart.theme === 'Bootstrap5Dark' ? '#DEE2E6' : argsData.textStyle.color),
                opacity: argsData.textStyle.opacity
            };
            var element = {
                width: size.width,
                height: size.height,
                label: commonEventArgs.data,
                textAnchor: 'middle',
                tag: tag,
                font: font,
                angle: series.dataLabel.angle,
                id: chart.element.id + '-svg' + '-series-' + series.index + '-point-' + pointIndex + '-data-label',
                child: chart.chart3D,
                argsData: argsData,
                fill: (contrast >= 128) ? '#000000' : '#FFFFFF'
            };
            if (!argsData.cancel) {
                chart.graphics.addVisual(chart.polygon.createTextElement(chart.vector.vector3D(pointX, pointY, point.symbolLocations.z), element, 0, -size.height), chart);
            }
        };
        DataLabel3D.prototype.getLabelText = function (currentPoint, series, chart) {
            var labelFormat = series.dataLabel.format ? series.dataLabel.format : series.yAxis.labelFormat;
            var text = [];
            var customLabelFormat = labelFormat.match('{value}') !== null;
            text.push(currentPoint.text || currentPoint.yValue.toString());
            if ((labelFormat || chart.useGroupingSeparator) && !currentPoint.text) {
                series.yAxis.format = chart.intl.getNumberFormat({
                    format: customLabelFormat ? '' : labelFormat,
                    useGrouping: chart.useGroupingSeparator
                });
                for (var i = 0; i < text.length; i++) {
                    text[i] = customLabelFormat ? labelFormat.replace('{value}', series.yAxis.format(parseFloat(text[i]))) :
                        series.yAxis.format(parseFloat(text[i]));
                }
            }
            return text;
        };
        DataLabel3D.prototype.createDataLabelTemplate = function (parentElement, series, dataLabel, point, data, labelIndex, redraw, location) {
            this.margin = { left: 0, right: 0, bottom: 0, top: 0 };
            var clip = series.clipRect;
            var childElement = this.createTemplate(ej2_base_1.createElement('div', {
                id: this.chart.element.id + '-series-' + series.index + '-data-label-' + labelIndex,
                styles: 'position: absolute;background-color:' + data.color + ';' +
                    helper_1.getFontStyle(dataLabel.font, this.chart.themeStyle.datalabelFont) + ';border:' + data.border.width + 'px solid ' + data.border.color + ';'
            }), data.template, this.chart, point, series, this.chart.element.id + '-data-label-', labelIndex, location);
            this.calculateTemplateLabelSize(parentElement, childElement, point, series, dataLabel, clip, redraw, location);
        };
        DataLabel3D.prototype.calculateTemplateLabelSize = function (parentElement, childElement, point, series, dataLabel, clip, redraw, location, isReactCallback) {
            var elementRect = helper_1.measureElementRect(childElement, redraw, isReactCallback);
            var rect = { x: 0, y: 0, width: 0, height: 0 };
            var rectPosition = this.calculateTextPosition(series, point, elementRect, location);
            var clipWidth = 0;
            var clipHeight = 0;
            var isOverlap = false;
            if (isReactCallback) {
                isOverlap = (elementRect.width === 0 || elementRect.height === 0);
            }
            childElement.style.left = (rectPosition.left - clipWidth) + 'px';
            childElement.style.top = (rectPosition.top + clipHeight) + 'px';
            var backgroundColor = this.fontBackground === 'transparent' ? (this.chart.theme.indexOf('Dark') > -1 ? 'black' : 'white') : this.fontBackground;
            var rgbValue = helper_1.convertHexToColor(helper_1.colorNameToHex(backgroundColor));
            var vAxis = series.chart.requireInvertedAxis ? series.xAxis : series.yAxis;
            var hAxis = series.chart.requireInvertedAxis ? series.yAxis : series.xAxis;
            childElement.style.color = dataLabel.font.color || this.chart.theme === 'Bootstrap5' ? '#212529' : this.chart.theme === 'Bootstrap5Dark' ? '#DEE2E6' :
                ((Math.round((rgbValue.r * 299 + rgbValue.g * 587 + rgbValue.b * 114) / 1000)) >= 128 ? 'black' : 'white');
            if (childElement.childElementCount && !isOverlap && (!helper_1.isCollide(rect, this.chart.dataLabelCollections, clip))
                && (point.yValue === undefined ||
                    helper_1.withIn(point.yValue, series.yAxis.visibleRange) || (series.type.indexOf('Stacking') > -1) ||
                    (series.type.indexOf('100') > -1 && helper_1.withIn(series.stackedValues.endValues[point.index], series.yAxis.visibleRange))) &&
                helper_1.withIn(point.xValue, series.xAxis.visibleRange) && parseFloat(childElement.style.top) >= vAxis.rect.y &&
                parseFloat(childElement.style.left) >= hAxis.rect.x &&
                parseFloat(childElement.style.top) <= vAxis.rect.y + vAxis.rect.height &&
                parseFloat(childElement.style.left) <= hAxis.rect.x + hAxis.rect.width) {
                this.chart.dataLabelCollections.push(new ej2_svg_base_1.Rect(rect.x + clip.x, rect.y + clip.y, rect.width, rect.height));
                helper_1.appendChildElement(false, parentElement, childElement, redraw, true, 'left', 'top');
            }
        };
        DataLabel3D.prototype.calculateTextPosition = function (series, point, elementSize, location) {
            var width = elementSize.width / 2;
            var height = elementSize.height;
            var left;
            var top;
            var right;
            if (series.type.indexOf('Bar') !== -1) {
                left = location.x - width;
                top = location.y - height + series.xAxis.plotOffset;
                right = location.x + width;
            }
            else {
                left = location.x - width;
                top = location.y - height;
                right = location.x + width;
            }
            return { left: left, top: top, right: right };
        };
        DataLabel3D.prototype.chartReactTemplate = function (childElement, chart, point, series, labelIndex, redraw, location) {
            var parentElement = document.getElementById(chart.element.id + '-series-' + series.index + '-data-label-collections');
            if (parentElement) {
                if (point.index === 0) {
                    chart.dataLabelCollections = [];
                }
                chart.dataLabel3DModule.calculateTemplateLabelSize(parentElement, childElement, point, series, series.dataLabel, series.clipRect, redraw, location, true);
            }
        };
        DataLabel3D.prototype.createTemplate = function (childElement, content, chart, point, series, dataLabelId, labelIndex, location, redraw) {
            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;
                Eif (chart.getModuleName() === 'chart3d') {
                    reactCallback = (point && series) ? this.chartReactTemplate.bind(this, childElement, chart, point, series, labelIndex, redraw, location) : reactCallback;
                    Iif (chart.isReact) {
                        chart.renderReactTemplates(reactCallback);
                    }
                }
            }
            catch (e) {
                return childElement;
            }
            return childElement;
        };
        DataLabel3D.prototype.getModuleName = function () {
            return 'DataLabel3D';
        };
        DataLabel3D.prototype.destroy = function () {
        };
        return DataLabel3D;
    }());
    exports.DataLabel3D = DataLabel3D;
});