Chartjs-New datases do not shows Chart js

1πŸ‘

βœ…

Just destroy and redraw the chart. While Chart.js does support adding and removing line points – it is by label (and not by dataset – which is what you want)

Your problem was that you were emptying out the actual dataset objects – if you link it to a copy of the dataset everything works.

Here is your updated change handler

$('input:checkbox').change(function () {
    var chartId = $(this).attr('data-chart-id');
    var datasetId = $(this).attr('data-data-set');

    dataSets[datasetId].hide = !$(this).prop('checked')

    lineChartData.datasets = []
    dataSets.forEach(function (dataset) {
        // create a copy of the dataset to modify (if required) and put into the chart dataset array
        var copy = jQuery.extend(true, {}, dataset)
        if (dataset.hide)
            copy.data = []
        lineChartData.datasets.push(copy)
    })

    // create the graph from scratch
    myNewChart.destroy()
    myNewChart = new Chart(ctx).Line(lineChartData, chartOpt);
});

Fiddle – http://jsfiddle.net/6qdm7nwu/

Leave a comment