填充在我的 chartjs 线图中不起作用
padding not working in my chartjs line-graph
我使用的是最新版本的 chart.js (3.2.1)。如果我将其设置为正值,则填充有效。
但是填充 0(或负值)并没有将折线图精确地设置在 canvas
的边界处
options: {
layout: {
padding:0
},
interaction: {
intersect: false,
},
plugins: {
tooltip: {
backgroundColor: 'transparent',
displayColors: false,
bodyFontSize: 14,
titleColor: 'rgba(83,255,228,1)',
callbacks: {
label: function (tooltipItems, data) {
return 'BMI: ' + tooltipItems.raw;
}
}
},
legend: {
display: false,
},
},
elements: {
point: {
radius: 6,
hitRadius: 6,
hoverRadius: 6
}
},
scales: {
xAxes: {
display: false,
},
yAxes: {
display: false,
},
}
}
图表配置中的填充不是用于将 canvas 样式化到其他元素,填充唯一做的就是在 [= 之间添加一点 space 的能力24=] 边缘和线,从您提供的图像看来,它似乎到达了 canvas 的末尾,空的 space 是 canvas.[=14 下面的另一个元素=]
如您在此示例中所见,canvas 背景颜色为灰色,并且 padding: 0
线按预期触及 canvas 的末尾
var options = {
type: 'line',
data: {
labels: ["Red", "Blue", "Yellow", "Green", "Purple", "Orange"],
datasets: [{
label: '# of Votes',
data: [12, 19, 3, 5, 2, 3],
borderWidth: 1
},
{
label: '# of Points',
data: [7, 11, 5, 8, 3, 7],
borderWidth: 1
}
]
},
options: {
plugins: {
tooltip: {
backgroundColor: 'transparent',
displayColors: false,
bodyFontSize: 14,
titleColor: 'rgba(83,255,228,1)',
callbacks: {
label: function(tooltipItems, data) {
return 'BMI: ' + tooltipItems.raw;
}
}
},
legend: {
display: false,
},
},
interaction: {
intersect: false,
},
layout: {
padding: 0
},
elements: {
point: {
radius: 0
}
},
scales: {
y: {
display: false
},
x: {
display: false
}
}
}
}
var ctx = document.getElementById('chartJSContainer').getContext('2d');
new Chart(ctx, options);
canvas {
background-color: #eee;
}
<body>
<canvas id="chartJSContainer" width="600" height="400"></canvas>
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/3.2.0/chart.js" integrity="sha512-opXrgVcTHsEVdBUZqTPlW9S8+99hNbaHmXtAdXXc61OUU6gOII5ku/PzZFqexHXc3hnK8IrJKHo+T7O4GRIJcw==" crossorigin="anonymous"></script>
</body>
我使用的是最新版本的 chart.js (3.2.1)。如果我将其设置为正值,则填充有效。 但是填充 0(或负值)并没有将折线图精确地设置在 canvas
的边界处options: {
layout: {
padding:0
},
interaction: {
intersect: false,
},
plugins: {
tooltip: {
backgroundColor: 'transparent',
displayColors: false,
bodyFontSize: 14,
titleColor: 'rgba(83,255,228,1)',
callbacks: {
label: function (tooltipItems, data) {
return 'BMI: ' + tooltipItems.raw;
}
}
},
legend: {
display: false,
},
},
elements: {
point: {
radius: 6,
hitRadius: 6,
hoverRadius: 6
}
},
scales: {
xAxes: {
display: false,
},
yAxes: {
display: false,
},
}
}
图表配置中的填充不是用于将 canvas 样式化到其他元素,填充唯一做的就是在 [= 之间添加一点 space 的能力24=] 边缘和线,从您提供的图像看来,它似乎到达了 canvas 的末尾,空的 space 是 canvas.[=14 下面的另一个元素=]
如您在此示例中所见,canvas 背景颜色为灰色,并且 padding: 0
线按预期触及 canvas 的末尾
var options = {
type: 'line',
data: {
labels: ["Red", "Blue", "Yellow", "Green", "Purple", "Orange"],
datasets: [{
label: '# of Votes',
data: [12, 19, 3, 5, 2, 3],
borderWidth: 1
},
{
label: '# of Points',
data: [7, 11, 5, 8, 3, 7],
borderWidth: 1
}
]
},
options: {
plugins: {
tooltip: {
backgroundColor: 'transparent',
displayColors: false,
bodyFontSize: 14,
titleColor: 'rgba(83,255,228,1)',
callbacks: {
label: function(tooltipItems, data) {
return 'BMI: ' + tooltipItems.raw;
}
}
},
legend: {
display: false,
},
},
interaction: {
intersect: false,
},
layout: {
padding: 0
},
elements: {
point: {
radius: 0
}
},
scales: {
y: {
display: false
},
x: {
display: false
}
}
}
}
var ctx = document.getElementById('chartJSContainer').getContext('2d');
new Chart(ctx, options);
canvas {
background-color: #eee;
}
<body>
<canvas id="chartJSContainer" width="600" height="400"></canvas>
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/3.2.0/chart.js" integrity="sha512-opXrgVcTHsEVdBUZqTPlW9S8+99hNbaHmXtAdXXc61OUU6gOII5ku/PzZFqexHXc3hnK8IrJKHo+T7O4GRIJcw==" crossorigin="anonymous"></script>
</body>