|
1 | | -# @gravity-ui/chartkit · [](LICENSE) [](https://www.npmjs.com/package/@gravity-ui/chartkit) [](https://preview.gravity-ui.com/chartkit/) |
| 1 | +# @gravity-ui/chartkit · [npm package](https://www.npmjs.com/package/@gravity-ui/chartkit) [License](LICENSE) [CI](https://github.com/gravity-ui/ChartKit/actions/workflows/ci.yml?query=branch:main) [storybook](https://preview.gravity-ui.com/chartkit/) |
2 | 2 |
|
3 | | -React component used to render charts based on any sources you need |
| 3 | +Plugin-based React component that provides a unified rendering interface for multiple charting libraries. You register one or more plugins and render charts via `<ChartKit type="..." data={...} />` — ChartKit dispatches to the correct renderer automatically. |
4 | 4 |
|
5 | | -## Install |
| 5 | +Each plugin renderer is lazy-loaded, so the underlying library code is only downloaded when ChartKit is actually rendered in the UI. ChartKit also handles mobile-friendly tooltip display out of the box. You can use the built-in plugins or implement your own. |
| 6 | + |
| 7 | +**When to use:** |
| 8 | + |
| 9 | +- You need modern declarative charts (`gravity-charts`) or time-series / monitoring charts (`yagr`) |
| 10 | +- You need multiple chart types under a single consistent API |
| 11 | +- You're building in the Gravity UI ecosystem |
| 12 | + |
| 13 | +**When not to use:** |
| 14 | + |
| 15 | +- You only need one specific chart library — prefer using [@gravity-ui/charts](https://github.com/gravity-ui/charts) directly |
| 16 | + |
| 17 | +## Table of contents |
| 18 | + |
| 19 | +- [Get started](#get-started) |
| 20 | +- [Development](#development) |
| 21 | + |
| 22 | +## Get started |
| 23 | + |
| 24 | +### Requirements |
| 25 | + |
| 26 | +- React 16, 17, or 18 |
| 27 | +- `[@gravity-ui/uikit](https://github.com/gravity-ui/uikit)` — required peer dependency (provides theming and UI primitives) |
| 28 | + |
| 29 | +### Install |
6 | 30 |
|
7 | 31 | ```shell |
8 | | -npm i --save-dev @gravity-ui/chartkit @gravity-ui/uikit |
| 32 | +npm install @gravity-ui/chartkit @gravity-ui/uikit |
9 | 33 | ``` |
10 | 34 |
|
11 | | -Make sure you have `@gravity-ui/uikit` styles enabled in your project. |
| 35 | +### Styles |
12 | 36 |
|
13 | | -```typescript |
14 | | -import '@gravity-ui/uikit/styles/styles.scss'; |
| 37 | +Import the styles from `@gravity-ui/uikit` in your entry point: |
| 38 | + |
| 39 | +```tsx |
| 40 | +import '@gravity-ui/uikit/styles/fonts.css'; |
| 41 | +import '@gravity-ui/uikit/styles/styles.css'; |
| 42 | +``` |
| 43 | + |
| 44 | +For full setup details see the [uikit styles guide](https://github.com/gravity-ui/uikit?tab=readme-ov-file#styles). |
| 45 | + |
| 46 | +### Basic usage |
| 47 | + |
| 48 | +ChartKit uses a global plugin registry. Call `settings.set` once at your app entry point to register the plugins you need. When `<ChartKit type="..." />` renders, it looks up the matching plugin — if none is found, an error is thrown. Each plugin's renderer is a `React.lazy` component, so its code is fetched only when ChartKit first appears in the UI. |
| 49 | + |
| 50 | +You can register multiple plugins at once: |
| 51 | + |
| 52 | +```ts |
| 53 | +settings.set({plugins: [GravityChartsPlugin, YagrPlugin]}); |
15 | 54 | ``` |
16 | 55 |
|
17 | | -## Usage |
| 56 | +Or call `settings.set` multiple times — it merges the plugin list rather than replacing it. |
| 57 | + |
| 58 | +**Basic example:** |
18 | 59 |
|
19 | | -```typescript |
| 60 | +```tsx |
20 | 61 | import {ThemeProvider} from '@gravity-ui/uikit'; |
21 | 62 | import ChartKit, {settings} from '@gravity-ui/chartkit'; |
22 | | -import {YagrPlugin} from '@gravity-ui/chartkit/yagr'; |
23 | | -import type {YagrWidgetData} from '@gravity-ui/chartkit/yagr'; |
| 63 | +import {GravityChartsPlugin} from '@gravity-ui/chartkit/gravity-charts'; |
24 | 64 |
|
25 | | -import '@gravity-ui/uikit/styles/styles.scss'; |
| 65 | +import '@gravity-ui/uikit/styles/fonts.css'; |
| 66 | +import '@gravity-ui/uikit/styles/styles.css'; |
26 | 67 |
|
27 | | -settings.set({plugins: [YagrPlugin]}); |
| 68 | +settings.set({plugins: [GravityChartsPlugin]}); |
28 | 69 |
|
29 | | -const data: YagrWidgetData = { |
30 | | - data: { |
31 | | - timeline: [ |
32 | | - 1636838612441, 1636925012441, 1637011412441, 1637097812441, 1637184212441, 1637270612441, |
33 | | - 1637357012441, 1637443412441, 1637529812441, 1637616212441, |
34 | | - ], |
35 | | - graphs: [ |
36 | | - { |
37 | | - id: '0', |
38 | | - name: 'Serie 1', |
39 | | - color: '#6c59c2', |
40 | | - data: [25, 52, 89, 72, 39, 49, 82, 59, 36, 5], |
41 | | - }, |
| 70 | +const data = { |
| 71 | + series: { |
| 72 | + data: [ |
42 | 73 | { |
43 | | - id: '1', |
44 | | - name: 'Serie 2', |
45 | | - color: '#6e8188', |
46 | | - data: [37, 6, 51, 10, 65, 35, 72, 0, 94, 54], |
47 | | - }, |
48 | | - ], |
49 | | - }, |
50 | | - libraryConfig: { |
51 | | - chart: { |
52 | | - series: { |
53 | 74 | type: 'line', |
| 75 | + name: 'Series', |
| 76 | + data: [ |
| 77 | + {x: 0, y: 10}, |
| 78 | + {x: 1, y: 25}, |
| 79 | + {x: 2, y: 18}, |
| 80 | + {x: 3, y: 30}, |
| 81 | + ], |
54 | 82 | }, |
55 | | - }, |
56 | | - title: { |
57 | | - text: 'line: random 10 pts', |
58 | | - }, |
| 83 | + ], |
59 | 84 | }, |
60 | 85 | }; |
61 | 86 |
|
62 | | -function App() { |
| 87 | +export default function App() { |
63 | 88 | return ( |
64 | | - <ThemeProvider> |
65 | | - <div className="app" style={{height: 500}}> |
66 | | - <ChartKit type="yagr" data={data} /> |
| 89 | + <ThemeProvider theme="light"> |
| 90 | + <div style={{height: 300}}> |
| 91 | + <ChartKit type="gravity-charts" data={data} /> |
67 | 92 | </div> |
68 | 93 | </ThemeProvider> |
69 | 94 | ); |
70 | 95 | } |
| 96 | +``` |
| 97 | + |
| 98 | +`ChartKit` adapts to its parent's size — make sure the container has an explicit height. |
| 99 | + |
| 100 | +## Development |
| 101 | + |
| 102 | +### Prerequisites |
| 103 | + |
| 104 | +- [Node.js](https://nodejs.org/) 22 (see [.nvmrc](https://github.com/gravity-ui/ChartKit/blob/main/.nvmrc)) |
| 105 | +- [npm](https://www.npmjs.com/) 10 or later |
71 | 106 |
|
72 | | -export default App; |
| 107 | +### Setup |
| 108 | + |
| 109 | +Clone the repository and install dependencies: |
| 110 | + |
| 111 | +```shell |
| 112 | +git clone https://github.com/gravity-ui/ChartKit.git |
| 113 | +cd ChartKit |
| 114 | +npm ci |
| 115 | +``` |
| 116 | + |
| 117 | +### Running Storybook |
| 118 | + |
| 119 | +```shell |
| 120 | +npm run start |
73 | 121 | ``` |
| 122 | + |
| 123 | +Storybook will be available at `http://localhost:7007`. |
| 124 | + |
| 125 | +### Developing with a local dependency |
| 126 | + |
| 127 | +To work on a dependency (e.g. `@gravity-ui/charts`) and see your changes live in Storybook without publishing to npm: |
| 128 | + |
| 129 | +**1. Link the local package** |
| 130 | + |
| 131 | +```shell |
| 132 | +# In your local clone of @gravity-ui/charts: |
| 133 | +git clone https://github.com/gravity-ui/charts.git |
| 134 | +cd charts |
| 135 | +npm ci |
| 136 | +# make your changes |
| 137 | +npm run build |
| 138 | +npm link |
| 139 | + |
| 140 | +# In ChartKit: |
| 141 | +npm link @gravity-ui/charts |
| 142 | +``` |
| 143 | + |
| 144 | +**2. Configure local package watching** |
| 145 | + |
| 146 | +Create a `.env.local` file in the ChartKit root (it is gitignored): |
| 147 | + |
| 148 | +```shell |
| 149 | +LOCAL_PKG=@gravity-ui/charts |
| 150 | +``` |
| 151 | + |
| 152 | +This tells Vite to watch that package in `node_modules` and skip pre-bundling it. After rebuilding `@gravity-ui/charts`, Storybook will hot-reload automatically. |
| 153 | + |
| 154 | +For multiple packages, use a comma-separated list: |
| 155 | + |
| 156 | +```shell |
| 157 | +LOCAL_PKG=@gravity-ui/charts,@gravity-ui/uikit |
| 158 | +``` |
| 159 | + |
| 160 | +**3. Start Storybook** |
| 161 | + |
| 162 | +```shell |
| 163 | +npm run start |
| 164 | +``` |
| 165 | + |
| 166 | +**4. Restore the original package** |
| 167 | + |
| 168 | +When you're done: |
| 169 | + |
| 170 | +1. Comment out `LOCAL_PKG` in `.env.local` |
| 171 | +2. Run `npm install` in ChartKit — it replaces the symlink with the registry version |
| 172 | + |
| 173 | +```shell |
| 174 | +# In ChartKit: |
| 175 | +npm ci |
| 176 | +``` |
| 177 | + |
| 178 | +### Running tests |
| 179 | + |
| 180 | +```shell |
| 181 | +npm test |
| 182 | +``` |
| 183 | + |
| 184 | +Visual regression tests run in Docker to ensure consistent screenshots across environments: |
| 185 | + |
| 186 | +```shell |
| 187 | +npm run test:docker |
| 188 | +``` |
| 189 | + |
| 190 | +To update the reference screenshots after intentional UI changes: |
| 191 | + |
| 192 | +```shell |
| 193 | +npm run test:docker:update |
| 194 | +``` |
| 195 | + |
| 196 | +### Contributing |
| 197 | + |
| 198 | +Please refer to the [contributing guide](CONTRIBUTING.md) before submitting a pull request. |
0 commit comments