pkhiev
pkhiev

Reputation: 67

Javscript Plotly error: 'calling plotly as if redrawing but this container doesn't have a plot yet"

I am trying to plot a different pie chart based on user selection on a dropdown list. The console.log is outputting the correct info, but the plotting area throws an error: "Calling Plotly.plot as if redrawing but this container doesn't yet have a plot." Below is my code (sorry it's a bit long), but I'll try to summarize the issue I face.

function nameChanged(){
    var selected_month = d3.select('#selMonth').property('value');
    var selected_researcher = d3.select('#selName').property('value');     
    var proj_name = []
    var proj_hrs = []
    d3.json('mar_byweek.json').then((data)=>{ 

    if ((selected_month ==="march")&& (selected_researcher ==='amber')
        //some other code here to do something, which works //
        d3.json('marAmber_byprod.json').then((data) =>{
            proj_name = data.map(row => row.Product);
            proj_hrs = data.map(row =>row.Total);
            console.log(proj_name, proj_hrs)
        })};

    if ((selected_month ==="march")&& (selected_researcher ==='gwen')) {
        //some other code here to do something, which works//

        d3.json('marGwen_byprod.json').then((data) =>{
            proj_name = data.map(row => row.Product);
            proj_hrs = data.map(row =>row.Total);
            console.log(proj_name, proj_hrs)
        })};

        var trace_project = {
            labels: [proj_name],
            values: [proj_hrs],
            type: "pie",
        }
        Plotly.newPlot('month_pie', trace_project)
    }) 

For EACH "if" condition, the console.log is showing the correct "proj_name" and "proj_hrs" arrays. But in plotting, the error shows "Calling Plotly.plot as if redrawing but this container doesn't yet have a plot."

Please help! Deeply appreciated!!!

Upvotes: 3

Views: 3447

Answers (2)

user1738687
user1738687

Reputation: 477

Put trace_project inside an array: [trace_project]

Upvotes: 5

pkhiev
pkhiev

Reputation: 67

I solved it by putting the Plotly.newPlot inside each if condition. I'm hoping that there is a simpler way instead of repeating it three times, but that's what did it for me. If someone else has a better way of doing it, I would appreciate it.

Upvotes: 0

Related Questions