score:22
Since you wish to use time for x-axis, your labels
array should be an array of date/time string (labels
array is correspondent to x-axis).
You would also need to set the parser
property (to parse the date/time correctly), and x-axis' ticks source to data
(to properly generate x-axis ticks).
UPDATE
If you only have a min and max date then, you can create a nice little plugin to populate the labels
(date) array dynamically, as such :
plugins: [{
beforeInit: function(chart) {
var time = chart.options.scales.xAxes[0].time, // 'time' object reference
// difference (in days) between min and max date
timeDiff = moment(time.max).diff(moment(time.min), 'd');
// populate 'labels' array
// (create a date string for each date between min and max, inclusive)
for (i = 0; i <= timeDiff; i++) {
var _label = moment(time.min).add(i, 'd').format('YYYY-MM-DD HH:mm:ss');
chart.data.labels.push(_label);
}
}
}]
note: moment.js is used to make calculations easier.
ᴡᴏʀᴋɪɴɢ ᴇxᴀᴍᴘʟᴇ ⧩
( for demonstration purposes, I've changed the time unit
to day
)
$(document).ready(function() {
new Chart(document.getElementById("chartBox"), {
type: 'line',
data: {
datasets: [{
data: [12, 19, 3, 5, 2, 3, 32, 15],
label: "",
borderWidth: 2,
borderColor: "#3e95cd",
fill: false,
pointRadius: 0
}]
},
options: {
scales: {
xAxes: [{
type: 'time',
time: {
parser: 'YYYY-MM-DD HH:mm:ss',
unit: 'day',
displayFormats: {
day: 'ddd'
},
min: '2017-10-02 18:43:53',
max: '2017-10-09 18:43:53'
},
ticks: {
source: 'data'
}
}]
},
legend: {
display: false
},
animation: {
duration: 0,
},
hover: {
animationDuration: 0,
},
responsiveAnimationDuration: 0
},
plugins: [{
beforeInit: function(chart) {
var time = chart.options.scales.xAxes[0].time, // 'time' object reference
timeDiff = moment(time.max).diff(moment(time.min), 'd'); // difference (in days) between min and max date
// populate 'labels' array
// (create a date string for each date between min and max, inclusive)
for (i = 0; i <= timeDiff; i++) {
var _label = moment(time.min).add(i, 'd').format('YYYY-MM-DD HH:mm:ss');
chart.data.labels.push(_label);
}
}
}]
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/moment@latest/moment.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.7.0/Chart.min.js"></script>
<canvas id="chartBox"></canvas>
score:1
You might want to change a few things.
Your data should include time data.
If you set scale unit to be
month
, your min max should be more than one month to see the actual scales.
Here's simplified working example.
score:3
If the xAxis label is in date format use this code
time:
{
format: 'MM',
unit: 'month',
displayFormats: { month: 'MM' },
max: '2017-10-09 18:43:53',
min: '2017-10-02 18:43:53'
}
If the xAxis label is as what u used numerals use
time:
{
format: 'MM',
unit: 'month',
parser:'MM',
displayFormats: { month: 'MM' },
max: '2017-10-09 18:43:53',
min: '2017-00-02 18:43:53'
}
score:7
The dataset should be an array of objects with properties x for time and y for value.
$(document).ready(function() {
var data = [{
x: new moment().add(-10, "months"),
y: Math.random() * 100
},
{
x: new moment().add(-8, "months"),
y: Math.random() * 100
},
{
x: new moment().add(-6, "months"),
y: Math.random() * 100
},
{
x: new moment().add(-4, "months"),
y: Math.random() * 100
},
{
x: new moment().add(-2, "months"),
y: Math.random() * 100
},
{
x: new moment().add(-0, "months"),
y: Math.random() * 100
},
];
new Chart(document.getElementById("chartBox"), {
type: 'line',
data: {
datasets: [{
data: data,
borderColor: "#3e95cd",
fill: false
}]
},
options: {
scales: {
xAxes: [{
type: 'time'
}]
},
legend: false
}
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.0/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.7.0/Chart.bundle.min.js"></script>
<canvas id="chartBox"></canvas>
Source: stackoverflow.com
Related Query
- Chart JS - Use time for xAxes
- Chart JS - set start of week for x axis time series
- How to use 'time' (data from database, data type: timestamp ) for plotting graph in Chart JS
- How to use set the color for each bar in a bar chart using chartjs?
- How to set the xAxes min and max values of time cartesian chart in Chart.js
- how can i use chart.js to create a chart that has one time series line and one linear line on it at the same time?
- How to use Real time chart in react?
- Use fetch to get JSON Data for Chart JS
- Retrieving daily & monthly totals from MySQL for use in a chart
- ChartJS not showing data for time series bar chart
- How to use chart.js to plot line chart with x axis as time stamp in seconds
- getting additional value fields from data source for dx.chartjs doughnut chart
- I'm trying to use chart.js in angular to create a simple bar chart but I'm getting an error as 'legend' type doesn't exist for options
- how can i use inline plugin inner title for chart js?
- How to use computed property with data from JSON in data object for time axis in Vue using Chart.js
- Use Google Spreadsheet as data source for Chart.js
- Chart for Real time data with duplicate x axis
- How can labels/legends be added for all chart types in chart.js (chartjs.org)?
- Chart Js Change Label orientation on x-Axis for Line Charts
- Chartjs random colors for each part of pie chart with data dynamically from database
- How to prevent first/last bars from being cut off in a chart with time scale
- Chart.js how to show cursor pointer for labels & legends in line chart
- show label in tooltip but not in x axis for chartjs line chart
- How to add second Y-axis for Bar and Line chart in Chart.js?
- chart.js Line chart with different background colors for each section
- Problem for display a chart with Chart.js and Angular
- How to use PrimeNg Chart of Width and Height?
- Chart js. How to change font styles for "labels" array?
- Different color for each column in angular-chartjs bar chart
- Chart.js Dynamically Updating Chart with X Axis Time
More Query from same tag
- How to add overlay color to chart.js pie chart segment?
- Change point style in Chartjs via external event
- Two data sets with different time instances on the same ChartJs chart
- how I can get label name when user click on particular area in doughnut chart.js? I am not able to find the index of selected area for chart.js
- Create stacked bar chart with a single dataset per stack
- charts.js won't show legend and on chart descriptions
- ChartJS bar chart - trigger hover even when the cursor is outside of the bar
- Charts Js Stacked Bar Graph displays no values?
- Beginner in JS,Stuck with ChartJS and multiple filters and ways to display
- Chartjs bar-chart does not render when values are equal
- How can I add user inputted values to my chart.js line graph?
- Chart.js Bar and Line chart
- Background-color for area below line chart in chartJs?
- How to add a coloured legend box to a pie chart with Chart.js v1?
- Chart JS - How to display Label over Slices instead of text on hover
- tooltip messes up bar chart in Chart.js
- Uncaught TypeError: lineChart.Line is not a function in chart.js
- ChartJS glitches
- Dynamically populating data into chart in Chart.js
- Call ChartJS details in main Javascript file
- Changing the Y axis unit in Chartjs
- How do I create a legend for a bar chart?
- Chart.js line chart tooltip shows wrong label when line doesn't start at first label
- Draw a single frame in Chart.js
- How to configure linear labels in Time Cartesian Chartjs?
- How to create Waterfall model chart using QuickChart?
- Change Angular js charts.js chart type dynamically
- how to sort tooltip value in Chart.js
- Chart.js how to change point radius of scatter charts on resize events?
- How to add space between two scales in chart-js?