Line.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  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';
  42. import * as vector from 'zrender/lib/core/vector';
  43. import * as symbolUtil from '../../util/symbol';
  44. import ECLinePath from './LinePath';
  45. import * as graphic from '../../util/graphic';
  46. import { enableHoverEmphasis, enterEmphasis, leaveEmphasis, SPECIAL_STATES } from '../../util/states';
  47. import { getLabelStatesModels, setLabelStyle } from '../../label/labelStyle';
  48. import { round } from '../../util/number';
  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; // Optimization for large dataset
  156. if (!seriesScope || lineData.hasItemOption) {
  157. var itemModel = lineData.getItemModel(idx);
  158. emphasisLineStyle = itemModel.getModel(['emphasis', 'lineStyle']).getLineStyle();
  159. blurLineStyle = itemModel.getModel(['blur', 'lineStyle']).getLineStyle();
  160. selectLineStyle = itemModel.getModel(['select', 'lineStyle']).getLineStyle();
  161. labelStatesModels = getLabelStatesModels(itemModel);
  162. }
  163. var lineStyle = lineData.getItemVisual(idx, 'style');
  164. var visualColor = lineStyle.stroke;
  165. line.useStyle(lineStyle);
  166. line.style.fill = null;
  167. line.style.strokeNoScale = true;
  168. line.ensureState('emphasis').style = emphasisLineStyle;
  169. line.ensureState('blur').style = blurLineStyle;
  170. line.ensureState('select').style = selectLineStyle; // Update symbol
  171. each(SYMBOL_CATEGORIES, function (symbolCategory) {
  172. var symbol = this.childOfName(symbolCategory);
  173. if (symbol) {
  174. // Share opacity and color with line.
  175. symbol.setColor(visualColor);
  176. symbol.style.opacity = lineStyle.opacity;
  177. for (var i = 0; i < SPECIAL_STATES.length; i++) {
  178. var stateName = SPECIAL_STATES[i];
  179. var lineState = line.getState(stateName);
  180. if (lineState) {
  181. var lineStateStyle = lineState.style || {};
  182. var state = symbol.ensureState(stateName);
  183. var stateStyle = state.style || (state.style = {});
  184. if (lineStateStyle.stroke != null) {
  185. stateStyle[symbol.__isEmptyBrush ? 'stroke' : 'fill'] = lineStateStyle.stroke;
  186. }
  187. if (lineStateStyle.opacity != null) {
  188. stateStyle.opacity = lineStateStyle.opacity;
  189. }
  190. }
  191. }
  192. symbol.markRedraw();
  193. }
  194. }, this);
  195. var rawVal = seriesModel.getRawValue(idx);
  196. setLabelStyle(this, labelStatesModels, {
  197. labelDataIndex: idx,
  198. labelFetcher: {
  199. getFormattedLabel: function (dataIndex, stateName) {
  200. return seriesModel.getFormattedLabel(dataIndex, stateName, lineData.dataType);
  201. }
  202. },
  203. inheritColor: visualColor || '#000',
  204. defaultOpacity: lineStyle.opacity,
  205. defaultText: (rawVal == null ? lineData.getName(idx) : isFinite(rawVal) ? round(rawVal) : rawVal) + ''
  206. });
  207. var label = this.getTextContent(); // Always set `textStyle` even if `normalStyle.text` is null, because default
  208. // values have to be set on `normalStyle`.
  209. if (label) {
  210. var labelNormalModel = labelStatesModels.normal;
  211. label.__align = label.style.align;
  212. label.__verticalAlign = label.style.verticalAlign; // 'start', 'middle', 'end'
  213. label.__position = labelNormalModel.get('position') || 'middle';
  214. var distance = labelNormalModel.get('distance');
  215. if (!isArray(distance)) {
  216. distance = [distance, distance];
  217. }
  218. label.__labelDistance = distance;
  219. }
  220. this.setTextConfig({
  221. position: null,
  222. local: true,
  223. inside: false // Can't be inside for stroke element.
  224. });
  225. enableHoverEmphasis(this);
  226. };
  227. Line.prototype.highlight = function () {
  228. enterEmphasis(this);
  229. };
  230. Line.prototype.downplay = function () {
  231. leaveEmphasis(this);
  232. };
  233. Line.prototype.updateLayout = function (lineData, idx) {
  234. this.setLinePoints(lineData.getItemLayout(idx));
  235. };
  236. Line.prototype.setLinePoints = function (points) {
  237. var linePath = this.childOfName('line');
  238. setLinePoints(linePath.shape, points);
  239. linePath.dirty();
  240. };
  241. Line.prototype.beforeUpdate = function () {
  242. var lineGroup = this;
  243. var symbolFrom = lineGroup.childOfName('fromSymbol');
  244. var symbolTo = lineGroup.childOfName('toSymbol');
  245. var label = lineGroup.getTextContent(); // Quick reject
  246. if (!symbolFrom && !symbolTo && (!label || label.ignore)) {
  247. return;
  248. }
  249. var invScale = 1;
  250. var parentNode = this.parent;
  251. while (parentNode) {
  252. if (parentNode.scaleX) {
  253. invScale /= parentNode.scaleX;
  254. }
  255. parentNode = parentNode.parent;
  256. }
  257. var line = lineGroup.childOfName('line'); // If line not changed
  258. // FIXME Parent scale changed
  259. if (!this.__dirty && !line.__dirty) {
  260. return;
  261. }
  262. var percent = line.shape.percent;
  263. var fromPos = line.pointAt(0);
  264. var toPos = line.pointAt(percent);
  265. var d = vector.sub([], toPos, fromPos);
  266. vector.normalize(d, d);
  267. function setSymbolRotation(symbol, percent) {
  268. // Fix #12388
  269. // when symbol is set to be 'arrow' in markLine,
  270. // symbolRotate value will be ignored, and compulsively use tangent angle.
  271. // rotate by default if symbol rotation is not specified
  272. var specifiedRotation = symbol.__specifiedRotation;
  273. if (specifiedRotation == null) {
  274. var tangent = line.tangentAt(percent);
  275. symbol.attr('rotation', (percent === 1 ? -1 : 1) * Math.PI / 2 - Math.atan2(tangent[1], tangent[0]));
  276. } else {
  277. symbol.attr('rotation', specifiedRotation);
  278. }
  279. }
  280. if (symbolFrom) {
  281. symbolFrom.setPosition(fromPos);
  282. setSymbolRotation(symbolFrom, 0);
  283. symbolFrom.scaleX = symbolFrom.scaleY = invScale * percent;
  284. symbolFrom.markRedraw();
  285. }
  286. if (symbolTo) {
  287. symbolTo.setPosition(toPos);
  288. setSymbolRotation(symbolTo, 1);
  289. symbolTo.scaleX = symbolTo.scaleY = invScale * percent;
  290. symbolTo.markRedraw();
  291. }
  292. if (label && !label.ignore) {
  293. label.x = label.y = 0;
  294. label.originX = label.originY = 0;
  295. var textAlign = void 0;
  296. var textVerticalAlign = void 0;
  297. var distance = label.__labelDistance;
  298. var distanceX = distance[0] * invScale;
  299. var distanceY = distance[1] * invScale;
  300. var halfPercent = percent / 2;
  301. var tangent = line.tangentAt(halfPercent);
  302. var n = [tangent[1], -tangent[0]];
  303. var cp = line.pointAt(halfPercent);
  304. if (n[1] > 0) {
  305. n[0] = -n[0];
  306. n[1] = -n[1];
  307. }
  308. var dir = tangent[0] < 0 ? -1 : 1;
  309. if (label.__position !== 'start' && label.__position !== 'end') {
  310. var rotation = -Math.atan2(tangent[1], tangent[0]);
  311. if (toPos[0] < fromPos[0]) {
  312. rotation = Math.PI + rotation;
  313. }
  314. label.rotation = rotation;
  315. }
  316. var dy = void 0;
  317. switch (label.__position) {
  318. case 'insideStartTop':
  319. case 'insideMiddleTop':
  320. case 'insideEndTop':
  321. case 'middle':
  322. dy = -distanceY;
  323. textVerticalAlign = 'bottom';
  324. break;
  325. case 'insideStartBottom':
  326. case 'insideMiddleBottom':
  327. case 'insideEndBottom':
  328. dy = distanceY;
  329. textVerticalAlign = 'top';
  330. break;
  331. default:
  332. dy = 0;
  333. textVerticalAlign = 'middle';
  334. }
  335. switch (label.__position) {
  336. case 'end':
  337. label.x = d[0] * distanceX + toPos[0];
  338. label.y = d[1] * distanceY + toPos[1];
  339. textAlign = d[0] > 0.8 ? 'left' : d[0] < -0.8 ? 'right' : 'center';
  340. textVerticalAlign = d[1] > 0.8 ? 'top' : d[1] < -0.8 ? 'bottom' : 'middle';
  341. break;
  342. case 'start':
  343. label.x = -d[0] * distanceX + fromPos[0];
  344. label.y = -d[1] * distanceY + fromPos[1];
  345. textAlign = d[0] > 0.8 ? 'right' : d[0] < -0.8 ? 'left' : 'center';
  346. textVerticalAlign = d[1] > 0.8 ? 'bottom' : d[1] < -0.8 ? 'top' : 'middle';
  347. break;
  348. case 'insideStartTop':
  349. case 'insideStart':
  350. case 'insideStartBottom':
  351. label.x = distanceX * dir + fromPos[0];
  352. label.y = fromPos[1] + dy;
  353. textAlign = tangent[0] < 0 ? 'right' : 'left';
  354. label.originX = -distanceX * dir;
  355. label.originY = -dy;
  356. break;
  357. case 'insideMiddleTop':
  358. case 'insideMiddle':
  359. case 'insideMiddleBottom':
  360. case 'middle':
  361. label.x = cp[0];
  362. label.y = cp[1] + dy;
  363. textAlign = 'center';
  364. label.originY = -dy;
  365. break;
  366. case 'insideEndTop':
  367. case 'insideEnd':
  368. case 'insideEndBottom':
  369. label.x = -distanceX * dir + toPos[0];
  370. label.y = toPos[1] + dy;
  371. textAlign = tangent[0] >= 0 ? 'right' : 'left';
  372. label.originX = distanceX * dir;
  373. label.originY = -dy;
  374. break;
  375. }
  376. label.scaleX = label.scaleY = invScale;
  377. label.setStyle({
  378. // Use the user specified text align and baseline first
  379. verticalAlign: label.__verticalAlign || textVerticalAlign,
  380. align: label.__align || textAlign
  381. });
  382. }
  383. };
  384. return Line;
  385. }(graphic.Group);
  386. export default Line;