ome*_*ga1 5 javascript chart.js
我正在使用 Chart.js 显示一个正常工作的折线图,但我希望 y 轴从 0 开始作为最小值。现在,y 轴从显示的数据中的最小值开始。我希望它总是从零开始显示。
这是我正在使用的代码(whcih 有效,除了不在 y 轴上从 0 开始)。
<script type="text/javascript">
$(document).ready(function(){
$.ajax({
url : "Get data from here",
type : "GET",
success : function(data){
console.log(data);
var userid = [];
var viewers = [];
for(var i in data) {
userid.push(data[i].date_time);
viewers.push(data[i].viewers);
}
var chartdata =
{
labels: userid,
datasets:
[
{
label: "Viewers for <?php echo $month . " - " . $year; ?>",
fill: false,
lineTension: 0.1,
backgroundColor: "rgba(59, 89, 152, 0.75)",
borderColor: "rgba(59, 89, 152, 1)",
pointHoverBackgroundColor: "rgba(59, 89, 152, 1)",
pointHoverBorderColor: "rgba(59, 89, 152, 1)",
data: viewers,
options:
{
responsive: true,
scales: {
yAxes: [{
ticks: {
beginAtZero: true
}
}]
}
}
}
]
};
var ctx = $("#mycanvas");
var LineGraph = new Chart(ctx, {
type: 'line',
data: chartdata
});
},
error : function(data) {
}
});
});
</script>
Run Code Online (Sandbox Code Playgroud)
我添加了这部分:
options:
{
responsive: true,
scales: {
yAxes: [{
ticks: {
beginAtZero: true
}
}]
}
}
Run Code Online (Sandbox Code Playgroud)
但它似乎没有做任何事情,而是继续从显示的数据中的最小值开始显示。
任何帮助,将不胜感激。
谢谢你。
将options零件移动到图表创建:
var LineGraph = new Chart(ctx, {
type: 'line',
data: chartdata,
options: {
responsive: true,
scales: {
yAxes: [{
ticks: {
beginAtZero: true
}
}]
}
}
});
Run Code Online (Sandbox Code Playgroud)