图表JS对数折线图,显示NaN值而不是null

时间:2020-02-13 10:43:08

标签: javascript chart.js

据我了解,Chart js折线图中未显示空值,并且该行在这些点处中断。

这是我想要的功能,并且可以与y轴类型“线性”很好地配合使用,但是一旦我将类型更改为“对数”,所有空值仍会显示在0位置当鼠标悬停在这些点上时,工具提示上会显示NaN。

以下是同一张图表的两张图片,一张带有线性图,另一张带有对数类型:

enter image description here ^线性 enter image description here ^对数

是否可以像第一个图片一样隐藏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值应该是不可见的,但不是:

enter image description here

1 个答案:

答案 0 :(得分:2)

我制作了一个简单的折线图来测试空值。但是在我的测试中,这些值未显示在图中。因此,我不确定您使用的是什么设置。您可以显示一些代码吗?

这是我的测试用例:

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',
      }]
    }
  }
});