|
| 1 | +--- |
| 2 | +id: stock-market |
| 3 | +title: Stock Market |
| 4 | +keywords: |
| 5 | + - javascript |
| 6 | + - table |
| 7 | + - javascript table |
| 8 | + - gridjs |
| 9 | + - grid js |
| 10 | + - formatting |
| 11 | + - stock market |
| 12 | + |
| 13 | +--- |
| 14 | + |
| 15 | +import { Grid, h, createRef as gCreateRef } from "gridjs"; |
| 16 | +import CodeBlock from "@theme/CodeBlock" |
| 17 | +import { useEffect, useRef, useState } from "react"; |
| 18 | +import Chartist from 'chartist'; |
| 19 | +import "chartist/dist/chartist.min.css"; |
| 20 | + |
| 21 | +Grid.js can render complex elements in the table cells. In this example, we will take advantage of the builtin Virtual DOM |
| 22 | +and Chartist library to render line charts in cells. |
| 23 | + |
| 24 | +```js |
| 25 | +import { Grid, h, createRef as gCreateRef } from "gridjs"; |
| 26 | +import Chartist from 'chartist'; |
| 27 | +``` |
| 28 | + |
| 29 | +<CodeBlock children={ |
| 30 | +` |
| 31 | +// Chartist options |
| 32 | +const opts = { |
| 33 | + height: '30px', |
| 34 | + showPoint: false, |
| 35 | + fullWidth:true, |
| 36 | + chartPadding: {top: 0,right: 0,bottom: 0,left: 0}, |
| 37 | + axisX: {showGrid: false, showLabel: false, offset: 0}, |
| 38 | + axisY: {showGrid: false, showLabel: false, offset: 0} |
| 39 | +}; |
| 40 | +const grid = new Grid({ |
| 41 | + sort: true, |
| 42 | + columns: [ |
| 43 | + 'Symbol', |
| 44 | + 'Last price', |
| 45 | + { |
| 46 | + name: 'Difference', |
| 47 | + formatter: (cell) => { |
| 48 | + return h('b', { style: { |
| 49 | + 'color': cell > 0 ? 'green' : 'red' |
| 50 | + }}, cell); |
| 51 | + } |
| 52 | + }, |
| 53 | + { |
| 54 | + name: 'Trend', |
| 55 | + sort: false, |
| 56 | + width: '100px', |
| 57 | + formatter: (cell) => { |
| 58 | + const ref = gCreateRef(); |
| 59 | + const chart = h('div', { ref: ref }) |
| 60 | + |
| 61 | + // setTimeout to ensure that the chart wrapper is mounted |
| 62 | + setTimeout(() => { |
| 63 | + // render the chart based on cell data |
| 64 | + ref.current && new Chartist.Line(ref.current, { |
| 65 | + series: [cell] |
| 66 | + }, opts); |
| 67 | + }, 0); |
| 68 | + |
| 69 | + return chart; |
| 70 | + } |
| 71 | + }], |
| 72 | + data: [ |
| 73 | + ['AAPL', 360.2, 20.19, [360, 363, 366, 361, 366, 350, 370]], |
| 74 | + ['ETSY', 102.1, 8.22, [90, 91, 92, 90, 94, 95, 99, 102]], |
| 75 | + ['AMZN', 2734.8, -30.01, [2779, 2786, 2792, 2780, 2750, 2765, 2740, 2734]], |
| 76 | + ['TSLA', 960.85, -40.91, [993, 990, 985, 983, 970, 985, 988, 960]], |
| 77 | + ] |
| 78 | +}); |
| 79 | +` |
| 80 | +} |
| 81 | + transformCode={(code) => |
| 82 | +` |
| 83 | +function () { |
| 84 | + ${code} |
| 85 | + |
| 86 | + const wrapperRef = useRef(null); |
| 87 | + |
| 88 | + useEffect(() => { |
| 89 | + grid.render(wrapperRef.current); |
| 90 | + }); |
| 91 | + |
| 92 | + return ( |
| 93 | + <div ref={wrapperRef} /> |
| 94 | + ); |
| 95 | +} |
| 96 | +` |
| 97 | +} live={true} scope={{ Grid, h, gCreateRef, CodeBlock, useEffect, useRef, useState, Chartist }} /> |
| 98 | + |
0 commit comments