【发布时间】:2017-08-03 13:15:46
【问题描述】:
我无法让 chart.js 折线图同时响应高度和宽度。
查看示例它应该如何工作:
这是我的代码:
var randomScalingFactor = function(){ return Math.round(Math.random()*100)};
var lineChartData = {
labels : ['January','February','March','April','May','June','July'],
datasets : [
{
label: 'My First dataset',
labelColor : '#fff',
fontColor : '#fff' ,
backgroundColor : 'rgba(220,220,220,0.2)',
borderColor : 'rgba(220,220,220,1)',
pointBackgroundColor : 'rgba(220,220,220,1)',
pointBorderColor : '#fff',
data : [randomScalingFactor(),randomScalingFactor(),randomScalingFactor(),randomScalingFactor(),randomScalingFactor(),randomScalingFactor(),randomScalingFactor()]
}
]
}
var options = {
maintainAspectRatio: false,
legend: {
display: false,
},
scales: {
xAxes: [{
gridLines: {
display: false,
color: '#03A5C5',
lineWidth: 8,
},
ticks: {
fontColor: "white",
},
}],
yAxes: [{
gridLines: {
display: false,
color: '#03A5C5',
lineWidth: 8,
},
ticks: {
fontColor: "white",
beginAtZero: true,
}
}]
}
};
var ctx = document.getElementById('canvas-1');
var chart = new Chart(ctx, {
responsive: true,
type: 'line',
data: lineChartData,
options: options
});
【问题讨论】:
-
他们在docs 中说图表在响应方面存在问题。希望您能找到解决方案,因为我找不到
-
在文档中,他们建议将画布标签包装到容器 div 中,并将其设置为自己的宽度和高度。基本上,这就是您提到的响应示例中的完成方式。对解决你的案子有帮助吗?
-
@blewherself 成功了!谢谢
标签: javascript charts chart.js