Skip to content

Commit 5ac5740

Browse files
committed
feat: stock market
1 parent 535ef50 commit 5ac5740

File tree

5 files changed

+118
-0
lines changed

5 files changed

+118
-0
lines changed

docs/examples/stock-market.md

+98
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
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+

package-lock.json

+5
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
"@docusaurus/core": "^2.0.0-alpha.58",
1414
"@docusaurus/preset-classic": "^2.0.0-alpha.58",
1515
"@docusaurus/theme-live-codeblock": "^2.0.0-alpha.39",
16+
"chartist": "^0.11.4",
1617
"classnames": "^2.2.6",
1718
"emotion": "^10.0.27",
1819
"faker": "^4.1.0",

sidebars.js

+1
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ module.exports = {
7676
'examples/multi-sort',
7777
'examples/custom-sort',
7878
'examples/i18n',
79+
'examples/stock-market',
7980
]
8081
}]
8182
},

src/css/custom.css

+13
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,16 @@ ul.contents {
3737
width: 100%;
3838
display: table;
3939
}
40+
41+
.ct-chart svg {
42+
margin:1px;
43+
vertical-align:text-bottom;
44+
}
45+
.ct-series-a .ct-line,
46+
.ct-series-a .ct-bar {
47+
stroke: red;
48+
stroke-width: 1px;
49+
}
50+
.ct-chart-pie .ct-label {
51+
display:none;
52+
}

0 commit comments

Comments
 (0)