有没有办法只调整 Chart.js 中图表标题的底部填充?
Is there a way to adjust only the bottom padding of a chart's title in Chart.js?
在documentation的基础上,Chart.js中标题的padding
属性调整了上下边距。但是,我只想调整底部填充。
我试过使用填充 object:
title:{
display: true,
text:'Population of cities in California',
padding:{
left:0,
right:0,
bottom:30,
top:0
}
}
但这可能不受支持,因为它把一切都搞砸了,甚至使图表不可见。
我搜索了文档并用谷歌搜索了一下,看看是否已经有人问过这个问题,但我找不到任何解决方案。
是否有 built-in 方法可以在 Chart.js 中执行此操作?如果没有,最简单的解决方法是什么?
title.padding
获取要在标题文本上方和下方添加的像素数。
存在一种仅在图表标题底部添加填充的解决方法。 Plugin Core API offers a range of hooks that may be used for performing custom code. You can use the afterDraw
hook to draw the title yourself directly on the canvas using CanvasRenderingContext2D.fillText()
.
plugins: [{
afterDraw: chart => {
var ctx = chart.chart.ctx;
ctx.save();
ctx.textAlign = 'center';
ctx.font = "18px Arial";
ctx.fillStyle = "gray";
ctx.fillText('My Title', chart.chart.width / 2, 20);
ctx.restore();
}
}],
除此之外,您还必须为图表定义顶部 padding。这决定了您的标题和图表之间的 space(基本上是标题底部填充)。
layout: {
padding: {
top: 80
}
},
请查看下面的代码,展示它的外观。
new Chart(document.getElementById('myChart'), {
type: 'bar',
plugins: [{
afterDraw: chart => {
var ctx = chart.chart.ctx;
ctx.save();
ctx.textAlign = 'center';
ctx.font = "18px Arial";
ctx.fillStyle = "gray";
ctx.fillText('My Title', chart.chart.width / 2, 20);
ctx.restore();
}
}],
data: {
labels: ['A', 'B', 'C', 'D'],
datasets: [{
data: [10, 12, 8, 6],
backgroundColor: ['rgba(255, 99, 132, 0.2)', 'rgba(255, 159, 64, 0.2)', 'rgba(255, 205, 86, 0.2)', 'rgba(75, 192, 192, 0.2)'],
borderColor: ['rgb(255, 99, 132)', 'rgb(255, 159, 64)', 'rgb(255, 205, 86)', 'rgb(75, 192, 192)'],
borderWidth: 1
}]
},
options: {
layout: {
padding: {
top: 80
}
},
legend: {
display: false
},
scales: {
yAxes: [{
ticks: {
beginAtZero: true
}
}]
}
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.9.3/Chart.min.js"></script>
<canvas id="myChart" height="100"></canvas>
在documentation的基础上,Chart.js中标题的padding
属性调整了上下边距。但是,我只想调整底部填充。
我试过使用填充 object:
title:{
display: true,
text:'Population of cities in California',
padding:{
left:0,
right:0,
bottom:30,
top:0
}
}
但这可能不受支持,因为它把一切都搞砸了,甚至使图表不可见。 我搜索了文档并用谷歌搜索了一下,看看是否已经有人问过这个问题,但我找不到任何解决方案。
是否有 built-in 方法可以在 Chart.js 中执行此操作?如果没有,最简单的解决方法是什么?
title.padding
获取要在标题文本上方和下方添加的像素数。
存在一种仅在图表标题底部添加填充的解决方法。 Plugin Core API offers a range of hooks that may be used for performing custom code. You can use the afterDraw
hook to draw the title yourself directly on the canvas using CanvasRenderingContext2D.fillText()
.
plugins: [{
afterDraw: chart => {
var ctx = chart.chart.ctx;
ctx.save();
ctx.textAlign = 'center';
ctx.font = "18px Arial";
ctx.fillStyle = "gray";
ctx.fillText('My Title', chart.chart.width / 2, 20);
ctx.restore();
}
}],
除此之外,您还必须为图表定义顶部 padding。这决定了您的标题和图表之间的 space(基本上是标题底部填充)。
layout: {
padding: {
top: 80
}
},
请查看下面的代码,展示它的外观。
new Chart(document.getElementById('myChart'), {
type: 'bar',
plugins: [{
afterDraw: chart => {
var ctx = chart.chart.ctx;
ctx.save();
ctx.textAlign = 'center';
ctx.font = "18px Arial";
ctx.fillStyle = "gray";
ctx.fillText('My Title', chart.chart.width / 2, 20);
ctx.restore();
}
}],
data: {
labels: ['A', 'B', 'C', 'D'],
datasets: [{
data: [10, 12, 8, 6],
backgroundColor: ['rgba(255, 99, 132, 0.2)', 'rgba(255, 159, 64, 0.2)', 'rgba(255, 205, 86, 0.2)', 'rgba(75, 192, 192, 0.2)'],
borderColor: ['rgb(255, 99, 132)', 'rgb(255, 159, 64)', 'rgb(255, 205, 86)', 'rgb(75, 192, 192)'],
borderWidth: 1
}]
},
options: {
layout: {
padding: {
top: 80
}
},
legend: {
display: false
},
scales: {
yAxes: [{
ticks: {
beginAtZero: true
}
}]
}
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.9.3/Chart.min.js"></script>
<canvas id="myChart" height="100"></canvas>