CandlestickView.js 11 KB

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