Skip to content

Commit 69dfcee

Browse files
docs(website): new website (#825)
* docs(website): new website * refactor(website): updates to the unpublished website (#876) * refactor: change CodeSandbox theme from dark to light to improve look and feel * refactor: update "Working with events" * refactor: update "Working with datasets" * refactor: update "Migration to v4" * docs: add "Why v2" * docs: add "Using with Chart.js v2" * refactor: tune logo size * refactor: update index page * style: format svg logo * fix: fix description for SEO; fix link to section * docs: dynamic codesandbox theme Co-authored-by: dangreen <danon0404@gmail.com> * refactor: add logo and link to Stack Overflow (#877) * chore: install peer dependencies * docs: add more links to docs section in footer Co-authored-by: Igor Lukanin <mail+github@igor.lukanin.name>
1 parent a2636d1 commit 69dfcee

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

59 files changed

+10872
-249
lines changed

.editorconfig

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,3 @@ trim_trailing_whitespace = true
99
insert_final_newline = true
1010
indent_style = space
1111
indent_size = 2
12-
13-
[*.json]
14-
indent_style = space
15-
indent_size = 2
16-
17-
[*.md]
18-
trim_trailing_whitespace = false
19-
indent_style = tab

README.md

Lines changed: 18 additions & 240 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,25 @@
1-
<div align="center">
2-
3-
<h1>React components for <a href="https://www.chartjs.org">Chart.js</a> 📊📈</h1>
4-
5-
Simple yet flexible charts for your React app
6-
1+
# react-chartjs-2
2+
3+
<img align="right" width="150" height="150" alt="Logo" src="https://raw.githubusercontent.com/reactchartjs/react-chartjs-2/master/website/static/img/logo.svg">
4+
5+
React components for <a href="https://www.chartjs.org">Chart.js</a>, the most popular charting library.
6+
7+
Supports Chart.js v3 and v2.
8+
79
[![version](https://img.shields.io/npm/v/react-chartjs-2.svg)](https://www.npmjs.com/package/react-chartjs-2)
810
[![downloads](https://img.shields.io/npm/dm/react-chartjs-2.svg)](https://www.npmjs.com/package/react-chartjs-2)
911
[![license](https://shields.io/badge/license-MIT-green)](http://opensource.org/licenses/MIT)
1012

1113
<br />
1214
<a href="#quickstart">Quickstart</a>
1315
<span>&nbsp;&nbsp;&nbsp;&nbsp;</span>
14-
<a href="#examples">Examples</a>
16+
<a href="#docs">Docs</a>
1517
<span>&nbsp;&nbsp;&nbsp;&nbsp;</span>
1618
<a href="https://slack.cube.dev/?ref=eco-react-chartjs">Slack</a>
19+
<span>&nbsp;&nbsp;&nbsp;&nbsp;</span>
20+
<a href="https://stackoverflow.com/questions/tagged/react-chartjs-2">Stack Overflow</a>
1721
<br />
1822
<hr />
19-
</div>
2023

2124
## Quickstart
2225

@@ -38,239 +41,14 @@ import { Doughnut } from 'react-chartjs-2';
3841

3942
Need an API to fetch data? Consider [Cube](https://cube.dev/?ref=eco-react-chartjs), an open-source API for data apps.
4043

41-
## Examples
42-
43-
Please see [live examples](https://reactchartjs.github.io/react-chartjs-2/) or their [source code](example).
44-
45-
## Configure
46-
47-
### Chart props
48-
49-
```js
50-
id?: string;
51-
className?: string;
52-
height?: number;
53-
width?: number;
54-
redraw?: boolean;
55-
type: Chart.ChartType
56-
data: Chart.ChartData | (canvas: HTMLCanvasElement | null) => Chart.ChartData;
57-
options?: Chart.ChartOptions;
58-
fallbackContent?: React.ReactNode;
59-
plugins?: Chart.Plugin[];
60-
getDatasetAtEvent?: (dataset: Chart.InteractionItem[], event: React.MouseEvent<HTMLCanvasElement>) => void;
61-
getElementAtEvent?: (element: Chart.InteractionItem[], event: React.MouseEvent<HTMLCanvasElement>) => void;
62-
getElementsAtEvent?: (elements: Chart.InteractionItem[], event: React.MouseEvent<HTMLCanvasElement>) => void;
63-
```
64-
65-
In TypeScript, you can import chart props types like this:
66-
67-
```ts
68-
import type { ChartProps } from 'react-chartjs-2';
69-
```
70-
71-
#### id
72-
73-
Type `string`
74-
Default: `undefined`
75-
76-
ID attribute applied to the rendered canvas
77-
78-
#### className
79-
80-
Type `string`
81-
Default: `undefined`
82-
83-
class attribute applied to the rendered canvas
84-
85-
#### height
86-
87-
Type: `number`
88-
Default: `150`
89-
90-
Height attribute applied to the rendered canvas
91-
92-
#### width
93-
94-
Type: `number`
95-
Default: `300`
96-
97-
Width attribute applied to the rendered canvas
98-
99-
#### redraw
100-
101-
Type: `boolean`
102-
Default: `false`
103-
104-
If true, will tear down and redraw chart on all updates
105-
106-
#### type
107-
108-
Type: `'bar' | 'line' | 'scatter' | 'bubble' | 'pie' | 'doughnut' | 'polarArea' | 'radar'`
109-
110-
Chart.js chart type (required only on ChartComponent)
111-
112-
#### data (required)
113-
114-
Type: `Chart.ChartData | (canvas: HTMLCanvasElement | null) => Chart.ChartData`
115-
116-
The data object that is passed into the Chart.js chart ([more info](https://www.chartjs.org/docs/latest/getting-started/)).
117-
118-
This can also be a function, that receives a canvas element and returns the data object.
119-
120-
```tsx
121-
const data = canvas => {
122-
const ctx = canvas.getContext('2d');
123-
const g = ctx.createLinearGradient(...);
124-
125-
return {
126-
datasets: [{
127-
backgroundColor: g,
128-
// ...the rest
129-
}],
130-
};
131-
}
132-
```
133-
134-
#### options
135-
136-
Type: `Chart.ChartOptions`
137-
138-
The options object that is passed into the Chart.js chart ([more info](https://www.chartjs.org/docs/latest/general/options.html))
139-
140-
### fallbackContent
141-
142-
Type: `React.ReactNode`
143-
144-
A fallback for when the canvas cannot be rendered. Can be used for accessible chart descriptions ([more info](https://www.chartjs.org/docs/latest/general/accessibility.html))
145-
146-
#### plugins
147-
148-
Type: `Chart.PluginServiceRegistrationOptions[]`
149-
150-
The plugins array that is passed into the Chart.js chart ([more info](https://www.chartjs.org/docs/latest/developers/plugins.html))
151-
152-
#### getDatasetAtEvent
153-
154-
Type: `(dataset: Array<{}>, event: React.MouseEvent<HTMLCanvasElement>) => void`
155-
Default: `undefined`
156-
157-
Proxy for Chart.js `getDatasetAtEvent`. Calls with dataset and triggering event
158-
159-
#### getElementAtEvent
160-
161-
Type: `(element: [{}], event: React.MouseEvent<HTMLCanvasElement>) => void`
162-
Default: `undefined`
163-
164-
Proxy for Chart.js `getElementAtEvent`. Calls with single element array and triggering event
165-
166-
#### getElementsAtEvent
167-
168-
Type: `(elements: Array<{}>, event: React.MouseEvent<HTMLCanvasElement>) => void`
169-
Default: `undefined`
170-
171-
Proxy for Chart.js `getElementsAtEvent`. Calls with element array and triggering event
172-
173-
## FAQ
174-
175-
### Why doesn't my chart maintain it's width/height?
176-
177-
In order for Chart.js to obey the custom size you need to set `maintainAspectRatio` to false
178-
179-
```tsx
180-
<Bar
181-
data={data}
182-
width={100}
183-
height={50}
184-
options={{ maintainAspectRatio: false }}
185-
/>
186-
```
187-
188-
### How do I access my chart's instance?
189-
190-
The Chart.js instance can be accessed by placing a ref to the element as:
191-
192-
```tsx
193-
const App => {
194-
const ref = useRef();
195-
196-
return <Doughnut ref={ref} data={data} options={options} />;
197-
};
198-
```
199-
200-
### How do I access the canvas context?
201-
202-
The canvas node and hence context can be accessed within the data function.
203-
This approach is useful when you want to keep your components pure.
204-
205-
```tsx
206-
render() {
207-
const data = (canvas) => {
208-
const ctx = canvas.getContext('2d')
209-
const gradient = ctx.createLinearGradient(0,0,100,0);
210-
211-
return {
212-
backgroundColor: gradient
213-
// ...the rest
214-
}
215-
}
216-
217-
return <Line data={data} />;
218-
}
219-
```
220-
221-
## Additional Information
222-
223-
### Defaults
224-
225-
Chart.js defaults can be set by importing the `defaults` object:
226-
227-
```tsx
228-
import { defaults } from 'react-chartjs-2';
229-
230-
// Disable animating charts by default.
231-
defaults.animation = false;
232-
```
233-
234-
If you want to bulk set properties, try using the [lodash.merge](https://lodash.com/docs/#merge) function. This function will do a deep recursive merge preserving previously set values that you don't want to update.
235-
236-
````tsx
237-
import { defaults } from 'react-chartjs-2';
238-
import merge from 'lodash.merge';
239-
240-
merge(defaults, {
241-
animation: false,
242-
line: {
243-
borderColor: '#F85F73',
244-
}
245-
});
246-
``` -->
247-
248-
<!-- ### Chart.js object
249-
250-
You can access the internal Chart.js object to register plugins or extend charts like this:
251-
252-
```JavaScript
253-
import { Chart } from 'react-chartjs-2';
254-
255-
componentWillMount() {
256-
Chart.register({
257-
afterDraw: function (chart, easing) {
258-
// Plugin code.
259-
}
260-
});
261-
}
262-
````
263-
264-
### Working with Multiple Datasets
265-
266-
You will find that any event which causes the chart to re-render, such as hover tooltips, etc., will cause the first dataset to be copied over to other datasets, causing your lines and bars to merge together. This is because to track changes in the dataset series, the library needs a `key` to be specified - if none is found, it can't tell the difference between the datasets while updating. To get around this issue, you can take these two approaches:
267-
268-
1. Add a `label` property on each dataset. By default, this library uses the `label` property as the key to distinguish datasets.
269-
2. Specify a different property to be used as a key by passing a `datasetKeyProvider` prop to your chart component, which would return a unique string value for each dataset.
270-
271-
## Development
44+
# Docs
27245

273-
**NOTE:** The source code for the component is in `src`. A transpiled CommonJS version (generated with Babel) is available in `dist` for use with node.js, browserify and webpack. A UMD bundle is also built to `dist`, which can be included without the need for any build system.
46+
- [Migration to v4](https://react-chartjs-2.netlify.app/docs/migration-to-v4)
47+
- [Working with datasets](https://react-chartjs-2.netlify.app/docs/working-with-datasets)
48+
- [Working with events](https://react-chartjs-2.netlify.app/docs/events)
49+
- [FAQ](https://react-chartjs-2.netlify.app/faq)
50+
- [Components](https://react-chartjs-2.netlify.app/components)
51+
- [Examples](https://react-chartjs-2.netlify.app/examples)
27452

27553
## License
27654

src/types.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,41 @@ export interface ChartProps<
1313
TData = DefaultDataPoint<TType>,
1414
TLabel = unknown
1515
> extends CanvasHTMLAttributes<HTMLCanvasElement> {
16+
/**
17+
* Chart.js chart type
18+
*/
1619
type: TType;
20+
/**
21+
* The data object that is passed into the Chart.js chart
22+
* @see https://www.chartjs.org/docs/latest/getting-started/
23+
*/
1724
data: ChartData<TType, TData, TLabel>;
25+
/**
26+
* The options object that is passed into the Chart.js chart
27+
* @see https://www.chartjs.org/docs/latest/general/options.html
28+
* @default {}
29+
*/
1830
options?: ChartOptions<TType>;
31+
/**
32+
* The plugins array that is passed into the Chart.js chart
33+
* @see https://www.chartjs.org/docs/latest/developers/plugins.html
34+
* @default []
35+
*/
1936
plugins?: Plugin<TType>[];
37+
/**
38+
* Teardown and redraw chart on everu update
39+
* @default false
40+
*/
2041
redraw?: boolean;
42+
/**
43+
* Key name to identificate dataset
44+
* @default 'label'
45+
*/
2146
datasetIdKey?: string;
2247
/**
48+
* A fallback for when the canvas cannot be rendered. Can be used for accessible chart descriptions
49+
* @see https://www.chartjs.org/docs/latest/general/accessibility.html
50+
* @default null
2351
* @todo Replace with `children` prop.
2452
*/
2553
fallbackContent?: ReactNode;

src/utils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ export function getElementAtEvent(
124124
}
125125

126126
/**
127-
* Get dataset element with dataset from mouse click event
127+
* Get all dataset elements from mouse click event
128128
* @param chart - Chart.js instance
129129
* @param event - Mouse click event
130130
* @returns Dataset

website/.gitignore

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Dependencies
2+
/node_modules
3+
4+
# Production
5+
/build
6+
7+
# Generated files
8+
.docusaurus
9+
.cache-loader
10+
11+
# Misc
12+
.DS_Store
13+
.env.local
14+
.env.development.local
15+
.env.test.local
16+
.env.production.local
17+
18+
npm-debug.log*
19+
yarn-debug.log*
20+
yarn-error.log*

website/README.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# Website
2+
3+
This website is built using [Docusaurus 2](https://docusaurus.io/), a modern static website generator.
4+
5+
### Installation
6+
7+
```
8+
$ yarn
9+
```
10+
11+
### Local Development
12+
13+
```
14+
$ yarn start
15+
```
16+
17+
This command starts a local development server and opens up a browser window. Most changes are reflected live without having to restart the server.
18+
19+
### Build
20+
21+
```
22+
$ yarn build
23+
```
24+
25+
This command generates static content into the `build` directory and can be served using any static contents hosting service.
26+
27+
### Deployment
28+
29+
```
30+
$ GIT_USER=<Your GitHub username> USE_SSH=true yarn deploy
31+
```
32+
33+
If you are using GitHub pages for hosting, this command is a convenient way to build the website and push to the `gh-pages` branch.

0 commit comments

Comments
 (0)