|
1 | 1 | # Getting Started |
2 | 2 |
|
3 | | -Let's get started using Chart.js! |
| 3 | +Let's get started with Chart.js! |
4 | 4 |
|
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: |
6 | 14 |
|
7 | 15 | ```html |
8 | 16 | <div> |
9 | 17 | <canvas id="myChart"></canvas> |
10 | 18 | </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> |
11 | 44 | ``` |
12 | 45 |
|
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 | + |
| 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). |
14 | 53 |
|
15 | 54 | ```html |
16 | | -<script src="https://cdn.jsdelivr.net/npm/chart.js"></script> |
| 55 | +<div> |
| 56 | + <canvas id="myChart"></canvas> |
| 57 | +</div> |
17 | 58 | ``` |
18 | 59 |
|
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. |
20 | 61 |
|
21 | 62 | ```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> |
48 | 64 | ``` |
49 | 65 |
|
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. |
51 | 67 |
|
52 | 68 | ```html |
53 | 69 | <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 | + }); |
58 | 90 | </script> |
59 | 91 | ``` |
60 | 92 |
|
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). |
0 commit comments