score:4
But it displays the value in the wrong position as it is in the image
That's because the indexes
of the data you provided corresponds to the first 3 values of your labels.
Solution #1:
To resolve this, you have the even out the length of the labels to the length of your data set by providing placeholder values for the lacking points in your data.
Do not use zeros as placeholder for non-existent values in your dataset if you don't want Chartjs to display the chart data in this manner. [See the short grayed-out areas indicating the zero values.]
So the better way to handle this is to provide null
values instead. See working example below.
const labels = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'June', 'July', 'Aug', 'Sept', 'Oct', 'Nov', 'Dec'];
const data = [{'x':'Apr', 'y':40},{'x':'July', 'y':70},{'x':'Dec', 'y':120}];
const filledMonths = data.map((month) => month.x);
const dataset = labels.map(month => {
const indexOfFilledData = filledMonths.indexOf(month);
if( indexOfFilledData!== -1) return data[indexOfFilledData].y;
return null;
});
console.log(dataset);
Solution #2: In Chartjs > 2.x you can use time scale
If you have a lot of data and don't want to usey placeholder values for your missing data then you set your xAxes
scale type as time
in your chart options.
Here's an answer using this implementation.
score:-2
Chart.js supports all of the formats that Moment.js accepts ('MMM YYYY'). Just adding the year should do the trick.
score:0
If you don't have any value for any month, provide 0 as data for the same.
var options = {
type: 'bar',
data: {
labels: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'June', 'July', 'Aug', 'Sept', 'Oct', 'Nov', 'Dec'],
datasets:[
{
label: 'Test',
backgroundColor: 'red',
data: [0,0,0,40,0,0,70,0,0,0,0,120]
}
]
},
options: {
responsive: true,
legend: {
position: 'top',
},
title: {
display: true,
text: 'My Test'
},
scales: {
yAxes: [{
ticks: {
beginAtZero: true
}
}]
}
}
}
var ctx = document.getElementById('canvas').getContext('2d');
new Chart(ctx, options);
Source: stackoverflow.com
Related Query
- Different amount of label and data in Chart.js
- How to reuse a Chartjs Chart component in with different Data and get past the **Canvas is already in use** error?
- Chart Piechart in only show percentage on hover but it shows data and label with percentage both i just want to show percentage only percentage
- ChartJs multiaxis chart show different label bottom and top
- I am using chart js to draw a chart. I did everything right but i don't know why the x axis and y axis label is not comming in chart. code below
- Bar Chart cannot read data and label in laravel
- ChartJS - Draw chart with label by month, data by day
- Chart.js Mixed Bar and Line chart with different scales
- Chart.js bar chart : Grid color and hide label
- using array values in chart.js data and label field
- Chart.js Timeseries chart - formatting and missing data values
- Chart JS show multiple data points for the same label
- ng2-charts customize data and whole html content of tooltip displayed when hovering on bar chart
- How to hide the y axis and x axis line and label in my bar chart for chart.js
- Remove the label and show only value in tooltips of a bar chart
- chart js - Apply different color for each x-axes label
- How can I display the xAxes and yAxes data in the tooltip, Chart JS?
- Chart JS: Ignoring x values and putting point data on first available labels
- How to show data values in top of bar chart and line chart in chart.js 3
- Custom data in label on ChartJS pie chart
- ChartJS (React) Line Chart - How to show single tooltip with data and labels from 3 (multiple) dataset?
- How to get chart from data points (arrays) with inconsistent time intervals and chart.js?
- Chart.js Bar Chart - grouping and mapping data
- How to change the label and grid line position on a timeseries chart in Chart.js 3?
- How do I customize y-axis labels and randomly pick the value from the data range for x-axis in Chart js
- Doughnut Chart not displaying data with Chart Js and Backbone js
- How can I have different values for the chart and the tooltip in chart.js?
- create different labels for different data chart js
- Charts js and laravel: Render chart after passing in json data
- Chart.js Doughnut chart inner label different than outer
More Query from same tag
- Bubble Chart in Chartjs not working
- How can I draw lines inside the bars in charts.js?
- Resizing chart before downloading as image
- Why aren't my data points getting placed in the corresponding locations in my chart?
- ChartJS plugin with live label update
- how to put a second pie chart right next to first pie chart inside the bootstrap card
- How to Reset a Chart of Chart.Js?
- Chart.register is not a function
- Loading or Activating ChartJS on Load
- Chart.js legend text showing undefined
- Why must I create my VueJS application before using ChartJS?
- chart.js: Chart in chart
- Pause horizontal scrolling in chart.js for real time data
- Make chartjs pie chart wiyh dynamic data
- VueJS + Chartjs - Chart only renders after code change
- chart js bar chart not displaying array
- ChartJs: Is there a way to control the font options per line for a multiline axis label
- Plot a Line chart using Chart.js with CSV Data
- Char.js Interpolation linear
- Vue-Chartjs onComplete custom labels - prevent blinking
- Use PHP to populate chart.js with data from an SQLite Database
- Update all Chart.js instances in order to apply changed defaults?
- Chart.js error: type lacks a call signature '((...items: number[]) => number) | ((...items: ChartPoint[]) => number)' in Angular
- I have a issue with chart.js, php and infomix database?
- Stepped Line with angular-chart.js
- Update angular-chart.js on filtered list
- Chart.js: bad alignment of label during ajax update
- Problem to create dynamic pie chart in ChartJS
- Placing JavaScript content in Bootstrap Popover
- Data not fully displaying on bar chart