7π
JavaScript has allowed trailing commas in array literals since the
beginning, and later added them to object literals (ECMAScript 5) and
most recently (ECMAScript 2017) to function parameters.
This is a relatively new change in syntax, but the basic idea is that by putting a comma on each line allows for:
- Easier to add an item or re-order items. Before you always had to check the trailing comma and make sure it was present or removed depending on location.
- Removes the need to have one line item be special because it lacks the
,
. - Allows for cleaner Git diffs.
You can read up on the full documentation if you like β it goes into further detail:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Trailing_commas
As far as the issue with your chart not displaying, unless you are using a very old browser, a trailing comma should not cause an error/information to not display.
2π
You need to update the configuration of prettier
extension.
There are two ways. Below one is mostly used.
-
Create a
.prettierrc
file at the root of your project and
specifying the below configuration.{ "trailingComma": "es5" }
-
In order to honor the configuration make sure to enable the below
setting in vs code configuration."prettier.requireConfig": true
0π
Prettier adds those commas at the end just because if you wanna add another data after that you donβt need to type a comma. it does the same for semicolons(;).
you got the error because you havenβt provided datasets.
data takes an object which contains labels & datasets values.
{/* <canvas id="myChart" width="400" height="400"></canvas> */}
// var ctx = document.getElementById('myChart');
var myChart = new Chart(ctx, {
type: 'bar',
data: {
labels: ['1','2'],
datasets: [
{
label: '1st',
data: '120',
borderColor: Utils.CHART_COLORS.red,
backgroundColor: Utils.transparentize(Utils.CHART_COLORS.red, 0.5),
},
{
label: '2',
data: '240',
borderColor: Utils.CHART_COLORS.red,
backgroundColor: Utils.transparentize(Utils.CHART_COLORS.blue, 0.5),
}
]
},
// options: {
// indexAxis: 'y',
// elements: {
// bar: {
// borderWidth: 2,
// }
// },
// responsive: true,
// plugins: {
// legend: {
// position: 'right',
// },
// title: {
// display: true,
// text: 'Chart.js Horizontal Bar Chart'
// }
// }
// },
// };
you can know more about it on official docs https://www.chartjs.org/docs/latest/charts/bar.html