score:1
Accepted answer
- Define different
displayFormats
forday
andmonth
. - Enable
ticks.major
. - Mark the desired ticks as
major
through theafterBuildTicks
callback.
time: {
...
displayFormats: {
day: 'D',
month: 'MMM D',
},
},
ticks: {
major: {
enabled: true
}
},
afterBuildTicks: (scale, ticks) => {
ticks.forEach((t, i) => t.major = i == 0 || new Date(t.value).getMonth() != new Date(ticks[i - 1].value).getMonth());
return ticks;
}
Please take a look at your amended code and see how it works.
const startDate = new Date(2020, 4, 15)
//===fake data===
const json = '{"responses":[{"rows":[{"values":["1"]},{"values":["0"]},{"values":["0"]},{"values":["0"]},{"values":["0"]},{"values":["0"]},{"values":["0"]},{"values":["0"]},{"values":["0"]},{"values":["0"]},{"values":["0"]},{"values":["0"]},{"values":["0"]},{"values":["0"]},{"values":["0"]},{"values":["0"]},{"values":["0"]},{"values":["0"]},{"values":["0"]},{"values":["0"]},{"values":["0"]},{"values":["0"]},{"values":["0"]},{"values":["0"]},{"values":["0"]},{"values":["0"]},{"values":["0"]},{"values":["0"]},{"values":["0"]},{"values":["0"]},{"values":["0"]},{"values":["0"]},{"values":["0"]},{"values":["0"]},{"values":["0"]},{"values":["0"]},{"values":["0"]},{"values":["0"]},{"values":["0"]},{"values":["0"]},{"values":["0"]},{"values":["0"]},{"values":["0"]},{"values":["0"]},{"values":["0"]},{"values":["0"]},{"values":["0"]},{"values":["0"]},{"values":["1"]},{"values":["6"]},{"values":["7"]},{"values":["5"]},{"values":["8"]},{"values":["9"]},{"values":["2"]},{"values":["1"]},{"values":["1"]},{"values":["1"]},{"values":["6"]},{"values":["3"]},{"values":["0"]},{"values":["20"]},{"values":["9"]},{"values":["3"]},{"values":["2"]},{"values":["1"]},{"values":["13"]},{"values":["3"]},{"values":["13"]},{"values":["13"]},{"values":["7"]},{"values":["12"]},{"values":["0"]}]}]}'
const values = JSON.parse(json).responses[0].rows.map((row, index) => {
let date = new Date(2020, 4, 20);
date.setDate(startDate.getDate() + index)
return {
y: row.values[0],
x: date
}
})
//===============
const options = {
legend: {
display: false
},
hover: {
mode: 'index',
intersect: false,
animationDuration: 0
},
scales: {
yAxes: [{
position: 'right'
}],
xAxes: [{
gridLines: {
display: false
},
distribution: 'linear',
type: 'time',
time: {
tooltipFormat: 'MMM D',
unit: 'day',
unitStepSize: 3,
displayFormats: {
day: 'D',
month: 'MMM D',
},
},
ticks: {
major: {
enabled: true
}
},
afterBuildTicks: (scale, ticks) => {
ticks.forEach((t, i) => t.major = i == 0 || new Date(t.value).getMonth() != new Date(ticks[i - 1].value).getMonth());
return ticks;
}
}]
},
tooltips: {
mode: 'x-axis',
}
};
const data = {
datasets: [{
label: 'test',
fill: false,
data: values,
backgroundColor: '#fff',
borderWidth: 2,
lineTension: 0,
borderColor: 'forestgreen',
hoverBorderWidth: 2,
pointBorderColor: 'rgba(0, 0, 0, 0)',
pointBackgroundColor: 'rgba(0, 0, 0, 0)',
pointHoverBackgroundColor: '#fff',
pointHoverBorderColor: 'forestgreen',
showLine: true,
}],
};
new Chart('myChart', {
type: 'line',
data: data,
options: options
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.9.3/Chart.bundle.min.js"></script>
<canvas id="myChart" height="90"></canvas>
score:3
Solution 1 label filter: According to the filtering labels sample you can set a function to define what should be displayed:
options: {
scales: {
x: {
display: true,
ticks: {
callback: function(dataLabel, index) {
// Apply logic to remove name of the month
return dataLabel
}
}
},
y: {
display: true,
beginAtZero: false
}
}
}
Solution 2: You could prepare your labels array beforehand. Filter the occurence of all the upcoming mentions and feed this array to chart.js.
Source: stackoverflow.com
Related Query
- Limit labels number on Chart.js line chart
- Chart.js Show labels on Pie chart
- ChartJS New Lines '\n' in X axis Labels or Displaying More Information Around Chart or Tooltip with ChartJS V2
- chart.js: Show labels outside pie chart
- Chart.js how to show cursor pointer for labels & legends in line chart
- Chart JS data labels getting cut
- How to display Line Chart dataset point labels with Chart.js?
- Making the labels responsive in chart js
- Chart.js - Draw bar chart with multiple labels
- Format chart.js x labels
- Chart JS grouped sub labels
- Display a limited number of labels only on X-Axis of Line Chart using Chart.js
- Changing x axis labels in Chart.js line chart
- Format Bar Chart's yAxis labels in Chart.js
- How to display data labels outside in pie chart with lines in ionic
- Line chart with large number of labels on X axis
- Center point labels between ticks (Polar Area Chart JS)
- How to align Chart.JS line chart labels to the center
- Chartjs - data format for bar chart with multi-level x-axes
- How to access labels array using chart plugin (Chart.pluginService.register) in Chartjs 2.x?
- How to display the labels in doughnut chart using ng2 charts?
- Chart.js Bubble chart with custome X-axis labels
- Change labels colors for radar chart js
- ChartJS: Show all labels of a mixed chart in the tooltip
- Chart.js Radar Chart How to Remove Outer Labels
- ChartJS 2.0 - Huddle labels on pie chart
- Multiple line labels for chart js
- Remove radar chart labels in chart.js
- Chart JS: Ignoring x values and putting point data on first available labels
- Show labels on each sector to polar chart using angular js chart
More Query from same tag
- How to access global variable from outside of canvas.onmousemove function?
- Chart.js time object labels not updating correctly
- Hide labels from pie chart in chartjs
- PHP string data passed to Chart.js not rendering well
- how to change color of dots in graph using chart.js
- Two pie charts in one flexbox container
- Put JSON data into chart.js pie chart
- Chart for Real time data with duplicate x axis
- In which stage of the program do I remove my legend labels in charts.js?
- Render chartjs and export image to data not in the DOM
- How to set dynamic value for horizontal line in annotation plugin for Chart.js
- How to Find the Sum of items Per Year Using Chart Js
- Legend not displaying on Line or Bar chart - React-Chartjs-2
- Display legend of grouped data (different colors) in chart
- How to customize Data Labels of area chart on ChartJS?
- Change size of bubble radius without changing r value
- bind first property value of an array of object into chart.js
- custom label title for scatter in React-ChartJS-2
- How to make Chart JS responsive?
- destroy method implementation in pie chart chartjs
- How to use generated HTML legends to enable or disable datasets in Chart.js
- chartjs : how to set custom scale in bar chart
- chart js download to png with canvas.toDataURL() not working in IE and firefox
- Chart.js Start Y-Axis at a defined value
- ChartJs doesn't work on a local downloaded page of Circular Gauge chart example
- How to fetch all data from API files and assign their values to chart?
- Chart.js bar chart is overflowing its containing element
- Updating Chartjs with new data
- How do I make the y-axis intersect the x-axis at 0 in chart.js
- How could I skip drawing empty/zero value and its value on tooltip