对不起,我的英语不好。
将 chart.js 从 2.8.0 更新到 3.4.1 后,我在图表区域看到了奇怪的“填充”(左右)(请看截图)。
在 Samples 的官方网站上,您可以看到没有任何填充的折线图。但我在条形图示例中看到了它(可能是条形图的某些设置可以生效?)。
这是图表配置、图表实例化、插件和带有样式的模板的代码:
// chart options
chartOptions = {
type: "line",
data: {
datasets: [],
},
options: {
elements: {
point: { radius: 0 },
line: { tension: 0, borderWidth: 1 }
},
animation: false,
resizeDelay: 100,
responsive: true,
maintainAspectRatio: true,
legend: { display: false },
tooltips: {
mode: 'nearest',
intersect: false,
callbacks: {label: (item, data) => this.onLabel(item, data)}
},
hover: {
mode: 'nearest',
intersect: false,
},
scales: {
x: {
offset: true,
stacked: true,
type: 'time',
time: {
tooltipFormat: 'DD MMMM YYYY HH:mm:ss',
displayFormats: {
millisecond: 'HH:mm:ss.SSS',
second: 'HH:mm:ss',
minute: 'HH:mm',
hour: 'HH:mm',
day: 'DD MMM',
},
},
ticks: {
major: {
enabled: true,
fontStyle: 'bold',
fontColor: 'rgb(54, 143, 3)'
},
sampleSize: 10,
maxRotation: 30,
minRotation: 30,
min: undefined,
max: undefined,
},
afterFit: (scale) => {
scale.height = 40;
}
},
A: {
id: 'A',
type: 'linear',
position: 'left',
display: 'auto',
ticks: {
backdropPadding: 0,
},
},
B: {
id: 'B',
type: 'linear',
position: 'right',
display: 'auto',
ticks: {
max: 2,
min: -1,
stepSize: 1,
suggestedMin: 0,
suggestedMax: 1,
},
},
},
plugins: {
zoom: {
pan: {
enabled: true,
mode: 'xy',
overScaleMode: 'y',
rangeMax: {x: new Date()},
onPanComplete: chart => this.onZoom(chart, false)
},
zoom: {
wheel: {
enabled: true,
},
mode: 'xy',
overScaleMode: 'y',
onZoomComplete: chart => this.onZoom(chart, true)
}
}
},
},
} as Chart.ChartConfiguration<'line'>;
// call of Chart#register()
import zoomPlugin from 'chartjs-plugin-zoom';
Chart.Chart.register(
Chart.TimeScale, Chart.LinearScale, Chart.LineController,
Chart.PointElement, Chart.LineElement, zoomPlugin,
);
// chart instantiating
this.chart = new Chart.Chart<'line'>(this.chartContainer.nativeElement, this.chartOptions);
模板
<div class="chart-wrapper">
<canvas #chart_container id="chart"></canvas>
</div>
风格
.chart-wrapper {
width: 99%;
height: calc(100% - 7px);
margin: 0 auto;
overflow: hidden;
}
我尝试了什么:
- 检查数据和数据集;
- 禁用 chartjs-zoom-plugin;
- 设置
options.layout.padding
为 0; - 设置
options.responsive
为false
; - 设置
data.datasets.borderWidth
为 1;
我怎样才能删除这个“填充”?