Plotly.deleteTraces using trace's attribute name

Viewed 84

I am trying to use Plotly.deleteTraces by attribute of JSON array. In this case I want to delete traces with attribute meta = "center"

data :

[  
 1: {line: {…}, marker: {…}, meta: "center", mode: "lines+markers", name: "TOP", …}
    2: {line: {…}, marker: {…}, meta: "center", mode: "lines+markers", name: "TOP", …}
    3: {line: {…}, marker: {…}, meta: "center", mode: "lines+markers", name: "TOP", …}
    4: {line: {…}, marker: {…}, meta: "center", mode: "lines+markers", name: "TOP", …}]

I tried below but its not working. Please help

function remove_trace(name) {
    let indices = []
    
    Plotly.deleteTraces(plot_main.data.meta = 'center', indices)
}
1 Answers

This could could be the easiest solution to your answer.

name = 'center'

    function remove_trace(name) {
        let indices = []
        let traces_to_delete = plot_main.data

        $.each(traces_to_delete, function (idx, trace) {
            if (traces_to_delete[idx]['meta'] == name) {

                indices.push(idx)

            }

        })

        Plotly.deleteTraces(plot_main, indices)
    }
Related