Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
71
rated 0 times [  78] [ 7]  / answers: 1 / hits: 11336  / 5 Years ago, fri, august 30, 2019, 12:00:00

So I'm making this mobile app using Cordova, which allows me to use HTML, CSS and JavaScript. I'm currently using SQLite as a database (works almost same as mySQL) but that isn't that important. All I do is get my data from that database. I get both the date (of when the data was added to the Database) and the weight of a person.



I'm using a linegraph which show the weight on the Y-axis and the data on the X-axis.



What I want to do is only show the months as labels on the x-axis but still make it so that it show data for each day seperate. So for example if I add data on the 1st of January, the 2nd of January and the 3rd of January. I want to be able to see all 3 days as dots on my graph but on the X-axis it should only say 'January'.



I've been looking into the 'time' option of Chart.JS but can't really make any sense of how I'm supposed to do it.



HTML:



<canvas id=myChart style=margin-top: 20px;></canvas>


JavaScript:



//these are the arrays that I will fill dynamically. Labels will be my months and data will be the weight

var labels = [];
var data = [];

var ctx = document.getElementById('myChart').getContext('2d');
var chart = new Chart(ctx, {
// The type of chart we want to create
type: 'line',

// The data for our dataset
data: {
labels: labels,
datasets: [{
backgroundColor: 'rgba(255, 119, 0, 0.5)',
borderColor: 'rgba(255, 119, 0, 1)',
data: data
}]
},

// Configuration options go here
options: {
legend: {
display: false
},
scales: {
yAxes: [{
ticks: {
beginAtZero: true
}
}]
}
}
});

More From » sqlite

 Answers
3

Use the time-type in your x-axis. With time: { unit: 'month' } (always months) or minUnit (months and years if necessary) you can get the month labels.



As label for your data you need to pass a Date or moment



scales: {
xAxes: [{
type: 'time',
time: {
unit: 'month'
}
}],
}


Here is a complete example.
Check the moment docs for dates, especially the creation and parsing of dates. Chart.js works with moment dates so it's quite important (and quite easy btw).


[#6400] Wednesday, August 28, 2019, 5 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
hailey

Total Points: 355
Total Questions: 91
Total Answers: 91

Location: India
Member since Wed, Aug 4, 2021
3 Years ago
hailey questions
Sun, Jul 11, 21, 00:00, 3 Years ago
Mon, Nov 2, 20, 00:00, 4 Years ago
Fri, Jul 24, 20, 00:00, 4 Years ago
Sun, Apr 28, 19, 00:00, 5 Years ago
;