Skip to content

Commit 4ac11a7

Browse files
authored
Try to improve documentation for new users (#9952)
* Try to improve documentation for new users * Review update
1 parent e7aec8c commit 4ac11a7

File tree

5 files changed

+135
-27
lines changed

5 files changed

+135
-27
lines changed

docs/configuration/index.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,38 @@
22

33
The configuration is used to change how the chart behaves. There are properties to control styling, fonts, the legend, etc.
44

5+
## Cofiguration object structure
6+
7+
The top level structure of Chart.js configuration:
8+
9+
```javascript
10+
const config = {
11+
type: 'line'
12+
data: {}
13+
options: {}
14+
plugins: []
15+
}
16+
```
17+
18+
### type
19+
20+
Chart type determines the main type of the chart.
21+
22+
**note** A dataset can override the `type`, this is how mixed charts are constructed.
23+
24+
### data
25+
26+
See [Data Structures](../general/data-structures) for details.
27+
28+
### options
29+
30+
Majority of the documentation talks about these options.
31+
32+
### plugins
33+
34+
Inline plugins can be included in this array. It is an alternative way of adding plugins for single chart (vs registering the plugin globally).
35+
More about plugins in the [developers section](../developers/plugins.md).
36+
537
## Global Configuration
638

739
This concept was introduced in Chart.js 1.0 to keep configuration [DRY](https://en.wikipedia.org/wiki/Don%27t_repeat_yourself), and allow for changing options globally across chart types, avoiding the need to specify options for each instance, or the default for a particular chart type.

docs/configuration/interactions.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,8 @@ When configuring the interaction with the graph via `interaction`, `hover` or `t
108108

109109
The modes are detailed below and how they behave in conjunction with the `intersect` setting.
110110

111+
See how different modes work with the tooltip in [tooltip interactions sample](../samples/tooltip/interactions.md )
112+
111113
### point
112114

113115
Finds all of the items that intersect the point.
@@ -126,7 +128,7 @@ const chart = new Chart(ctx, {
126128

127129
### nearest
128130

129-
Gets the items that are at the nearest distance to the point. The nearest item is determined based on the distance to the center of the chart item (point, bar). You can use the `axis` setting to define which directions are used in distance calculation. If `intersect` is true, this is only triggered when the mouse position intersects an item in the graph. This is very useful for combo charts where points are hidden behind bars.
131+
Gets the items that are at the nearest distance to the point. The nearest item is determined based on the distance to the center of the chart item (point, bar). You can use the `axis` setting to define which coordinates are considered in distance calculation. If `intersect` is true, this is only triggered when the mouse position intersects an item in the graph. This is very useful for combo charts where points are hidden behind bars.
130132

131133
```javascript
132134
const chart = new Chart(ctx, {

docs/general/data-structures.md

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,24 +8,44 @@ The provides labels can be of the type string or number to be rendered correctly
88
## Primitive[]
99

1010
```javascript
11-
data: [20, 10],
12-
labels: ['a', 'b']
11+
type: 'bar',
12+
data: {
13+
datasets: [{
14+
data: [20, 10],
15+
}],
16+
labels: ['a', 'b']
17+
}
1318
```
1419

1520
When the `data` is an array of numbers, values from `labels` array at the same index are used for the index axis (`x` for vertical, `y` for horizontal charts).
1621

1722
## Object[]
1823

1924
```javascript
20-
data: [{x: 10, y: 20}, {x: 15, y: null}, {x: 20, y: 10}]
25+
type: 'line',
26+
data: {
27+
datasets: [{
28+
data: [{x: 10, y: 20}, {x: 15, y: null}, {x: 20, y: 10}]
29+
}]
30+
}
2131
```
2232

2333
```javascript
24-
data: [{x:'2016-12-25', y:20}, {x:'2016-12-26', y:10}]
34+
type: 'line',
35+
data: {
36+
datasets: [{
37+
data: [{x:'2016-12-25', y:20}, {x:'2016-12-26', y:10}]
38+
}]
39+
}
2540
```
2641

2742
```javascript
28-
data: [{x:'Sales', y:20}, {x:'Revenue', y:10}]
43+
type: 'bar',
44+
data: {
45+
datasets: [{
46+
data: [{x:'Sales', y:20}, {x:'Revenue', y:10}]
47+
}]
48+
}
2949
```
3050

3151
This is also the internal format used for parsed data. In this mode, parsing can be disabled by specifying `parsing: false` at chart options or dataset. If parsing is disabled, data must be sorted and in the formats the associated chart type and scales use internally.
@@ -68,9 +88,14 @@ options: {
6888
## Object
6989

7090
```javascript
91+
type: 'pie',
7192
data: {
72-
January: 10,
73-
February: 20
93+
datasets: [{
94+
data: {
95+
January: 10,
96+
February: 20
97+
}
98+
}]
7499
}
75100
```
76101

docs/getting-started/index.md

Lines changed: 48 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,50 @@ Now that we have a canvas we can use, we need to include Chart.js in our page.
1818

1919
Now, we can create a chart. We add a script to our page:
2020

21+
```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>
48+
```
49+
50+
Finally, render the chart using our configuration:
51+
52+
```html
53+
<script>
54+
const myChart = new Chart(
55+
document.getElementById('myChart'),
56+
config
57+
);
58+
</script>
59+
```
60+
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+
2165
```js chart-editor
2266
// <block:setup:1>
2367
const labels = [
@@ -53,21 +97,10 @@ module.exports = {
5397
};
5498
```
5599

56-
Finally, render the chart using our configuration:
57-
58-
```html
59-
<script>
60-
// === include 'setup' then 'config' above ===
61-
62-
const myChart = new Chart(
63-
document.getElementById('myChart'),
64-
config
65-
);
66-
</script>
67-
```
68-
69-
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.
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+
:::
70103

71-
All our examples are [available online](/samples/) but you can also download the `Chart.js.zip` archive attached to every [release](https://github.com/chartjs/Chart.js/releases) to experiment with our samples locally from the `/samples` folder.
104+
All our examples are [available online](/samples/).
72105

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

docs/samples/tooltip/interactions.md

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,29 @@ const actions = [
3030
}
3131
},
3232
{
33-
name: 'Mode: nearest',
33+
name: 'Mode: nearest, axis: xy',
3434
handler(chart) {
3535
chart.options.interaction.axis = 'xy';
3636
chart.options.interaction.mode = 'nearest';
3737
chart.update();
3838
}
3939
},
40+
{
41+
name: 'Mode: nearest, axis: x',
42+
handler(chart) {
43+
chart.options.interaction.axis = 'x';
44+
chart.options.interaction.mode = 'nearest';
45+
chart.update();
46+
}
47+
},
48+
{
49+
name: 'Mode: nearest, axis: y',
50+
handler(chart) {
51+
chart.options.interaction.axis = 'y';
52+
chart.options.interaction.mode = 'nearest';
53+
chart.update();
54+
}
55+
},
4056
{
4157
name: 'Mode: x',
4258
handler(chart) {
@@ -98,8 +114,8 @@ const config = {
98114
title: {
99115
display: true,
100116
text: (ctx) => {
101-
const {intersect, mode} = ctx.chart.options.interaction;
102-
return 'Mode: ' + mode + ', intersect: ' + intersect;
117+
const {axis = 'xy', intersect, mode} = ctx.chart.options.interaction;
118+
return 'Mode: ' + mode + ', axis: ' + axis + ', intersect: ' + intersect;
103119
}
104120
},
105121
}
@@ -111,4 +127,4 @@ module.exports = {
111127
actions: actions,
112128
config: config,
113129
};
114-
```
130+
```

0 commit comments

Comments
 (0)