score:0
Accepted answer
you can omit labels
in the chart configuration and instead generate data
as individual points through objects containing x
and y
properties as shown here.
const labels = ["2020-05-13", "2020-05-11", "2020-05-12", "2020-05-14", "2020-05-09", "2020-05-10"];
const basedata = [20, 11, 9, 22, 11, 9];
const data = labels.map((l, i) => ({ x: l, y: basedata[i] }));
this produces the following data
.
[
{ "x": "2020-05-13", "y": 20 },
{ "x": "2020-05-11", "y": 11 },
{ "x": "2020-05-12", "y": 9 },
{ "x": "2020-05-14", "y": 22 },
{ "x": "2020-05-09", "y": 11 },
{ "x": "2020-05-10", "y": 9 }
]
the xaxis
would then have to be defined as follows:
xaxes: [{
offset: true,
type: 'time',
time: {
unit: 'day',
source: 'data',
tooltipformat: 'mmm dd'
}
}],
please have a look at your amended code below.
const labels = ["2020-05-13", "2020-05-11", "2020-05-12", "2020-05-14", "2020-05-09", "2020-05-10"];
const basedata = [20, 11, 9, 22, 11, 9];
const data = labels.map((l, i) => ({ x: l, y: basedata[i] }));
var chartdata = {
datasets: [{
label: "all detections",
backgroundcolor: "#02a499",
bordercolor: "#ffffff",
borderwidth: 1,
hoverbackgroundcolor: "#02a499",
hoverbordercolor: "#02a499",
data: data
}]
};
new chart("chartbydate", {
type: 'bar',
data: chartdata,
options: {
scales: {
xaxes: [{
offset: true,
type: 'time',
time: {
unit: 'day',
source: 'data',
tooltipformat: 'mmm dd'
}
}],
yaxes: [{
ticks: {
beginatzero: true
}
}]
}
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/chart.js/2.9.3/chart.bundle.min.js"></script>
<canvas id="chartbydate"></canvas>
Source: stackoverflow.com
Related Query
- ChartJS Bar chart with time scale - Bars overlap with each other
- Issue with chartjs linear gradient for the mixed bar chart in ReactJS is not calculated for each individual Bars
- How to prevent first/last bars from being cut off in a chart with time scale
- ChartJS bar chart with legend which corresponds to each bar
- chart js - bar chart with time scale on Y axis in Hours and Minutes
- ChartJS 2.7.3 stacked bar chart with overlap
- Chartjs v3 overlap stacked bar chart with groups
- Chartjs random colors for each part of pie chart with data dynamically from database
- chartjs : how to set custom scale in bar chart
- Chartjs 2 - Stacked bar and unstacked line on same chart with same y axis
- ChartJs bar chart - keep bars left instead of equally spread across the width
- Category scale on Y-axis and time on x-axis in bubble chart in Chartjs
- Chartjs - data format for bar chart with multi-level x-axes
- Fill Chart.js bar chart with diagonal stripes or other patterns
- chart.js v2: Align time scale labels with the center of the bars
- How to remove bars for those bars with zero value in Chartjs bar chart?
- ChartJS horizontal chart display legend on each bar
- Chartjs 3.x - How to duplicate X axis on both sides of horizontal bar chart with only 1 dataset?
- Chartjs with Vue, Bar Chart Border Radius not working
- Chart.js: Reverse bar chart with regular bars (Bottom to top instead of top to bottom)
- Bar chart with min height and zero values - ChartJs
- How to set a time scale to a ChartJS chart from JSON?
- How to set X coordinate for each bar with react chart js 2?
- Stacked bar chart with chartjs with included values
- ChartJS not showing data for time series bar chart
- chart.js wide bar with time scale
- Chartjs v2.7 - combo chart with time-series (financial) data; bar width's are irregular
- Chartjs - Stacked bar chart data order causes invisible other values
- Chartjs - Stacked bar chart blocking other values
- ChartJs - stacked bar chart with groups - how to create second x-axis with stack id
More Query from same tag
- Chart.js - scaleType='date' not working
- Click anchors the label to its respective section HTML in Chart.js
- Displaying Chart with data retrieved from Entity Framework in Controller
- Chart.js two columns on page - left column is squished until page resize
- Add watermark/logo to Chart.js
- Changing the background of a ChartJS on node js
- Accessing a slice of a react-chartjs Pie chart
- Parse an object to chart.js instead of arrays?
- Dynamically updating time data in chart.js freezes the chart
- How to make a step line with charts.JS
- Dynamically set data on Angular-chart.js
- Chart JS - Title missing when clearing canvas if no data is available
- How to create a Doughnut chart in django admin interface using foreign key field data?
- Chartjs change horizontal axis value
- "Responsive" Chart.js chart is taller than the containing window
- Chartjs: Is it possible to add phases in line chart?
- Filling area between two lines - Chart.js v2
- How to make 2 bar in a certain section of chart
- How to use major tick marks in chart.js 2.9.3?
- How to give break line in data label
- chartjs tooltip is cut off when out of canvas
- Defining y-starting point for floating horizontal bar charts in Chart.js?
- How can I fasten up the Chart.js graph generation from JQuery over PHP and mySQL?
- Chart.js line chart looking as area chart with smooth lines but not sharp
- Chart.js - Fill Text only appearing when hovering over one part of the doughnut
- Chart.js Types of property 'type' are incompatible. Type 'string' is not assignable to type '"line" | "bar" | "scatter"
- Inserting Graphs into DomPDF
- Bar values in Chart.js 2.X - dataset.metadata undefined
- Outputting multiple SQL queries in chart.js
- Tooltips displaying "rgb(0,0,0)" instead of label value on bar chart