score:5
Accepted answer
The correct syntax for globally changing the bar categoryPercentage
is the following:
Chart.defaults.global.datasets.bar.categoryPercentage = 0.95;
And here's a working sample:
Chart.defaults.global.datasets.bar.categoryPercentage = 0.95;
var canvas = document.getElementById('myChart');
var data = {
labels: ["A", "B", "C", "D", "E"],
datasets: [{
label: "Occurrences",
data: [3, 5, 2, 4, 6],
fill: false,
backgroundColor: ["rgba(255, 99, 132, 0.2)", "rgba(255, 159, 64, 0.2)", "rgba(255, 205, 86, 0.2)", "rgba(75, 192, 192, 0.2)", "rgba(54, 162, 235, 0.2)"],
borderColor: ["rgb(255, 99, 132)", "rgb(255, 159, 64)", "rgb(255, 205, 86)", "rgb(75, 192, 192)", "rgb(54, 162, 235)"],
borderWidth: 1
}]
};
var option = {
scales: {
yAxes:[{
ticks: {
beginAtZero: true
}
}]
}
};
var myBarChart = Chart.Bar(canvas, {
data:data,
options:option
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.9.3/Chart.min.js"></script>
<canvas id="myChart" width="400" height="200"></canvas>
To find out about other valid options contained in
Chart.defaults
, simply log it to the console (console.log(Chart.defaults)
) and search for the option name.
score:0
You don't need to set it globally, I actually recommend you to set it for each chart according to documentation.
var canvas = document.getElementById('myChart');
var data = {
labels: ["A", "B", "C", "D", "E"],
datasets: [{
label: "Occurrences",
data: [3, 5, 2, 4, 6],
fill: false,
backgroundColor: ["rgba(255, 99, 132, 0.2)", "rgba(255, 159, 64, 0.2)", "rgba(255, 205, 86, 0.2)", "rgba(75, 192, 192, 0.2)", "rgba(54, 162, 235, 0.2)"],
borderColor: ["rgb(255, 99, 132)", "rgb(255, 159, 64)", "rgb(255, 205, 86)", "rgb(75, 192, 192)", "rgb(54, 162, 235)"],
borderWidth: 1
}]
};
var option = {
datasets: {
bar: {
categoryPercentage: 0.95
}
},
scales: {
yAxes: [{
ticks: {
beginAtZero: true
}
}]
}
};
var myBarChart = Chart.Bar(canvas, {
data: data,
options: option
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.9.3/Chart.min.js"></script>
<canvas id="myChart" width="400" height="200"></canvas>
Source: stackoverflow.com
Related Query
- Chartjs defaults deprecation warning
- ChartJS / MomentJS - Unable to remove deprecation warning. Graph not showing in firefox/opera
- How to add ChartJS code in Html2Pdf to view image
- How do I add time sourced from an external source as an X axis to a ChartJS graph?
- Chartjs not working with d3 from csv source
- ChartJS have xAxes labels match data source
- Updating Chartjs to 2.5 with custom code
- Calling data from outside of Chartjs code
- VueJS + Chartjs - Chart only renders after code change
- ChartJS 2.0 differences in code help needed
- My Chartjs is not updating after setstate full code attached Reactjs Update
- Chartjs doc examples are lightning fast but same code is slow when reproducing
- 'require is not defined' error when attempting to use chartjs in javascript code
- How to run Chart.js samples using source code
- Code for Chartjs Typescript definition file
- Dynamically update values of a chartjs chart
- Different color for each bar in a bar chart; ChartJS
- Hide points in ChartJS LineGraph
- ChartJS Line Charts - remove color underneath lines
- Skip decimal points on y-axis in chartJS
- How to set ChartJS Y axis title?
- Chartjs Bar Chart showing old data when hovering
- Chartjs change grid line color
- Chart area background color chartjs
- Custom Legend with ChartJS v2.0
- Chartjs Tooltip Line Breaks
- Is it possible in chartjs to hide certain dataset legends?
- Chartjs v2.0: stacked bar chart
- ChartJS - Different color per data point
- Chartjs random colors for each part of pie chart with data dynamically from database
More Query from same tag
- How to change the Y-axis value in chartjs
- How to append more data in tooltip of graph in chart.js
- Hiding Chart.js line, but showing it's data in the tooltip
- ChartJS : How to display two "y axis" scales on a chart
- Charts.js - Need to remove the data value from the Tooltip
- Chart.js: How to handle very large and very small values in radar chart?
- ChartJS add custom tooltip to a stacked bar chart
- Issue in implementing ChartJS in my new assignment of ReactJS App, please let me know where I am wrong
- is there anything wrong with this If Condition in Javascript code?
- Chartjs doughnut chart rounded corners for half doghnut
- How to apply data dynamically on initialisation for chart.js in angular?
- Changing x axis labels in Chart.js line chart
- How to hide zero data value from chartjs?
- Chartsjs multi-axis, make the scale appear/disappear when the dataset is activated/deactivated
- JavaScript after dynamically updating ChartJS
- ChartJS multiple dataset issue. second dataset gets over 1st instead of overlaying
- Grouped bar charts, in chart.js
- How to show data values in top of bar chart and line chart in chart.js 3
- generating a Chart.js chart with python data
- Chartjs: How do you toggle the visibility of charts in Ionic 4
- Bootstrap Card Render Problem in Google Chrome
- Show multiple charts at HTML page?
- Cannot make my chart to work with data from mongoDB
- ChartJS: Show all labels of a mixed chart in the tooltip
- (Chart.js 3.7.0) change position of x-axis ticks to alternate between each tick?
- yAxes not working with min max option
- Fill between two lines Chartjs
- Where do I use chartjs's destroy function?
- Maximum call stack error when attempting to update Chart in Vue js
- Chart Js + Angular