据我了解,Chart js 折线图中不显示空值,并且线条在这些点处被中断。
这是我想要的功能,它与 y 轴类型“线性”配合得很好,但是一旦我将类型更改为“对数”,所有空值仍然显示在 0 的位置当鼠标悬停在图表上时,工具提示会显示 NaN。
下面是同一张图表的两张图片,一张是线性的,另一张是对数类型的:
是否可以像第一张图片中所示隐藏 NaN 数据点?
我的代码是一个非常基本的图表,所以我不明白这怎么可能是我的代码中的错误,因为破坏它所需的唯一区别是类型的更改。
我搜索了遇到此问题的其他人或 Chart js 文档中的某些条目,但没有运气。
如果有人已经为类似的问题创建了解决方案,或者可以想到解决这种奇怪效果的方法,我将非常感谢您的任何回应!
编辑
这是我的 html 和 javascript 文件的示例,用于创建新图表,基于用户 @DJ 的回答的基本示例:
HTML:
<div class="content-container">
<canvas id="newChart" class="chart"></canvas>
</div>
JavaScript:
angular.module('myApp').controller('MobileAnalysisController', function () {
init();
return;
function init() {
drawNewChart();
}
function drawNewChart() {
var myNewChart = {
type: "line",
data: {
labels: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
datasets: [
{
data: [null, null, 106, 106, 107, 111, 133, 221, 783, 2478],
}
]
},
options: {
scales: {
yAxes: [{
type: 'logarithmic',
}]
}
}
};
var newCtx = document.getElementById("newChart").getContext("2d");
var newChart = new Chart(newCtx, myNewChart);
newChart.update();
}
});
但即使这样仍然给我一个结果,其中 null/NaN 值应该是不可见的,但事实并非如此:
最佳答案
我制作了一个简单的折线图来测试空值。但在我的测试中,这些值没有显示在图表中。所以我不确定你正在使用什么设置。你能展示一些代码吗?
这是我的测试用例:
import "./styles.css";
import Chart from "chart.js";
new Chart(document.getElementById("line-chart"), {
type: "line",
data: {
labels: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
datasets: [
{
data: [NaN, NaN, 106, 106, 107, 111, 133, 221, 783, 2478],
}
]
},
options: {
scales: {
yAxes: [{
type: 'logarithmic',
}]
}
}
});
关于javascript - Chart js 对数折线图显示 NaN 值而不是 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60205923/