Skip to content

Commit

Permalink
Try to improve documentation for new users
Browse files Browse the repository at this point in the history
  • Loading branch information
kurkle committed Dec 5, 2021
1 parent 6552a01 commit bd0c40f
Show file tree
Hide file tree
Showing 5 changed files with 114 additions and 22 deletions.
32 changes: 32 additions & 0 deletions docs/configuration/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,38 @@

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

## Cofiguration object structure

The top level structure of Chart.js configuration:

```javascript
const config = {
type: 'line'
data: {}
options: {}
plugins: []
}
```

### type

Chart type determines the main type of the chart.

**note** A dataset can override the `type`, this is how mixed charts are constructed.

### data

See [Data Structures](../general/data-structures) for details.

### options

Majority of the documentation talks about these options.

### plugins

Inline plugines can be included in this array. It is an alternative way of adding plugins for single chart (vs registering the plugin globally).
More about plugins in the [developers section](../developers/plugins.md).

## Global Configuration

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.
Expand Down
4 changes: 3 additions & 1 deletion docs/configuration/interactions.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,8 @@ When configuring the interaction with the graph via `interaction`, `hover` or `t

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

See how different modes work with tooltip in [tooltip interactions sample](../samples/tooltip/interactions.md )

### point

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

### nearest

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

```javascript
const chart = new Chart(ctx, {
Expand Down
13 changes: 11 additions & 2 deletions docs/general/data-structures.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ The provides labels can be of the type string or number to be rendered correctly

## Primitive[]

Namespace: `data.datasets[]`

```javascript
data: [20, 10],
labels: ['a', 'b']
Expand All @@ -16,6 +18,8 @@ When the `data` is an array of numbers, values from `labels` array at the same i

## Object[]

Namespace: `data.datasets[]`

```javascript
data: [{x: 10, y: 20}, {x: 15, y: null}, {x: 20, y: 10}]
```
Expand Down Expand Up @@ -68,9 +72,14 @@ options: {
## Object

```javascript
type: 'pie',
data: {
January: 10,
February: 20
datasets: [{
data: {
January: 10,
February: 20
}
}]
}
```

Expand Down
63 changes: 48 additions & 15 deletions docs/getting-started/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,50 @@ Now that we have a canvas we can use, we need to include Chart.js in our page.

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

```html
<script>
const labels = [
'January',
'February',
'March',
'April',
'May',
'June',
];
const data = {
labels: labels,
datasets: [{
label: 'My First dataset',
backgroundColor: 'rgb(255, 99, 132)',
borderColor: 'rgb(255, 99, 132)',
data: [0, 10, 5, 2, 20, 30, 45],
}]
};
const config = {
type: 'line',
data: data,
options: {}
};
</script>
```

Finally, render the chart using our configuration:

```html
<script>
const myChart = new Chart(
document.getElementById('myChart'),
config
);
</script>
```

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.

Here the sample above is presented with our sample block:

```js chart-editor
// <block:setup:1>
const labels = [
Expand Down Expand Up @@ -53,21 +97,10 @@ module.exports = {
};
```

Finally, render the chart using our configuration:

```html
<script>
// === include 'setup' then 'config' above ===
const myChart = new Chart(
document.getElementById('myChart'),
config
);
</script>
```

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.
:::tip Note
As you can see, some of the boilerplate needed is not visible in our sample blocks, as the samples focus on the configuration options.
:::

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.
All our examples are [available online](/samples/).

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.
24 changes: 20 additions & 4 deletions docs/samples/tooltip/interactions.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,29 @@ const actions = [
}
},
{
name: 'Mode: nearest',
name: 'Mode: nearest, axis: yx',
handler(chart) {
chart.options.interaction.axis = 'xy';
chart.options.interaction.mode = 'nearest';
chart.update();
}
},
{
name: 'Mode: nearest, axis: x',
handler(chart) {
chart.options.interaction.axis = 'x';
chart.options.interaction.mode = 'nearest';
chart.update();
}
},
{
name: 'Mode: nearest, axis: y',
handler(chart) {
chart.options.interaction.axis = 'y';
chart.options.interaction.mode = 'nearest';
chart.update();
}
},
{
name: 'Mode: x',
handler(chart) {
Expand Down Expand Up @@ -98,8 +114,8 @@ const config = {
title: {
display: true,
text: (ctx) => {
const {intersect, mode} = ctx.chart.options.interaction;
return 'Mode: ' + mode + ', intersect: ' + intersect;
const {axis = 'xy', intersect, mode} = ctx.chart.options.interaction;
return 'Mode: ' + mode + ', axis: ' + axis + ', intersect: ' + intersect;
}
},
}
Expand All @@ -111,4 +127,4 @@ module.exports = {
actions: actions,
config: config,
};
```
```

0 comments on commit bd0c40f

Please sign in to comment.