candlestickLayout.js 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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. var _graphic = require("../../util/graphic");
  20. var subPixelOptimize = _graphic.subPixelOptimize;
  21. var createRenderPlanner = require("../helper/createRenderPlanner");
  22. var _number = require("../../util/number");
  23. var parsePercent = _number.parsePercent;
  24. var _util = require("zrender/lib/core/util");
  25. var retrieve2 = _util.retrieve2;
  26. /*
  27. * Licensed to the Apache Software Foundation (ASF) under one
  28. * or more contributor license agreements. See the NOTICE file
  29. * distributed with this work for additional information
  30. * regarding copyright ownership. The ASF licenses this file
  31. * to you under the Apache License, Version 2.0 (the
  32. * "License"); you may not use this file except in compliance
  33. * with the License. You may obtain a copy of the License at
  34. *
  35. * http://www.apache.org/licenses/LICENSE-2.0
  36. *
  37. * Unless required by applicable law or agreed to in writing,
  38. * software distributed under the License is distributed on an
  39. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  40. * KIND, either express or implied. See the License for the
  41. * specific language governing permissions and limitations
  42. * under the License.
  43. */
  44. /* global Float32Array */
  45. var LargeArr = typeof Float32Array !== 'undefined' ? Float32Array : Array;
  46. var _default = {
  47. seriesType: 'candlestick',
  48. plan: createRenderPlanner(),
  49. reset: function (seriesModel) {
  50. var coordSys = seriesModel.coordinateSystem;
  51. var data = seriesModel.getData();
  52. var candleWidth = calculateCandleWidth(seriesModel, data);
  53. var cDimIdx = 0;
  54. var vDimIdx = 1;
  55. var coordDims = ['x', 'y'];
  56. var cDim = data.mapDimension(coordDims[cDimIdx]);
  57. var vDims = data.mapDimension(coordDims[vDimIdx], true);
  58. var openDim = vDims[0];
  59. var closeDim = vDims[1];
  60. var lowestDim = vDims[2];
  61. var highestDim = vDims[3];
  62. data.setLayout({
  63. candleWidth: candleWidth,
  64. // The value is experimented visually.
  65. isSimpleBox: candleWidth <= 1.3
  66. });
  67. if (cDim == null || vDims.length < 4) {
  68. return;
  69. }
  70. return {
  71. progress: seriesModel.pipelineContext.large ? largeProgress : normalProgress
  72. };
  73. function normalProgress(params, data) {
  74. var dataIndex;
  75. while ((dataIndex = params.next()) != null) {
  76. var axisDimVal = data.get(cDim, dataIndex);
  77. var openVal = data.get(openDim, dataIndex);
  78. var closeVal = data.get(closeDim, dataIndex);
  79. var lowestVal = data.get(lowestDim, dataIndex);
  80. var highestVal = data.get(highestDim, 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. data.setItemLayout(dataIndex, {
  92. sign: getSign(data, dataIndex, openVal, closeVal, closeDim),
  93. initBaseline: openVal > closeVal ? ocHighPoint[vDimIdx] : ocLowPoint[vDimIdx],
  94. // open point.
  95. ends: ends,
  96. brushRect: makeBrushRect(lowestVal, highestVal, axisDimVal)
  97. });
  98. }
  99. function getPoint(val, axisDimVal) {
  100. var p = [];
  101. p[cDimIdx] = axisDimVal;
  102. p[vDimIdx] = val;
  103. return isNaN(axisDimVal) || isNaN(val) ? [NaN, NaN] : coordSys.dataToPoint(p);
  104. }
  105. function addBodyEnd(ends, point, start) {
  106. var point1 = point.slice();
  107. var point2 = point.slice();
  108. point1[cDimIdx] = subPixelOptimize(point1[cDimIdx] + candleWidth / 2, 1, false);
  109. point2[cDimIdx] = subPixelOptimize(point2[cDimIdx] - candleWidth / 2, 1, true);
  110. start ? ends.push(point1, point2) : ends.push(point2, point1);
  111. }
  112. function makeBrushRect(lowestVal, highestVal, axisDimVal) {
  113. var pmin = getPoint(lowestVal, axisDimVal);
  114. var pmax = getPoint(highestVal, axisDimVal);
  115. pmin[cDimIdx] -= candleWidth / 2;
  116. pmax[cDimIdx] -= candleWidth / 2;
  117. return {
  118. x: pmin[0],
  119. y: pmin[1],
  120. width: vDimIdx ? candleWidth : pmax[0] - pmin[0],
  121. height: vDimIdx ? pmax[1] - pmin[1] : candleWidth
  122. };
  123. }
  124. function subPixelOptimizePoint(point) {
  125. point[cDimIdx] = subPixelOptimize(point[cDimIdx], 1);
  126. return point;
  127. }
  128. }
  129. function largeProgress(params, data) {
  130. // Structure: [sign, x, yhigh, ylow, sign, x, yhigh, ylow, ...]
  131. var points = new LargeArr(params.count * 4);
  132. var offset = 0;
  133. var point;
  134. var tmpIn = [];
  135. var tmpOut = [];
  136. var dataIndex;
  137. while ((dataIndex = params.next()) != null) {
  138. var axisDimVal = data.get(cDim, dataIndex);
  139. var openVal = data.get(openDim, dataIndex);
  140. var closeVal = data.get(closeDim, dataIndex);
  141. var lowestVal = data.get(lowestDim, dataIndex);
  142. var highestVal = data.get(highestDim, dataIndex);
  143. if (isNaN(axisDimVal) || isNaN(lowestVal) || isNaN(highestVal)) {
  144. points[offset++] = NaN;
  145. offset += 3;
  146. continue;
  147. }
  148. points[offset++] = getSign(data, dataIndex, openVal, closeVal, closeDim);
  149. tmpIn[cDimIdx] = axisDimVal;
  150. tmpIn[vDimIdx] = lowestVal;
  151. point = coordSys.dataToPoint(tmpIn, null, tmpOut);
  152. points[offset++] = point ? point[0] : NaN;
  153. points[offset++] = point ? point[1] : NaN;
  154. tmpIn[vDimIdx] = highestVal;
  155. point = coordSys.dataToPoint(tmpIn, null, tmpOut);
  156. points[offset++] = point ? point[1] : NaN;
  157. }
  158. data.setLayout('largePoints', points);
  159. }
  160. }
  161. };
  162. function getSign(data, dataIndex, openVal, closeVal, closeDim) {
  163. var sign;
  164. if (openVal > closeVal) {
  165. sign = -1;
  166. } else if (openVal < closeVal) {
  167. sign = 1;
  168. } else {
  169. sign = dataIndex > 0 // If close === open, compare with close of last record
  170. ? data.get(closeDim, dataIndex - 1) <= closeVal ? 1 : -1 : // No record of previous, set to be positive
  171. 1;
  172. }
  173. return sign;
  174. }
  175. function calculateCandleWidth(seriesModel, data) {
  176. var baseAxis = seriesModel.getBaseAxis();
  177. var extent;
  178. var bandWidth = baseAxis.type === 'category' ? baseAxis.getBandWidth() : (extent = baseAxis.getExtent(), Math.abs(extent[1] - extent[0]) / data.count());
  179. var barMaxWidth = parsePercent(retrieve2(seriesModel.get('barMaxWidth'), bandWidth), bandWidth);
  180. var barMinWidth = parsePercent(retrieve2(seriesModel.get('barMinWidth'), 1), bandWidth);
  181. var barWidth = seriesModel.get('barWidth');
  182. return barWidth != null ? parsePercent(barWidth, bandWidth) // Put max outer to ensure bar visible in spite of overlap.
  183. : Math.max(Math.min(bandWidth / 2, barMaxWidth), barMinWidth);
  184. }
  185. module.exports = _default;