score:2
Accepted answer
Your file bars shall be equally spread over the available space on the x-axis. Using a time axis in this context is not an option since it won't correctly match the positions of the bars.
It would take time to explain everything in detail to obtain what you're looking for. Therefore, you best study below runnable code and see how it can be done.
var data = [
{ date: "1/1/2020 11:35:44 PM", file: "file1", value: 0.0056 },
{ date: "1/1/2020 23:35", file: "file2", value: 0.00103 },
{ date: "1/1/2020 23:35", file: "file3", value: 0.00513 },
{ date: "1/1/2020 23:59", file: "file4", value: 0.00589 },
{ date: "3/2/2020 0:24", file: "file5", value: 0.03017 },
{ date: "3/2/2020 0:30", file: "file6", value: 0.00347 },
{ date: "3/2/2020 0:43", file: "file7", value: 0.00784 },
{ date: "5/2/2020 0:51", file: "file8", value: 0.02477 },
{ date: "5/2/2020 1:08", file: "file9", value: 0.00858 },
{ date: "8/2/2020 2:03", file: "file10", value: 0.00753 },
{ date: "8/2/2020 2:36", file: "file11", value: 0.0091 },
{ date: "1/2/2021 4:38", file: "file12", value: 0.01175 },
{ date: "2/4/2021 5:15", file: "file13", value: 0.01092 }
];
const moments = data.map(o => moment(o.date, 'M/D/YYYY'));
const months = moments.map(m => m.format('MMM'));
const monthLabels = months.map((m, i) => i == 0 || months[i - 1] != m ? m : '〃');
const years = moments.map(m => m.format('YYYY'));
const yearLabels = years.map((y, i) => i == 0 || years[i - 1] != y ? y : '');
new Chart('CanvasFileData', {
type: 'bar',
data: {
labels: data.map(o => o.file),
datasets: [{
data: data.map(o => o.value),
backgroundColor: 'rgb(68, 114, 196)',
barPercentage: 0.5
}]
},
options: {
responsive: true,
title: {
display: true,
text: "File data"
},
legend: {
display: false
},
scales: {
xAxes: [{
ticks: {
minRotation: 90,
padding: -32
},
gridLines: {
drawOnChartArea: false,
tickMarkLength: 40
}
},
{
offset: true,
labels: monthLabels,
gridLines: {
display: false
},
},
{
offset: true,
labels: yearLabels,
gridLines: {
display: false
},
scaleLabel: {
display: true,
labelString: 'Date'
}
}],
yAxes: [{
scaleLabel: {
display: true,
labelString: 'value'
},
ticks: {
beginAtZero: true
}
}]
},
tooltips: {
callbacks: {
label: tooltipItem => "File: " + tooltipItem.xLabel + ", Date:" + data[tooltipItem.index].date + ", Value:" + tooltipItem.value
}
}
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.1/moment.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.9.4/Chart.min.js"></script>
<canvas id="CanvasFileData" height="150"></canvas>
Source: stackoverflow.com
Related Query
- Issue while plotting bar chart with custom x-axis with month and year in chart.js
- Chartjs 2 - Stacked bar and unstacked line on same chart with same y axis
- chart js - bar chart with time scale on Y axis in Hours and Minutes
- Chart.js bar chart with time on X axis and category on Y axis is not rendered
- How to add custom text inside the bar and how to reduce the step size in y axis in chart js( Bar chart )
- Chart.js Mixed Bar and Line chart with different scales
- Show X axis on top and bottom in line chart rendered with Chart.js 2.4.0
- How to hide the y axis and x axis line and label in my bar chart for chart.js
- Chart.js with dual axis on bar and line graph
- to increase space between x axis and first horizontal bar in chart js
- Chart JS : Getting issue with draw bar chart
- Chartjs 3.x - How to duplicate X axis on both sides of horizontal bar chart with only 1 dataset?
- How to set custom Y Axis Values (Chart.js v2.8.0) in Chart Bar JS
- Bar chart with min height and zero values - ChartJs
- TypeScript issue with custom plugin and afterDraw callback Chart.js and react-chartjs-2
- Issue with chartjs linear gradient for the mixed bar chart in ReactJS is not calculated for each individual Bars
- Chart with Time axis only displaying first grid line and tick label (unitStepSize)
- issue with customisation of Chart new.js for bar chart
- Chart.js combined line and bar chart with differing data points
- how to write labels along with data on top and bottom of each stack in bar chart
- ChartJS - would like to create a mixed chart with horizontal Bar and a dot to represent the answer from the current user
- 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
- Line chart plotting multiple points for duplicate data on x and y axis using chart.js
- Chart.js issue in plotting numeric X and Y in line chart
- Multiple axis line chart with Chart.js and JSON data from Google Sheet
- Chart.js with line chart and bar chart - bar chart not rendered although the max value of it is shown
- How can I build some chart with Chart.js and ng2-charts using an array of revenus & budgets per month date as Data source?
- How to change bar color on a angular-chart and chart js bar graph with multiple datasets?
- ChartJS: Bar chart with axis ticks wider than categories
- how to create bar chart with group and sam color for each group using chart.js?
More Query from same tag
- Calculate Ideal Burndown
- ChartJS: Position labels at end of doughnut segment
- Chartjs - show elements in all datasets on hover using bar chart
- Fit outlabels to canvas and use different colors chartjs
- Remove x-axis label/text in chart.js
- Maximum call stack error when attempting to update Chart in Vue js
- Add padding between two y-axis scales using chart.js
- How do I use NuGet to install ChartJS?
- ChartJS xAxis time formats don't change for days
- How can I display different values on xAxes than on tooltip Chart.js V3
- Add Text to Doughnut Chart - ChartJS
- Chart.js + Angular 5 - Destroy() multiple dynamic charts created through *ngFor loop
- How can I show chartjs datalabels only last bar?
- React-Redux and Chart.js props Do not rerender
- How to save the previous state and current state for a barchart in reactjs
- ChartJS "Unable to get property 'getTime' of undefined or null reference"
- ChartJS tooltip label for pie cart being cut
- How can I change the legend label without affecting my tooltip label?
- Loop datasets into chart.js?
- How do I get a chart.js chart to display data in a Node/React web application?
- How do I make my Type:Time to work with my Line Chart?
- Can't create separate charts using chart.js
- Setting Chart.js Pie-Graph Options
- chart.js:4 Uncaught ReferenceError: require is not defined in ionic 2
- chartjs - multi axis line chart - cannot read property 'min' of undefined
- Angular: chart.js chart is not displaying
- showing tooltips all the time in chartjs 2.4 not working
- How to align elements using a ChartBar from Chart.js and Alerts from Bootstrap 4?
- Drilldown on barchart using CHARTJS Devexpress
- Vue.js - this.<value>.<value> is undefined