123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683 |
- var _util = require("zrender/lib/core/util");
- var retrieve = _util.retrieve;
- var defaults = _util.defaults;
- var extend = _util.extend;
- var each = _util.each;
- var formatUtil = require("../../util/format");
- var graphic = require("../../util/graphic");
- var Model = require("../../model/Model");
- var _number = require("../../util/number");
- var isRadianAroundZero = _number.isRadianAroundZero;
- var remRadian = _number.remRadian;
- var _symbol = require("../../util/symbol");
- var createSymbol = _symbol.createSymbol;
- var matrixUtil = require("zrender/lib/core/matrix");
- var _vector = require("zrender/lib/core/vector");
- var v2ApplyTransform = _vector.applyTransform;
- var _axisHelper = require("../../coord/axisHelper");
- var shouldShowAllLabels = _axisHelper.shouldShowAllLabels;
- var PI = Math.PI;
- var AxisBuilder = function (axisModel, opt) {
-
- this.opt = opt;
-
- this.axisModel = axisModel;
- defaults(opt, {
- labelOffset: 0,
- nameDirection: 1,
- tickDirection: 1,
- labelDirection: 1,
- silent: true
- });
-
- this.group = new graphic.Group();
- var dumbGroup = new graphic.Group({
- position: opt.position.slice(),
- rotation: opt.rotation
- });
-
- dumbGroup.updateTransform();
- this._transform = dumbGroup.transform;
- this._dumbGroup = dumbGroup;
- };
- AxisBuilder.prototype = {
- constructor: AxisBuilder,
- hasBuilder: function (name) {
- return !!builders[name];
- },
- add: function (name) {
- builders[name].call(this);
- },
- getGroup: function () {
- return this.group;
- }
- };
- var builders = {
-
- axisLine: function () {
- var opt = this.opt;
- var axisModel = this.axisModel;
- if (!axisModel.get('axisLine.show')) {
- return;
- }
- var extent = this.axisModel.axis.getExtent();
- var matrix = this._transform;
- var pt1 = [extent[0], 0];
- var pt2 = [extent[1], 0];
- if (matrix) {
- v2ApplyTransform(pt1, pt1, matrix);
- v2ApplyTransform(pt2, pt2, matrix);
- }
- var lineStyle = extend({
- lineCap: 'round'
- }, axisModel.getModel('axisLine.lineStyle').getLineStyle());
- this.group.add(new graphic.Line({
-
- anid: 'line',
- subPixelOptimize: true,
- shape: {
- x1: pt1[0],
- y1: pt1[1],
- x2: pt2[0],
- y2: pt2[1]
- },
- style: lineStyle,
- strokeContainThreshold: opt.strokeContainThreshold || 5,
- silent: true,
- z2: 1
- }));
- var arrows = axisModel.get('axisLine.symbol');
- var arrowSize = axisModel.get('axisLine.symbolSize');
- var arrowOffset = axisModel.get('axisLine.symbolOffset') || 0;
- if (typeof arrowOffset === 'number') {
- arrowOffset = [arrowOffset, arrowOffset];
- }
- if (arrows != null) {
- if (typeof arrows === 'string') {
-
- arrows = [arrows, arrows];
- }
- if (typeof arrowSize === 'string' || typeof arrowSize === 'number') {
-
- arrowSize = [arrowSize, arrowSize];
- }
- var symbolWidth = arrowSize[0];
- var symbolHeight = arrowSize[1];
- each([{
- rotate: opt.rotation + Math.PI / 2,
- offset: arrowOffset[0],
- r: 0
- }, {
- rotate: opt.rotation - Math.PI / 2,
- offset: arrowOffset[1],
- r: Math.sqrt((pt1[0] - pt2[0]) * (pt1[0] - pt2[0]) + (pt1[1] - pt2[1]) * (pt1[1] - pt2[1]))
- }], function (point, index) {
- if (arrows[index] !== 'none' && arrows[index] != null) {
- var symbol = createSymbol(arrows[index], -symbolWidth / 2, -symbolHeight / 2, symbolWidth, symbolHeight, lineStyle.stroke, true);
- var r = point.r + point.offset;
- var pos = [pt1[0] + r * Math.cos(opt.rotation), pt1[1] - r * Math.sin(opt.rotation)];
- symbol.attr({
- rotation: point.rotate,
- position: pos,
- silent: true,
- z2: 11
- });
- this.group.add(symbol);
- }
- }, this);
- }
- },
-
- axisTickLabel: function () {
- var axisModel = this.axisModel;
- var opt = this.opt;
- var ticksEls = buildAxisMajorTicks(this, axisModel, opt);
- var labelEls = buildAxisLabel(this, axisModel, opt);
- fixMinMaxLabelShow(axisModel, labelEls, ticksEls);
- buildAxisMinorTicks(this, axisModel, opt);
- },
-
- axisName: function () {
- var opt = this.opt;
- var axisModel = this.axisModel;
- var name = retrieve(opt.axisName, axisModel.get('name'));
- if (!name) {
- return;
- }
- var nameLocation = axisModel.get('nameLocation');
- var nameDirection = opt.nameDirection;
- var textStyleModel = axisModel.getModel('nameTextStyle');
- var gap = axisModel.get('nameGap') || 0;
- var extent = this.axisModel.axis.getExtent();
- var gapSignal = extent[0] > extent[1] ? -1 : 1;
- var pos = [nameLocation === 'start' ? extent[0] - gapSignal * gap : nameLocation === 'end' ? extent[1] + gapSignal * gap : (extent[0] + extent[1]) / 2,
-
- isNameLocationCenter(nameLocation) ? opt.labelOffset + nameDirection * gap : 0];
- var labelLayout;
- var nameRotation = axisModel.get('nameRotate');
- if (nameRotation != null) {
- nameRotation = nameRotation * PI / 180;
- }
- var axisNameAvailableWidth;
- if (isNameLocationCenter(nameLocation)) {
- labelLayout = innerTextLayout(opt.rotation, nameRotation != null ? nameRotation : opt.rotation,
- nameDirection);
- } else {
- labelLayout = endTextLayout(opt, nameLocation, nameRotation || 0, extent);
- axisNameAvailableWidth = opt.axisNameAvailableWidth;
- if (axisNameAvailableWidth != null) {
- axisNameAvailableWidth = Math.abs(axisNameAvailableWidth / Math.sin(labelLayout.rotation));
- !isFinite(axisNameAvailableWidth) && (axisNameAvailableWidth = null);
- }
- }
- var textFont = textStyleModel.getFont();
- var truncateOpt = axisModel.get('nameTruncate', true) || {};
- var ellipsis = truncateOpt.ellipsis;
- var maxWidth = retrieve(opt.nameTruncateMaxWidth, truncateOpt.maxWidth, axisNameAvailableWidth);
-
- var truncatedText = ellipsis != null && maxWidth != null ? formatUtil.truncateText(name, maxWidth, textFont, ellipsis, {
- minChar: 2,
- placeholder: truncateOpt.placeholder
- }) : name;
- var tooltipOpt = axisModel.get('tooltip', true);
- var mainType = axisModel.mainType;
- var formatterParams = {
- componentType: mainType,
- name: name,
- $vars: ['name']
- };
- formatterParams[mainType + 'Index'] = axisModel.componentIndex;
- var textEl = new graphic.Text({
-
- anid: 'name',
- __fullText: name,
- __truncatedText: truncatedText,
- position: pos,
- rotation: labelLayout.rotation,
- silent: isLabelSilent(axisModel),
- z2: 1,
- tooltip: tooltipOpt && tooltipOpt.show ? extend({
- content: name,
- formatter: function () {
- return name;
- },
- formatterParams: formatterParams
- }, tooltipOpt) : null
- });
- graphic.setTextStyle(textEl.style, textStyleModel, {
- text: truncatedText,
- textFont: textFont,
- textFill: textStyleModel.getTextColor() || axisModel.get('axisLine.lineStyle.color'),
- textAlign: textStyleModel.get('align') || labelLayout.textAlign,
- textVerticalAlign: textStyleModel.get('verticalAlign') || labelLayout.textVerticalAlign
- });
- if (axisModel.get('triggerEvent')) {
- textEl.eventData = makeAxisEventDataBase(axisModel);
- textEl.eventData.targetType = 'axisName';
- textEl.eventData.name = name;
- }
- this._dumbGroup.add(textEl);
- textEl.updateTransform();
- this.group.add(textEl);
- textEl.decomposeTransform();
- }
- };
- var makeAxisEventDataBase = AxisBuilder.makeAxisEventDataBase = function (axisModel) {
- var eventData = {
- componentType: axisModel.mainType,
- componentIndex: axisModel.componentIndex
- };
- eventData[axisModel.mainType + 'Index'] = axisModel.componentIndex;
- return eventData;
- };
- var innerTextLayout = AxisBuilder.innerTextLayout = function (axisRotation, textRotation, direction) {
- var rotationDiff = remRadian(textRotation - axisRotation);
- var textAlign;
- var textVerticalAlign;
- if (isRadianAroundZero(rotationDiff)) {
-
- textVerticalAlign = direction > 0 ? 'top' : 'bottom';
- textAlign = 'center';
- } else if (isRadianAroundZero(rotationDiff - PI)) {
-
- textVerticalAlign = direction > 0 ? 'bottom' : 'top';
- textAlign = 'center';
- } else {
- textVerticalAlign = 'middle';
- if (rotationDiff > 0 && rotationDiff < PI) {
- textAlign = direction > 0 ? 'right' : 'left';
- } else {
- textAlign = direction > 0 ? 'left' : 'right';
- }
- }
- return {
- rotation: rotationDiff,
- textAlign: textAlign,
- textVerticalAlign: textVerticalAlign
- };
- };
- function endTextLayout(opt, textPosition, textRotate, extent) {
- var rotationDiff = remRadian(textRotate - opt.rotation);
- var textAlign;
- var textVerticalAlign;
- var inverse = extent[0] > extent[1];
- var onLeft = textPosition === 'start' && !inverse || textPosition !== 'start' && inverse;
- if (isRadianAroundZero(rotationDiff - PI / 2)) {
- textVerticalAlign = onLeft ? 'bottom' : 'top';
- textAlign = 'center';
- } else if (isRadianAroundZero(rotationDiff - PI * 1.5)) {
- textVerticalAlign = onLeft ? 'top' : 'bottom';
- textAlign = 'center';
- } else {
- textVerticalAlign = 'middle';
- if (rotationDiff < PI * 1.5 && rotationDiff > PI / 2) {
- textAlign = onLeft ? 'left' : 'right';
- } else {
- textAlign = onLeft ? 'right' : 'left';
- }
- }
- return {
- rotation: rotationDiff,
- textAlign: textAlign,
- textVerticalAlign: textVerticalAlign
- };
- }
- var isLabelSilent = AxisBuilder.isLabelSilent = function (axisModel) {
- var tooltipOpt = axisModel.get('tooltip');
- return axisModel.get('silent')
- || !(axisModel.get('triggerEvent') || tooltipOpt && tooltipOpt.show);
- };
- function fixMinMaxLabelShow(axisModel, labelEls, tickEls) {
- if (shouldShowAllLabels(axisModel.axis)) {
- return;
- }
-
-
- var showMinLabel = axisModel.get('axisLabel.showMinLabel');
- var showMaxLabel = axisModel.get('axisLabel.showMaxLabel');
-
- labelEls = labelEls || [];
- tickEls = tickEls || [];
- var firstLabel = labelEls[0];
- var nextLabel = labelEls[1];
- var lastLabel = labelEls[labelEls.length - 1];
- var prevLabel = labelEls[labelEls.length - 2];
- var firstTick = tickEls[0];
- var nextTick = tickEls[1];
- var lastTick = tickEls[tickEls.length - 1];
- var prevTick = tickEls[tickEls.length - 2];
- if (showMinLabel === false) {
- ignoreEl(firstLabel);
- ignoreEl(firstTick);
- } else if (isTwoLabelOverlapped(firstLabel, nextLabel)) {
- if (showMinLabel) {
- ignoreEl(nextLabel);
- ignoreEl(nextTick);
- } else {
- ignoreEl(firstLabel);
- ignoreEl(firstTick);
- }
- }
- if (showMaxLabel === false) {
- ignoreEl(lastLabel);
- ignoreEl(lastTick);
- } else if (isTwoLabelOverlapped(prevLabel, lastLabel)) {
- if (showMaxLabel) {
- ignoreEl(prevLabel);
- ignoreEl(prevTick);
- } else {
- ignoreEl(lastLabel);
- ignoreEl(lastTick);
- }
- }
- }
- function ignoreEl(el) {
- el && (el.ignore = true);
- }
- function isTwoLabelOverlapped(current, next, labelLayout) {
-
- var firstRect = current && current.getBoundingRect().clone();
- var nextRect = next && next.getBoundingRect().clone();
- if (!firstRect || !nextRect) {
- return;
- }
-
- var mRotationBack = matrixUtil.identity([]);
- matrixUtil.rotate(mRotationBack, mRotationBack, -current.rotation);
- firstRect.applyTransform(matrixUtil.mul([], mRotationBack, current.getLocalTransform()));
- nextRect.applyTransform(matrixUtil.mul([], mRotationBack, next.getLocalTransform()));
- return firstRect.intersect(nextRect);
- }
- function isNameLocationCenter(nameLocation) {
- return nameLocation === 'middle' || nameLocation === 'center';
- }
- function createTicks(ticksCoords, tickTransform, tickEndCoord, tickLineStyle, aniid) {
- var tickEls = [];
- var pt1 = [];
- var pt2 = [];
- for (var i = 0; i < ticksCoords.length; i++) {
- var tickCoord = ticksCoords[i].coord;
- pt1[0] = tickCoord;
- pt1[1] = 0;
- pt2[0] = tickCoord;
- pt2[1] = tickEndCoord;
- if (tickTransform) {
- v2ApplyTransform(pt1, pt1, tickTransform);
- v2ApplyTransform(pt2, pt2, tickTransform);
- }
- var tickEl = new graphic.Line({
-
- anid: aniid + '_' + ticksCoords[i].tickValue,
- subPixelOptimize: true,
- shape: {
- x1: pt1[0],
- y1: pt1[1],
- x2: pt2[0],
- y2: pt2[1]
- },
- style: tickLineStyle,
- z2: 2,
- silent: true
- });
- tickEls.push(tickEl);
- }
- return tickEls;
- }
- function buildAxisMajorTicks(axisBuilder, axisModel, opt) {
- var axis = axisModel.axis;
- var tickModel = axisModel.getModel('axisTick');
- if (!tickModel.get('show') || axis.scale.isBlank()) {
- return;
- }
- var lineStyleModel = tickModel.getModel('lineStyle');
- var tickEndCoord = opt.tickDirection * tickModel.get('length');
- var ticksCoords = axis.getTicksCoords();
- var ticksEls = createTicks(ticksCoords, axisBuilder._transform, tickEndCoord, defaults(lineStyleModel.getLineStyle(), {
- stroke: axisModel.get('axisLine.lineStyle.color')
- }), 'ticks');
- for (var i = 0; i < ticksEls.length; i++) {
- axisBuilder.group.add(ticksEls[i]);
- }
- return ticksEls;
- }
- function buildAxisMinorTicks(axisBuilder, axisModel, opt) {
- var axis = axisModel.axis;
- var minorTickModel = axisModel.getModel('minorTick');
- if (!minorTickModel.get('show') || axis.scale.isBlank()) {
- return;
- }
- var minorTicksCoords = axis.getMinorTicksCoords();
- if (!minorTicksCoords.length) {
- return;
- }
- var lineStyleModel = minorTickModel.getModel('lineStyle');
- var tickEndCoord = opt.tickDirection * minorTickModel.get('length');
- var minorTickLineStyle = defaults(lineStyleModel.getLineStyle(), defaults(axisModel.getModel('axisTick').getLineStyle(), {
- stroke: axisModel.get('axisLine.lineStyle.color')
- }));
- for (var i = 0; i < minorTicksCoords.length; i++) {
- var minorTicksEls = createTicks(minorTicksCoords[i], axisBuilder._transform, tickEndCoord, minorTickLineStyle, 'minorticks_' + i);
- for (var k = 0; k < minorTicksEls.length; k++) {
- axisBuilder.group.add(minorTicksEls[k]);
- }
- }
- }
- function buildAxisLabel(axisBuilder, axisModel, opt) {
- var axis = axisModel.axis;
- var show = retrieve(opt.axisLabelShow, axisModel.get('axisLabel.show'));
- if (!show || axis.scale.isBlank()) {
- return;
- }
- var labelModel = axisModel.getModel('axisLabel');
- var labelMargin = labelModel.get('margin');
- var labels = axis.getViewLabels();
- var labelRotation = (retrieve(opt.labelRotate, labelModel.get('rotate')) || 0) * PI / 180;
- var labelLayout = innerTextLayout(opt.rotation, labelRotation, opt.labelDirection);
- var rawCategoryData = axisModel.getCategories && axisModel.getCategories(true);
- var labelEls = [];
- var silent = isLabelSilent(axisModel);
- var triggerEvent = axisModel.get('triggerEvent');
- each(labels, function (labelItem, index) {
- var tickValue = labelItem.tickValue;
- var formattedLabel = labelItem.formattedLabel;
- var rawLabel = labelItem.rawLabel;
- var itemLabelModel = labelModel;
- if (rawCategoryData && rawCategoryData[tickValue] && rawCategoryData[tickValue].textStyle) {
- itemLabelModel = new Model(rawCategoryData[tickValue].textStyle, labelModel, axisModel.ecModel);
- }
- var textColor = itemLabelModel.getTextColor() || axisModel.get('axisLine.lineStyle.color');
- var tickCoord = axis.dataToCoord(tickValue);
- var pos = [tickCoord, opt.labelOffset + opt.labelDirection * labelMargin];
- var textEl = new graphic.Text({
-
- anid: 'label_' + tickValue,
- position: pos,
- rotation: labelLayout.rotation,
- silent: silent,
- z2: 10
- });
- graphic.setTextStyle(textEl.style, itemLabelModel, {
- text: formattedLabel,
- textAlign: itemLabelModel.getShallow('align', true) || labelLayout.textAlign,
- textVerticalAlign: itemLabelModel.getShallow('verticalAlign', true) || itemLabelModel.getShallow('baseline', true) || labelLayout.textVerticalAlign,
- textFill: typeof textColor === 'function' ? textColor(
-
-
-
-
-
-
- axis.type === 'category' ? rawLabel : axis.type === 'value' ? tickValue + '' : tickValue, index) : textColor
- });
- if (triggerEvent) {
- textEl.eventData = makeAxisEventDataBase(axisModel);
- textEl.eventData.targetType = 'axisLabel';
- textEl.eventData.value = rawLabel;
- }
- axisBuilder._dumbGroup.add(textEl);
- textEl.updateTransform();
- labelEls.push(textEl);
- axisBuilder.group.add(textEl);
- textEl.decomposeTransform();
- });
- return labelEls;
- }
- var _default = AxisBuilder;
- module.exports = _default;
|