index.js 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064
  1. import React from 'react';
  2. import * as d3Scale from 'd3-scale';
  3. import * as d3Array from 'd3-array';
  4. import * as d3Color from 'd3-color';
  5. import * as d3Format from 'd3-format';
  6. import * as d3Interpolate from 'd3-interpolate';
  7. import * as d3Shape from 'd3-shape';
  8. import * as d3Timer from 'd3-timer';
  9. import * as d3Ease from 'd3-ease';
  10. import {
  11. extractBidPrices,
  12. extractBidVolumes,
  13. extractMaxTradedVolume,
  14. extractAvgTradedVolume,
  15. extractMaxVolume,
  16. zoomTimeFormat,
  17. extractMaxAskBidVolume
  18. } from "./utils";
  19. export const d3 = Object.assign(
  20. Object.assign(
  21. Object.assign({}, d3Scale, d3Array, d3Color)
  22. , d3Format, d3Interpolate, d3Shape
  23. )
  24. , d3Ease, d3Timer
  25. );
  26. /**
  27. * Stock Heatmap
  28. *
  29. */
  30. export default class StockHeatmap extends React.Component {
  31. /** @type {React.RefObject<HTMLCanvasElement>} */
  32. canvasRef = React.createRef();
  33. /** @type {CanvasRenderingContext2D} */
  34. drawingContext = null;
  35. data = [];
  36. windowedData = [];
  37. windowLength = 20;
  38. windowPosition = 0;
  39. isMerged = false;
  40. mouse = {
  41. x: 0,
  42. y: 0
  43. }
  44. circles = [];
  45. maxBidAskVolume = 0;
  46. orderbookColors = [
  47. '#086892',
  48. '#2f9dd2',
  49. '#fffc19',
  50. '#ff9a01',
  51. ];
  52. /** Default Theme colors and dimensions */
  53. defaults = {
  54. borderPadding: [5, 5, 0, 0],
  55. bidAskWidth: 100,
  56. axisYWidth: 50,
  57. axisXHeight: 50,
  58. buyColor: '#388e3c',
  59. textOnBuyColor: '#ffffff',
  60. sellColor: '#d32f2f',
  61. textOnSellColor: '#ffffff',
  62. textOnBackground: '#000000',
  63. textHighlightOnBackground: '#ff0000',
  64. tradeColor: '#7434eb',
  65. axisTickSize: 6,
  66. axisColor: '#000000',
  67. xAxisTextPadding: 6,
  68. yAxisTextPadding: 6,
  69. bidAskGraphPaddingLeft: 10,
  70. bidAskTransitionDuration: 500,
  71. volumeCircleMaxRadius: 20,
  72. runningRatioSeconds: 5,
  73. hmWidth: () => (this.props.width - this.defaults.borderPadding[1] - this.defaults.borderPadding[3] - this.defaults.bidAskWidth - this.defaults.axisYWidth),
  74. hmHeight: () => (this.props.height - this.defaults.borderPadding[0] - this.defaults.borderPadding[2] - this.defaults.axisXHeight),
  75. clearColor: '#ffffff',
  76. };
  77. shouldComponentUpdate(nextProps, nextState) {
  78. // console.log('shouldComponentUpdate', nextProps);
  79. const shouldUpdate = this.props.width !== nextProps.width
  80. || this.props.height !== nextProps.height;
  81. if (shouldUpdate) {
  82. this.detachMouseListeners();
  83. }
  84. return shouldUpdate;
  85. }
  86. // -------------------START:: Lifecycle methods to retrive 2d context from updated dom-------------------------
  87. componentDidMount() {
  88. // console.log('component mouted');
  89. if (this.canvasRef.current !== null) {
  90. this.drawingContext = this.canvasRef.current.getContext('2d');
  91. this.updateHeatmap();
  92. this.attachMouseListeners();
  93. }
  94. const panel = this
  95. setInterval(() => {
  96. panel.updateHeatmap()
  97. }, 10)
  98. }
  99. componentDidUpdate() {
  100. // console.log('component updtated');
  101. if (this.canvasRef.current !== null) {
  102. this.drawingContext = this.canvasRef.current.getContext('2d');
  103. this.updateHeatmap();
  104. this.attachMouseListeners();
  105. }
  106. }
  107. componentWillUnmount() {
  108. this.detachMouseListeners();
  109. }
  110. // -------------------END:: Lifecycle methods to retrive 2d context from updated dom---------------------------
  111. // ------------------ START:: Mouse Event listeners -------------------
  112. isMouseDown = false;
  113. mouseDownX = 0;
  114. /**
  115. * Attaches mouse interaction event listeners
  116. */
  117. attachMouseListeners = () => {
  118. if (this.canvasRef.current !== null) {
  119. this.canvasRef.current.addEventListener('mousedown', this.eventMouseDown);
  120. this.canvasRef.current.addEventListener('mousemove', this.eventMouseMove);
  121. this.canvasRef.current.addEventListener('mouseup', this.eventMouseUp);
  122. this.canvasRef.current.addEventListener('wheel', this.eventZoomWheel);
  123. this.canvasRef.current.addEventListener('keydown', this.eventKeyDown);
  124. }
  125. }
  126. /**
  127. * Detaches mouse interaction event listeners
  128. */
  129. detachMouseListeners = () => {
  130. if (this.canvasRef.current !== null) {
  131. this.canvasRef.current.removeEventListener('mousedown', this.eventMouseDown);
  132. this.canvasRef.current.removeEventListener('mousemove', this.eventMouseMove);
  133. this.canvasRef.current.removeEventListener('mouseup', this.eventMouseUp);
  134. this.canvasRef.current.removeEventListener('wheel', this.eventZoomWheel);
  135. this.canvasRef.current.removeEventListener('keydown', this.eventKeyDown);
  136. }
  137. }
  138. /**
  139. * Mouse down event on canvas
  140. * @param {MouseEvent} e
  141. */
  142. eventMouseDown = (e) => {
  143. // console.log('eventMouseDown', e);
  144. if (!this.isMouseDown) {
  145. this.isMouseDown = true;
  146. this.mouseDownX = e.clientX;
  147. }
  148. }
  149. /**
  150. * Mouse move event on canvas
  151. * @param {MouseEvent} e
  152. */
  153. eventMouseMove = (e) => {
  154. // 判断鼠标拖拽距离,这个只是判断拖拽是否满足阈值,使其画面滚动暂停
  155. const downDragLength = Math.abs(e.clientX - this.mouseDownX);
  156. if (this.isMouseDown && downDragLength > 200) {
  157. this.props.toggleAutoScroll(false)
  158. }
  159. // 其他事件处理
  160. if (this.isMouseDown && this.xScale) {
  161. // Mouse drag, scroll the time series,距离上一次移动视野的鼠标拖拽距离
  162. let dragX = e.clientX - this.mouse.x;
  163. // 合并数据,拖动范围要增加
  164. if (this.isMerged) {
  165. dragX = dragX * 10;
  166. }
  167. const moveDataPointsCount = Math.floor(Math.abs(dragX) / this.xScale.bandwidth());
  168. if (moveDataPointsCount > 0) this.mouse.x = e.x
  169. // const moveDataPointDirection = dragLength >= 0 ? 'right' : 'left';
  170. // console.log('drag x=', dragLength, moveDataPointsCount, this.windowPosition);
  171. this.moveDataWindow(this.windowPosition + moveDataPointsCount * (dragX >= 0 ? -1 : 1));
  172. } else {
  173. if (e.clientX < this.defaults.hmWidth() && e.clientY < this.defaults.hmHeight()) {
  174. this.mouse = {
  175. x: e.clientX,
  176. y: e.clientY
  177. };
  178. }
  179. }
  180. }
  181. /**
  182. * Mouse up event on canvas
  183. * @param {MouseEvent} e
  184. */
  185. eventMouseUp = (e) => {
  186. // console.log('eventMouseUp',e);
  187. this.isMouseDown = false;
  188. this.mouseDownX = 0;
  189. }
  190. /**
  191. * Wheel event on canvas to zoom
  192. * @param {WheelEvent} e
  193. */
  194. eventZoomWheel = (e) => {
  195. const direction = e.deltaY < 0 ? 'zoom-in' : 'zoom-out';
  196. let l = 0, l2 = 0;
  197. switch (direction) {
  198. case 'zoom-in':
  199. l = Math.max(this.windowLength - 25, 10);
  200. break;
  201. case 'zoom-out':
  202. l = Math.min(this.windowLength + 25, this.data.length);
  203. break;
  204. }
  205. l2 = this.windowLength - l;
  206. this.windowLength = l;
  207. this.moveDataWindow(this.windowPosition + l2);
  208. // console.log('zoom Level=', this.windowLength);
  209. }
  210. /**
  211. * Event to be triggered when keyboard key is pressed
  212. * @param {KeyboardEvent} e
  213. */
  214. eventKeyDown = (e) => {
  215. e.preventDefault();
  216. console.log('key event', e.isComposing, e.key, e.ctrlKey);
  217. switch (e.key) {
  218. case 'ArrowLeft':
  219. this.moveDataWindow(this.windowPosition - (e.ctrlKey ? 10 : 1));
  220. break;
  221. case 'ArrowRight':
  222. this.moveDataWindow(this.windowPosition + (e.ctrlKey ? 10 : 1));
  223. break;
  224. }
  225. }
  226. // ------------------ END:: Mouse Event listeners ---------------------
  227. // ------------------ D3 Variables ---------------------
  228. /** @type {d3Scale.ScaleBand<string>} */
  229. xScale = null;
  230. /** @type {d3Scale.ScaleLinear<number, number>} */
  231. bidAskScale = null;
  232. /** @type {d3Scale.ScaleBand<string>} */
  233. yScale = null;
  234. /** @type {number[]} */
  235. yDomainValues = null;
  236. /** @type {d3Timer.Timer} */
  237. bidAskAnimTimer = null;
  238. // ------------------ D3 Variables ---------------------
  239. /**
  240. * This function will be called if there is any dimension change on heatmap
  241. * This function changes the d3 scales based on windowed data
  242. */
  243. updateHeatmapDimensions = () => {
  244. const { width, height } = this.props;
  245. if (width > 0 && height > 0 && this.windowedData.length > 0) {
  246. // setup x-scale
  247. this.xScale = d3.scaleBand()
  248. .range([0, this.defaults.hmWidth()])
  249. .domain(this.windowedData.map(d => d.ts));
  250. // setup y-scale
  251. this.yDomainValues = extractBidPrices(this.windowedData).sort((a, b) => a - b);
  252. this.yScale = d3.scaleBand()
  253. .range([this.defaults.hmHeight(), 0])
  254. .domain(this.yDomainValues);
  255. // setup bid ask scale
  256. this.bidAskScale = d3.scaleLinear()
  257. .range([0, this.defaults.bidAskWidth])
  258. .domain([0, d3.max(extractBidVolumes(this.windowedData[this.windowedData.length - 1]))]);
  259. }
  260. }
  261. /**
  262. * This method will be called after an update of internal data is performed.
  263. */
  264. updateHeatmap = () => {
  265. if (this.drawingContext !== null) {
  266. // 1. update scale and dimensions
  267. this.updateHeatmapDimensions();
  268. // 2. Draw the bid ask spread heatmap
  269. this.clearCanvas(this.defaults.borderPadding[3], this.defaults.borderPadding[0],
  270. this.defaults.hmWidth(), this.defaults.hmHeight(), this.defaults.clearColor);
  271. this.drawMainGraph();
  272. // 3. Draw xy Axis
  273. this.drawXAxis();
  274. this.drawYAxisAndBidAskGraph();
  275. // 4. Draw buy-to-sell ratio
  276. this.drawBuy2SellRatio();
  277. // 5. Draw mouse
  278. this.drawMouse();
  279. // console.log('heatmap draw update');
  280. // this.clearCanvas(0, 0, this.defaults.hmWidth(), this.defaults.hmHeight(), '#aaaaaa');
  281. }
  282. }
  283. // ------------------------------ START: Canvas draw functions ---------------------------------------
  284. getMousePos = (canvas, x, y) => {
  285. var rect = canvas.getBoundingClientRect();
  286. // 使用容器的内部宽度进行计算,排除 padding 影响
  287. var style = window.getComputedStyle(canvas.parentNode);
  288. var paddingLeft = parseFloat(style.paddingLeft) || 0;
  289. var paddingRight = parseFloat(style.paddingRight) || 0;
  290. var effectiveWidth = rect.width - paddingLeft - paddingRight;
  291. return {
  292. x: (x - rect.left - paddingLeft) * (canvas.width / effectiveWidth) * 1.03,
  293. y: (y - rect.top) * (canvas.height / rect.height)
  294. };
  295. }
  296. drawMouse = () => {
  297. const canvas = this.canvasRef.current;
  298. if (!canvas) return;
  299. const context = canvas.getContext('2d');
  300. if (!context) return;
  301. let mouse = this.getMousePos(canvas, this.mouse.x, this.mouse.y)
  302. const x = mouse.x;
  303. const y = mouse.y;
  304. // 移动事件处理,指上圆球事件处理
  305. let drawPending = []
  306. this.circles.forEach(circle => {
  307. const distance = Math.sqrt(Math.pow(x - circle.x, 2) + Math.pow(y - circle.y, 2));
  308. if (distance <= circle.radius) {
  309. let d = circle.data
  310. drawPending.push(d)
  311. }
  312. });
  313. if (drawPending.length > 0) {
  314. const WIDTH = 100
  315. const HEIGHT = drawPending.length * 45
  316. // 绘制方块
  317. let color = d3.color('#fff').rgb();
  318. color.opacity = 0.7
  319. this.drawingContext.fillStyle = color.toString();
  320. this.drawingContext.fillRect(
  321. x + 2,
  322. y + 2,
  323. WIDTH,
  324. HEIGHT
  325. );
  326. // 绘制方块内数据
  327. drawPending.map((d, index) => {
  328. let depth = d.marketDepth;
  329. let text = depth.lastSellQty !== 0 ? `卖出 ${depth.lastSellQty} 在 ${depth.lastSellPrice}` : `买入 ${depth.lastBuyQty} 在 ${depth.lastBuyPrice}`
  330. context.fillStyle = d3.color(depth.side === 'sell' ? '#222' : '#222').rgb();
  331. context.fillText(`${d.ts}`, x + 10, y + 7 + 15 + index * 45);
  332. context.fillText(text, x + 10, y + 7 + 30 + index * 45);
  333. if (index < drawPending.length - 1) {
  334. context.fillText("--------------", x + 10, y + 7 + 45 + index * 45);
  335. }
  336. })
  337. }
  338. //
  339. // // 清除canvas并重新绘制
  340. // context.clearRect(0, 0, canvas.width, canvas.height);
  341. // 绘制交叉线和文本
  342. context.beginPath();
  343. context.moveTo(x, 30);
  344. context.lineTo(x, this.defaults.hmHeight());
  345. context.moveTo(30, y);
  346. context.lineTo(this.defaults.hmWidth(), y);
  347. context.strokeStyle = '#000';
  348. context.stroke();
  349. }
  350. /**
  351. * Draw buy/sell ratio at bottom right corner
  352. */
  353. drawBuy2SellRatio = () => {
  354. if (this.windowedData.length > 0) {
  355. // dimension
  356. const d = this.windowedData[this.windowedData.length - 1];
  357. const x = this.defaults.borderPadding[3] + this.defaults.hmWidth() + this.defaults.axisTickSize;
  358. const y = this.defaults.borderPadding[0] + this.defaults.hmHeight() + this.defaults.axisTickSize;
  359. const w = this.props.width - x;
  360. const h = this.props.height - y;
  361. this.clearCanvas(x, y, w, h, this.defaults.clearColor);
  362. let textHeight = (h - 10) / 2;
  363. this.drawingContext.save();
  364. this.drawingContext.textAlign = 'center';
  365. this.drawingContext.textBaseline = 'middle';
  366. // this.drawingContext.font = `bold ${textHeight}px sans-serif`;
  367. // this.drawingContext.fillText(, x + w * 1/2, y + textHeight / 2);
  368. // // Runing average ratio
  369. // if(this.windowedData.length >= this.defaults.runningRatioSeconds) {
  370. // let sellT20RunningSum = 0;
  371. // let buyT20RunningSum = 0;
  372. // for (let i = this.windowedData.length - 1; i >= this.windowedData.length - this.defaults.runningRatioSeconds; i--) {
  373. // sellT20RunningSum += (this.windowedData[i].marketDepth.sells || []).reduce((vol, s) => vol + s.qty,0);
  374. // buyT20RunningSum += (this.windowedData[i].marketDepth.buys || []).reduce((vol, s) => vol + s.qty,0);
  375. // }
  376. // const newBSTPFactor = (buyT20RunningSum / sellT20RunningSum);
  377. // this.drawingContext.fillText(newBSTPFactor.toFixed(2), x + w /4, y + textHeight *0.5);
  378. // }
  379. this.drawingContext.font = `bold ${13}px sans-serif`;
  380. this.drawingContext.textBaseline = 'bottom';
  381. this.drawingContext.fillText(`买卖比: ${(d.marketDepth.buyOrderVolume / d.marketDepth.sellOrderVolume).toFixed(2)}`, x + w / 2, y + textHeight * 2 + 5);
  382. this.drawingContext.restore();
  383. }
  384. }
  385. /**
  386. * Draws X Axis
  387. */
  388. drawXAxis = () => {
  389. // clear canvas before axis draw
  390. this.clearCanvas(
  391. this.defaults.borderPadding[3], this.defaults.borderPadding[0] + this.defaults.hmHeight(),
  392. this.defaults.hmWidth(), this.defaults.axisXHeight, this.defaults.clearColor
  393. );
  394. // draw axis
  395. this.drawingContext.save();
  396. this.drawingContext.beginPath();
  397. this.drawingContext.translate(this.defaults.borderPadding[3], this.defaults.borderPadding[0] + this.defaults.hmHeight());
  398. this.drawingContext.moveTo(0, 0);
  399. this.drawingContext.lineTo(this.defaults.hmWidth(), 0);
  400. this.drawingContext.textAlign = 'center';
  401. this.drawingContext.textBaseline = 'top';
  402. // const assumedTextWidth = this.drawingContext.measureText('77:77:77').width + 20;
  403. // const maxTickInterval = this.defaults.hmWidth() / assumedTextWidth;
  404. const bandInterval = Math.max(1, parseInt(this.windowedData.length / (this.defaults.hmWidth() / 102)));
  405. // console.log('bandInterval=', bandInterval);
  406. let panel = this;
  407. this.windowedData.map((d, i) => {
  408. if (i !=0 && i % bandInterval === 0) {
  409. let x = this.xScale(d.ts);
  410. if (x + d.ts.length * 5 < panel.defaults.hmWidth()) {
  411. this.drawingContext.moveTo(x, 0);
  412. this.drawingContext.lineTo(x, this.defaults.axisTickSize);
  413. this.drawingContext.fillText(d.ts, x, this.defaults.axisTickSize + this.defaults.xAxisTextPadding);
  414. }
  415. }
  416. });
  417. this.drawingContext.textAlign = 'left';
  418. this.drawingContext.font = '12px Arial';
  419. let zoomLevelText = `当前视域: ${zoomTimeFormat(this.windowLength)}`
  420. this.drawingContext.fillText(zoomLevelText, 20, this.defaults.axisTickSize + this.defaults.xAxisTextPadding + 20);
  421. let w = this.drawingContext.measureText(zoomLevelText).width;
  422. const maxVolumeInWindowData = extractMaxTradedVolume(this.windowedData);
  423. const maxVolumeText = `最近${zoomTimeFormat(this.windowLength, 1)}内最大交易量: `;
  424. this.drawingContext.fillText(maxVolumeText, 20 + w + 20, this.defaults.axisTickSize + this.defaults.xAxisTextPadding + 20);
  425. this.drawingContext.fillStyle = this.defaults.textHighlightOnBackground;
  426. w += this.drawingContext.measureText(maxVolumeText).width;
  427. this.drawingContext.font = 'bold 12px Arial';
  428. this.drawingContext.fillText(`${maxVolumeInWindowData}`, 20 + w + 20, this.defaults.axisTickSize + this.defaults.xAxisTextPadding + 20);
  429. w += this.drawingContext.measureText(`${maxVolumeInWindowData}`).width;
  430. let latested = this.windowedData[this.windowedData.length - 1]
  431. if (this.windowedData.length > 0) {
  432. this.drawingContext.fillStyle = this.defaults.textOnBackground;
  433. this.drawingContext.font = '12px Arial';
  434. this.drawingContext.fillText(`
  435. 最后交易价格: ${latested.marketDepth.side === 'buy' ? latested.marketDepth.lastBuyPrice : latested.marketDepth.lastSellPrice}
  436. 最后交易数量: ${latested.marketDepth.side === 'buy' ? latested.marketDepth.lastBuyQty : latested.marketDepth.lastSellQty}
  437. 最后交易时间: ${latested.ts}
  438. `, 20 + w + 40, this.defaults.axisTickSize + this.defaults.xAxisTextPadding + 20);
  439. }
  440. this.drawingContext.fillStyle = this.defaults.textOnBackground;
  441. this.drawingContext.lineWidth = 1.2;
  442. this.drawingContext.strokeStyle = this.defaults.axisColor;
  443. this.drawingContext.stroke();
  444. this.drawingContext.restore();
  445. }
  446. /**
  447. * Draws Y Axis and Bid Ask graph at the same time
  448. */
  449. drawYAxisAndBidAskGraph = () => {
  450. if (this.yDomainValues !== null) {
  451. const yh2 = this.yScale.bandwidth() * 0.5;
  452. // clear canvas before axis draw
  453. this.clearCanvas(
  454. this.defaults.borderPadding[3] + this.defaults.hmWidth(), this.defaults.borderPadding[0],
  455. this.defaults.axisYWidth, this.defaults.hmHeight() + this.defaults.axisTickSize, this.defaults.clearColor
  456. );
  457. // translate and draw
  458. this.drawingContext.save();
  459. this.drawingContext.beginPath();
  460. this.drawingContext.translate(this.defaults.borderPadding[3] + this.defaults.hmWidth(), this.defaults.borderPadding[0]);
  461. this.drawingContext.moveTo(0, 0);
  462. this.drawingContext.lineTo(0, this.defaults.hmHeight() + this.defaults.axisTickSize);
  463. this.drawingContext.textAlign = 'start';
  464. this.drawingContext.textBaseline = 'top';
  465. let maxTextWidth = 0;
  466. this.yDomainValues.map(d => {
  467. let y = this.yScale(d) + yh2;
  468. this.drawingContext.moveTo(0, y);
  469. this.drawingContext.lineTo(this.defaults.axisTickSize, y);
  470. // 大于7位,换行绘制
  471. if (String(d).length <= 7) {
  472. y -= 5
  473. this.drawingContext.fillText(d, this.defaults.axisTickSize + this.defaults.yAxisTextPadding, y, this.defaults.axisYWidth - this.defaults.axisTickSize + this.defaults.yAxisTextPadding);
  474. } else {
  475. y -= 10
  476. let text = String(d)
  477. let [t0, t1] = text.split('.')
  478. this.drawingContext.fillText(t0, this.defaults.axisTickSize + this.defaults.yAxisTextPadding, y, this.defaults.axisYWidth - this.defaults.axisTickSize + this.defaults.yAxisTextPadding);
  479. this.drawingContext.fillText('.'.concat(t1), this.defaults.axisTickSize + this.defaults.yAxisTextPadding, y + 10, this.defaults.axisYWidth - this.defaults.axisTickSize + this.defaults.yAxisTextPadding);
  480. }
  481. let tw = this.drawingContext.measureText(d).width;
  482. maxTextWidth = maxTextWidth >= tw ? maxTextWidth : tw;
  483. });
  484. this.drawingContext.lineWidth = 1.2;
  485. this.drawingContext.strokeStyle = this.defaults.axisColor;
  486. this.drawingContext.stroke();
  487. this.drawingContext.restore();
  488. // Now I will draw the bid ask strength graph,
  489. const x = this.defaults.borderPadding[3] + this.defaults.hmWidth() + maxTextWidth + this.defaults.axisTickSize + this.defaults.yAxisTextPadding + this.defaults.bidAskGraphPaddingLeft;
  490. const y = this.defaults.borderPadding[0];
  491. this.drawBidAskGraph(x, y);
  492. }
  493. }
  494. /**
  495. * Draw and animate Bid Ask graph
  496. * @param {number} x
  497. * @param {number} y
  498. */
  499. drawBidAskGraph = (x, y) => {
  500. if (this.windowedData.length > 0) {
  501. if (this.bidAskAnimTimer !== null) {
  502. this.bidAskAnimTimer.stop();
  503. this.bidAskAnimTimer = null;
  504. }
  505. this.bidAskAnimTimer = d3.timer(elapsed => {
  506. // compute how far through the animation we are (0 to 1)
  507. const t = Math.min(1, d3.easeCubic(elapsed / this.defaults.bidAskTransitionDuration));
  508. // ----------------draw--------------------
  509. // console.log('drawing bid ask graph');
  510. this.clearCanvas(
  511. x, y, this.defaults.bidAskWidth, this.defaults.hmHeight() + this.defaults.axisTickSize, this.defaults.clearColor
  512. );
  513. const h = this.yScale.bandwidth() - 2;
  514. const d = this.windowedData[this.windowedData.length - 1];
  515. if (!d) {
  516. return
  517. }
  518. const maxBidAskVol = extractMaxVolume(d);
  519. this.drawingContext.save();
  520. this.drawingContext.translate(x, y);
  521. this.drawingContext.lineWidth = 0;
  522. this.drawingContext.textBaseline = 'middle';
  523. const drawBars = (arr, color, textColor) => {
  524. arr.map(v => {
  525. this.drawingContext.fillStyle = color;
  526. const l = this.defaults.bidAskWidth * (+v.qty / maxBidAskVol);
  527. // save v bars length
  528. this.drawingContext.fillRect(0, this.yScale(v.rate), l, h);
  529. let textWidth = this.drawingContext.measureText(v.qty).width;
  530. if (this.defaults.bidAskWidth - l - textWidth >= textWidth) {
  531. // text outside bar
  532. this.drawingContext.textAlign = 'start';
  533. this.drawingContext.fillStyle = this.defaults.textOnBackground;
  534. this.drawingContext.fillText(v.qty, l + 1, this.yScale(v.rate) + h / 2 + 1);
  535. } else {
  536. this.drawingContext.textAlign = 'end';
  537. this.drawingContext.fillStyle = textColor;
  538. this.drawingContext.fillText(v.qty, l - textWidth, this.yScale(v.rate) + h / 2 + 1);
  539. }
  540. });
  541. }
  542. drawBars(d.marketDepth.buys, this.defaults.buyColor, this.defaults.textOnBuyColor);
  543. drawBars(d.marketDepth.sells, this.defaults.sellColor, this.defaults.textOnSellColor);
  544. this.drawingContext.restore();
  545. // ----------------draw--------------------
  546. // if this animation is over
  547. if (t === 1) this.bidAskAnimTimer.stop();
  548. });
  549. }
  550. }
  551. /**
  552. * Draws background heatmap for both buys and sells
  553. */
  554. drawMainGraph = () => {
  555. this.drawingContext.save();
  556. if (this.xScale && this.yScale && this.bidAskScale && this.drawingContext !== null) {
  557. const avgTradedVolume = extractAvgTradedVolume(this.data);
  558. const maxBidAskVolume = extractMaxAskBidVolume(this.windowedData);
  559. const xh2 = this.xScale.bandwidth() * 0.5;
  560. const yh2 = this.yScale.bandwidth() * 0.5;
  561. const panel = this
  562. this.drawingContext.translate(this.defaults.borderPadding[3], this.defaults.borderPadding[0]);
  563. this.windowedData.map(d => {
  564. const marketDepth = d.marketDepth;
  565. const ask1 = marketDepth.sells[0];
  566. const bid1 = marketDepth.buys[0];
  567. const ts = d.ts;
  568. // draw buys
  569. let color = d3.color('#1a506d').rgb();
  570. if (marketDepth.buys && marketDepth.buys.length > 0) {
  571. marketDepth.buys.map(buy => {
  572. // let rate = buy.qty / maxBidAskVolume;
  573. // let colorIndex = Math.min(parseInt(rate / 0.25), panel.orderbookColors.length - 1);
  574. // let color = d3.color(panel.orderbookColors[colorIndex]).rgb()
  575. //
  576. // color.opacity = 0.5 + 0.5 * (rate % 0.25) / 0.25;
  577. color.opacity = buy.qty / maxBidAskVolume;
  578. this.drawingContext.fillStyle = color.toString();
  579. this.drawingContext.fillRect(
  580. this.xScale(ts),
  581. this.yScale(buy.rate), // 减去半个方块的高度
  582. this.xScale.bandwidth(),
  583. this.yScale.bandwidth()
  584. );
  585. });
  586. }
  587. // draw sells
  588. if (marketDepth.sells && marketDepth.sells.length > 0) {
  589. marketDepth.sells.map(sell => {
  590. // let rate = sell.qty / maxBidAskVolume;
  591. // let colorIndex = Math.min(parseInt(rate / 0.25), panel.orderbookColors.length - 1);
  592. // let color = d3.color(panel.orderbookColors[colorIndex]).rgb()
  593. //
  594. // color.opacity = 0.5 + 0.5 * (rate % 0.25) / 0.25;
  595. color.opacity = sell.qty / maxBidAskVolume;
  596. this.drawingContext.fillStyle = color.toString();
  597. this.drawingContext.fillRect(
  598. this.xScale(ts),
  599. this.yScale(sell.rate), // 减去半个方块的高度
  600. this.xScale.bandwidth(),
  601. this.yScale.bandwidth()
  602. );
  603. });
  604. }
  605. });
  606. // draw trade size
  607. this.circles = []
  608. this.windowedData.map(d => {
  609. const marketDepth = d.marketDepth;
  610. const maxBidAskVol = extractMaxVolume(d);
  611. const ts = d.ts;
  612. const ask1 = marketDepth.sells[0];
  613. const bid1 = marketDepth.buys[0];
  614. // 绘制买入的圆圈
  615. if (marketDepth.lastBuyQty !== 0) {
  616. let trade_color = d3.color("#44c98b").rgb();
  617. trade_color.opacity = 0.7;
  618. this.drawingContext.fillStyle = trade_color.toString();
  619. const r = 50 - 45 * (2.71 ** (-0.01 * (+marketDepth.lastBuyQty / avgTradedVolume)));
  620. this.drawingContext.beginPath();
  621. this.drawingContext.arc(
  622. this.xScale(ts) + xh2,
  623. this.yScale(+marketDepth.lastBuyPrice) + yh2,
  624. r,
  625. 0,
  626. 2 * Math.PI
  627. );
  628. this.drawingContext.strokeStyle = trade_color;
  629. this.drawingContext.fill();
  630. // 为球添加白色边框
  631. this.drawingContext.lineWidth = 1; // 设置线宽,可以根据需要调整
  632. this.drawingContext.strokeStyle = "white"; // 设置描边颜色为白色
  633. this.drawingContext.stroke(); // 执行描边操作
  634. // 事件触发用
  635. let d_copy = JSON.parse(JSON.stringify(d))
  636. // 只保存需要展示的那边
  637. d_copy.marketDepth.lastSellQty = 0
  638. d_copy.marketDepth.lastSellPrice = 0
  639. this.circles.push({
  640. x: this.xScale(ts) + xh2,
  641. y: this.yScale(+marketDepth.lastBuyPrice) + yh2,
  642. radius: r,
  643. data: d_copy // 存储与圆相关的数据,便于在事件处理时使用
  644. });
  645. }
  646. // 绘制卖出的圆圈
  647. if (marketDepth.lastSellQty !== 0) {
  648. let trade_color = d3.color("#cc5040").rgb();
  649. trade_color.opacity = 0.7;
  650. this.drawingContext.fillStyle = trade_color.toString();
  651. const r = 50 - 45 * (2.71 ** (-0.01 * (+marketDepth.lastSellQty / avgTradedVolume)));
  652. this.drawingContext.beginPath();
  653. this.drawingContext.arc(
  654. this.xScale(ts) + xh2,
  655. this.yScale(+marketDepth.lastSellPrice) + yh2,
  656. r,
  657. 0,
  658. 2 * Math.PI
  659. );
  660. this.drawingContext.strokeStyle = trade_color;
  661. this.drawingContext.fill();
  662. // 为球添加白色边框
  663. this.drawingContext.lineWidth = 1; // 设置线宽,可以根据需要调整
  664. this.drawingContext.strokeStyle = "white"; // 设置描边颜色为白色
  665. this.drawingContext.stroke(); // 执行描边操作
  666. // 事件触发用
  667. let d_copy = JSON.parse(JSON.stringify(d))
  668. // 只保存需要展示的那边
  669. d_copy.marketDepth.lastBuyQty = 0
  670. d_copy.marketDepth.lastBuyPrice = 0
  671. this.circles.push({
  672. x: this.xScale(ts) + xh2,
  673. y: this.yScale(+marketDepth.lastSellPrice) + yh2,
  674. radius: r,
  675. data: d_copy // 存储与圆相关的数据,便于在事件处理时使用
  676. });
  677. }
  678. })
  679. // draw buy line path
  680. let buy_line_color = d3.color(this.defaults.buyColor).rgb();
  681. this.drawingContext.fillStyle = buy_line_color.toString();
  682. this.drawingContext.beginPath();
  683. d3.line()
  684. .x(d => this.xScale(d.ts))
  685. .y(d => this.yScale(d.marketDepth.buys[0].rate) + yh2)
  686. // .curve(d3.curveLinear)
  687. .context(this.drawingContext)
  688. (this.windowedData);
  689. this.drawingContext.lineWidth = 2;
  690. this.drawingContext.strokeStyle = this.defaults.buyColor;
  691. this.drawingContext.stroke();
  692. // draw sell line path
  693. let sell_line_color = d3.color(this.defaults.sellColor).rgb();
  694. this.drawingContext.fillStyle = sell_line_color.toString();
  695. this.drawingContext.beginPath();
  696. d3.line()
  697. .x(d => this.xScale(d.ts))
  698. .y(d => this.yScale(d.marketDepth.sells[0].rate) + yh2)
  699. // .curve(d3.curveLinear)
  700. .context(this.drawingContext)
  701. (this.windowedData);
  702. this.drawingContext.lineWidth = 2;
  703. this.drawingContext.strokeStyle = this.defaults.sellColor;
  704. this.drawingContext.stroke();
  705. }
  706. this.drawingContext.restore();
  707. }
  708. /**
  709. * Clear the canvas area
  710. * @param {number} x x coordinate
  711. * @param {number} y y xoordinate
  712. * @param {number} w width
  713. * @param {number} h height
  714. * @param {string} color color string
  715. */
  716. clearCanvas = (x, y, w, h, color) => {
  717. // console.log('clear canvas area', x, y, w, h, color);
  718. if (this.drawingContext !== null) {
  719. this.drawingContext.save();
  720. this.drawingContext.fillStyle = color || this.defaults.clearColor;
  721. this.drawingContext.fillRect(x, y, w, h);
  722. this.drawingContext.restore();
  723. }
  724. }
  725. // ------------------------------ END: Canvas draw functions ---------------------------------------
  726. /**
  727. * Set Data for the Heatmap to generate
  728. * @param {any[]} data The data to set
  729. */
  730. setData = (data) => {
  731. // console.log('setdata called=', data);
  732. if (data && data.length > 0) {
  733. this.data = data;
  734. this.updateWindowedData();
  735. }
  736. }
  737. /**
  738. * Add as extra data to existing data array.
  739. * @param {any} data
  740. */
  741. addData = (data) => {
  742. if (typeof (data) === 'object') {
  743. this.data.push(data);
  744. this.updateWindowedData();
  745. }
  746. }
  747. /**
  748. * This updates the data in array to be viewed in graph
  749. */
  750. updateWindowedData = () => {
  751. // console.log('window data updated');
  752. if (this.props.autoScroll) {
  753. this.moveDataWindow(this.data.length - this.windowLength - 1);
  754. }
  755. }
  756. isOrderBooksEquals = (orderbooks1, orderbooks2) => {
  757. for (let i = 0; i < orderbooks1.length; i++) {
  758. let orderbook1 = orderbooks1[i]
  759. let orderbook2 = orderbooks2[i]
  760. // 如果d1有,d2没有,那证明不相等
  761. if (!orderbook2) return false
  762. if (orderbook1.rate !== orderbook2.rate) return false
  763. let r1 = (orderbook1.qty / this.maxBidAskVolume);
  764. let o1ColorIndex = Math.min(parseInt(r1 / 0.25), this.orderbookColors.length - 1);
  765. let r2 = (orderbook2.qty / this.maxBidAskVolume);
  766. let o2ColorIndex = Math.min(parseInt(r2 / 0.25), this.orderbookColors.length - 1);
  767. if (o1ColorIndex !== o2ColorIndex) return false
  768. }
  769. return true
  770. }
  771. isDepthEquals = (d1, d2) => {
  772. // buys
  773. if (!this.isOrderBooksEquals(d1.buys, d2.buys)) return false
  774. // sells
  775. if (!this.isOrderBooksEquals(d1.sells, d2.sells)) return false
  776. return true
  777. }
  778. mergeSnapshots = (snapshots) => {
  779. // 初始化合并后的结构
  780. const merged = {
  781. marketDepth: {
  782. avgPrice: 0,
  783. buyOrderVolume: 0,
  784. sellOrderVolume: 0,
  785. lastBuyPrice: 0,
  786. lastBuyQty: 0,
  787. lastSellPrice: 0,
  788. lastSellQty: 0,
  789. lastTradedTS: 0,
  790. open: 0,
  791. high: 0,
  792. low: 0,
  793. close: 0,
  794. priceChangeAmt: 0,
  795. priceChangePct: "0",
  796. buys: [],
  797. sells: []
  798. },
  799. pendingOrders: [],
  800. time: "",
  801. tradingsymbol: "",
  802. ts: ""
  803. };
  804. // 初始化计数器
  805. let totalAvgPrice = 0;
  806. let totalBuyOrderVolume = 0;
  807. let totalSellOrderVolume = 0;
  808. let totalSnapshots = snapshots.length;
  809. // 初始化买卖数组
  810. let buySums = Array(5).fill({ rate: 0, orders: 0, qty: 0 });
  811. let sellSums = Array(5).fill({ rate: 0, orders: 0, qty: 0 });
  812. // 初始化交易数量
  813. let totalLastTradeQtyBuy = 0;
  814. let totalLastTradeQtySell = 0;
  815. // 记录第一个快照的时间和交易符号
  816. merged.time = snapshots[0].time;
  817. merged.tradingsymbol = snapshots[0].tradingsymbol;
  818. merged.ts = snapshots[0].ts;
  819. // 遍历所有快照数据
  820. snapshots.forEach(snapshot => {
  821. // 累加市场深度数据
  822. totalAvgPrice += snapshot.marketDepth.avgPrice;
  823. totalBuyOrderVolume += snapshot.marketDepth.buyOrderVolume;
  824. totalSellOrderVolume += snapshot.marketDepth.sellOrderVolume;
  825. // 合并买单和卖单
  826. snapshot.marketDepth.buys.forEach((buy, index) => {
  827. buySums[index] = {
  828. rate: buy.rate,
  829. orders: buySums[index].orders + buy.orders,
  830. qty: buySums[index].qty + buy.qty
  831. };
  832. });
  833. snapshot.marketDepth.sells.forEach((sell, index) => {
  834. sellSums[index] = {
  835. rate: sell.rate,
  836. orders: sellSums[index].orders + sell.orders,
  837. qty: sellSums[index].qty + sell.qty
  838. };
  839. });
  840. // 合并最后交易的数量和价格
  841. totalLastTradeQtyBuy += snapshot.marketDepth.lastBuyQty;
  842. totalLastTradeQtySell += snapshot.marketDepth.lastSellQty;
  843. // 合并其他字段
  844. merged.marketDepth.close = snapshot.marketDepth.close;
  845. merged.marketDepth.high = snapshot.marketDepth.high;
  846. merged.marketDepth.low = snapshot.marketDepth.low;
  847. merged.marketDepth.open = snapshot.marketDepth.open;
  848. merged.marketDepth.priceChangeAmt = snapshot.marketDepth.priceChangeAmt;
  849. merged.marketDepth.priceChangePct = snapshot.marketDepth.priceChangePct;
  850. merged.marketDepth.lastTradedTS = snapshot.marketDepth.lastTradedTS;
  851. });
  852. // 计算平均市场深度数据
  853. merged.marketDepth.avgPrice = totalAvgPrice / totalSnapshots;
  854. merged.marketDepth.buyOrderVolume = totalBuyOrderVolume / totalSnapshots;
  855. merged.marketDepth.sellOrderVolume = totalSellOrderVolume / totalSnapshots;
  856. // 计算平均买单和卖单
  857. merged.marketDepth.buys = buySums.map(buy => ({
  858. rate: buy.rate,
  859. orders: buy.orders / totalSnapshots,
  860. qty: buy.qty / totalSnapshots
  861. }));
  862. merged.marketDepth.sells = sellSums.map(sell => ({
  863. rate: sell.rate,
  864. orders: sell.orders / totalSnapshots,
  865. qty: sell.qty / totalSnapshots
  866. }));
  867. // 计算最终的最后交易的数量和价格
  868. merged.marketDepth.lastBuyPrice = merged.marketDepth.sells[0].rate;
  869. merged.marketDepth.lastBuyQty = totalLastTradeQtyBuy;
  870. merged.marketDepth.lastSellPrice = merged.marketDepth.buys[0].rate;
  871. merged.marketDepth.lastSellQty = totalLastTradeQtySell;
  872. merged.marketDepth.side = 'both';
  873. return merged;
  874. }
  875. mergeWindowedData = () => {
  876. let windowedData = this.windowedData;
  877. this.maxBidAskVolume = extractMaxAskBidVolume(this.windowedData);
  878. let mergedWindowedData = [];
  879. let panel = this
  880. let prevData = undefined;
  881. let snapshots = [];
  882. windowedData.map((d, i) => {
  883. // 最后一个元素要展示,不然会丢失盘口细节
  884. if (i === windowedData.length - 1) {
  885. mergedWindowedData.push(d)
  886. prevData = d
  887. // 如果是第一个数据,则进行初始化
  888. } else if (!prevData) {
  889. prevData = d
  890. // 如果是中间的数据,则进行逻辑判断,是否需要合并
  891. } else if (!panel.isDepthEquals(prevData.marketDepth, d.marketDepth)) {
  892. let mergedData = panel.mergeSnapshots(snapshots)
  893. mergedWindowedData.push(mergedData)
  894. prevData = d
  895. snapshots = [];
  896. }
  897. snapshots.push(d)
  898. });
  899. return mergedWindowedData;
  900. }
  901. /**
  902. * Move the position of data window within the main data.
  903. * @param {number} position The target position of the window to be moved to.
  904. */
  905. moveDataWindow = (position) => {
  906. if (position !== this.windowPosition && position > -1 && position < this.data.length - this.windowLength) {
  907. // move position only if within valid range
  908. this.windowedData = this.data.slice(position, position + this.windowLength + 1);
  909. if (this.windowedData.length > 1000) {
  910. this.windowedData = this.mergeWindowedData();
  911. this.isMerged = true;
  912. } else {
  913. this.isMerged = false;
  914. }
  915. // 延迟日志
  916. if (this.windowedData.length > 1) {
  917. let last = this.windowedData[this.windowedData.length - 1]
  918. console.log(new Date().getTime() - last.time, last)
  919. }
  920. this.windowPosition = position;
  921. if (this.windowPosition === this.data.length - this.windowLength - 1) {
  922. // enable auto scroll
  923. this.props.toggleAutoScroll(true);
  924. }
  925. // console.log('moveDataWindow = ', position, this.windowPosition, this.windowLength, this.data.length, this.autoScroll, this.windowedData);
  926. // update the map
  927. this.updateHeatmap();
  928. }
  929. }
  930. /**
  931. * This sets the Heatmap Zoom level aka. window.
  932. * @param {number} zoom The seconds to zoom into
  933. */
  934. setZoomLevel = (zoom) => {
  935. let l = Math.min(Math.max(zoom * 4, 3), this.data.length - 1);
  936. let l2 = this.windowLength - l;
  937. this.windowLength = l;
  938. this.moveDataWindow(this.windowPosition + l2);
  939. }
  940. /**
  941. * Render Function
  942. */
  943. render() {
  944. const { width, height } = this.props;
  945. // console.log('heatmap rendered', width, height, this.data);
  946. return (
  947. <canvas ref={this.canvasRef} width={width || 300} height={height || 150} tabIndex={1}
  948. style={{
  949. display: 'block',
  950. width: '100%',
  951. height: '100%',
  952. cursor: 'crosshair',
  953. paddingRight: '3%',
  954. boxSizing: "border-box"
  955. }}></canvas>
  956. );
  957. }
  958. }