CandlestickView.js 10 KB

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