Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

Trouble removing chart dataset using ChartJS

I can't figure this out for the life of me. I have a chart using ChartJS and I'm wanting to turn different datasets on and off. The first step for this is to just remove one set of chart data when clicking on a button. It seems logical to me to select the second dataset object in the datasets array then remove it, but I can't get it to work. My code is below.

var line = document.getElementById("lineChart");

var myLineChart = new Chart(line, {
    type: 'line',
    data: {
        labels: ["16-22", "23-29", "30-5", "6-12", "13-19", "20-26", "27-3", "4-10", "11-17", "18-24", "25-31"],
        datasets: [
            {
                label: "My First dataset",
                fill: true,
                lineTension: 0.1,
                backgroundColor: "rgba(116,119,191,0.2)",
                borderColor: "rgba(116,119,191,1)",
                borderCapStyle: 'butt',
                borderDash: [],
                borderDashOffset: 0.0,
                borderJoinStyle: 'miter',
                pointBorderColor: "rgba(116,119,191,1)",
                pointBackgroundColor: "#fff",
                pointBorderWidth: 1,
                pointHoverRadius: 5,
                pointHoverBackgroundColor: "rgba(75,192,192,1)",
                pointHoverBorderColor: "rgba(220,220,220,1)",
                pointHoverBorderWidth: 2,
                pointRadius: 6,
                pointHitRadius: 10,
                data: [750, 1250, 1000, 1500, 2000, 1500, 1750, 1250, 1750, 2250, 1750, 2250],
                spanGaps: false,         
            },
            {
                label: "My Second dataset",
                fill: true,
                lineTension: 0.1,
                backgroundColor: "rgba(116,119,191,0.2)",
                borderColor: "rgba(116,119,191,1)",
                borderCapStyle: 'butt',
                borderDash: [],
                borderDashOffset: 0.0,
                borderJoinStyle: 'miter',
                pointBorderColor: "rgba(116,119,191,1)",
                pointBackgroundColor: "#fff",
                pointBorderWidth: 1,
                pointHoverRadius: 5,
                pointHoverBackgroundColor: "rgba(75,192,192,1)",
                pointHoverBorderColor: "rgba(220,220,220,1)",
                pointHoverBorderWidth: 2,
                pointRadius: 6,
                pointHitRadius: 10,
                data: [1750, 250, 7000, 1100, 1000, 1300, 750, 1550, 1350, 2650, 750, 2550],
                spanGaps: false,
            },   
        ],
  },
    options: {
         responsive: true,
         maintainAspectRatio: false, 
        }
});

var secondChart = myLineChart.data.datasets[1];


//Click button to remove second dataset
$('#hourlyBtn').on("click", function(){
    $(secondChart).remove();
});

1 Answer

sorry but I have really used chartJs before, but Here is the documentation, maybe that will help:

ChartJs