Skip to content

Commit d87289d

Browse files
authored
chore: add work with local deps & improve readme (#814)
* docs: improve README * docs: support local package development via npm link * fix: fix restore original pkg instruction * fix: fix restore original pkg instruction 2 * fix: prettier fixes
1 parent fb44ada commit d87289d

3 files changed

Lines changed: 198 additions & 45 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
node_modules
2+
.env.local
23
coverage
34
.nyc_output
45
.DS_Store

.storybook/main.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import type {StorybookConfig} from '@storybook/react-vite';
2+
import type {InlineConfig} from 'vite';
23

34
const config: StorybookConfig = {
45
framework: {
@@ -19,6 +20,32 @@ const config: StorybookConfig = {
1920
},
2021
};
2122
},
23+
viteFinal: (viteConfig: InlineConfig) => {
24+
const localPkg = process.env.LOCAL_PKG;
25+
26+
if (!localPkg) {
27+
return viteConfig;
28+
}
29+
30+
const localPkgs = localPkg.split(',').map((s) => s.trim());
31+
32+
return {
33+
...viteConfig,
34+
server: {
35+
...viteConfig.server,
36+
watch: {
37+
...viteConfig.server?.watch,
38+
ignored: (path: string) =>
39+
path.includes('node_modules') &&
40+
!localPkgs.some((pkg) => path.includes(pkg)),
41+
},
42+
},
43+
optimizeDeps: {
44+
...viteConfig.optimizeDeps,
45+
exclude: [...(viteConfig.optimizeDeps?.exclude ?? []), ...localPkgs],
46+
},
47+
};
48+
},
2249
};
2350

2451
export default config;

README.md

Lines changed: 170 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,73 +1,198 @@
1-
# @gravity-ui/chartkit · [![license](https://img.shields.io/badge/license-MIT-brightgreen.svg)](LICENSE) [![npm package](https://img.shields.io/npm/v/@gravity-ui/chartkit)](https://www.npmjs.com/package/@gravity-ui/chartkit) [![storybook](https://img.shields.io/badge/Storybook-deployed-ff4685)](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/)
22

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.
44

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
630

731
```shell
8-
npm i --save-dev @gravity-ui/chartkit @gravity-ui/uikit
32+
npm install @gravity-ui/chartkit @gravity-ui/uikit
933
```
1034

11-
Make sure you have `@gravity-ui/uikit` styles enabled in your project.
35+
### Styles
1236

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]});
1554
```
1655

17-
## Usage
56+
Or call `settings.set` multiple times — it merges the plugin list rather than replacing it.
57+
58+
**Basic example:**
1859

19-
```typescript
60+
```tsx
2061
import {ThemeProvider} from '@gravity-ui/uikit';
2162
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';
2464

25-
import '@gravity-ui/uikit/styles/styles.scss';
65+
import '@gravity-ui/uikit/styles/fonts.css';
66+
import '@gravity-ui/uikit/styles/styles.css';
2667

27-
settings.set({plugins: [YagrPlugin]});
68+
settings.set({plugins: [GravityChartsPlugin]});
2869

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: [
4273
{
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: {
5374
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+
],
5482
},
55-
},
56-
title: {
57-
text: 'line: random 10 pts',
58-
},
83+
],
5984
},
6085
};
6186

62-
function App() {
87+
export default function App() {
6388
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} />
6792
</div>
6893
</ThemeProvider>
6994
);
7095
}
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
71106

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
73121
```
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

Comments
 (0)