index.js 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020
  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.side === 'sell' ? '卖出' : '买入'} ${depth.lastTradedQty} 在 ${depth.lastTradedPrice} `;
  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. if (this.windowedData.length > 0) {
  431. this.drawingContext.fillStyle = this.defaults.textOnBackground;
  432. this.drawingContext.font = '12px Arial';
  433. this.drawingContext.fillText(`
  434. 最后交易价格: ${this.windowedData[this.windowedData.length - 1].marketDepth.lastTradedPrice}
  435. 最后交易数量: ${this.windowedData[this.windowedData.length - 1].marketDepth.lastTradedQty}
  436. 最后交易时间: ${this.windowedData[this.windowedData.length - 1].ts}
  437. `, 20 + w + 40, this.defaults.axisTickSize + this.defaults.xAxisTextPadding + 20);
  438. }
  439. this.drawingContext.fillStyle = this.defaults.textOnBackground;
  440. this.drawingContext.lineWidth = 1.2;
  441. this.drawingContext.strokeStyle = this.defaults.axisColor;
  442. this.drawingContext.stroke();
  443. this.drawingContext.restore();
  444. }
  445. /**
  446. * Draws Y Axis and Bid Ask graph at the same time
  447. */
  448. drawYAxisAndBidAskGraph = () => {
  449. if (this.yDomainValues !== null) {
  450. const yh2 = this.yScale.bandwidth() * 0.5;
  451. // clear canvas before axis draw
  452. this.clearCanvas(
  453. this.defaults.borderPadding[3] + this.defaults.hmWidth(), this.defaults.borderPadding[0],
  454. this.defaults.axisYWidth, this.defaults.hmHeight() + this.defaults.axisTickSize, this.defaults.clearColor
  455. );
  456. // translate and draw
  457. this.drawingContext.save();
  458. this.drawingContext.beginPath();
  459. this.drawingContext.translate(this.defaults.borderPadding[3] + this.defaults.hmWidth(), this.defaults.borderPadding[0]);
  460. this.drawingContext.moveTo(0, 0);
  461. this.drawingContext.lineTo(0, this.defaults.hmHeight() + this.defaults.axisTickSize);
  462. this.drawingContext.textAlign = 'start';
  463. this.drawingContext.textBaseline = 'top';
  464. let maxTextWidth = 0;
  465. this.yDomainValues.map(d => {
  466. let y = this.yScale(d) + yh2;
  467. this.drawingContext.moveTo(0, y);
  468. this.drawingContext.lineTo(this.defaults.axisTickSize, y);
  469. // 大于7位,换行绘制
  470. if (String(d).length <= 7) {
  471. y -= 5
  472. this.drawingContext.fillText(d, this.defaults.axisTickSize + this.defaults.yAxisTextPadding, y, this.defaults.axisYWidth - this.defaults.axisTickSize + this.defaults.yAxisTextPadding);
  473. } else {
  474. y -= 10
  475. let text = String(d)
  476. let [t0, t1] = text.split('.')
  477. this.drawingContext.fillText(t0, this.defaults.axisTickSize + this.defaults.yAxisTextPadding, y, this.defaults.axisYWidth - this.defaults.axisTickSize + this.defaults.yAxisTextPadding);
  478. this.drawingContext.fillText('.'.concat(t1), this.defaults.axisTickSize + this.defaults.yAxisTextPadding, y + 10, this.defaults.axisYWidth - this.defaults.axisTickSize + this.defaults.yAxisTextPadding);
  479. }
  480. let tw = this.drawingContext.measureText(d).width;
  481. maxTextWidth = maxTextWidth >= tw ? maxTextWidth : tw;
  482. });
  483. this.drawingContext.lineWidth = 1.2;
  484. this.drawingContext.strokeStyle = this.defaults.axisColor;
  485. this.drawingContext.stroke();
  486. this.drawingContext.restore();
  487. // Now I will draw the bid ask strength graph,
  488. const x = this.defaults.borderPadding[3] + this.defaults.hmWidth() + maxTextWidth + this.defaults.axisTickSize + this.defaults.yAxisTextPadding + this.defaults.bidAskGraphPaddingLeft;
  489. const y = this.defaults.borderPadding[0];
  490. this.drawBidAskGraph(x, y);
  491. }
  492. }
  493. /**
  494. * Draw and animate Bid Ask graph
  495. * @param {number} x
  496. * @param {number} y
  497. */
  498. drawBidAskGraph = (x, y) => {
  499. if (this.windowedData.length > 0) {
  500. if (this.bidAskAnimTimer !== null) {
  501. this.bidAskAnimTimer.stop();
  502. this.bidAskAnimTimer = null;
  503. }
  504. this.bidAskAnimTimer = d3.timer(elapsed => {
  505. // compute how far through the animation we are (0 to 1)
  506. const t = Math.min(1, d3.easeCubic(elapsed / this.defaults.bidAskTransitionDuration));
  507. // ----------------draw--------------------
  508. // console.log('drawing bid ask graph');
  509. this.clearCanvas(
  510. x, y, this.defaults.bidAskWidth, this.defaults.hmHeight() + this.defaults.axisTickSize, this.defaults.clearColor
  511. );
  512. const h = this.yScale.bandwidth() - 2;
  513. const d = this.windowedData[this.windowedData.length - 1];
  514. if (!d) {
  515. return
  516. }
  517. const maxBidAskVol = extractMaxVolume(d);
  518. this.drawingContext.save();
  519. this.drawingContext.translate(x, y);
  520. this.drawingContext.lineWidth = 0;
  521. this.drawingContext.textBaseline = 'middle';
  522. const drawBars = (arr, color, textColor) => {
  523. arr.map(v => {
  524. this.drawingContext.fillStyle = color;
  525. const l = this.defaults.bidAskWidth * (+v.qty / maxBidAskVol);
  526. // save v bars length
  527. this.drawingContext.fillRect(0, this.yScale(v.rate), l, h);
  528. let textWidth = this.drawingContext.measureText(v.qty).width;
  529. if (this.defaults.bidAskWidth - l - textWidth >= textWidth) {
  530. // text outside bar
  531. this.drawingContext.textAlign = 'start';
  532. this.drawingContext.fillStyle = this.defaults.textOnBackground;
  533. this.drawingContext.fillText(v.qty, l + 1, this.yScale(v.rate) + h / 2 + 1);
  534. } else {
  535. this.drawingContext.textAlign = 'end';
  536. this.drawingContext.fillStyle = textColor;
  537. this.drawingContext.fillText(v.qty, l - textWidth, this.yScale(v.rate) + h / 2 + 1);
  538. }
  539. });
  540. }
  541. drawBars(d.marketDepth.buys, this.defaults.buyColor, this.defaults.textOnBuyColor);
  542. drawBars(d.marketDepth.sells, this.defaults.sellColor, this.defaults.textOnSellColor);
  543. this.drawingContext.restore();
  544. // ----------------draw--------------------
  545. // if this animation is over
  546. if (t === 1) this.bidAskAnimTimer.stop();
  547. });
  548. }
  549. }
  550. /**
  551. * Draws background heatmap for both buys and sells
  552. */
  553. drawMainGraph = () => {
  554. this.drawingContext.save();
  555. if (this.xScale && this.yScale && this.bidAskScale && this.drawingContext !== null) {
  556. const avgTradedVolume = extractAvgTradedVolume(this.windowedData);
  557. const maxBidAskVolume = extractMaxAskBidVolume(this.windowedData);
  558. const xh2 = this.xScale.bandwidth() * 0.5;
  559. const yh2 = this.yScale.bandwidth() * 0.5;
  560. const panel = this
  561. this.drawingContext.translate(this.defaults.borderPadding[3], this.defaults.borderPadding[0]);
  562. this.windowedData.map(d => {
  563. const marketDepth = d.marketDepth;
  564. const ask1 = marketDepth.sells[0];
  565. const bid1 = marketDepth.buys[0];
  566. const ts = d.ts;
  567. // draw buys
  568. if (marketDepth.buys && marketDepth.buys.length > 0) {
  569. marketDepth.buys.map(buy => {
  570. let rate = buy.qty / maxBidAskVolume;
  571. let colorIndex = Math.min(parseInt(rate / 0.25), panel.orderbookColors.length - 1);
  572. let color = d3.color(panel.orderbookColors[colorIndex]).rgb()
  573. color.opacity = 0.5 + 0.5 * (rate % 0.25) / 0.25;
  574. this.drawingContext.fillStyle = color.toString();
  575. this.drawingContext.fillRect(
  576. this.xScale(ts),
  577. this.yScale(buy.rate), // 减去半个方块的高度
  578. this.xScale.bandwidth(),
  579. this.yScale.bandwidth()
  580. );
  581. });
  582. }
  583. // draw sells
  584. if (marketDepth.sells && marketDepth.sells.length > 0) {
  585. marketDepth.sells.map(sell => {
  586. let rate = sell.qty / maxBidAskVolume;
  587. let colorIndex = Math.min(parseInt(rate / 0.25), panel.orderbookColors.length - 1);
  588. let color = d3.color(panel.orderbookColors[colorIndex]).rgb()
  589. color.opacity = 0.5 + 0.5 * (rate % 0.25) / 0.25;
  590. this.drawingContext.fillStyle = color.toString();
  591. this.drawingContext.fillRect(
  592. this.xScale(ts),
  593. this.yScale(sell.rate), // 减去半个方块的高度
  594. this.xScale.bandwidth(),
  595. this.yScale.bandwidth()
  596. );
  597. });
  598. }
  599. });
  600. // draw trade size
  601. this.circles = []
  602. this.windowedData.map(d => {
  603. const marketDepth = d.marketDepth;
  604. if (+marketDepth.lastTradedQty === 0) {
  605. return
  606. }
  607. const ask1 = marketDepth.sells[0];
  608. const bid1 = marketDepth.buys[0];
  609. const ts = d.ts;
  610. const maxBidAskVol = extractMaxVolume(d);
  611. let trade_color = d3.color(marketDepth.side == 'sell' ? '#cc5040' : '#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.lastTradedQty / avgTradedVolume)))
  615. this.drawingContext.beginPath();
  616. this.drawingContext.arc(
  617. this.xScale(ts) + xh2,
  618. this.yScale(+marketDepth.lastTradedPrice) + yh2,
  619. r, 0, 2 * Math.PI
  620. );
  621. this.drawingContext.strokeStyle = trade_color;
  622. this.drawingContext.fill();
  623. // 为球添加白色边框
  624. this.drawingContext.lineWidth = 1; // 设置线宽,可以根据需要调整
  625. this.drawingContext.strokeStyle = 'white'; // 设置描边颜色为白色
  626. this.drawingContext.stroke(); // 执行描边操作
  627. // 事件
  628. this.circles.push({
  629. x: this.xScale(ts) + xh2,
  630. y: this.yScale(+marketDepth.lastTradedPrice) + yh2,
  631. radius: r,
  632. data: d // 存储与圆相关的数据,便于在事件处理时使用
  633. });
  634. })
  635. // draw buy line path
  636. let buy_line_color = d3.color(this.defaults.buyColor).rgb();
  637. this.drawingContext.fillStyle = buy_line_color.toString();
  638. this.drawingContext.beginPath();
  639. d3.line()
  640. .x(d => this.xScale(d.ts))
  641. .y(d => this.yScale(d.marketDepth.buys[0].rate) + yh2)
  642. // .curve(d3.curveLinear)
  643. .context(this.drawingContext)
  644. (this.windowedData);
  645. this.drawingContext.lineWidth = 2;
  646. this.drawingContext.strokeStyle = this.defaults.buyColor;
  647. this.drawingContext.stroke();
  648. // draw sell line path
  649. let sell_line_color = d3.color(this.defaults.sellColor).rgb();
  650. this.drawingContext.fillStyle = sell_line_color.toString();
  651. this.drawingContext.beginPath();
  652. d3.line()
  653. .x(d => this.xScale(d.ts))
  654. .y(d => this.yScale(d.marketDepth.sells[0].rate) + yh2)
  655. // .curve(d3.curveLinear)
  656. .context(this.drawingContext)
  657. (this.windowedData);
  658. this.drawingContext.lineWidth = 2;
  659. this.drawingContext.strokeStyle = this.defaults.sellColor;
  660. this.drawingContext.stroke();
  661. }
  662. this.drawingContext.restore();
  663. }
  664. /**
  665. * Clear the canvas area
  666. * @param {number} x x coordinate
  667. * @param {number} y y xoordinate
  668. * @param {number} w width
  669. * @param {number} h height
  670. * @param {string} color color string
  671. */
  672. clearCanvas = (x, y, w, h, color) => {
  673. // console.log('clear canvas area', x, y, w, h, color);
  674. if (this.drawingContext !== null) {
  675. this.drawingContext.save();
  676. this.drawingContext.fillStyle = color || this.defaults.clearColor;
  677. this.drawingContext.fillRect(x, y, w, h);
  678. this.drawingContext.restore();
  679. }
  680. }
  681. // ------------------------------ END: Canvas draw functions ---------------------------------------
  682. /**
  683. * Set Data for the Heatmap to generate
  684. * @param {any[]} data The data to set
  685. */
  686. setData = (data) => {
  687. // console.log('setdata called=', data);
  688. if (data && data.length > 0) {
  689. this.data = data;
  690. this.updateWindowedData();
  691. }
  692. }
  693. /**
  694. * Add as extra data to existing data array.
  695. * @param {any} data
  696. */
  697. addData = (data) => {
  698. if (typeof (data) === 'object') {
  699. this.data.push(data);
  700. this.updateWindowedData();
  701. }
  702. }
  703. /**
  704. * This updates the data in array to be viewed in graph
  705. */
  706. updateWindowedData = () => {
  707. // console.log('window data updated');
  708. if (this.props.autoScroll) {
  709. this.moveDataWindow(this.data.length - this.windowLength - 1);
  710. }
  711. }
  712. isOrderBooksEquals = (orderbooks1, orderbooks2) => {
  713. for (let i = 0; i < orderbooks1.length; i++) {
  714. let orderbook1 = orderbooks1[i]
  715. let orderbook2 = orderbooks2[i]
  716. // 如果d1有,d2没有,那证明不相等
  717. if (!orderbook2) return false
  718. if (orderbook1.rate !== orderbook2.rate) return false
  719. let r1 = (orderbook1.qty / this.maxBidAskVolume);
  720. let o1ColorIndex = Math.min(parseInt(r1 / 0.25), this.orderbookColors.length - 1);
  721. let r2 = (orderbook2.qty / this.maxBidAskVolume);
  722. let o2ColorIndex = Math.min(parseInt(r2 / 0.25), this.orderbookColors.length - 1);
  723. if (o1ColorIndex !== o2ColorIndex) return false
  724. }
  725. return true
  726. }
  727. isDepthEquals = (d1, d2) => {
  728. // buys
  729. if (!this.isOrderBooksEquals(d1.buys, d2.buys)) return false
  730. // sells
  731. if (!this.isOrderBooksEquals(d1.sells, d2.sells)) return false
  732. return true
  733. }
  734. mergeSnapshots = (snapshots) => {
  735. // 初始化合并后的结构
  736. const merged = {
  737. marketDepth: {
  738. avgPrice: 0,
  739. buyOrderVolume: 0,
  740. sellOrderVolume: 0,
  741. lastTradedPrice: 0,
  742. lastTradedQty: 0,
  743. lastTradedTS: 0,
  744. open: 0,
  745. high: 0,
  746. low: 0,
  747. close: 0,
  748. priceChangeAmt: 0,
  749. priceChangePct: "0",
  750. buys: [],
  751. sells: []
  752. },
  753. pendingOrders: [],
  754. time: "",
  755. tradingsymbol: "",
  756. ts: ""
  757. };
  758. // 初始化计数器
  759. let totalAvgPrice = 0;
  760. let totalBuyOrderVolume = 0;
  761. let totalSellOrderVolume = 0;
  762. let totalSnapshots = snapshots.length;
  763. // 初始化买卖数组
  764. let buySums = Array(5).fill({ rate: 0, orders: 0, qty: 0 });
  765. let sellSums = Array(5).fill({ rate: 0, orders: 0, qty: 0 });
  766. // 初始化交易数量
  767. let totalLastTradeQtyBuy = 0;
  768. let totalLastTradeQtySell = 0;
  769. // 记录第一个快照的时间和交易符号
  770. merged.time = snapshots[0].time;
  771. merged.tradingsymbol = snapshots[0].tradingsymbol;
  772. merged.ts = snapshots[0].ts;
  773. // 遍历所有快照数据
  774. snapshots.forEach(snapshot => {
  775. // 累加市场深度数据
  776. totalAvgPrice += snapshot.marketDepth.avgPrice;
  777. totalBuyOrderVolume += snapshot.marketDepth.buyOrderVolume;
  778. totalSellOrderVolume += snapshot.marketDepth.sellOrderVolume;
  779. // 合并买单和卖单
  780. snapshot.marketDepth.buys.forEach((buy, index) => {
  781. buySums[index] = {
  782. rate: buy.rate,
  783. orders: buySums[index].orders + buy.orders,
  784. qty: buySums[index].qty + buy.qty
  785. };
  786. });
  787. snapshot.marketDepth.sells.forEach((sell, index) => {
  788. sellSums[index] = {
  789. rate: sell.rate,
  790. orders: sellSums[index].orders + sell.orders,
  791. qty: sellSums[index].qty + sell.qty
  792. };
  793. });
  794. // 合并最后交易的数量和价格
  795. if (snapshot.marketDepth.side === 'buy') {
  796. totalLastTradeQtyBuy += snapshot.marketDepth.lastTradedQty;
  797. } else if (snapshot.marketDepth.side === 'sell') {
  798. totalLastTradeQtySell += snapshot.marketDepth.lastTradedQty;
  799. }
  800. // 合并其他字段
  801. merged.marketDepth.close = snapshot.marketDepth.close;
  802. merged.marketDepth.high = snapshot.marketDepth.high;
  803. merged.marketDepth.low = snapshot.marketDepth.low;
  804. merged.marketDepth.open = snapshot.marketDepth.open;
  805. merged.marketDepth.priceChangeAmt = snapshot.marketDepth.priceChangeAmt;
  806. merged.marketDepth.priceChangePct = snapshot.marketDepth.priceChangePct;
  807. merged.marketDepth.lastTradedTS = snapshot.marketDepth.lastTradedTS;
  808. });
  809. // 计算平均市场深度数据
  810. merged.marketDepth.avgPrice = totalAvgPrice / totalSnapshots;
  811. merged.marketDepth.buyOrderVolume = totalBuyOrderVolume / totalSnapshots;
  812. merged.marketDepth.sellOrderVolume = totalSellOrderVolume / totalSnapshots;
  813. // 计算平均买单和卖单
  814. merged.marketDepth.buys = buySums.map(buy => ({
  815. rate: buy.rate,
  816. orders: buy.orders / totalSnapshots,
  817. qty: buy.qty / totalSnapshots
  818. }));
  819. merged.marketDepth.sells = sellSums.map(sell => ({
  820. rate: sell.rate,
  821. orders: sell.orders / totalSnapshots,
  822. qty: sell.qty / totalSnapshots
  823. }));
  824. // 计算最终的最后交易的数量和价格
  825. let lastTradeQtyDiff = totalLastTradeQtyBuy - totalLastTradeQtySell;
  826. merged.marketDepth.lastTradedQty = Math.abs(lastTradeQtyDiff);
  827. merged.marketDepth.side = lastTradeQtyDiff > 0 ? 'buy' : 'sell';
  828. merged.marketDepth.lastTradedPrice = lastTradeQtyDiff > 0 ? merged.marketDepth.sells[0].rate : merged.marketDepth.buys[0].rate;
  829. return merged;
  830. }
  831. mergeWindowedData = () => {
  832. let windowedData = this.windowedData;
  833. this.maxBidAskVolume = extractMaxAskBidVolume(this.windowedData);
  834. let mergedWindowedData = [];
  835. let panel = this
  836. let prevData = undefined;
  837. let snapshots = [];
  838. windowedData.map((d, i) => {
  839. // 最后一个元素要展示,不然会丢失盘口细节
  840. if (i === windowedData.length - 1) {
  841. mergedWindowedData.push(d)
  842. prevData = d
  843. // 如果是第一个数据,则进行初始化
  844. } else if (!prevData) {
  845. prevData = d
  846. // 如果是中间的数据,则进行逻辑判断,是否需要合并
  847. } else if (!panel.isDepthEquals(prevData.marketDepth, d.marketDepth)) {
  848. let mergedData = panel.mergeSnapshots(snapshots)
  849. mergedWindowedData.push(mergedData)
  850. prevData = d
  851. snapshots = [];
  852. }
  853. snapshots.push(d)
  854. });
  855. return mergedWindowedData;
  856. }
  857. /**
  858. * Move the position of data window within the main data.
  859. * @param {number} position The target position of the window to be moved to.
  860. */
  861. moveDataWindow = (position) => {
  862. if (position !== this.windowPosition && position > -1 && position < this.data.length - this.windowLength) {
  863. // move position only if within valid range
  864. this.windowedData = this.data.slice(position, position + this.windowLength + 1);
  865. if (this.windowedData.length > 1000) {
  866. this.windowedData = this.mergeWindowedData();
  867. this.isMerged = true;
  868. } else {
  869. this.isMerged = false;
  870. }
  871. // 延迟日志
  872. if (this.windowedData.length > 1) {
  873. let last = this.windowedData[this.windowedData.length - 1]
  874. console.log(new Date().getTime() - last.time, last)
  875. }
  876. this.windowPosition = position;
  877. if (this.windowPosition === this.data.length - this.windowLength - 1) {
  878. // enable auto scroll
  879. this.props.toggleAutoScroll(true);
  880. }
  881. // console.log('moveDataWindow = ', position, this.windowPosition, this.windowLength, this.data.length, this.autoScroll, this.windowedData);
  882. // update the map
  883. this.updateHeatmap();
  884. }
  885. }
  886. /**
  887. * This sets the Heatmap Zoom level aka. window.
  888. * @param {number} zoom The seconds to zoom into
  889. */
  890. setZoomLevel = (zoom) => {
  891. let l = Math.min(Math.max(zoom * 4, 3), this.data.length - 1);
  892. let l2 = this.windowLength - l;
  893. this.windowLength = l;
  894. this.moveDataWindow(this.windowPosition + l2);
  895. }
  896. /**
  897. * Render Function
  898. */
  899. render() {
  900. const { width, height } = this.props;
  901. // console.log('heatmap rendered', width, height, this.data);
  902. return (
  903. <canvas ref={this.canvasRef} width={width || 300} height={height || 150} tabIndex={1}
  904. style={{
  905. display: 'block',
  906. width: '100%',
  907. height: '100%',
  908. cursor: 'crosshair',
  909. paddingRight: '3%',
  910. boxSizing: "border-box"
  911. }}></canvas>
  912. );
  913. }
  914. }