Line.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing,
  13. * software distributed under the License is distributed on an
  14. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  15. * KIND, either express or implied. See the License for the
  16. * specific language governing permissions and limitations
  17. * under the License.
  18. */
  19. /**
  20. * AUTO-GENERATED FILE. DO NOT MODIFY.
  21. */
  22. /*
  23. * Licensed to the Apache Software Foundation (ASF) under one
  24. * or more contributor license agreements. See the NOTICE file
  25. * distributed with this work for additional information
  26. * regarding copyright ownership. The ASF licenses this file
  27. * to you under the Apache License, Version 2.0 (the
  28. * "License"); you may not use this file except in compliance
  29. * with the License. You may obtain a copy of the License at
  30. *
  31. * http://www.apache.org/licenses/LICENSE-2.0
  32. *
  33. * Unless required by applicable law or agreed to in writing,
  34. * software distributed under the License is distributed on an
  35. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  36. * KIND, either express or implied. See the License for the
  37. * specific language governing permissions and limitations
  38. * under the License.
  39. */
  40. import { __extends } from "tslib";
  41. import { isArray, each } from 'zrender/lib/core/util.js';
  42. import * as vector from 'zrender/lib/core/vector.js';
  43. import * as symbolUtil from '../../util/symbol.js';
  44. import ECLinePath from './LinePath.js';
  45. import * as graphic from '../../util/graphic.js';
  46. import { toggleHoverEmphasis, enterEmphasis, leaveEmphasis, SPECIAL_STATES } from '../../util/states.js';
  47. import { getLabelStatesModels, setLabelStyle } from '../../label/labelStyle.js';
  48. import { round } from '../../util/number.js';
  49. var SYMBOL_CATEGORIES = ['fromSymbol', 'toSymbol'];
  50. function makeSymbolTypeKey(symbolCategory) {
  51. return '_' + symbolCategory + 'Type';
  52. }
  53. function makeSymbolTypeValue(name, lineData, idx) {
  54. var symbolType = lineData.getItemVisual(idx, name);
  55. if (!symbolType || symbolType === 'none') {
  56. return symbolType;
  57. }
  58. var symbolSize = lineData.getItemVisual(idx, name + 'Size');
  59. var symbolRotate = lineData.getItemVisual(idx, name + 'Rotate');
  60. var symbolOffset = lineData.getItemVisual(idx, name + 'Offset');
  61. var symbolKeepAspect = lineData.getItemVisual(idx, name + 'KeepAspect');
  62. var symbolSizeArr = symbolUtil.normalizeSymbolSize(symbolSize);
  63. var symbolOffsetArr = symbolUtil.normalizeSymbolOffset(symbolOffset || 0, symbolSizeArr);
  64. return symbolType + symbolSizeArr + symbolOffsetArr + (symbolRotate || '') + (symbolKeepAspect || '');
  65. }
  66. /**
  67. * @inner
  68. */
  69. function createSymbol(name, lineData, idx) {
  70. var symbolType = lineData.getItemVisual(idx, name);
  71. if (!symbolType || symbolType === 'none') {
  72. return;
  73. }
  74. var symbolSize = lineData.getItemVisual(idx, name + 'Size');
  75. var symbolRotate = lineData.getItemVisual(idx, name + 'Rotate');
  76. var symbolOffset = lineData.getItemVisual(idx, name + 'Offset');
  77. var symbolKeepAspect = lineData.getItemVisual(idx, name + 'KeepAspect');
  78. var symbolSizeArr = symbolUtil.normalizeSymbolSize(symbolSize);
  79. var symbolOffsetArr = symbolUtil.normalizeSymbolOffset(symbolOffset || 0, symbolSizeArr);
  80. var symbolPath = symbolUtil.createSymbol(symbolType, -symbolSizeArr[0] / 2 + symbolOffsetArr[0], -symbolSizeArr[1] / 2 + symbolOffsetArr[1], symbolSizeArr[0], symbolSizeArr[1], null, symbolKeepAspect);
  81. symbolPath.__specifiedRotation = symbolRotate == null || isNaN(symbolRotate) ? void 0 : +symbolRotate * Math.PI / 180 || 0;
  82. symbolPath.name = name;
  83. return symbolPath;
  84. }
  85. function createLine(points) {
  86. var line = new ECLinePath({
  87. name: 'line',
  88. subPixelOptimize: true
  89. });
  90. setLinePoints(line.shape, points);
  91. return line;
  92. }
  93. function setLinePoints(targetShape, points) {
  94. targetShape.x1 = points[0][0];
  95. targetShape.y1 = points[0][1];
  96. targetShape.x2 = points[1][0];
  97. targetShape.y2 = points[1][1];
  98. targetShape.percent = 1;
  99. var cp1 = points[2];
  100. if (cp1) {
  101. targetShape.cpx1 = cp1[0];
  102. targetShape.cpy1 = cp1[1];
  103. } else {
  104. targetShape.cpx1 = NaN;
  105. targetShape.cpy1 = NaN;
  106. }
  107. }
  108. var Line =
  109. /** @class */
  110. function (_super) {
  111. __extends(Line, _super);
  112. function Line(lineData, idx, seriesScope) {
  113. var _this = _super.call(this) || this;
  114. _this._createLine(lineData, idx, seriesScope);
  115. return _this;
  116. }
  117. Line.prototype._createLine = function (lineData, idx, seriesScope) {
  118. var seriesModel = lineData.hostModel;
  119. var linePoints = lineData.getItemLayout(idx);
  120. var line = createLine(linePoints);
  121. line.shape.percent = 0;
  122. graphic.initProps(line, {
  123. shape: {
  124. percent: 1
  125. }
  126. }, seriesModel, idx);
  127. this.add(line);
  128. each(SYMBOL_CATEGORIES, function (symbolCategory) {
  129. var symbol = createSymbol(symbolCategory, lineData, idx); // symbols must added after line to make sure
  130. // it will be updated after line#update.
  131. // Or symbol position and rotation update in line#beforeUpdate will be one frame slow
  132. this.add(symbol);
  133. this[makeSymbolTypeKey(symbolCategory)] = makeSymbolTypeValue(symbolCategory, lineData, idx);
  134. }, this);
  135. this._updateCommonStl(lineData, idx, seriesScope);
  136. }; // TODO More strict on the List type in parameters?
  137. Line.prototype.updateData = function (lineData, idx, seriesScope) {
  138. var seriesModel = lineData.hostModel;
  139. var line = this.childOfName('line');
  140. var linePoints = lineData.getItemLayout(idx);
  141. var target = {
  142. shape: {}
  143. };
  144. setLinePoints(target.shape, linePoints);
  145. graphic.updateProps(line, target, seriesModel, idx);
  146. each(SYMBOL_CATEGORIES, function (symbolCategory) {
  147. var symbolType = makeSymbolTypeValue(symbolCategory, lineData, idx);
  148. var key = makeSymbolTypeKey(symbolCategory); // Symbol changed
  149. if (this[key] !== symbolType) {
  150. this.remove(this.childOfName(symbolCategory));
  151. var symbol = createSymbol(symbolCategory, lineData, idx);
  152. this.add(symbol);
  153. }
  154. this[key] = symbolType;
  155. }, this);
  156. this._updateCommonStl(lineData, idx, seriesScope);
  157. };
  158. ;
  159. Line.prototype.getLinePath = function () {
  160. return this.childAt(0);
  161. };
  162. Line.prototype._updateCommonStl = function (lineData, idx, seriesScope) {
  163. var seriesModel = lineData.hostModel;
  164. var line = this.childOfName('line');
  165. var emphasisLineStyle = seriesScope && seriesScope.emphasisLineStyle;
  166. var blurLineStyle = seriesScope && seriesScope.blurLineStyle;
  167. var selectLineStyle = seriesScope && seriesScope.selectLineStyle;
  168. var labelStatesModels = seriesScope && seriesScope.labelStatesModels;
  169. var emphasisDisabled = seriesScope && seriesScope.emphasisDisabled;
  170. var focus = seriesScope && seriesScope.focus;
  171. var blurScope = seriesScope && seriesScope.blurScope; // Optimization for large dataset
  172. if (!seriesScope || lineData.hasItemOption) {
  173. var itemModel = lineData.getItemModel(idx);
  174. var emphasisModel = itemModel.getModel('emphasis');
  175. emphasisLineStyle = emphasisModel.getModel('lineStyle').getLineStyle();
  176. blurLineStyle = itemModel.getModel(['blur', 'lineStyle']).getLineStyle();
  177. selectLineStyle = itemModel.getModel(['select', 'lineStyle']).getLineStyle();
  178. emphasisDisabled = emphasisModel.get('disabled');
  179. focus = emphasisModel.get('focus');
  180. blurScope = emphasisModel.get('blurScope');
  181. labelStatesModels = getLabelStatesModels(itemModel);
  182. }
  183. var lineStyle = lineData.getItemVisual(idx, 'style');
  184. var visualColor = lineStyle.stroke;
  185. line.useStyle(lineStyle);
  186. line.style.fill = null;
  187. line.style.strokeNoScale = true;
  188. line.ensureState('emphasis').style = emphasisLineStyle;
  189. line.ensureState('blur').style = blurLineStyle;
  190. line.ensureState('select').style = selectLineStyle; // Update symbol
  191. each(SYMBOL_CATEGORIES, function (symbolCategory) {
  192. var symbol = this.childOfName(symbolCategory);
  193. if (symbol) {
  194. // Share opacity and color with line.
  195. symbol.setColor(visualColor);
  196. symbol.style.opacity = lineStyle.opacity;
  197. for (var i = 0; i < SPECIAL_STATES.length; i++) {
  198. var stateName = SPECIAL_STATES[i];
  199. var lineState = line.getState(stateName);
  200. if (lineState) {
  201. var lineStateStyle = lineState.style || {};
  202. var state = symbol.ensureState(stateName);
  203. var stateStyle = state.style || (state.style = {});
  204. if (lineStateStyle.stroke != null) {
  205. stateStyle[symbol.__isEmptyBrush ? 'stroke' : 'fill'] = lineStateStyle.stroke;
  206. }
  207. if (lineStateStyle.opacity != null) {
  208. stateStyle.opacity = lineStateStyle.opacity;
  209. }
  210. }
  211. }
  212. symbol.markRedraw();
  213. }
  214. }, this);
  215. var rawVal = seriesModel.getRawValue(idx);
  216. setLabelStyle(this, labelStatesModels, {
  217. labelDataIndex: idx,
  218. labelFetcher: {
  219. getFormattedLabel: function (dataIndex, stateName) {
  220. return seriesModel.getFormattedLabel(dataIndex, stateName, lineData.dataType);
  221. }
  222. },
  223. inheritColor: visualColor || '#000',
  224. defaultOpacity: lineStyle.opacity,
  225. defaultText: (rawVal == null ? lineData.getName(idx) : isFinite(rawVal) ? round(rawVal) : rawVal) + ''
  226. });
  227. var label = this.getTextContent(); // Always set `textStyle` even if `normalStyle.text` is null, because default
  228. // values have to be set on `normalStyle`.
  229. if (label) {
  230. var labelNormalModel = labelStatesModels.normal;
  231. label.__align = label.style.align;
  232. label.__verticalAlign = label.style.verticalAlign; // 'start', 'middle', 'end'
  233. label.__position = labelNormalModel.get('position') || 'middle';
  234. var distance = labelNormalModel.get('distance');
  235. if (!isArray(distance)) {
  236. distance = [distance, distance];
  237. }
  238. label.__labelDistance = distance;
  239. }
  240. this.setTextConfig({
  241. position: null,
  242. local: true,
  243. inside: false // Can't be inside for stroke element.
  244. });
  245. toggleHoverEmphasis(this, focus, blurScope, emphasisDisabled);
  246. };
  247. Line.prototype.highlight = function () {
  248. enterEmphasis(this);
  249. };
  250. Line.prototype.downplay = function () {
  251. leaveEmphasis(this);
  252. };
  253. Line.prototype.updateLayout = function (lineData, idx) {
  254. this.setLinePoints(lineData.getItemLayout(idx));
  255. };
  256. Line.prototype.setLinePoints = function (points) {
  257. var linePath = this.childOfName('line');
  258. setLinePoints(linePath.shape, points);
  259. linePath.dirty();
  260. };
  261. Line.prototype.beforeUpdate = function () {
  262. var lineGroup = this;
  263. var symbolFrom = lineGroup.childOfName('fromSymbol');
  264. var symbolTo = lineGroup.childOfName('toSymbol');
  265. var label = lineGroup.getTextContent(); // Quick reject
  266. if (!symbolFrom && !symbolTo && (!label || label.ignore)) {
  267. return;
  268. }
  269. var invScale = 1;
  270. var parentNode = this.parent;
  271. while (parentNode) {
  272. if (parentNode.scaleX) {
  273. invScale /= parentNode.scaleX;
  274. }
  275. parentNode = parentNode.parent;
  276. }
  277. var line = lineGroup.childOfName('line'); // If line not changed
  278. // FIXME Parent scale changed
  279. if (!this.__dirty && !line.__dirty) {
  280. return;
  281. }
  282. var percent = line.shape.percent;
  283. var fromPos = line.pointAt(0);
  284. var toPos = line.pointAt(percent);
  285. var d = vector.sub([], toPos, fromPos);
  286. vector.normalize(d, d);
  287. function setSymbolRotation(symbol, percent) {
  288. // Fix #12388
  289. // when symbol is set to be 'arrow' in markLine,
  290. // symbolRotate value will be ignored, and compulsively use tangent angle.
  291. // rotate by default if symbol rotation is not specified
  292. var specifiedRotation = symbol.__specifiedRotation;
  293. if (specifiedRotation == null) {
  294. var tangent = line.tangentAt(percent);
  295. symbol.attr('rotation', (percent === 1 ? -1 : 1) * Math.PI / 2 - Math.atan2(tangent[1], tangent[0]));
  296. } else {
  297. symbol.attr('rotation', specifiedRotation);
  298. }
  299. }
  300. if (symbolFrom) {
  301. symbolFrom.setPosition(fromPos);
  302. setSymbolRotation(symbolFrom, 0);
  303. symbolFrom.scaleX = symbolFrom.scaleY = invScale * percent;
  304. symbolFrom.markRedraw();
  305. }
  306. if (symbolTo) {
  307. symbolTo.setPosition(toPos);
  308. setSymbolRotation(symbolTo, 1);
  309. symbolTo.scaleX = symbolTo.scaleY = invScale * percent;
  310. symbolTo.markRedraw();
  311. }
  312. if (label && !label.ignore) {
  313. label.x = label.y = 0;
  314. label.originX = label.originY = 0;
  315. var textAlign = void 0;
  316. var textVerticalAlign = void 0;
  317. var distance = label.__labelDistance;
  318. var distanceX = distance[0] * invScale;
  319. var distanceY = distance[1] * invScale;
  320. var halfPercent = percent / 2;
  321. var tangent = line.tangentAt(halfPercent);
  322. var n = [tangent[1], -tangent[0]];
  323. var cp = line.pointAt(halfPercent);
  324. if (n[1] > 0) {
  325. n[0] = -n[0];
  326. n[1] = -n[1];
  327. }
  328. var dir = tangent[0] < 0 ? -1 : 1;
  329. if (label.__position !== 'start' && label.__position !== 'end') {
  330. var rotation = -Math.atan2(tangent[1], tangent[0]);
  331. if (toPos[0] < fromPos[0]) {
  332. rotation = Math.PI + rotation;
  333. }
  334. label.rotation = rotation;
  335. }
  336. var dy = void 0;
  337. switch (label.__position) {
  338. case 'insideStartTop':
  339. case 'insideMiddleTop':
  340. case 'insideEndTop':
  341. case 'middle':
  342. dy = -distanceY;
  343. textVerticalAlign = 'bottom';
  344. break;
  345. case 'insideStartBottom':
  346. case 'insideMiddleBottom':
  347. case 'insideEndBottom':
  348. dy = distanceY;
  349. textVerticalAlign = 'top';
  350. break;
  351. default:
  352. dy = 0;
  353. textVerticalAlign = 'middle';
  354. }
  355. switch (label.__position) {
  356. case 'end':
  357. label.x = d[0] * distanceX + toPos[0];
  358. label.y = d[1] * distanceY + toPos[1];
  359. textAlign = d[0] > 0.8 ? 'left' : d[0] < -0.8 ? 'right' : 'center';
  360. textVerticalAlign = d[1] > 0.8 ? 'top' : d[1] < -0.8 ? 'bottom' : 'middle';
  361. break;
  362. case 'start':
  363. label.x = -d[0] * distanceX + fromPos[0];
  364. label.y = -d[1] * distanceY + fromPos[1];
  365. textAlign = d[0] > 0.8 ? 'right' : d[0] < -0.8 ? 'left' : 'center';
  366. textVerticalAlign = d[1] > 0.8 ? 'bottom' : d[1] < -0.8 ? 'top' : 'middle';
  367. break;
  368. case 'insideStartTop':
  369. case 'insideStart':
  370. case 'insideStartBottom':
  371. label.x = distanceX * dir + fromPos[0];
  372. label.y = fromPos[1] + dy;
  373. textAlign = tangent[0] < 0 ? 'right' : 'left';
  374. label.originX = -distanceX * dir;
  375. label.originY = -dy;
  376. break;
  377. case 'insideMiddleTop':
  378. case 'insideMiddle':
  379. case 'insideMiddleBottom':
  380. case 'middle':
  381. label.x = cp[0];
  382. label.y = cp[1] + dy;
  383. textAlign = 'center';
  384. label.originY = -dy;
  385. break;
  386. case 'insideEndTop':
  387. case 'insideEnd':
  388. case 'insideEndBottom':
  389. label.x = -distanceX * dir + toPos[0];
  390. label.y = toPos[1] + dy;
  391. textAlign = tangent[0] >= 0 ? 'right' : 'left';
  392. label.originX = distanceX * dir;
  393. label.originY = -dy;
  394. break;
  395. }
  396. label.scaleX = label.scaleY = invScale;
  397. label.setStyle({
  398. // Use the user specified text align and baseline first
  399. verticalAlign: label.__verticalAlign || textVerticalAlign,
  400. align: label.__align || textAlign
  401. });
  402. }
  403. };
  404. return Line;
  405. }(graphic.Group);
  406. export default Line;