Line.js 15 KB

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