UNPKG

@antv/g2plot

Version:

G2 Plot, a market of plots built with the Grammar of Graphics'

127 lines 5 kB
import { __extends } from "tslib"; import { ElementLabels, registerElementLabels } from '@antv/g2'; import * as _ from '@antv/util'; import { rgb2arr } from '../../../../util/color'; var TOP_MARGIN = 20; var ScatterLabels = /** @class */ (function (_super) { __extends(ScatterLabels, _super); function ScatterLabels() { return _super !== null && _super.apply(this, arguments) || this; } ScatterLabels.prototype.setLabelPosition = function (point, originPoint, index, originPosition) { var position = originPosition; if (_.isFunction(position)) { position = position(originPoint); } var size = originPoint.size; var p = originPoint.points[0]; var coord = this.get('coord'); var width = size; var height = size; switch (position) { case 'right': point.x += width * 2; point.textAlign = point.textAlign || 'left'; break; case 'left': point.x -= width * 2; point.textAlign = point.textAlign || 'right'; break; case 'bottom': point.y += height * 2; point.textAlign = point.textAlign || 'center'; point.textBaseline = 'top'; break; case 'middle': point.textBaseline = 'middle'; point.textAlign = point.textAlign || 'center'; break; case 'top': point.y -= height * 2; point.textAlign = point.textAlign || 'center'; point.textBaseline = 'bottom'; break; default: break; } }; ScatterLabels.prototype.showLabels = function (points, shapes) { var _this = this; _super.prototype.showLabels.call(this, points, shapes); var renderer = this.get('labelsRenderer'); var labels = renderer.get('group').get('children'); var items = renderer.get('items'); var view = this.get('element').get('view'); _.each(labels, function (label, index) { var l = label; var item = items[index]; var origin = l.get('origin'); var shapeId = _this.get('element').getShapeId(origin); var shape = _this._getShape(shapeId, shapes); var _a = _this.get('labelOptions'), adjustColor = _a.adjustColor, adjustPosition = _a.adjustPosition; if (adjustPosition) { _this.adjustPosition(l, shape, item); } if (adjustColor) { _this.adjustColor(l, shape); } }); view.get('canvas').draw(); }; ScatterLabels.prototype._getShape = function (shapeId, shapes) { var target; _.each(shapes, function (shape) { var s = shape; var id = s.id; if (id === shapeId) { target = s; } }); return target; }; ScatterLabels.prototype.adjustPosition = function (label, shape, item) { var originData = shape.get('origin')._origin; var labelRange = label.getBBox(); var shapeRange = shape.getBBox(); if (shapeRange.height <= labelRange.height || (shapeRange.width <= labelRange.width && item.position === 'middle')) { label.attr('text', ''); } }; ScatterLabels.prototype.adjustColor = function (label, shape) { var labelRange = label.getBBox(); var shapeRange = shape.getBBox(); if (labelRange.minY >= shapeRange.minY && labelRange.maxY <= shapeRange.maxY) { var shapeColor = shape.attr('fill'); var shapeOpacity = shape.attr('opacity') ? shape.attr('opacity') : 1; var rgb = rgb2arr(shapeColor); var gray = Math.round(rgb[0] * 0.299 + rgb[1] * 0.587 + rgb[2] * 0.114) / shapeOpacity; var colorBand = [ { from: 0, to: 85, color: 'white' }, { from: 85, to: 170, color: '#F6F6F6' }, { from: 170, to: 255, color: 'black' }, ]; var reflect = this._mappingColor(colorBand, gray); label.attr('fill', reflect); } else if (labelRange.maxY < shapeRange.minY) { var theme = this.get('theme'); var labelTextColor = _.get(theme, 'label.textStyle.fill', 'black'); label.attr('fill', labelTextColor); } }; ScatterLabels.prototype._mappingColor = function (band, gray) { var reflect; _.each(band, function (b) { var map = b; if (gray >= map.from && gray < map.to) { reflect = map.color; } }); return reflect; }; return ScatterLabels; }(ElementLabels)); export { ScatterLabels }; registerElementLabels('scatterLabel', ScatterLabels); //# sourceMappingURL=scatter-label.js.map