score:138
You were right : The attribute you have to edit is barPercentage
.
But maybe the error comes from where you edited the value.
As you can see in the bar chart options :
Name : barPercentage
- Type : Number
- Default : 0.9
- Description : Percent (0-1) of the available width each bar should be within the category percentage. 1.0 will take the whole category width and put the bars right next to each other. Read More
The attribute is actually stored in scales.xAxes
("Options for xAxes" table).
So you just have to edit your chart this way :
var options = {
scales: {
xAxes: [{
barPercentage: 0.4
}]
}
}
Here is a fully working example with a custom width (
0.2
) for the bar :
var data = {
labels: ["January", "February", "March", "April", "May", "June", "July"],
datasets: [{
label: "My First dataset",
backgroundColor: "rgba(75,192,192,0.4)",
borderColor: "rgba(75,192,192,1)",
data: [65, 59, 75, 81, 56, 55, 40],
}]
};
var ctx = document.getElementById("myChart");
var myChart = new Chart(ctx, {
type: 'bar',
data: data,
options: {
scales: {
yAxes: [{
ticks: {
beginAtZero: true
}
}],
xAxes: [{
// Change here
barPercentage: 0.2
}]
}
}
});
console.log(myChart);
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.1.6/Chart.js"></script>
<canvas id="myChart"></canvas>
Update (Chart.js Version 2.2.0+)
As stated in the Release Version 2.2.0 - Candidate 2 :
Enhancements
- Can now manually configure the thickness of a bar in a bar chart. Use a new
barThickness
option on the correct axis to set the thickness of a bar.- And so on ...
score:0
As per above answer
Here is complete Bar chart graph using react chartjs2.
import React from 'react';
import {
Chart as ChartJS,
CategoryScale,
LinearScale,
BarElement,
Title,
Tooltip,
Legend,
} from 'chart.js';
import { Bar } from 'react-chartjs-2';
ChartJS.register(
CategoryScale,
LinearScale,
BarElement,
Title,
Tooltip,
Legend
);
export const options = {
responsive: true,
plugins: {
legend: {
position: 'top', // lable position left/right/top/bottom
labels: {
boxWidth: 0, // lable box size
}
},
},
elements: {
point: {
radius: 1
}
},
scales: {
x: {
display: false, // show/ hide x-axis
grid: {
display: false // show/hide grid line in x-axis
},
},
y: {
display: false, // same as x-axis
grid: {
display: false
}
}
}
};
const labels = ['January', 'February', 'March', 'April', 'May', 'June', 'July'];
export const data = {
labels,
datasets: [
{
label: 'datasets', // label text
data: [100, 300, 500, 700],
backgroundColor: '#7b62ff', // bar / column color
barThickness: 6, // <<<<<<<<<<<< bar / column size
},
],
};
export default function ResumesGraph() {
return (
<div>
<Bar
data={data}
options={options}
width={'500px'}
height={'180px'}
/>
</div>
);
}
score:9
barThickness
and maxBarThickness
(previously in ChartOptions[]
) are now a part of ChartDataSets[]
.
score:12
In case if you are using ng2-chart in an angular project then the bar chart configuration looks Alike this:
npm install ng2-charts chart.js --save
import 'ng2-charts' in your module.
import { ChartsModule } from 'ng2-charts';
Now the bar chart configurations:
barChartOptions: ChartOptions = {
responsive: true,
maintainAspectRatio: false,
legend: {
display: false
},
};
barChartLabels: Label[] = ['2006', '2007', '2008', '2009', '2010', '2011', '2012'];
barChartType: ChartType = 'bar';
barChartLegend = true;
barChartPlugins = [];
barChartData: ChartDataSets[] = [
{
barThickness: 16,
barPercentage: 0.5,
data: [65, 59, 80],
label: 'Growth'
},
{
barThickness: 16,
barPercentage: 0.5,
data: [28, 48, 40],
label: 'Net'
}
];
barChartColors: Color[] = [
{ backgroundColor: '#24d2b5' },
{ backgroundColor: '#20aee3' },
];
Now the HTML part:
<div class="bar-chart-wrapper">
<canvas baseChart [datasets]="barChartData" [colors]="barChartColors"
[labels]="barChartLabels"
[options]="barChartOptions" [plugins]="barChartPlugins" [legend]="barChartLegend"
[chartType]="barChartType">
</canvas>
</div>
You can control the height of your chart container
.bar-chart-wrapper {
height: 310px;
}
score:30
As of v2.7.2 it can be done by:
scales: {
xAxes: [{
maxBarThickness: 100,
}],
}
score:31
For version 2.8+ (and apparently as far back as 2.2), there are now some excellent controls over the bar thickness, max thickness, etc.
Per the Chart.js documentation, you would set them like so:
{
type: 'bar', // or 'horizontalBar'
data: ...,
options: {
scales: {
xAxes: [{
barThickness: 6, // number (pixels) or 'flex'
maxBarThickness: 8 // number (pixels)
}]
}
}
}
Source: stackoverflow.com
Related Query
- chart js 2 how to set bar width
- chartjs : how to set custom scale in bar chart
- How to dynamically set ChartJs line chart width based on dataset size?
- How to set single color on each bar in angular chart js
- How do I set a bar chart to default to a suggested maximum in chart.js v1.01?
- How to use set the color for each bar in a bar chart using chartjs?
- How to set custom Y Axis Values (Chart.js v2.8.0) in Chart Bar JS
- How to set X coordinate for each bar with react chart js 2?
- How to prevent empty bars from taking up width in Chart.js bar chart
- Chart js - set width to a specific bar
- How to set Solid label in bar chart using Chart JS
- How to get bar chart width in pixel
- How to set minimal value for bar chart in Chart.js? (ver. 2.0.2)
- How to set different colors in each stacked bar chart cell?
- How to set width of bar dynamically in chart.js?
- How to set percentage value by default on each bars in horizontal bar chart js
- Chart.js - How to set a line chart dataset as disabled on load
- How set color family to pie chart in chart.js
- How to modify bar width in Chartjs 2 bar charts
- Chart.js Bar Chart - how to chart bars from 0
- How to put rounded corners on a Chart.js Bar chart
- How to set labels align left in Horizontal Bar using chart.js?
- How to add second Y-axis for Bar and Line chart in Chart.js?
- How to create stacked bar chart using react-chartjs-2?
- How do I draw a vertical line on a horizontal bar chart with ChartJS?
- How to use PrimeNg Chart of Width and Height?
- How to display inline values in a stacked bar chart with Chart.js?
- ChartJs bar chart - keep bars left instead of equally spread across the width
- How to hide the y axis and x axis line and label in my bar chart for chart.js
- Chart js - Get bar width after render
More Query from same tag
- chartJS - points overplotting, jittering, noise?
- chartjs time cartesian axis adapter and date library setup
- Highlight a particular point in chart JS
- Can I arrange labels of legend in Doughnut chart one by one?
- Chart.js time scale not showing data
- How to constantly update y axis using a C# function to update a Live (Streaming) Chart.js Chart in ASP.NET core Web Application Razor Pages
- ChartJS: Two Titles for positive and negative part of y axis
- Add different labels in a line chart - Chart.js
- chart.js 2.7.1 - polar area chart has too much padding
- ChartJS : Hook before values are given to doughnut (absolute value in chart and real values in the tooltips)
- Cannot change font color and box width in chart
- Showing a duration of time within a Chart.js chart
- Chart.js canvas cuts off doughnut stroke
- How to change width of the chart using Chart.js
- React - Display Chart when button is clicked
- Chart.JS Global Options
- C# MVC5 View dynamically filled Chart.js dont show up
- Chart js with ng-repeat
- Getting 'Uncaught TypeError: Cannot read property 'helpers' of undefined'
- react-chartjs-2 (chart.js) Radar - Remove values?
- How to set max smaller than min in ChartJS
- chart.js data wont update
- Rendering charts using local Chart.js in overriding Django templates
- Convert two lists into a dictionary of X and Y Format
- Chart.js animation: onComplete event fired on hover
- chart.js: alternating background-color for ticks
- How can I hide the first Y Axis tick mark on ChartJs?
- Chartjs initial animation want to change from left to right (default it is bottom to top)
- Chart not displaying on page load
- How to commaize the data values provided to a chart in Chart.JS?