123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489 |
- "use strict";
- var Util = require('../util/common');
- var Marker = require('./marker');
- var Container = require('./list');
- var TextBox = require('./text-box');
- var GAP = 4;
- /**
- * TODOList:
- * 1. 移除 fixed 参数
- */
- var Tooltip = /*#__PURE__*/function () {
- var _proto = Tooltip.prototype;
- _proto.getDefaultCfg = function getDefaultCfg() {
- return {
- /**
- * wether show the crosshairs
- * @type {Object}
- */
- showCrosshairs: false,
- /**
- * the style for crosshairs
- * @type {Object}
- */
- crosshairsStyle: {
- stroke: 'rgba(0, 0, 0, 0.25)',
- lineWidth: 1
- },
- /**
- * the type of crosshairs, optional value is 'x', 'y' or 'xy', default is 'y'
- */
- crosshairsType: 'y',
- /**
- * show or hide the x axis tip
- */
- showXTip: false,
- /**
- * show or hide the y axis tip
- */
- showYTip: false,
- xTip: null,
- xTipBackground: {
- radius: 1,
- fill: 'rgba(0, 0, 0, 0.65)',
- padding: [3, 5]
- },
- yTip: null,
- yTipBackground: {
- radius: 1,
- fill: 'rgba(0, 0, 0, 0.65)',
- padding: [3, 5]
- },
- /**
- * the style for tooltip container's background
- * @type {Object}
- */
- background: null,
- /**
- * layout, can be horizontal or vertical
- * @type {String}
- */
- layout: 'horizontal',
- offsetX: 0,
- offsetY: 0
- };
- };
- function Tooltip(cfg) {
- Util.deepMix(this, this.getDefaultCfg(), cfg);
- var frontPlot = this.frontPlot,
- custom = this.custom;
- if (!custom) {
- // custom means user do customize
- var container = new Container(Util.mix({
- parent: frontPlot,
- zIndex: 3
- }, cfg));
- this.container = container;
- var fixed = this.fixed,
- background = this.background;
- if (!fixed) {
- this.tooltipArrow = frontPlot.addShape('Polygon', {
- className: 'tooltip-arrow',
- visible: false,
- zIndex: 2,
- attrs: Util.mix({
- points: []
- }, background)
- });
- }
- }
- if (this.showXTip) {
- var xTipBackground = this.xTipBackground;
- var xTipBox = new TextBox({
- className: 'xTip',
- background: xTipBackground,
- visible: false
- });
- frontPlot.add(xTipBox.container);
- this.xTipBox = xTipBox;
- }
- if (this.showYTip) {
- var yTipBackground = this.yTipBackground;
- var yTipBox = new TextBox({
- className: 'yTip',
- background: yTipBackground,
- visible: false
- });
- frontPlot.add(yTipBox.container);
- this.yTipBox = yTipBox;
- }
- if (this.showCrosshairs) {
- this._renderCrosshairs();
- }
- frontPlot.sort();
- }
- _proto.setContent = function setContent(title, items) {
- this.title = title;
- this.items = items;
- if (!this.custom) {
- var container = this.container;
- container.setTitle(title);
- container.setItems(items);
- }
- };
- _proto.setYTipContent = function setYTipContent(val) {
- var yTip = this.yTip;
- if (Util.isFunction(yTip)) {
- val = yTip(val);
- } else {
- val = Util.mix({
- text: val
- }, yTip);
- }
- this.yTipBox && this.yTipBox.updateContent(val);
- };
- _proto.setYTipPosition = function setYTipPosition(pos) {
- var plotRange = this.plotRange;
- var crosshairsShapeX = this.crosshairsShapeX;
- if (this.showYTip) {
- var yTipBox = this.yTipBox;
- var yTipHeight = yTipBox.getHeight();
- var yTipWidth = yTipBox.getWidth();
- var posX = plotRange.tl.x - yTipWidth;
- var posY = pos - yTipHeight / 2;
- if (posY <= plotRange.tl.y) {
- posY = plotRange.tl.y;
- }
- if (posY + yTipHeight >= plotRange.br.y) {
- posY = plotRange.br.y - yTipHeight;
- }
- if (posX < 0) {
- posX = plotRange.tl.x;
- crosshairsShapeX && crosshairsShapeX.attr('x1', plotRange.tl.x + yTipWidth);
- }
- yTipBox.updatePosition(posX, posY);
- }
- };
- _proto.setXTipContent = function setXTipContent(val) {
- var xTip = this.xTip;
- if (Util.isFunction(xTip)) {
- val = xTip(val);
- } else {
- val = Util.mix({
- text: val
- }, xTip);
- }
- this.xTipBox && this.xTipBox.updateContent(val);
- };
- _proto.setXTipPosition = function setXTipPosition(pos) {
- var showXTip = this.showXTip,
- canvas = this.canvas,
- plotRange = this.plotRange,
- xTipBox = this.xTipBox,
- crosshairsShapeY = this.crosshairsShapeY;
- if (showXTip) {
- // const el = canvas.get('el');
- // const canvasHeight = Util.getHeight(el);
- var canvasHeight = canvas.get('height');
- var xTipWidth = xTipBox.getWidth();
- var xTipHeight = xTipBox.getHeight();
- var posX = pos - xTipWidth / 2;
- var posY = plotRange.br.y;
- if (posX <= plotRange.tl.x) {
- posX = plotRange.tl.x;
- }
- if (posX + xTipWidth >= plotRange.tr.x) {
- posX = plotRange.tr.x - xTipWidth;
- }
- if (canvasHeight - posY < xTipHeight) {
- posY -= xTipHeight;
- }
- xTipBox.updatePosition(posX, posY);
- crosshairsShapeY && crosshairsShapeY.attr('y1', posY);
- }
- };
- _proto.setXCrosshairPosition = function setXCrosshairPosition(pos) {
- this.crosshairsShapeX && this.crosshairsShapeX.moveTo(0, pos);
- };
- _proto.setYCrosshairPosition = function setYCrosshairPosition(pos) {
- this.crosshairsShapeY && this.crosshairsShapeY.moveTo(pos, 0);
- };
- _proto.setPosition = function setPosition(items) {
- var container = this.container,
- plotRange = this.plotRange,
- offsetX = this.offsetX,
- offsetY = this.offsetY,
- fixed = this.fixed,
- tooltipArrow = this.tooltipArrow;
- if (!container) {
- return;
- }
- var containerBBox = container.container.getBBox();
- var minX = containerBBox.minX,
- minY = containerBBox.minY,
- width = containerBBox.width,
- height = containerBBox.height;
- var tl = plotRange.tl,
- tr = plotRange.tr;
- var posX = 0;
- var posY = tl.y - height - GAP + offsetY;
- if (fixed) {
- var x = (tl.x + tr.x) / 2;
- posX = x - width / 2 + offsetX;
- } else {
- var _x;
- if (items.length > 1) {
- _x = (items[0].x + items[items.length - 1].x) / 2;
- } else {
- _x = items[0].x;
- }
- posX = _x - width / 2 + offsetX;
- if (posX < tl.x) {
- posX = tl.x;
- }
- if (posX + width > tr.x) {
- posX = tr.x - width;
- }
- if (tooltipArrow) {
- tooltipArrow.attr('points', [{
- x: _x - 3,
- y: tl.y - GAP + offsetY
- }, {
- x: _x + 3,
- y: tl.y - GAP + offsetY
- }, {
- x: _x,
- y: tl.y + offsetY
- }]);
- var backShape = container.backShape;
- var radius = Util.parsePadding(backShape.attr('radius'));
- if (_x === tl.x) {
- radius[3] = 0;
- tooltipArrow.attr('points', [{
- x: tl.x,
- y: tl.y + offsetY
- }, {
- x: tl.x,
- y: tl.y - GAP + offsetY
- }, {
- x: tl.x + GAP,
- y: tl.y - GAP + offsetY
- }]);
- } else if (_x === tr.x) {
- radius[2] = 0;
- tooltipArrow.attr('points', [{
- x: tr.x,
- y: tl.y + offsetY
- }, {
- x: tr.x - GAP,
- y: tl.y - GAP + offsetY
- }, {
- x: tr.x,
- y: tl.y - GAP + offsetY
- }]);
- }
- backShape.attr('radius', radius);
- }
- }
- container.moveTo(posX - minX, posY - minY);
- };
- _proto.setMarkers = function setMarkers(cfg) {
- if (cfg === void 0) {
- cfg = {};
- }
- var self = this;
- var _cfg = cfg,
- items = _cfg.items,
- style = _cfg.style,
- type = _cfg.type;
- var markerGroup = self._getMarkerGroup(type);
- if (type === 'circle') {
- for (var i = 0, length = items.length; i < length; i++) {
- var item = items[i];
- var marker = new Marker({
- className: 'tooltip-circle-marker',
- attrs: Util.mix({
- x: item.x,
- y: item.y,
- stroke: item.color
- }, style)
- });
- markerGroup.add(marker);
- }
- } else {
- markerGroup.addShape('rect', {
- className: 'tooltip-rect-marker',
- attrs: style
- });
- }
- };
- _proto.clearMarkers = function clearMarkers() {
- var markerGroup = this.markerGroup;
- markerGroup && markerGroup.clear();
- };
- _proto.show = function show() {
- var crosshairsShapeX = this.crosshairsShapeX;
- var crosshairsShapeY = this.crosshairsShapeY;
- var markerGroup = this.markerGroup;
- var container = this.container;
- var tooltipArrow = this.tooltipArrow;
- var xTipBox = this.xTipBox;
- var yTipBox = this.yTipBox;
- var canvas = this.canvas;
- crosshairsShapeX && crosshairsShapeX.show();
- crosshairsShapeY && crosshairsShapeY.show();
- markerGroup && markerGroup.show();
- container && container.show();
- tooltipArrow && tooltipArrow.show();
- xTipBox && xTipBox.show();
- yTipBox && yTipBox.show();
- canvas.draw();
- };
- _proto.hide = function hide() {
- var crosshairsShapeX = this.crosshairsShapeX;
- var crosshairsShapeY = this.crosshairsShapeY;
- var markerGroup = this.markerGroup;
- var container = this.container;
- var tooltipArrow = this.tooltipArrow;
- var xTipBox = this.xTipBox;
- var yTipBox = this.yTipBox;
- crosshairsShapeX && crosshairsShapeX.hide();
- crosshairsShapeY && crosshairsShapeY.hide();
- markerGroup && markerGroup.hide();
- container && container.hide();
- tooltipArrow && tooltipArrow.hide();
- xTipBox && xTipBox.hide();
- yTipBox && yTipBox.hide();
- };
- _proto.destroy = function destroy() {
- var crosshairsShapeX = this.crosshairsShapeX;
- var crosshairsShapeY = this.crosshairsShapeY;
- var markerGroup = this.markerGroup;
- var container = this.container;
- var tooltipArrow = this.tooltipArrow;
- var xTipBox = this.xTipBox;
- var yTipBox = this.yTipBox;
- crosshairsShapeX && crosshairsShapeX.remove(true);
- crosshairsShapeY && crosshairsShapeY.remove(true);
- markerGroup && markerGroup.remove(true);
- tooltipArrow && tooltipArrow.remove(true);
- container && container.clear();
- xTipBox && xTipBox.clear();
- yTipBox && yTipBox.clear();
- this.destroyed = true;
- };
- _proto._getMarkerGroup = function _getMarkerGroup(type) {
- var markerGroup = this.markerGroup;
- if (!markerGroup) {
- if (type === 'circle') {
- markerGroup = this.frontPlot.addGroup({
- zIndex: 1
- });
- this.frontPlot.sort();
- } else {
- markerGroup = this.backPlot.addGroup();
- }
- this.markerGroup = markerGroup;
- } else {
- markerGroup.clear();
- }
- return markerGroup;
- };
- _proto._renderCrosshairs = function _renderCrosshairs() {
- var crosshairsType = this.crosshairsType,
- crosshairsStyle = this.crosshairsStyle,
- frontPlot = this.frontPlot,
- plotRange = this.plotRange;
- var tl = plotRange.tl,
- br = plotRange.br;
- if (Util.directionEnabled(crosshairsType, 'x')) {
- this.crosshairsShapeX = frontPlot.addShape('Line', {
- className: 'tooltip-crosshairs-x',
- zIndex: 0,
- visible: false,
- attrs: Util.mix({
- x1: tl.x,
- y1: 0,
- x2: br.x,
- y2: 0
- }, crosshairsStyle)
- });
- }
- if (Util.directionEnabled(crosshairsType, 'y')) {
- this.crosshairsShapeY = frontPlot.addShape('Line', {
- className: 'tooltip-crosshairs-y',
- zIndex: 0,
- visible: false,
- attrs: Util.mix({
- x1: 0,
- y1: br.y,
- x2: 0,
- y2: tl.y
- }, crosshairsStyle)
- });
- }
- };
- return Tooltip;
- }();
- module.exports = Tooltip;
|