javascript - 在 chartjs 的顶部和底部边缘处减半的点
问题描述
如图所示,点在到达底部或顶部边缘时被切成两半(在此示例中,当数据为 1 或 5 时)。
我尝试了填充,添加了一些“假”数据以扩展 1 和 5 的限制,并使用刻度上的回调函数将其删除。没有按预期工作
这是我对此图表的配置。
const config = {
type: 'line' as ChartType,
data: data,
options: {
pointStyle: 'circle',
//pointBackgroundColor: 'white',
scales: {
y: {
ticks: {
maxTicksLimit: 5,
stepSize: 1,
},
min: 1,
max: 5,
reverse: true,
},
},
},
};
但我需要最小值和最大值,因为我想要固定的 Y 轴值
任何线索如何解决这个问题?
解决方案
您可以使用afterDataLimits
钩子设置比例的最大值和最小值,这样它仍然会溢出图表区域:
const ctx = document.getElementById('chart').getContext('2d');
const myChart = new Chart(ctx, {
type: "line",
data: {
labels: ["Red", "Blue", "Yellow", "Green", "Purple", "Orange"],
datasets: [{
data: [12, 19, 3, 10, 2, 3],
borderColor: 'pink',
backgroundColor: 'pink',
radius: 10
}]
},
options: {
scales: {
y: {
afterDataLimits: (scale) => {
scale.max = 10;
scale.min = 0;
}
},
},
},
});
<canvas id="chart" width="250" height="120" />
<script src="https://cdn.jsdelivr.net/npm/chart.js@3.6.0/dist/chart.min.js"></script>
推荐阅读
- javascript - 在 Ionic 6 Vue 4.5.2 中更改单页(即组件)的背景:
- r - 如何修复 R 中的合并 if (quote) args 问题?
- c# - Microsoft.AspNetCore.Antiforgery.DefaultAntiforgery[7] 错误
- vba - 用于在 LibreCalc 中创建图表的宏 VBA,如何将标题、图例和图表主体定位到图表区域
- android - Google Play 扩展 APK
- 3d - 围绕另一个 3D 点旋转 3D 点
- c# - 基于 ASP.Net Core 弹出式身份验证?
- amazon-web-services - 在哪里保存 html 页面所需的 AWS 开发工具包配置凭证
- julia - 如何创建测试特定的 Julia 项目依赖项?
- django-rest-framework - Django Rest Framework - 如何验证来宾用户?