Avoid data resetting when toggling series

62 views Asked by At

In this fiddle when you toggle a serie the others reset to their inital values, instead of adding new values to themselves. How to avoid this behaviour?

1

There are 1 answers

1
potatopeelings On BEST ANSWER

You need to store the current state of the data for each series before blanking or resetting the corresponding dataset in enableCheck. Then use this saved state.

Example for the SP dataset

Initialization of the state (spData). I also initialize the dataset (SP)

var spData = [1, 2, 3, 4, 5, 6, 7, 8]
var SP = {}

Saving the state (in enableCheck()) - I get the values from the actual data points

if (SP.data) currentChart.datasets.forEach(function (d) {
    if (d.label === 'My First dataset') {
        spData = d.points.map(function (e) {
            return e.value
        });
    }
})

Using this saved state (in enableCheck())

SP = {
    label: "My First dataset",
    fillColor: "rgba(0,0,0,0)",
    strokeColor: "rgba(220,0,0,1)",
    pointColor: "rgba(220,0,0,1)",
    pointStrokeColor: "#fff",
    pointHighlightFill: "#fff",
    pointHighlightStroke: "rgba(220,0,0,1)",
    data: spData
};

And if you want the new values to be inserted for the hidden series as well, update your data insertion statements in your setTimeout

var d = []
if (First) d.push(randomScalingFactor())
else {
    spData.shift()
    spData.push(randomScalingFactor())
}
if (Second) d.push(randomScalingFactor())
else {
    ncData.shift()
    ncData.push(randomScalingFactor())
}
if (Third) d.push(randomScalingFactor())
else {
    spaData.shift()
    spaData.push(randomScalingFactor())
}

Working fiddle - http://jsfiddle.net/Lsg37amf/