candlestickLayout.js 9.2 KB

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