Skip to content

Commit 6917584

Browse files
Updated docs (why Chart.js + getting started + step-by-step guide) (#10816)
* Update docs * Minor fixes * Replace screenshots with live demos * Replace the last screenshot with a live demo * Bring back images * Bring back images #2 * Remove unnecessary files * Apply suggestions from code review Co-authored-by: Jacco van den Berg <[email protected]> * Very last tiny fixes Co-authored-by: Jacco van den Berg <[email protected]>
1 parent 718f460 commit 6917584

File tree

16 files changed

+712
-208
lines changed

16 files changed

+712
-208
lines changed

docs/developers/index.md

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,20 @@ Latest builds are available for testing at:
1616
- <https://www.chartjs.org/dist/master/chart.js>
1717
- <https://www.chartjs.org/dist/master/chart.min.js>
1818

19-
**WARNING: Development builds MUST not be used for production purposes or as replacement for CDN.**
19+
:::warning Warning
20+
21+
Development builds **must not** be used for production purposes or as replacement for a CDN. See [available CDNs](../getting-started/installation.html#cdn).
22+
23+
:::
2024

2125
## Browser support
2226

2327
All modern and up-to-date browsers are supported, including, but not limited to:
2428

25-
Chrome
26-
Edge
27-
Firefox
28-
Safari
29+
* Chrome
30+
* Edge
31+
* Firefox
32+
* Safari
2933

3034
As of version 3, we have dropped Internet Explorer 11 support.
3135

docs/getting-started/index.md

Lines changed: 69 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -1,106 +1,93 @@
11
# Getting Started
22

3-
Let's get started using Chart.js!
3+
Let's get started with Chart.js!
44

5-
First, we need to have a canvas in our page. It's recommended to give the chart its own container for [responsiveness](../configuration/responsive.md).
5+
* **[Follow a step-by-step guide](./usage) to get up to speed with Chart.js**
6+
* [Install Chart.js](./installation) from npm or a CDN
7+
* [Integrate Chart.js](./integration) with bundlers, loaders, and front-end frameworks
8+
9+
Alternatively, see the example below or check [samples](../samples).
10+
11+
## Create a Chart
12+
13+
In this example, we create a bar chart for a single dataset and render it on an HTML page. Add this code snippet to your page:
614

715
```html
816
<div>
917
<canvas id="myChart"></canvas>
1018
</div>
19+
20+
<script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
21+
22+
<script>
23+
const ctx = document.getElementById('myChart');
24+
25+
new Chart(ctx, {
26+
type: 'bar',
27+
data: {
28+
labels: ['Red', 'Blue', 'Yellow', 'Green', 'Purple', 'Orange'],
29+
datasets: [{
30+
label: '# of Votes',
31+
data: [12, 19, 3, 5, 2, 3],
32+
borderWidth: 1
33+
}]
34+
},
35+
options: {
36+
scales: {
37+
y: {
38+
beginAtZero: true
39+
}
40+
}
41+
}
42+
});
43+
</script>
1144
```
1245

13-
Now that we have a canvas we can use, we need to include Chart.js in our page.
46+
You should get a chart like this:
47+
48+
![demo](./preview.png)
49+
50+
Let's break this code down.
51+
52+
First, we need to have a canvas in our page. It's recommended to give the chart its own container for [responsiveness](../configuration/responsive.md).
1453

1554
```html
16-
<script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
55+
<div>
56+
<canvas id="myChart"></canvas>
57+
</div>
1758
```
1859

19-
Now, we can create a chart. We add a script to our page:
60+
Now that we have a canvas, we can include Chart.js from a CDN.
2061

2162
```html
22-
<script>
23-
const labels = [
24-
'January',
25-
'February',
26-
'March',
27-
'April',
28-
'May',
29-
'June',
30-
];
31-
32-
const data = {
33-
labels: labels,
34-
datasets: [{
35-
label: 'My First dataset',
36-
backgroundColor: 'rgb(255, 99, 132)',
37-
borderColor: 'rgb(255, 99, 132)',
38-
data: [0, 10, 5, 2, 20, 30, 45],
39-
}]
40-
};
41-
42-
const config = {
43-
type: 'line',
44-
data: data,
45-
options: {}
46-
};
47-
</script>
63+
<script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
4864
```
4965

50-
Finally, render the chart using our configuration:
66+
Finally, we can create a chart. We add a script that acquires the `myChart` canvas element and instantiates `new Chart` with desired configuration: `bar` chart type, labels, data points, and options.
5167

5268
```html
5369
<script>
54-
const myChart = new Chart(
55-
document.getElementById('myChart'),
56-
config
57-
);
70+
const ctx = document.getElementById('myChart');
71+
72+
new Chart(ctx, {
73+
type: 'bar',
74+
data: {
75+
labels: ['Red', 'Blue', 'Yellow', 'Green', 'Purple', 'Orange'],
76+
datasets: [{
77+
label: '# of Votes',
78+
data: [12, 19, 3, 5, 2, 3],
79+
borderWidth: 1
80+
}]
81+
},
82+
options: {
83+
scales: {
84+
y: {
85+
beginAtZero: true
86+
}
87+
}
88+
}
89+
});
5890
</script>
5991
```
6092

61-
It's that easy to get started using Chart.js! From here you can explore the many options that can help you customise your charts with scales, tooltips, labels, colors, custom actions, and much more.
62-
63-
Here the sample above is presented with our sample block:
64-
65-
```js chart-editor
66-
// <block:setup:1>
67-
const labels = [
68-
'January',
69-
'February',
70-
'March',
71-
'April',
72-
'May',
73-
'June',
74-
];
75-
const data = {
76-
labels: labels,
77-
datasets: [{
78-
label: 'My First dataset',
79-
backgroundColor: 'rgb(255, 99, 132)',
80-
borderColor: 'rgb(255, 99, 132)',
81-
data: [0, 10, 5, 2, 20, 30, 45],
82-
}]
83-
};
84-
// </block:setup>
85-
86-
// <block:config:0>
87-
const config = {
88-
type: 'line',
89-
data: data,
90-
options: {}
91-
};
92-
// </block:config>
93-
94-
module.exports = {
95-
actions: [],
96-
config: config,
97-
};
98-
```
99-
100-
:::tip Note
101-
As you can see, some of the boilerplate needed is not visible in our sample blocks, as the samples focus on the configuration options.
102-
:::
103-
104-
All our examples are [available online](../samples/).
105-
106-
To run the samples locally you first have to install all the necessary packages using the `npm ci` command, after this you can run `npm run docs:dev` to build the documentation. As soon as the build is done, you can go to [http://localhost:8080/samples/](http://localhost:8080/samples/) to see the samples.
93+
You can see all the ways to use Chart.js in the [step-by-step guide](./usage).

docs/getting-started/installation.md

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
---
2-
title: Installation
3-
---
1+
# Installation
42

53
## npm
64

@@ -29,7 +27,7 @@ Chart.js built files are also available through [jsDelivr](https://www.jsdelivr.
2927

3028
<https://www.jsdelivr.com/package/npm/chart.js?path=dist>
3129

32-
## Github
30+
## GitHub
3331

3432
[![github](https://img.shields.io/github/release/chartjs/Chart.js.svg?style=flat-square&maxAge=600)](https://github.com/chartjs/Chart.js/releases/latest)
3533

docs/getting-started/integration.md

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
Chart.js can be integrated with plain JavaScript or with different module loaders. The examples below show how to load Chart.js in different systems.
44

5+
If you're using a front-end framework (e.g., React, Angular, or Vue), please see [available integrations](https://github.com/chartjs/awesome#integrations).
6+
57
## Script Tag
68

79
```html
@@ -118,7 +120,7 @@ Because Chart.js is an ESM library, in CommonJS modules you should use a dynamic
118120
const { Chart } = await import('chart.js');
119121
```
120122

121-
## Require JS
123+
## RequireJS
122124

123125
**Important:** RequireJS can load only [AMD modules](https://requirejs.org/docs/whyamd.html), so be sure to require one of the UMD builds instead (i.e. `dist/chart.umd.js`).
124126

@@ -128,7 +130,9 @@ require(['path/to/chartjs/dist/chart.umd.js'], function(Chart){
128130
});
129131
```
130132

131-
**Note:** in order to use the time scale, you need to make sure [one of the available date adapters](https://github.com/chartjs/awesome#adapters) and corresponding date library are fully loaded **after** requiring Chart.js. For this you can use nested requires:
133+
:::tip Note
134+
135+
In order to use the time scale, you need to make sure [one of the available date adapters](https://github.com/chartjs/awesome#adapters) and corresponding date library are fully loaded **after** requiring Chart.js. For this you can use nested requires:
132136

133137
```javascript
134138
require(['chartjs'], function(Chart) {
@@ -139,3 +143,4 @@ require(['chartjs'], function(Chart) {
139143
});
140144
});
141145
```
146+
:::

docs/getting-started/preview.png

67 KB
Loading

docs/getting-started/usage-1.png

66.2 KB
Loading

docs/getting-started/usage-2.png

116 KB
Loading

docs/getting-started/usage-3.png

143 KB
Loading

docs/getting-started/usage-4.png

217 KB
Loading

docs/getting-started/usage-5.png

214 KB
Loading

0 commit comments

Comments
 (0)