index.js 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061
  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 - 16, 3);
  200. break;
  201. case 'zoom-out':
  202. l = Math.min(this.windowLength + 16, this.data.length - 16);
  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.windowedData);
  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. if (marketDepth.buys && marketDepth.buys.length > 0) {
  570. marketDepth.buys.map(buy => {
  571. let rate = buy.qty / maxBidAskVolume;
  572. let colorIndex = Math.min(parseInt(rate / 0.25), panel.orderbookColors.length - 1);
  573. let color = d3.color(panel.orderbookColors[colorIndex]).rgb()
  574. color.opacity = 0.5 + 0.5 * (rate % 0.25) / 0.25;
  575. this.drawingContext.fillStyle = color.toString();
  576. this.drawingContext.fillRect(
  577. this.xScale(ts),
  578. this.yScale(buy.rate), // 减去半个方块的高度
  579. this.xScale.bandwidth(),
  580. this.yScale.bandwidth()
  581. );
  582. });
  583. }
  584. // draw sells
  585. if (marketDepth.sells && marketDepth.sells.length > 0) {
  586. marketDepth.sells.map(sell => {
  587. let rate = sell.qty / maxBidAskVolume;
  588. let colorIndex = Math.min(parseInt(rate / 0.25), panel.orderbookColors.length - 1);
  589. let color = d3.color(panel.orderbookColors[colorIndex]).rgb()
  590. color.opacity = 0.5 + 0.5 * (rate % 0.25) / 0.25;
  591. this.drawingContext.fillStyle = color.toString();
  592. this.drawingContext.fillRect(
  593. this.xScale(ts),
  594. this.yScale(sell.rate), // 减去半个方块的高度
  595. this.xScale.bandwidth(),
  596. this.yScale.bandwidth()
  597. );
  598. });
  599. }
  600. });
  601. // draw trade size
  602. this.circles = []
  603. this.windowedData.map(d => {
  604. const marketDepth = d.marketDepth;
  605. const maxBidAskVol = extractMaxVolume(d);
  606. const ts = d.ts;
  607. const ask1 = marketDepth.sells[0];
  608. const bid1 = marketDepth.buys[0];
  609. // 绘制买入的圆圈
  610. if (marketDepth.lastBuyQty !== 0) {
  611. let trade_color = d3.color("#44c98b").rgb();
  612. trade_color.opacity = 0.7;
  613. this.drawingContext.fillStyle = trade_color.toString();
  614. const r = 50 - 45 * (2.71 ** (-0.01 * (+marketDepth.lastBuyQty / avgTradedVolume)));
  615. this.drawingContext.beginPath();
  616. this.drawingContext.arc(
  617. this.xScale(ts) + xh2,
  618. this.yScale(+marketDepth.lastBuyPrice) + yh2,
  619. r,
  620. 0,
  621. 2 * Math.PI
  622. );
  623. this.drawingContext.strokeStyle = trade_color;
  624. this.drawingContext.fill();
  625. // 为球添加白色边框
  626. this.drawingContext.lineWidth = 1; // 设置线宽,可以根据需要调整
  627. this.drawingContext.strokeStyle = "white"; // 设置描边颜色为白色
  628. this.drawingContext.stroke(); // 执行描边操作
  629. // 事件触发用
  630. let d_copy = JSON.parse(JSON.stringify(d))
  631. // 只保存需要展示的那边
  632. d_copy.marketDepth.lastSellQty = 0
  633. d_copy.marketDepth.lastSellPrice = 0
  634. this.circles.push({
  635. x: this.xScale(ts) + xh2,
  636. y: this.yScale(+marketDepth.lastBuyPrice) + yh2,
  637. radius: r,
  638. data: d_copy // 存储与圆相关的数据,便于在事件处理时使用
  639. });
  640. }
  641. // 绘制卖出的圆圈
  642. if (marketDepth.lastSellQty !== 0) {
  643. let trade_color = d3.color("#cc5040").rgb();
  644. trade_color.opacity = 0.7;
  645. this.drawingContext.fillStyle = trade_color.toString();
  646. const r = 50 - 45 * (2.71 ** (-0.01 * (+marketDepth.lastSellQty / avgTradedVolume)));
  647. this.drawingContext.beginPath();
  648. this.drawingContext.arc(
  649. this.xScale(ts) + xh2,
  650. this.yScale(+marketDepth.lastSellPrice) + yh2,
  651. r,
  652. 0,
  653. 2 * Math.PI
  654. );
  655. this.drawingContext.strokeStyle = trade_color;
  656. this.drawingContext.fill();
  657. // 为球添加白色边框
  658. this.drawingContext.lineWidth = 1; // 设置线宽,可以根据需要调整
  659. this.drawingContext.strokeStyle = "white"; // 设置描边颜色为白色
  660. this.drawingContext.stroke(); // 执行描边操作
  661. // 事件触发用
  662. let d_copy = JSON.parse(JSON.stringify(d))
  663. // 只保存需要展示的那边
  664. d_copy.marketDepth.lastBuyQty = 0
  665. d_copy.marketDepth.lastBuyPrice = 0
  666. this.circles.push({
  667. x: this.xScale(ts) + xh2,
  668. y: this.yScale(+marketDepth.lastSellPrice) + yh2,
  669. radius: r,
  670. data: d_copy // 存储与圆相关的数据,便于在事件处理时使用
  671. });
  672. }
  673. })
  674. // draw buy line path
  675. let buy_line_color = d3.color(this.defaults.buyColor).rgb();
  676. this.drawingContext.fillStyle = buy_line_color.toString();
  677. this.drawingContext.beginPath();
  678. d3.line()
  679. .x(d => this.xScale(d.ts))
  680. .y(d => this.yScale(d.marketDepth.buys[0].rate) + yh2)
  681. // .curve(d3.curveLinear)
  682. .context(this.drawingContext)
  683. (this.windowedData);
  684. this.drawingContext.lineWidth = 2;
  685. this.drawingContext.strokeStyle = this.defaults.buyColor;
  686. this.drawingContext.stroke();
  687. // draw sell line path
  688. let sell_line_color = d3.color(this.defaults.sellColor).rgb();
  689. this.drawingContext.fillStyle = sell_line_color.toString();
  690. this.drawingContext.beginPath();
  691. d3.line()
  692. .x(d => this.xScale(d.ts))
  693. .y(d => this.yScale(d.marketDepth.sells[0].rate) + yh2)
  694. // .curve(d3.curveLinear)
  695. .context(this.drawingContext)
  696. (this.windowedData);
  697. this.drawingContext.lineWidth = 2;
  698. this.drawingContext.strokeStyle = this.defaults.sellColor;
  699. this.drawingContext.stroke();
  700. }
  701. this.drawingContext.restore();
  702. }
  703. /**
  704. * Clear the canvas area
  705. * @param {number} x x coordinate
  706. * @param {number} y y xoordinate
  707. * @param {number} w width
  708. * @param {number} h height
  709. * @param {string} color color string
  710. */
  711. clearCanvas = (x, y, w, h, color) => {
  712. // console.log('clear canvas area', x, y, w, h, color);
  713. if (this.drawingContext !== null) {
  714. this.drawingContext.save();
  715. this.drawingContext.fillStyle = color || this.defaults.clearColor;
  716. this.drawingContext.fillRect(x, y, w, h);
  717. this.drawingContext.restore();
  718. }
  719. }
  720. // ------------------------------ END: Canvas draw functions ---------------------------------------
  721. /**
  722. * Set Data for the Heatmap to generate
  723. * @param {any[]} data The data to set
  724. */
  725. setData = (data) => {
  726. // console.log('setdata called=', data);
  727. if (data && data.length > 0) {
  728. this.data = data;
  729. this.updateWindowedData();
  730. }
  731. }
  732. /**
  733. * Add as extra data to existing data array.
  734. * @param {any} data
  735. */
  736. addData = (data) => {
  737. if (typeof (data) === 'object') {
  738. this.data.push(data);
  739. this.updateWindowedData();
  740. }
  741. }
  742. /**
  743. * This updates the data in array to be viewed in graph
  744. */
  745. updateWindowedData = () => {
  746. // console.log('window data updated');
  747. if (this.props.autoScroll) {
  748. this.moveDataWindow(this.data.length - this.windowLength - 1);
  749. }
  750. }
  751. isOrderBooksEquals = (orderbooks1, orderbooks2) => {
  752. for (let i = 0; i < orderbooks1.length; i++) {
  753. let orderbook1 = orderbooks1[i]
  754. let orderbook2 = orderbooks2[i]
  755. // 如果d1有,d2没有,那证明不相等
  756. if (!orderbook2) return false
  757. if (orderbook1.rate !== orderbook2.rate) return false
  758. let r1 = (orderbook1.qty / this.maxBidAskVolume);
  759. let o1ColorIndex = Math.min(parseInt(r1 / 0.25), this.orderbookColors.length - 1);
  760. let r2 = (orderbook2.qty / this.maxBidAskVolume);
  761. let o2ColorIndex = Math.min(parseInt(r2 / 0.25), this.orderbookColors.length - 1);
  762. if (o1ColorIndex !== o2ColorIndex) return false
  763. }
  764. return true
  765. }
  766. isDepthEquals = (d1, d2) => {
  767. // buys
  768. if (!this.isOrderBooksEquals(d1.buys, d2.buys)) return false
  769. // sells
  770. if (!this.isOrderBooksEquals(d1.sells, d2.sells)) return false
  771. return true
  772. }
  773. mergeSnapshots = (snapshots) => {
  774. // 初始化合并后的结构
  775. const merged = {
  776. marketDepth: {
  777. avgPrice: 0,
  778. buyOrderVolume: 0,
  779. sellOrderVolume: 0,
  780. lastBuyPrice: 0,
  781. lastBuyQty: 0,
  782. lastSellPrice: 0,
  783. lastSellQty: 0,
  784. lastTradedTS: 0,
  785. open: 0,
  786. high: 0,
  787. low: 0,
  788. close: 0,
  789. priceChangeAmt: 0,
  790. priceChangePct: "0",
  791. buys: [],
  792. sells: []
  793. },
  794. pendingOrders: [],
  795. time: "",
  796. tradingsymbol: "",
  797. ts: ""
  798. };
  799. // 初始化计数器
  800. let totalAvgPrice = 0;
  801. let totalBuyOrderVolume = 0;
  802. let totalSellOrderVolume = 0;
  803. let totalSnapshots = snapshots.length;
  804. // 初始化买卖数组
  805. let buySums = Array(5).fill({ rate: 0, orders: 0, qty: 0 });
  806. let sellSums = Array(5).fill({ rate: 0, orders: 0, qty: 0 });
  807. // 初始化交易数量
  808. let totalLastTradeQtyBuy = 0;
  809. let totalLastTradeQtySell = 0;
  810. // 记录第一个快照的时间和交易符号
  811. merged.time = snapshots[0].time;
  812. merged.tradingsymbol = snapshots[0].tradingsymbol;
  813. merged.ts = snapshots[0].ts;
  814. // 遍历所有快照数据
  815. snapshots.forEach(snapshot => {
  816. // 累加市场深度数据
  817. totalAvgPrice += snapshot.marketDepth.avgPrice;
  818. totalBuyOrderVolume += snapshot.marketDepth.buyOrderVolume;
  819. totalSellOrderVolume += snapshot.marketDepth.sellOrderVolume;
  820. // 合并买单和卖单
  821. snapshot.marketDepth.buys.forEach((buy, index) => {
  822. buySums[index] = {
  823. rate: buy.rate,
  824. orders: buySums[index].orders + buy.orders,
  825. qty: buySums[index].qty + buy.qty
  826. };
  827. });
  828. snapshot.marketDepth.sells.forEach((sell, index) => {
  829. sellSums[index] = {
  830. rate: sell.rate,
  831. orders: sellSums[index].orders + sell.orders,
  832. qty: sellSums[index].qty + sell.qty
  833. };
  834. });
  835. // 合并最后交易的数量和价格
  836. totalLastTradeQtyBuy += snapshot.marketDepth.lastBuyQty;
  837. totalLastTradeQtySell += snapshot.marketDepth.lastSellQty;
  838. // 合并其他字段
  839. merged.marketDepth.close = snapshot.marketDepth.close;
  840. merged.marketDepth.high = snapshot.marketDepth.high;
  841. merged.marketDepth.low = snapshot.marketDepth.low;
  842. merged.marketDepth.open = snapshot.marketDepth.open;
  843. merged.marketDepth.priceChangeAmt = snapshot.marketDepth.priceChangeAmt;
  844. merged.marketDepth.priceChangePct = snapshot.marketDepth.priceChangePct;
  845. merged.marketDepth.lastTradedTS = snapshot.marketDepth.lastTradedTS;
  846. });
  847. // 计算平均市场深度数据
  848. merged.marketDepth.avgPrice = totalAvgPrice / totalSnapshots;
  849. merged.marketDepth.buyOrderVolume = totalBuyOrderVolume / totalSnapshots;
  850. merged.marketDepth.sellOrderVolume = totalSellOrderVolume / totalSnapshots;
  851. // 计算平均买单和卖单
  852. merged.marketDepth.buys = buySums.map(buy => ({
  853. rate: buy.rate,
  854. orders: buy.orders / totalSnapshots,
  855. qty: buy.qty / totalSnapshots
  856. }));
  857. merged.marketDepth.sells = sellSums.map(sell => ({
  858. rate: sell.rate,
  859. orders: sell.orders / totalSnapshots,
  860. qty: sell.qty / totalSnapshots
  861. }));
  862. // 计算最终的最后交易的数量和价格
  863. merged.marketDepth.lastBuyPrice = merged.marketDepth.sells[0].rate;
  864. merged.marketDepth.lastBuyQty = totalLastTradeQtyBuy;
  865. merged.marketDepth.lastSellPrice = merged.marketDepth.buys[0].rate;
  866. merged.marketDepth.lastSellQty = totalLastTradeQtySell;
  867. merged.marketDepth.side = 'both';
  868. return merged;
  869. }
  870. mergeWindowedData = () => {
  871. let windowedData = this.windowedData;
  872. this.maxBidAskVolume = extractMaxAskBidVolume(this.windowedData);
  873. let mergedWindowedData = [];
  874. let panel = this
  875. let prevData = undefined;
  876. let snapshots = [];
  877. windowedData.map((d, i) => {
  878. // 最后一个元素要展示,不然会丢失盘口细节
  879. if (i === windowedData.length - 1) {
  880. mergedWindowedData.push(d)
  881. prevData = d
  882. // 如果是第一个数据,则进行初始化
  883. } else if (!prevData) {
  884. prevData = d
  885. // 如果是中间的数据,则进行逻辑判断,是否需要合并
  886. } else if (!panel.isDepthEquals(prevData.marketDepth, d.marketDepth)) {
  887. let mergedData = panel.mergeSnapshots(snapshots)
  888. mergedWindowedData.push(mergedData)
  889. prevData = d
  890. snapshots = [];
  891. }
  892. snapshots.push(d)
  893. });
  894. return mergedWindowedData;
  895. }
  896. /**
  897. * Move the position of data window within the main data.
  898. * @param {number} position The target position of the window to be moved to.
  899. */
  900. moveDataWindow = (position) => {
  901. if (position !== this.windowPosition && position > -1 && position < this.data.length - this.windowLength) {
  902. // move position only if within valid range
  903. this.windowedData = this.data.slice(position, position + this.windowLength + 1);
  904. if (this.windowedData.length > 200) {
  905. this.windowedData = this.mergeWindowedData();
  906. this.isMerged = true;
  907. } else {
  908. this.isMerged = false;
  909. }
  910. // 延迟日志
  911. if (this.windowedData.length > 1) {
  912. let last = this.windowedData[this.windowedData.length - 1]
  913. console.log(new Date().getTime() - last.time, last)
  914. }
  915. this.windowPosition = position;
  916. if (this.windowPosition === this.data.length - this.windowLength - 1) {
  917. // enable auto scroll
  918. this.props.toggleAutoScroll(true);
  919. }
  920. // console.log('moveDataWindow = ', position, this.windowPosition, this.windowLength, this.data.length, this.autoScroll, this.windowedData);
  921. // update the map
  922. this.updateHeatmap();
  923. }
  924. }
  925. /**
  926. * This sets the Heatmap Zoom level aka. window.
  927. * @param {number} zoom The seconds to zoom into
  928. */
  929. setZoomLevel = (zoom) => {
  930. let l = Math.min(Math.max(zoom * 4, 3), this.data.length - 1);
  931. let l2 = this.windowLength - l;
  932. this.windowLength = l;
  933. this.moveDataWindow(this.windowPosition + l2);
  934. }
  935. /**
  936. * Render Function
  937. */
  938. render() {
  939. const { width, height } = this.props;
  940. // console.log('heatmap rendered', width, height, this.data);
  941. return (
  942. <canvas ref={this.canvasRef} width={width || 300} height={height || 150} tabIndex={1}
  943. style={{
  944. display: 'block',
  945. width: '100%',
  946. height: '100%',
  947. cursor: 'crosshair',
  948. paddingRight: '3%',
  949. boxSizing: "border-box"
  950. }}></canvas>
  951. );
  952. }
  953. }