|
| 1 | +# @strawberry-vis/g2-rect |
| 2 | + |
| 3 | +A lightweight React Component for @antv/g2. |
| 4 | + |
| 5 | +## Installing |
| 6 | + |
| 7 | +```bash |
| 8 | +npm install @strawberry-vis/g2-rect |
| 9 | +``` |
| 10 | + |
| 11 | +```jsx |
| 12 | +import React, { useState } from "react"; |
| 13 | +import { Chart } from "@strawberry-vis/g2-rect"; |
| 14 | + |
| 15 | +export function Card() { |
| 16 | + return ( |
| 17 | + <Chart |
| 18 | + options={{ |
| 19 | + type: "interval", |
| 20 | + data: [ |
| 21 | + { genre: "Sports", sold: 275 }, |
| 22 | + { genre: "Strategy", sold: 115 }, |
| 23 | + { genre: "Action", sold: 120 }, |
| 24 | + { genre: "Shooter", sold: 350 }, |
| 25 | + { genre: "Other", sold: 150 }, |
| 26 | + ], |
| 27 | + encode: { x: "genre", y: "sold" }, |
| 28 | + }} |
| 29 | + /> |
| 30 | + ); |
| 31 | +} |
| 32 | +``` |
| 33 | + |
| 34 | +## API Reference |
| 35 | + |
| 36 | +<a name="props-options" href="#props-options">#</a> props.**options** |
| 37 | + |
| 38 | +```js |
| 39 | +import React, { useState } from "react"; |
| 40 | +import { Chart } from "@strawberry-vis/g2-rect"; |
| 41 | + |
| 42 | +export function Card() { |
| 43 | + const [options, setOptions] = useState({ |
| 44 | + type: "interval", |
| 45 | + data: [ |
| 46 | + { genre: "Sports", sold: 275 }, |
| 47 | + { genre: "Strategy", sold: 115 }, |
| 48 | + { genre: "Action", sold: 120 }, |
| 49 | + { genre: "Shooter", sold: 350 }, |
| 50 | + { genre: "Other", sold: 150 }, |
| 51 | + ], |
| 52 | + encode: { x: "genre", y: "sold" }, |
| 53 | + }); |
| 54 | + |
| 55 | + function onClick() { |
| 56 | + setOptions({ ...options, data: options.data.slice(0, 2) }); |
| 57 | + } |
| 58 | + |
| 59 | + return ( |
| 60 | + <div> |
| 61 | + <Chart options={options} /> |
| 62 | + <button onClick={onClick}>Update Options</button> |
| 63 | + </div> |
| 64 | + ); |
| 65 | +} |
| 66 | +``` |
| 67 | + |
| 68 | +<a name="props-chartref" href="#props-chartref">#</a> props.**chartRef** |
| 69 | + |
| 70 | +```js |
| 71 | +import React, { useRef, useEffect } from "react"; |
| 72 | +import { Chart } from "@strawberry-vis/g2-rect"; |
| 73 | + |
| 74 | +export function Card() { |
| 75 | + const chartRef = useRef(); |
| 76 | + |
| 77 | + useEffect(() => { |
| 78 | + const chart = chartRef.current; |
| 79 | + chart.on("afterrender", () => {}); |
| 80 | + }, []); |
| 81 | + |
| 82 | + return <Chart options={options} chartRef={chartRef} />; |
| 83 | +} |
| 84 | +``` |
0 commit comments