CandlestickView.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  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 * as zrUtil from 'zrender/lib/core/util.js';
  42. import ChartView from '../../view/Chart.js';
  43. import * as graphic from '../../util/graphic.js';
  44. import { setStatesStylesFromModel, toggleHoverEmphasis } from '../../util/states.js';
  45. import Path from 'zrender/lib/graphic/Path.js';
  46. import { createClipPath, SHAPE_CLIP_KIND_FULLY_CLIPPED, SHAPE_CLIP_KIND_NOT_CLIPPED, SHAPE_CLIP_KIND_PARTIALLY_CLIPPED, updateClipPath } from '../helper/createClipPathFromCoordSys.js';
  47. import { SERIES_TYPE_CANDLESTICK } from './CandlestickSeries.js';
  48. import { saveOldStyle } from '../../animation/basicTransition.js';
  49. import { getBorderColor, getColor } from './candlestickVisual.js';
  50. import { resolveNormalBoxClipping } from '../helper/whiskerBoxCommon.js';
  51. import { getIncrementalId } from '../../util/model.js';
  52. var SKIP_PROPS = ['color', 'borderColor'];
  53. var CandlestickView = /** @class */function (_super) {
  54. __extends(CandlestickView, _super);
  55. function CandlestickView() {
  56. var _this = _super !== null && _super.apply(this, arguments) || this;
  57. _this.type = CandlestickView.type;
  58. return _this;
  59. }
  60. CandlestickView.prototype.render = function (seriesModel, ecModel, api) {
  61. // If there is clipPath created in large mode. Remove it.
  62. this.group.removeClipPath();
  63. // Clear previously rendered progressive elements.
  64. this._progressiveEls = null;
  65. this._updateDrawMode(seriesModel);
  66. this._isLargeDraw ? this._renderLarge(seriesModel) : this._renderNormal(seriesModel);
  67. };
  68. CandlestickView.prototype.incrementalPrepareRender = function (seriesModel, ecModel, api) {
  69. this._clear();
  70. this._updateDrawMode(seriesModel);
  71. };
  72. CandlestickView.prototype.incrementalRender = function (params, seriesModel, ecModel, api) {
  73. this._progressiveEls = [];
  74. this._isLargeDraw ? this._incrementalRenderLarge(params, seriesModel) : this._incrementalRenderNormal(params, seriesModel);
  75. };
  76. CandlestickView.prototype.eachRendered = function (cb) {
  77. graphic.traverseElements(this._progressiveEls || this.group, cb);
  78. };
  79. CandlestickView.prototype._updateDrawMode = function (seriesModel) {
  80. var isLargeDraw = seriesModel.pipelineContext.large;
  81. if (this._isLargeDraw == null || isLargeDraw !== this._isLargeDraw) {
  82. this._isLargeDraw = isLargeDraw;
  83. this._clear();
  84. }
  85. };
  86. CandlestickView.prototype._renderNormal = function (seriesModel) {
  87. var data = seriesModel.getData();
  88. var oldData = this._data;
  89. var group = this.group;
  90. var isSimpleBox = data.getLayout('isSimpleBox');
  91. var needClip = seriesModel.get('clip', true);
  92. var coordSys = seriesModel.coordinateSystem;
  93. var clipArea = coordSys.getArea && coordSys.getArea();
  94. var clipPath = needClip && createClipPath(coordSys, false, seriesModel);
  95. // There is no old data only when first rendering or switching from
  96. // stream mode to normal mode, where previous elements should be removed.
  97. if (!this._data) {
  98. group.removeAll();
  99. }
  100. var transPointDim = getTransPointDimension(seriesModel);
  101. data.diff(oldData).add(function (newIdx) {
  102. if (data.hasValue(newIdx)) {
  103. var itemLayout = data.getItemLayout(newIdx);
  104. var clipKind = needClip ? resolveNormalBoxClipping(clipArea, itemLayout) : SHAPE_CLIP_KIND_NOT_CLIPPED;
  105. if (clipKind === SHAPE_CLIP_KIND_FULLY_CLIPPED) {
  106. return;
  107. }
  108. var el = createNormalBox(itemLayout, newIdx, transPointDim, true);
  109. graphic.initProps(el, {
  110. shape: {
  111. points: itemLayout.ends
  112. }
  113. }, seriesModel, newIdx);
  114. // When an item is partially inside and partially outside the Cartesian bounding rect,
  115. // the disappearance of the entire item may confuse users. Therefore, clipping is needed.
  116. // Consider performance of zr Element['clipPath'], only set to partially clipped elements.
  117. updateClipPath(clipKind === SHAPE_CLIP_KIND_PARTIALLY_CLIPPED, el, clipPath);
  118. setBoxCommon(el, data, newIdx, isSimpleBox);
  119. group.add(el);
  120. data.setItemGraphicEl(newIdx, el);
  121. }
  122. }).update(function (newIdx, oldIdx) {
  123. var el = oldData.getItemGraphicEl(oldIdx);
  124. // Empty data
  125. if (!data.hasValue(newIdx)) {
  126. group.remove(el);
  127. return;
  128. }
  129. var itemLayout = data.getItemLayout(newIdx);
  130. var clipKind = needClip ? resolveNormalBoxClipping(clipArea, itemLayout) : SHAPE_CLIP_KIND_NOT_CLIPPED;
  131. if (clipKind === SHAPE_CLIP_KIND_FULLY_CLIPPED) {
  132. group.remove(el);
  133. return;
  134. }
  135. if (!el) {
  136. el = createNormalBox(itemLayout, newIdx, transPointDim);
  137. } else {
  138. graphic.updateProps(el, {
  139. shape: {
  140. points: itemLayout.ends
  141. }
  142. }, seriesModel, newIdx);
  143. saveOldStyle(el);
  144. }
  145. setBoxCommon(el, data, newIdx, isSimpleBox);
  146. // See `updateClipPath` in `add`.
  147. updateClipPath(clipKind === SHAPE_CLIP_KIND_PARTIALLY_CLIPPED, el, clipPath);
  148. group.add(el);
  149. data.setItemGraphicEl(newIdx, el);
  150. }).remove(function (oldIdx) {
  151. var el = oldData.getItemGraphicEl(oldIdx);
  152. el && group.remove(el);
  153. }).execute();
  154. this._data = data;
  155. };
  156. CandlestickView.prototype._renderLarge = function (seriesModel) {
  157. this._clear();
  158. createLarge(seriesModel, this.group);
  159. var clipPath = seriesModel.get('clip', true) ? createClipPath(seriesModel.coordinateSystem, false, seriesModel) : null;
  160. updateClipPath(!!clipPath, this.group, clipPath);
  161. };
  162. CandlestickView.prototype._incrementalRenderNormal = function (params, seriesModel) {
  163. var data = seriesModel.getData();
  164. var isSimpleBox = data.getLayout('isSimpleBox');
  165. var transPointDim = getTransPointDimension(seriesModel);
  166. var dataIndex;
  167. while ((dataIndex = params.next()) != null) {
  168. var itemLayout = data.getItemLayout(dataIndex);
  169. var el = createNormalBox(itemLayout, dataIndex, transPointDim);
  170. setBoxCommon(el, data, dataIndex, isSimpleBox);
  171. el.incremental = getIncrementalId(seriesModel);
  172. this.group.add(el);
  173. this._progressiveEls.push(el);
  174. }
  175. };
  176. CandlestickView.prototype._incrementalRenderLarge = function (params, seriesModel) {
  177. createLarge(seriesModel, this.group, this._progressiveEls, true);
  178. };
  179. CandlestickView.prototype.remove = function (ecModel) {
  180. this._clear();
  181. };
  182. CandlestickView.prototype._clear = function () {
  183. this.group.removeAll();
  184. updateClipPath(false, this.group, null);
  185. this._data = null;
  186. };
  187. CandlestickView.type = SERIES_TYPE_CANDLESTICK;
  188. return CandlestickView;
  189. }(ChartView);
  190. var NormalBoxPathShape = /** @class */function () {
  191. function NormalBoxPathShape() {}
  192. return NormalBoxPathShape;
  193. }();
  194. var NormalBoxPath = /** @class */function (_super) {
  195. __extends(NormalBoxPath, _super);
  196. function NormalBoxPath(opts) {
  197. var _this = _super.call(this, opts) || this;
  198. _this.type = 'normalCandlestickBox';
  199. return _this;
  200. }
  201. NormalBoxPath.prototype.getDefaultShape = function () {
  202. return new NormalBoxPathShape();
  203. };
  204. NormalBoxPath.prototype.buildPath = function (ctx, shape) {
  205. var ends = shape.points;
  206. if (this.__simpleBox) {
  207. ctx.moveTo(ends[4][0], ends[4][1]);
  208. ctx.lineTo(ends[6][0], ends[6][1]);
  209. } else {
  210. ctx.moveTo(ends[0][0], ends[0][1]);
  211. ctx.lineTo(ends[1][0], ends[1][1]);
  212. ctx.lineTo(ends[2][0], ends[2][1]);
  213. ctx.lineTo(ends[3][0], ends[3][1]);
  214. ctx.closePath();
  215. ctx.moveTo(ends[4][0], ends[4][1]);
  216. ctx.lineTo(ends[5][0], ends[5][1]);
  217. ctx.moveTo(ends[6][0], ends[6][1]);
  218. ctx.lineTo(ends[7][0], ends[7][1]);
  219. }
  220. };
  221. return NormalBoxPath;
  222. }(Path);
  223. function createNormalBox(itemLayout, dataIndex, constDim, isInit) {
  224. var ends = itemLayout.ends;
  225. return new NormalBoxPath({
  226. shape: {
  227. points: isInit ? transInit(ends, constDim, itemLayout) : ends
  228. },
  229. z2: 100
  230. });
  231. }
  232. function setBoxCommon(el, data, dataIndex, isSimpleBox) {
  233. var itemModel = data.getItemModel(dataIndex);
  234. el.useStyle(data.getItemVisual(dataIndex, 'style'));
  235. el.style.strokeNoScale = true;
  236. var cursorStyle = itemModel.getShallow('cursor');
  237. cursorStyle && el.attr('cursor', cursorStyle);
  238. el.__simpleBox = isSimpleBox;
  239. setStatesStylesFromModel(el, itemModel);
  240. var sign = data.getItemLayout(dataIndex).sign;
  241. zrUtil.each(el.states, function (state, stateName) {
  242. var stateModel = itemModel.getModel(stateName);
  243. var color = getColor(sign, stateModel);
  244. var borderColor = getBorderColor(sign, stateModel) || color;
  245. var stateStyle = state.style || (state.style = {});
  246. color && (stateStyle.fill = color);
  247. borderColor && (stateStyle.stroke = borderColor);
  248. });
  249. var emphasisModel = itemModel.getModel('emphasis');
  250. toggleHoverEmphasis(el, emphasisModel.get('focus'), emphasisModel.get('blurScope'), emphasisModel.get('disabled'));
  251. }
  252. function transInit(points, dim, itemLayout) {
  253. return zrUtil.map(points, function (point) {
  254. point = point.slice();
  255. point[dim] = itemLayout.initBaseline;
  256. return point;
  257. });
  258. }
  259. function getTransPointDimension(seriesModel) {
  260. return seriesModel.getWhiskerBoxesLayout() === 'horizontal' ? 1 : 0;
  261. }
  262. var LargeBoxPathShape = /** @class */function () {
  263. function LargeBoxPathShape() {}
  264. return LargeBoxPathShape;
  265. }();
  266. var LargeBoxPath = /** @class */function (_super) {
  267. __extends(LargeBoxPath, _super);
  268. function LargeBoxPath(opts) {
  269. var _this = _super.call(this, opts) || this;
  270. _this.type = 'largeCandlestickBox';
  271. return _this;
  272. }
  273. LargeBoxPath.prototype.getDefaultShape = function () {
  274. return new LargeBoxPathShape();
  275. };
  276. LargeBoxPath.prototype.buildPath = function (ctx, shape) {
  277. // Drawing lines is more efficient than drawing
  278. // a whole line or drawing rects.
  279. var points = shape.points;
  280. for (var i = 0; i < points.length;) {
  281. if (this.__sign === points[i++]) {
  282. var x = points[i++];
  283. ctx.moveTo(x, points[i++]);
  284. ctx.lineTo(x, points[i++]);
  285. } else {
  286. i += 3;
  287. }
  288. }
  289. };
  290. return LargeBoxPath;
  291. }(Path);
  292. function createLarge(seriesModel, group, progressiveEls, incremental) {
  293. var data = seriesModel.getData();
  294. var largePoints = data.getLayout('largePoints');
  295. var elP = new LargeBoxPath({
  296. shape: {
  297. points: largePoints
  298. },
  299. __sign: 1,
  300. ignoreCoarsePointer: true
  301. });
  302. group.add(elP);
  303. var elN = new LargeBoxPath({
  304. shape: {
  305. points: largePoints
  306. },
  307. __sign: -1,
  308. ignoreCoarsePointer: true
  309. });
  310. group.add(elN);
  311. var elDoji = new LargeBoxPath({
  312. shape: {
  313. points: largePoints
  314. },
  315. __sign: 0,
  316. ignoreCoarsePointer: true
  317. });
  318. group.add(elDoji);
  319. setLargeStyle(1, elP, seriesModel, data);
  320. setLargeStyle(-1, elN, seriesModel, data);
  321. setLargeStyle(0, elDoji, seriesModel, data);
  322. if (incremental) {
  323. elP.incremental = getIncrementalId(seriesModel);
  324. elN.incremental = getIncrementalId(seriesModel);
  325. elDoji.incremental = getIncrementalId(seriesModel);
  326. }
  327. if (progressiveEls) {
  328. progressiveEls.push(elP, elN, elDoji);
  329. }
  330. }
  331. function setLargeStyle(sign, el, seriesModel, data) {
  332. // TODO put in visual?
  333. var borderColor = getBorderColor(sign, seriesModel)
  334. // Use color for border color by default.
  335. || getColor(sign, seriesModel);
  336. // Color must be excluded.
  337. // Because symbol provide setColor individually to set fill and stroke
  338. var itemStyle = seriesModel.getModel('itemStyle').getItemStyle(SKIP_PROPS);
  339. el.useStyle(itemStyle);
  340. el.style.fill = null;
  341. el.style.stroke = borderColor;
  342. var cursorStyle = seriesModel.get('cursor');
  343. cursorStyle && el.attr('cursor', cursorStyle);
  344. }
  345. export default CandlestickView;