candlestickLayout.js 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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 { subPixelOptimize } from '../../util/graphic.js';
  41. import createRenderPlanner from '../helper/createRenderPlanner.js';
  42. import { parsePercent } from '../../util/number.js';
  43. import { map, retrieve2 } from 'zrender/lib/core/util.js';
  44. import { createFloat32Array } from '../../util/vendor.js';
  45. var candlestickLayout = {
  46. seriesType: 'candlestick',
  47. plan: createRenderPlanner(),
  48. reset: function (seriesModel) {
  49. var coordSys = seriesModel.coordinateSystem;
  50. var data = seriesModel.getData();
  51. var candleWidth = calculateCandleWidth(seriesModel, data);
  52. var cDimIdx = 0;
  53. var vDimIdx = 1;
  54. var coordDims = ['x', 'y'];
  55. var cDimI = data.getDimensionIndex(data.mapDimension(coordDims[cDimIdx]));
  56. var vDimsI = map(data.mapDimensionsAll(coordDims[vDimIdx]), data.getDimensionIndex, data);
  57. var openDimI = vDimsI[0];
  58. var closeDimI = vDimsI[1];
  59. var lowestDimI = vDimsI[2];
  60. var highestDimI = vDimsI[3];
  61. data.setLayout({
  62. candleWidth: candleWidth,
  63. // The value is experimented visually.
  64. isSimpleBox: candleWidth <= 1.3
  65. });
  66. if (cDimI < 0 || vDimsI.length < 4) {
  67. return;
  68. }
  69. return {
  70. progress: seriesModel.pipelineContext.large ? largeProgress : normalProgress
  71. };
  72. function normalProgress(params, data) {
  73. var dataIndex;
  74. var store = data.getStore();
  75. while ((dataIndex = params.next()) != null) {
  76. var axisDimVal = store.get(cDimI, dataIndex);
  77. var openVal = store.get(openDimI, dataIndex);
  78. var closeVal = store.get(closeDimI, dataIndex);
  79. var lowestVal = store.get(lowestDimI, dataIndex);
  80. var highestVal = store.get(highestDimI, dataIndex);
  81. var ocLow = Math.min(openVal, closeVal);
  82. var ocHigh = Math.max(openVal, closeVal);
  83. var ocLowPoint = getPoint(ocLow, axisDimVal);
  84. var ocHighPoint = getPoint(ocHigh, axisDimVal);
  85. var lowestPoint = getPoint(lowestVal, axisDimVal);
  86. var highestPoint = getPoint(highestVal, axisDimVal);
  87. var ends = [];
  88. addBodyEnd(ends, ocHighPoint, 0);
  89. addBodyEnd(ends, ocLowPoint, 1);
  90. ends.push(subPixelOptimizePoint(highestPoint), subPixelOptimizePoint(ocHighPoint), subPixelOptimizePoint(lowestPoint), subPixelOptimizePoint(ocLowPoint));
  91. var itemModel = data.getItemModel(dataIndex);
  92. var hasDojiColor = !!itemModel.get(['itemStyle', 'borderColorDoji']);
  93. data.setItemLayout(dataIndex, {
  94. sign: getSign(store, dataIndex, openVal, closeVal, closeDimI, hasDojiColor),
  95. initBaseline: openVal > closeVal ? ocHighPoint[vDimIdx] : ocLowPoint[vDimIdx],
  96. ends: ends,
  97. brushRect: makeBrushRect(lowestVal, highestVal, axisDimVal)
  98. });
  99. }
  100. function getPoint(val, axisDimVal) {
  101. var p = [];
  102. p[cDimIdx] = axisDimVal;
  103. p[vDimIdx] = val;
  104. return isNaN(axisDimVal) || isNaN(val) ? [NaN, NaN] : coordSys.dataToPoint(p);
  105. }
  106. function addBodyEnd(ends, point, start) {
  107. var point1 = point.slice();
  108. var point2 = point.slice();
  109. point1[cDimIdx] = subPixelOptimize(point1[cDimIdx] + candleWidth / 2, 1, false);
  110. point2[cDimIdx] = subPixelOptimize(point2[cDimIdx] - candleWidth / 2, 1, true);
  111. start ? ends.push(point1, point2) : ends.push(point2, point1);
  112. }
  113. function makeBrushRect(lowestVal, highestVal, axisDimVal) {
  114. var pmin = getPoint(lowestVal, axisDimVal);
  115. var pmax = getPoint(highestVal, axisDimVal);
  116. pmin[cDimIdx] -= candleWidth / 2;
  117. pmax[cDimIdx] -= candleWidth / 2;
  118. return {
  119. x: pmin[0],
  120. y: pmin[1],
  121. width: vDimIdx ? candleWidth : pmax[0] - pmin[0],
  122. height: vDimIdx ? pmax[1] - pmin[1] : candleWidth
  123. };
  124. }
  125. function subPixelOptimizePoint(point) {
  126. point[cDimIdx] = subPixelOptimize(point[cDimIdx], 1);
  127. return point;
  128. }
  129. }
  130. function largeProgress(params, data) {
  131. // Structure: [sign, x, yhigh, ylow, sign, x, yhigh, ylow, ...]
  132. var points = createFloat32Array(params.count * 4);
  133. var offset = 0;
  134. var point;
  135. var tmpIn = [];
  136. var tmpOut = [];
  137. var dataIndex;
  138. var store = data.getStore();
  139. var hasDojiColor = !!seriesModel.get(['itemStyle', 'borderColorDoji']);
  140. while ((dataIndex = params.next()) != null) {
  141. var axisDimVal = store.get(cDimI, dataIndex);
  142. var openVal = store.get(openDimI, dataIndex);
  143. var closeVal = store.get(closeDimI, dataIndex);
  144. var lowestVal = store.get(lowestDimI, dataIndex);
  145. var highestVal = store.get(highestDimI, dataIndex);
  146. if (isNaN(axisDimVal) || isNaN(lowestVal) || isNaN(highestVal)) {
  147. points[offset++] = NaN;
  148. offset += 3;
  149. continue;
  150. }
  151. points[offset++] = getSign(store, dataIndex, openVal, closeVal, closeDimI, hasDojiColor);
  152. tmpIn[cDimIdx] = axisDimVal;
  153. tmpIn[vDimIdx] = lowestVal;
  154. point = coordSys.dataToPoint(tmpIn, null, tmpOut);
  155. points[offset++] = point ? point[0] : NaN;
  156. points[offset++] = point ? point[1] : NaN;
  157. tmpIn[vDimIdx] = highestVal;
  158. point = coordSys.dataToPoint(tmpIn, null, tmpOut);
  159. points[offset++] = point ? point[1] : NaN;
  160. }
  161. data.setLayout('largePoints', points);
  162. }
  163. }
  164. };
  165. /**
  166. * Get the sign of a single data.
  167. *
  168. * @returns 0 for doji with hasDojiColor: true,
  169. * 1 for positive,
  170. * -1 for negative.
  171. */
  172. function getSign(store, dataIndex, openVal, closeVal, closeDimI, hasDojiColor) {
  173. var sign;
  174. if (openVal > closeVal) {
  175. sign = -1;
  176. } else if (openVal < closeVal) {
  177. sign = 1;
  178. } else {
  179. sign = hasDojiColor
  180. // When doji color is set, use it instead of color/color0.
  181. ? 0 : dataIndex > 0
  182. // If close === open, compare with close of last record
  183. ? store.get(closeDimI, dataIndex - 1) <= closeVal ? 1 : -1
  184. // No record of previous, set to be positive
  185. : 1;
  186. }
  187. return sign;
  188. }
  189. function calculateCandleWidth(seriesModel, data) {
  190. var baseAxis = seriesModel.getBaseAxis();
  191. var extent;
  192. var bandWidth = baseAxis.type === 'category' ? baseAxis.getBandWidth() : (extent = baseAxis.getExtent(), Math.abs(extent[1] - extent[0]) / data.count());
  193. var barMaxWidth = parsePercent(retrieve2(seriesModel.get('barMaxWidth'), bandWidth), bandWidth);
  194. var barMinWidth = parsePercent(retrieve2(seriesModel.get('barMinWidth'), 1), bandWidth);
  195. var barWidth = seriesModel.get('barWidth');
  196. return barWidth != null ? parsePercent(barWidth, bandWidth)
  197. // Put max outer to ensure bar visible in spite of overlap.
  198. : Math.max(Math.min(bandWidth / 2, barMaxWidth), barMinWidth);
  199. }
  200. export default candlestickLayout;