-
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
Changes from 4 commits
4e0d8d0
b4b65bb
44a0053
7b6320c
37baff1
4a722d8
5218ca1
fc201fc
3ebe6f7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| <div> | ||
| <canvas id="myChart"></canvas> | ||
| </div> | ||
|
|
||
| <script src="https://cdn.jsdelivr.net/npm/[email protected]"></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> | ||
Large diffs are not rendered by default.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| <!doctype html><html lang="en"><head><title>Chart.js example</title></head><body> <div style="width:800px"><canvas id="acquisitions"></canvas></div> <script type="module" src="index.2979bcd3.js"></script> </body></html> |
Large diffs are not rendered by default.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| <!doctype html><html lang="en"><head><title>Chart.js example</title></head><body> <div style="width:800px"><canvas id="acquisitions"></canvas></div> <script type="module" src="index.1de0334e.js"></script> </body></html> |
Large diffs are not rendered by default.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| <!doctype html><html lang="en"><head><title>Chart.js example</title></head><body> <div style="width:800px"><canvas id="acquisitions"></canvas></div> <script type="module" src="index.1b874a14.js"></script> </body></html> |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Large diffs are not rendered by default.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| <!doctype html><html lang="en"><head><title>Chart.js example</title></head><body> <div style="width:500px"><canvas id="dimensions"></canvas></div><br> <div style="width:800px"><canvas id="acquisitions"></canvas></div> <script type="module" src="index.b26b1a85.js"></script> <script type="module" src="index.1fa2e270.js"></script> </body></html> |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Large diffs are not rendered by default.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| <!doctype html><html lang="en"><head><title>Chart.js example</title></head><body> <div style="width:500px"><canvas id="dimensions"></canvas></div><br> <div style="width:800px"><canvas id="acquisitions"></canvas></div> <script type="module" src="index.c2f80a4d.js"></script> <script type="module" src="index.1fa2e270.js"></script> </body></html> |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Large diffs are not rendered by default.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| <!doctype html><html lang="en"><head><title>Chart.js example</title></head><body> <div style="width:500px"><canvas id="dimensions"></canvas></div><br> <div style="width:800px"><canvas id="acquisitions"></canvas></div> <script type="module" src="index.a51116fb.js"></script> <script type="module" src="index.1fa2e270.js"></script> </body></html> |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Large diffs are not rendered by default.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| <!doctype html><html lang="en"><head><title>Chart.js example</title></head><body> <div style="width:500px"><canvas id="dimensions"></canvas></div><br> <div style="width:800px"><canvas id="acquisitions"></canvas></div> <script type="module" src="index.65688088.js"></script> <script type="module" src="index.1fa2e270.js"></script> </body></html> |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Large diffs are not rendered by default.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| <!doctype html><html lang="en"><head><title>Chart.js example</title></head><body> <div style="width:500px"><canvas id="dimensions"></canvas></div><br> <div style="width:800px"><canvas id="acquisitions"></canvas></div> <script type="module" src="index.4f16a936.js"></script> <script type="module" src="index.1fa2e270.js"></script> </body></html> |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Large diffs are not rendered by default.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| <!doctype html><html lang="en"><head><title>Chart.js example</title></head><body> <div style="width:500px"><canvas id="dimensions"></canvas></div><br> <div style="width:800px"><canvas id="acquisitions"></canvas></div> <script type="module" src="index.8aa3ce5e.js"></script> <script type="module" src="index.1fa2e270.js"></script> </body></html> |
| 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.
Show resolved
Hide resolved
|
||
|
|
||
| <iframe src="../demo.html" frameBorder="0" scrolling="no" width="825" height="400"></iframe> | ||
|
|
||
| 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). | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why are you adding these files in the public folder of vuepress?
Same goes for all the files and folders you added in the usage folder.
I think its better to just integrete these kind of examples in the docs pages itself (md files) and use place a chart chart custom chart undeaneath it with that config.
Maby we can look into the plugin to add them so we can disable the user input so the user only sees the chart
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@LeeLenaleee Cool question! So, these public files represent self-contained, pre-built Chart.js examples that I embed into the step-by-step guide. If you'd like to, consider running
pnpm docs:devand checking out http://localhost:8080/docs/master/getting-started/; you might enjoy it 😄Some of these examples rely on an external dependency to fetch substantially sized "real-world data," and embedding the library in the docs would be unreasonable. I've explored embedding the datasets directly, but they are dozens of KBs, so it's also not an option.
Yep, it's a viable option for the file you're commenting (
demo.html). I've submitted a PR tovuepress-theme-chartjsthat would allow that: simonbrunel/vuepress-theme-chartjs#4Right. With the PR above, that one can be replaced with something like this:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's wait for @simonbrunel to merge the PR to
vuepress-theme-chartjs. Alternatively, I can submit a follow-up PR later after the theme is updated so this one is not blocked.Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
While I appreciate the work I am against merging this for the following reasons:
I find it a security risk, those js files are big and hard to read so difficult to know if you putted something in there that's not supposed to be there.
Its a maintenance burden to update them Everytime something in the codebase has changed
Sample in the docs should be minimalistic in my eyes and not rely on external data which can change or go down. If we want an example with loads of data we should add it in the samples section with generated data.
So instead of embedding these kind of charts, just show simple charts and in the way we do it elsware in the docs with Simons plugin
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I fully agree with @LeeLenaleee, especially on the maintenance part. There should be no pre-built files in the repository. I'm not even sure how the
usage/*files are supposed to be generated? where are the sources? IMO, embedding complex examples in the docs should go through some services like CodePen or alternatives, so users have access to the entire source code.But as a getting started, I would indeed expect something minimalistic as mentioned by @LeeLenaleee.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@LeeLenaleee @simonbrunel This is excellent feedback; thanks! Honestly, I dislike the current state of things just like you do. It was an attempt to have interactive charts rather than images, but it looks ugly now. Let me roll this PR back to just images and remove all the bloat.
@simonbrunel has a great point on CodePen/CodeSandbox/etc. However, they would still be external to docs and could potentially break, so let's skip that options for now.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Rolled back to images. (No new files under
.vuepressanymore.) IMO, much better now. WDYT?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Images make sense to me. A CodePen or similar could work, but the maintenance burden is there. Maybe there are ways we could automatically update the codepen during deploys?