Chart.js纵横比/强制高度
Chart.js aspect ratio / forced height
我正在尝试使用 chart.js 创建缩略图,这将 link 到包含完整图表的页面。
linked 页面上的完整图表看起来不错,但对于缩略图,我无法正确调整尺寸。 canvas 覆盖了正确的区域,但图表没有垂直填充它。
var data = {
labels: ['','','','','','','','','',''],
datasets: [{
data:[22,25,23,24,25,22,25,23,24,25],
backgroundColor: "rgba(54, 255, 170, 0.4)",
borderColor: "rgba(54, 255, 170, 1)",
orderWidth: 1,
}]
};
var options = {
tooltips: false,
legend: {
display:false
},
scales: {
yAxes: [{
display:false,
ticks: {
beginAtZero:true
}
}],
xAxes: [{
gridLines: {
color: "rgba(0, 0, 0, 0)",
}
}]
}
};
new Chart($("#chart-"+this.model.id), {
type: 'bar',
data: data,
options: options
});
我尝试过调整 canvas 的最小高度之类的方法,但这会导致条形图模糊。
有什么办法可以调整条的高度以占据整个 canvas?
尝试设置这些选项:
options: {
responsive: true,
maintainAspectRatio: false
}
如果将 maintainAspectRatio 设置为 true,将自动计算高度。
实际上,在 Chart.js 3.2.1 中更新纵横比,为您的方案添加更多高度,您可以使用 aspectRatio
选项:
options: {
aspectRatio: 1,
}
因此根据文档:
Canvas aspect ratio (i.e. width / height, a value of 1 representing a square canvas). Note that this option is ignored if the height is explicitly defined either as attribute or via the style.
你可以这样设置:
1
为正方形
0.5
是宽度的两倍
2
宽度是高度的两倍
const config = {
type: 'bar',
data,
options: {
responsive: true,
maintainAspectRatio: false
}
};
var yourChart= new Chart(
document.getElementById('chartCanvas'),
config
);
});
yourChart.canvas.parentNode.style.height = '480px';
yourChart.canvas.parentNode.style.width = '280px';
使用maintainAspectRatio
和responsive
options: {
responsive: true,
maintainAspectRatio: false,
}
let chart = new Chart('myChart', {
type: 'bar',
data: {
labels: ['Red', 'Blue', 'Yellow', 'Green', 'Purple', 'Orange'],
datasets: [{
label: '# of Votes',
data: [12, 19, 3, 5, 2, 3],
backgroundColor: [
'rgba(255, 99, 132, 0.2)',
'rgba(54, 162, 235, 0.2)',
'rgba(255, 206, 86, 0.2)',
'rgba(75, 192, 192, 0.2)',
'rgba(153, 102, 255, 0.2)',
'rgba(255, 159, 64, 0.2)'
],
borderColor: [
'rgba(255, 99, 132, 1)',
'rgba(54, 162, 235, 1)',
'rgba(255, 206, 86, 1)',
'rgba(75, 192, 192, 1)',
'rgba(153, 102, 255, 1)',
'rgba(255, 159, 64, 1)'
],
borderWidth: 1
}]
},
options: {
responsive: true,
maintainAspectRatio: false,
scales: {
y: {
beginAtZero: true
}
}
}
});
<script src="https://cdn.jsdelivr.net/npm/chart.js@3.6.2/dist/chart.min.js"></script>
<canvas id="myChart" width="400" height="400"></canvas>
我正在尝试使用 chart.js 创建缩略图,这将 link 到包含完整图表的页面。
linked 页面上的完整图表看起来不错,但对于缩略图,我无法正确调整尺寸。 canvas 覆盖了正确的区域,但图表没有垂直填充它。
var data = {
labels: ['','','','','','','','','',''],
datasets: [{
data:[22,25,23,24,25,22,25,23,24,25],
backgroundColor: "rgba(54, 255, 170, 0.4)",
borderColor: "rgba(54, 255, 170, 1)",
orderWidth: 1,
}]
};
var options = {
tooltips: false,
legend: {
display:false
},
scales: {
yAxes: [{
display:false,
ticks: {
beginAtZero:true
}
}],
xAxes: [{
gridLines: {
color: "rgba(0, 0, 0, 0)",
}
}]
}
};
new Chart($("#chart-"+this.model.id), {
type: 'bar',
data: data,
options: options
});
我尝试过调整 canvas 的最小高度之类的方法,但这会导致条形图模糊。 有什么办法可以调整条的高度以占据整个 canvas?
尝试设置这些选项:
options: {
responsive: true,
maintainAspectRatio: false
}
如果将 maintainAspectRatio 设置为 true,将自动计算高度。
实际上,在 Chart.js 3.2.1 中更新纵横比,为您的方案添加更多高度,您可以使用 aspectRatio
选项:
options: {
aspectRatio: 1,
}
因此根据文档:
Canvas aspect ratio (i.e. width / height, a value of 1 representing a square canvas). Note that this option is ignored if the height is explicitly defined either as attribute or via the style.
你可以这样设置:
1
为正方形0.5
是宽度的两倍2
宽度是高度的两倍
const config = {
type: 'bar',
data,
options: {
responsive: true,
maintainAspectRatio: false
}
};
var yourChart= new Chart(
document.getElementById('chartCanvas'),
config
);
});
yourChart.canvas.parentNode.style.height = '480px';
yourChart.canvas.parentNode.style.width = '280px';
使用maintainAspectRatio
和responsive
options: {
responsive: true,
maintainAspectRatio: false,
}
let chart = new Chart('myChart', {
type: 'bar',
data: {
labels: ['Red', 'Blue', 'Yellow', 'Green', 'Purple', 'Orange'],
datasets: [{
label: '# of Votes',
data: [12, 19, 3, 5, 2, 3],
backgroundColor: [
'rgba(255, 99, 132, 0.2)',
'rgba(54, 162, 235, 0.2)',
'rgba(255, 206, 86, 0.2)',
'rgba(75, 192, 192, 0.2)',
'rgba(153, 102, 255, 0.2)',
'rgba(255, 159, 64, 0.2)'
],
borderColor: [
'rgba(255, 99, 132, 1)',
'rgba(54, 162, 235, 1)',
'rgba(255, 206, 86, 1)',
'rgba(75, 192, 192, 1)',
'rgba(153, 102, 255, 1)',
'rgba(255, 159, 64, 1)'
],
borderWidth: 1
}]
},
options: {
responsive: true,
maintainAspectRatio: false,
scales: {
y: {
beginAtZero: true
}
}
}
});
<script src="https://cdn.jsdelivr.net/npm/chart.js@3.6.2/dist/chart.min.js"></script>
<canvas id="myChart" width="400" height="400"></canvas>