score:4
You can provide a labels
array padded with empty strings to the required length, e.g. 15.
This will ensure a consistent number of categories on the axis, and thus ensure that the bars will stay the same width and left-aligned even when there are less bars (Fiddle).
(If you like, you can also pad the data
array with 0
's for consistency, but it's not necessary - the labels
array is what determines the axis looks like.)
var data = [12, 19, 3, 5, 2];
var labels = ["a", "b", "c", "d", "e"];
for (var i = labels.length; i < 15; i++) {
labels.push("");
}
var ctx = document.getElementById("myChart");
var myChart = new Chart(ctx, {
type: 'bar',
data: {
labels: labels,
datasets: [{
label: 'Count',
data: data
}]
},
options: {
scales: {
yAxes: [{
ticks: {
beginAtZero: true
}
}]
}
}
});
Source: stackoverflow.com
Related Query
- ChartJs bar chart - keep bars left instead of equally spread across the width
- Issue with chartjs linear gradient for the mixed bar chart in ReactJS is not calculated for each individual Bars
- All the bar in bar chart stays at left with very little width
- Chart JS horizontal bar chart width of bars and the chart
- chartjs bar chart align the legend to left position
- Border radius for the bar chart in ChartJS
- Grouping the object by key for Chartjs bar chart
- How to make bar chart animation where all bars grow at the same speed?
- ChartJS bar chart fixed width for dynamic data sets
- Chart.js bar chart mouse hovering highlights all the datasets instead of just the selected one
- ChartJS bar chart - trigger hover even when the cursor is outside of the bar
- Display Bar chart values in the graph - ChartJS
- Chart.js: Reverse bar chart with regular bars (Bottom to top instead of top to bottom)
- Can the colors of bars in a bar chart be varied based on their value?
- Stacked Bar chart fixed width bar chart issue - Chartjs
- How to show the chartjs bar chart data values labels as text?
- How to prevent empty bars from taking up width in Chart.js bar chart
- Bar chart (chart.js) with only 2 points does not show one of the bars
- How can I move chartJs legend left side and change the width and Hight of the chart?
- ChartJS - would like to create a mixed chart with horizontal Bar and a dot to represent the answer from the current user
- ChartJS vertical bar chart - measure percentage of the data in each column based on a specified max
- Chart.js Change color of the values *inside* the bars of a Bar chart
- Chart.js stacked bar chart text on top of the stacked bars
- chartjs add dots to bars in grouped bar chart
- how to highlight the bars in stacked bar chart of chart.js on clicking a legend
- How to keep side bars in Chartjs bar graph from clipping?
- How do you get the width of a datalabel from the Chartjs plugin after the chart animates?
- react-chart-js-2 keep bar width no matter what the number of bar is
- ChartJS align bars to left instead of center
- ChartJS Bar chart with time scale - Bars overlap with each other
More Query from same tag
- How to get chart size (without labels) with chart.js?
- Click event using Chart.js with AngularJS
- Ionic 3 + Angular 4 + chart.js - loading data from array
- Set minimum and maximum axis values on chart
- C# MVC5 View dynamically filled Chart.js dont show up
- Chart.JS value on top disappear when tooltip show up
- Chart.js - Getting data from HTML
- Chart.js Radial axes: is it possible to use multiple axes on radar chart?
- How can I implement Polar area charts with Chart.js and Canvas
- Can we draw a Line Chart with both solid and dotted line in it?
- Angular protractor tests timeout on pages containing Chart.js
- Testing Chart.js with Jest/Enzyme - Failed to create chart: can't acquire context from the given item
- How to show data from views on chartjs in django?
- Cant change labels in Chart.js 2.0 Beta
- How to set and adjust plots with equal axis aspect ratios - CHART.js
- Set min, max and number of steps in radar chart.js
- charts.js stacked y-Axis
- ChartJS 3.7.1 tooltip callback, get label value for the next index
- set y-axis scale manually in a bar chart using angular-chart.js
- How can I export data from a csv file or excel file to a javascript object?
- ChartJS bar chart - trigger hover even when the cursor is outside of the bar
- Pushing data from json to Chart.js labels and data
- chartjs - top and bottom padding of a chart area
- React with chart js, labelling problems in multiple datasets for doughnut chart
- Loading and updating dynamic charts with dynamic data (chart.js)
- Reduce space between ticks in horizontal bar-chart chartJS
- Bar chart.js legend showing "undefined"
- ChartJS Line Chart shoud not start at 0 with all Lines
- refs in reactjs Component empty
- hiding x-axes labels with 0 values on a bar chart