CandlestickView.js 12 KB

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