Chartjs : showLine depending on value

0👍

Here is an example of a Chart with two datasets. The first one is containing all points >10 and null for values smaller than 10. To span the line over the gaps note the spanGaps: true option. The second one is exactly the opposite. Here we hide the line by adding showLine: false.

var ctx = document.getElementById ('myChart').getContext ('2d');
var chart = new Chart (ctx, {
    
    type: 'line',

    data: {
        labels: ["January", "February", "March", "April", "May", "June", "July"],
        datasets: [
            {
                label: "My First dataset",
                backgroundColor: 'rgba(255, 99, 132, 0.5)',
                borderColor: 'rgba(255, 99, 132, 0.5)',
                spanGaps: true,
                data: [12, null, 15, 20, null, 30, null],
            },
            {
                label: "My second dataset",
                backgroundColor: 'rgba(135, 99, 225, 1)',
                borderColor: 'rgba(135, 99, 225, 1)',
                showLine: false,
                data: [null, 9, null, null, 8, null, 7],
            }
        ]
    },

    options: {}
    
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.7.2/Chart.js"></script>

<canvas id="myChart"></canvas>

Leave a comment