-
Notifications
You must be signed in to change notification settings - Fork 12k
Updated docs (why Chart.js + getting started + step-by-step guide) #10816
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 2 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
4e0d8d0
Update docs
igorlukanin b4b65bb
Minor fixes
igorlukanin 44a0053
Replace screenshots with live demos
igorlukanin 7b6320c
Replace the last screenshot with a live demo
igorlukanin 37baff1
Bring back images
igorlukanin 4a722d8
Bring back images #2
igorlukanin 5218ca1
Remove unnecessary files
igorlukanin fc201fc
Apply suggestions from code review
igorlukanin 3ebe6f7
Very last tiny fixes
igorlukanin File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,106 +1,93 @@ | ||
| # Getting Started | ||
|
|
||
| Let's get started using Chart.js! | ||
| Let's get started with Chart.js! | ||
|
|
||
| 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). | ||
| * **[Follow a step-by-step guide](./usage) to get up to speed with Chart.js** | ||
| * [Install Chart.js](./installation) from npm or a CDN | ||
| * [Integrate Chart.js](./integration) with bundlers, loaders, and front-end frameworks | ||
|
|
||
| Alternatively, see the example below or check [samples](../samples). | ||
|
|
||
| ## Create a Chart | ||
|
|
||
| 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: | ||
|
|
||
| ```html | ||
| <div> | ||
| <canvas id="myChart"></canvas> | ||
| </div> | ||
|
|
||
| <script src="https://cdn.jsdelivr.net/npm/chart.js"></script> | ||
|
|
||
| <script> | ||
| const ctx = document.getElementById('myChart'); | ||
|
|
||
| new Chart(ctx, { | ||
| type: 'bar', | ||
| data: { | ||
| labels: ['Red', 'Blue', 'Yellow', 'Green', 'Purple', 'Orange'], | ||
| datasets: [{ | ||
| label: '# of Votes', | ||
| data: [12, 19, 3, 5, 2, 3], | ||
| borderWidth: 1 | ||
| }] | ||
| }, | ||
| options: { | ||
| scales: { | ||
| y: { | ||
| beginAtZero: true | ||
| } | ||
| } | ||
| } | ||
| }); | ||
| </script> | ||
| ``` | ||
|
|
||
| Now that we have a canvas we can use, we need to include Chart.js in our page. | ||
| You should get a chart like this: | ||
|
|
||
|  | ||
igorlukanin marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| Let's break this code down. | ||
|
|
||
| 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). | ||
|
|
||
| ```html | ||
| <script src="https://cdn.jsdelivr.net/npm/chart.js"></script> | ||
| <div> | ||
| <canvas id="myChart"></canvas> | ||
| </div> | ||
| ``` | ||
|
|
||
| Now, we can create a chart. We add a script to our page: | ||
| Now that we have a canvas, we can include Chart.js from a CDN. | ||
|
|
||
| ```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> | ||
| <script src="https://cdn.jsdelivr.net/npm/chart.js"></script> | ||
| ``` | ||
|
|
||
| Finally, render the chart using our configuration: | ||
| 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. | ||
|
|
||
| ```html | ||
| <script> | ||
| const myChart = new Chart( | ||
| document.getElementById('myChart'), | ||
| config | ||
| ); | ||
| const ctx = document.getElementById('myChart'); | ||
|
|
||
| new Chart(ctx, { | ||
| type: 'bar', | ||
| data: { | ||
| labels: ['Red', 'Blue', 'Yellow', 'Green', 'Purple', 'Orange'], | ||
| datasets: [{ | ||
| label: '# of Votes', | ||
| data: [12, 19, 3, 5, 2, 3], | ||
| borderWidth: 1 | ||
| }] | ||
| }, | ||
| options: { | ||
| scales: { | ||
| y: { | ||
| beginAtZero: true | ||
| } | ||
| } | ||
| } | ||
| }); | ||
| </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 = [ | ||
| '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], | ||
| }] | ||
| }; | ||
| // </block:setup> | ||
|
|
||
| // <block:config:0> | ||
| const config = { | ||
| type: 'line', | ||
| data: data, | ||
| options: {} | ||
| }; | ||
| // </block:config> | ||
|
|
||
| module.exports = { | ||
| actions: [], | ||
| config: config, | ||
| }; | ||
| ``` | ||
|
|
||
| :::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/). | ||
|
|
||
| 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. | ||
| You can see all the ways to use Chart.js in the [step-by-step guide](./usage). | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file added
BIN
+376 KB
docs/getting-started/usage-images/Screenshot_2022-10-18_at_14.43.51.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+431 KB
docs/getting-started/usage-images/Screenshot_2022-10-19_at_11.59.01.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+466 KB
docs/getting-started/usage-images/Screenshot_2022-10-19_at_12.23.58.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+540 KB
docs/getting-started/usage-images/Screenshot_2022-10-19_at_12.35.04.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+537 KB
docs/getting-started/usage-images/Screenshot_2022-10-19_at_12.41.27.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+542 KB
docs/getting-started/usage-images/Screenshot_2022-10-19_at_13.17.58.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+547 KB
docs/getting-started/usage-images/Screenshot_2022-10-19_at_14.53.10.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+230 KB
docs/getting-started/usage-images/Screenshot_2022-10-19_at_17.16.42.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+372 KB
docs/getting-started/usage-images/Screenshot_2022-10-19_at_17.29.52.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+231 KB
docs/getting-started/usage-images/Screenshot_2022-10-19_at_17.35.50.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+535 KB
docs/getting-started/usage-images/Screenshot_2022-10-20_at_15.26.06.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.