Tom*_*yen 2 javascript sqlite cordova chart.js
所以我正在使用 Cordova 制作这个移动应用程序,它允许我使用 HTML、CSS 和 JavaScript。我目前使用 SQLite 作为数据库(工作方式与 mySQL 几乎相同),但这并不重要。我所做的就是从该数据库中获取我的数据。我得到了日期(数据添加到数据库的时间)和一个人的体重。
我使用的折线图显示 Y 轴上的重量和 X 轴上的数据。
我想要做的只是将月份显示为 x 轴上的标签,但仍使其显示每天的数据。例如,如果我在 1 月 1 日、1 月 2 日和 1 月 3 日添加数据。我希望能够在我的图表上看到所有 3 天的点,但在 X 轴上它应该只显示“一月”。
我一直在研究 Chart.JS 的“时间”选项,但无法真正理解我应该如何做。
HTML:
<canvas id="myChart" style="margin-top: 20px;"></canvas>
Run Code Online (Sandbox Code Playgroud)
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
}
}]
}
}
});
Run Code Online (Sandbox Code Playgroud)
使用 x 轴中的时间类型。使用time: { unit: 'month' }(总是月份)或minUnit(如有必要,月份和年份)您可以获得月份标签。
作为您数据的标签,您需要传递一个Date或moment
scales: {
xAxes: [{
type: 'time',
time: {
unit: 'month'
}
}],
}
Run Code Online (Sandbox Code Playgroud)
这是一个完整的例子。检查日期的时刻文档,尤其是日期的创建和解析。Chart.js 适用于时刻日期,因此它非常重要(顺便说一下也很简单)。
| 归档时间: |
|
| 查看次数: |
5616 次 |
| 最近记录: |