CandlestickView.js 11 KB

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