Line.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  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 = /** @class */function (_super) {
  109. __extends(Line, _super);
  110. function Line(lineData, idx, seriesScope) {
  111. var _this = _super.call(this) || this;
  112. _this._createLine(lineData, idx, seriesScope);
  113. return _this;
  114. }
  115. Line.prototype._createLine = function (lineData, idx, seriesScope) {
  116. var seriesModel = lineData.hostModel;
  117. var linePoints = lineData.getItemLayout(idx);
  118. var line = createLine(linePoints);
  119. line.shape.percent = 0;
  120. graphic.initProps(line, {
  121. shape: {
  122. percent: 1
  123. }
  124. }, seriesModel, idx);
  125. this.add(line);
  126. each(SYMBOL_CATEGORIES, function (symbolCategory) {
  127. var symbol = createSymbol(symbolCategory, lineData, idx);
  128. // symbols must added after line to make sure
  129. // it will be updated after line#update.
  130. // Or symbol position and rotation update in line#beforeUpdate will be one frame slow
  131. this.add(symbol);
  132. this[makeSymbolTypeKey(symbolCategory)] = makeSymbolTypeValue(symbolCategory, lineData, idx);
  133. }, this);
  134. this._updateCommonStl(lineData, idx, seriesScope);
  135. };
  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);
  149. // Symbol changed
  150. if (this[key] !== symbolType) {
  151. this.remove(this.childOfName(symbolCategory));
  152. var symbol = createSymbol(symbolCategory, lineData, idx);
  153. this.add(symbol);
  154. }
  155. this[key] = symbolType;
  156. }, this);
  157. this._updateCommonStl(lineData, idx, seriesScope);
  158. };
  159. ;
  160. Line.prototype.getLinePath = function () {
  161. return this.childAt(0);
  162. };
  163. Line.prototype._updateCommonStl = function (lineData, idx, seriesScope) {
  164. var seriesModel = lineData.hostModel;
  165. var line = this.childOfName('line');
  166. var emphasisLineStyle = seriesScope && seriesScope.emphasisLineStyle;
  167. var blurLineStyle = seriesScope && seriesScope.blurLineStyle;
  168. var selectLineStyle = seriesScope && seriesScope.selectLineStyle;
  169. var labelStatesModels = seriesScope && seriesScope.labelStatesModels;
  170. var emphasisDisabled = seriesScope && seriesScope.emphasisDisabled;
  171. var focus = seriesScope && seriesScope.focus;
  172. var blurScope = seriesScope && seriesScope.blurScope;
  173. // Optimization for large dataset
  174. if (!seriesScope || lineData.hasItemOption) {
  175. var itemModel = lineData.getItemModel(idx);
  176. var emphasisModel = itemModel.getModel('emphasis');
  177. emphasisLineStyle = emphasisModel.getModel('lineStyle').getLineStyle();
  178. blurLineStyle = itemModel.getModel(['blur', 'lineStyle']).getLineStyle();
  179. selectLineStyle = itemModel.getModel(['select', 'lineStyle']).getLineStyle();
  180. emphasisDisabled = emphasisModel.get('disabled');
  181. focus = emphasisModel.get('focus');
  182. blurScope = emphasisModel.get('blurScope');
  183. labelStatesModels = getLabelStatesModels(itemModel);
  184. }
  185. var lineStyle = lineData.getItemVisual(idx, 'style');
  186. var visualColor = lineStyle.stroke;
  187. line.useStyle(lineStyle);
  188. line.style.fill = null;
  189. line.style.strokeNoScale = true;
  190. line.ensureState('emphasis').style = emphasisLineStyle;
  191. line.ensureState('blur').style = blurLineStyle;
  192. line.ensureState('select').style = selectLineStyle;
  193. // Update symbol
  194. each(SYMBOL_CATEGORIES, function (symbolCategory) {
  195. var symbol = this.childOfName(symbolCategory);
  196. if (symbol) {
  197. // Share opacity and color with line.
  198. symbol.setColor(visualColor);
  199. symbol.style.opacity = lineStyle.opacity;
  200. for (var i = 0; i < SPECIAL_STATES.length; i++) {
  201. var stateName = SPECIAL_STATES[i];
  202. var lineState = line.getState(stateName);
  203. if (lineState) {
  204. var lineStateStyle = lineState.style || {};
  205. var state = symbol.ensureState(stateName);
  206. var stateStyle = state.style || (state.style = {});
  207. if (lineStateStyle.stroke != null) {
  208. stateStyle[symbol.__isEmptyBrush ? 'stroke' : 'fill'] = lineStateStyle.stroke;
  209. }
  210. if (lineStateStyle.opacity != null) {
  211. stateStyle.opacity = lineStateStyle.opacity;
  212. }
  213. }
  214. }
  215. symbol.markRedraw();
  216. }
  217. }, this);
  218. var rawVal = seriesModel.getRawValue(idx);
  219. setLabelStyle(this, labelStatesModels, {
  220. labelDataIndex: idx,
  221. labelFetcher: {
  222. getFormattedLabel: function (dataIndex, stateName) {
  223. return seriesModel.getFormattedLabel(dataIndex, stateName, lineData.dataType);
  224. }
  225. },
  226. inheritColor: visualColor || '#000',
  227. defaultOpacity: lineStyle.opacity,
  228. defaultText: (rawVal == null ? lineData.getName(idx) : isFinite(rawVal) ? round(rawVal) : rawVal) + ''
  229. });
  230. var label = this.getTextContent();
  231. // Always set `textStyle` even if `normalStyle.text` is null, because default
  232. // values have to be set on `normalStyle`.
  233. if (label) {
  234. var labelNormalModel = labelStatesModels.normal;
  235. label.__align = label.style.align;
  236. label.__verticalAlign = label.style.verticalAlign;
  237. // 'start', 'middle', 'end'
  238. label.__position = labelNormalModel.get('position') || 'middle';
  239. var distance = labelNormalModel.get('distance');
  240. if (!isArray(distance)) {
  241. distance = [distance, distance];
  242. }
  243. label.__labelDistance = distance;
  244. }
  245. this.setTextConfig({
  246. position: null,
  247. local: true,
  248. inside: false // Can't be inside for stroke element.
  249. });
  250. toggleHoverEmphasis(this, focus, blurScope, emphasisDisabled);
  251. };
  252. Line.prototype.highlight = function () {
  253. enterEmphasis(this);
  254. };
  255. Line.prototype.downplay = function () {
  256. leaveEmphasis(this);
  257. };
  258. Line.prototype.updateLayout = function (lineData, idx) {
  259. this.setLinePoints(lineData.getItemLayout(idx));
  260. };
  261. Line.prototype.setLinePoints = function (points) {
  262. var linePath = this.childOfName('line');
  263. setLinePoints(linePath.shape, points);
  264. linePath.dirty();
  265. };
  266. Line.prototype.beforeUpdate = function () {
  267. var lineGroup = this;
  268. var symbolFrom = lineGroup.childOfName('fromSymbol');
  269. var symbolTo = lineGroup.childOfName('toSymbol');
  270. var label = lineGroup.getTextContent();
  271. // Quick reject
  272. if (!symbolFrom && !symbolTo && (!label || label.ignore)) {
  273. return;
  274. }
  275. var invScale = 1;
  276. var parentNode = this.parent;
  277. while (parentNode) {
  278. if (parentNode.scaleX) {
  279. invScale /= parentNode.scaleX;
  280. }
  281. parentNode = parentNode.parent;
  282. }
  283. var line = lineGroup.childOfName('line');
  284. // If line not changed
  285. // FIXME Parent scale changed
  286. if (!this.__dirty && !line.__dirty) {
  287. return;
  288. }
  289. var percent = line.shape.percent;
  290. var fromPos = line.pointAt(0);
  291. var toPos = line.pointAt(percent);
  292. var d = vector.sub([], toPos, fromPos);
  293. vector.normalize(d, d);
  294. function setSymbolRotation(symbol, percent) {
  295. // Fix #12388
  296. // when symbol is set to be 'arrow' in markLine,
  297. // symbolRotate value will be ignored, and compulsively use tangent angle.
  298. // rotate by default if symbol rotation is not specified
  299. var specifiedRotation = symbol.__specifiedRotation;
  300. if (specifiedRotation == null) {
  301. var tangent = line.tangentAt(percent);
  302. symbol.attr('rotation', (percent === 1 ? -1 : 1) * Math.PI / 2 - Math.atan2(tangent[1], tangent[0]));
  303. } else {
  304. symbol.attr('rotation', specifiedRotation);
  305. }
  306. }
  307. if (symbolFrom) {
  308. symbolFrom.setPosition(fromPos);
  309. setSymbolRotation(symbolFrom, 0);
  310. symbolFrom.scaleX = symbolFrom.scaleY = invScale * percent;
  311. symbolFrom.markRedraw();
  312. }
  313. if (symbolTo) {
  314. symbolTo.setPosition(toPos);
  315. setSymbolRotation(symbolTo, 1);
  316. symbolTo.scaleX = symbolTo.scaleY = invScale * percent;
  317. symbolTo.markRedraw();
  318. }
  319. if (label && !label.ignore) {
  320. label.x = label.y = 0;
  321. label.originX = label.originY = 0;
  322. var textAlign = void 0;
  323. var textVerticalAlign = void 0;
  324. var distance = label.__labelDistance;
  325. var distanceX = distance[0] * invScale;
  326. var distanceY = distance[1] * invScale;
  327. var halfPercent = percent / 2;
  328. var tangent = line.tangentAt(halfPercent);
  329. var n = [tangent[1], -tangent[0]];
  330. var cp = line.pointAt(halfPercent);
  331. if (n[1] > 0) {
  332. n[0] = -n[0];
  333. n[1] = -n[1];
  334. }
  335. var dir = tangent[0] < 0 ? -1 : 1;
  336. if (label.__position !== 'start' && label.__position !== 'end') {
  337. var rotation = -Math.atan2(tangent[1], tangent[0]);
  338. if (toPos[0] < fromPos[0]) {
  339. rotation = Math.PI + rotation;
  340. }
  341. label.rotation = rotation;
  342. }
  343. var dy = void 0;
  344. switch (label.__position) {
  345. case 'insideStartTop':
  346. case 'insideMiddleTop':
  347. case 'insideEndTop':
  348. case 'middle':
  349. dy = -distanceY;
  350. textVerticalAlign = 'bottom';
  351. break;
  352. case 'insideStartBottom':
  353. case 'insideMiddleBottom':
  354. case 'insideEndBottom':
  355. dy = distanceY;
  356. textVerticalAlign = 'top';
  357. break;
  358. default:
  359. dy = 0;
  360. textVerticalAlign = 'middle';
  361. }
  362. switch (label.__position) {
  363. case 'end':
  364. label.x = d[0] * distanceX + toPos[0];
  365. label.y = d[1] * distanceY + toPos[1];
  366. textAlign = d[0] > 0.8 ? 'left' : d[0] < -0.8 ? 'right' : 'center';
  367. textVerticalAlign = d[1] > 0.8 ? 'top' : d[1] < -0.8 ? 'bottom' : 'middle';
  368. break;
  369. case 'start':
  370. label.x = -d[0] * distanceX + fromPos[0];
  371. label.y = -d[1] * distanceY + fromPos[1];
  372. textAlign = d[0] > 0.8 ? 'right' : d[0] < -0.8 ? 'left' : 'center';
  373. textVerticalAlign = d[1] > 0.8 ? 'bottom' : d[1] < -0.8 ? 'top' : 'middle';
  374. break;
  375. case 'insideStartTop':
  376. case 'insideStart':
  377. case 'insideStartBottom':
  378. label.x = distanceX * dir + fromPos[0];
  379. label.y = fromPos[1] + dy;
  380. textAlign = tangent[0] < 0 ? 'right' : 'left';
  381. label.originX = -distanceX * dir;
  382. label.originY = -dy;
  383. break;
  384. case 'insideMiddleTop':
  385. case 'insideMiddle':
  386. case 'insideMiddleBottom':
  387. case 'middle':
  388. label.x = cp[0];
  389. label.y = cp[1] + dy;
  390. textAlign = 'center';
  391. label.originY = -dy;
  392. break;
  393. case 'insideEndTop':
  394. case 'insideEnd':
  395. case 'insideEndBottom':
  396. label.x = -distanceX * dir + toPos[0];
  397. label.y = toPos[1] + dy;
  398. textAlign = tangent[0] >= 0 ? 'right' : 'left';
  399. label.originX = distanceX * dir;
  400. label.originY = -dy;
  401. break;
  402. }
  403. label.scaleX = label.scaleY = invScale;
  404. label.setStyle({
  405. // Use the user specified text align and baseline first
  406. verticalAlign: label.__verticalAlign || textVerticalAlign,
  407. align: label.__align || textAlign
  408. });
  409. }
  410. };
  411. return Line;
  412. }(graphic.Group);
  413. export default Line;