2015-08-20 10 views
12

Okay, so habe ich den folgenden Code:Plotly Aktualisierungsdaten

var element = document.getElementById(scope.changeid); 

function getData(division,redraw) { 
    var employeeData = []; 
    if (!division) { 
     $http.get(api.getUrl('competenceUserAverageByMyDivisions', null)).success(function (response) { 
      processData(response,redraw); 
     }); 
    } 
    else { 
     $http.get(api.getUrl('competenceUserAverageByDivision', division)).success(function (response) { 
      processData(response,redraw); 
     }) 
    } 

} 

function processData(data,redraw) { 
    var y = [], 
     x1 = [], 
     x2 = []; 

    data.forEach(function (item) { 
     y.push(item.user.profile.firstname); 
     x1.push(item.current_level); 
     x2.push(item.expected); 
    }); 

    var charData = [{ 
      x: x1, 
      y: y, 
      type: 'bar', 
      orientation: 'h', 

      name: 'Nuværende' 
     }, { 
      x: x2, 
      y: y, 
      type: 'bar', 
      orientation: 'h', 

      name: 'Forventet' 
     }], 
     layout = { 
      barmode: 'stack', 
      legend: { 
       traceorder: 'reversed', 
       orientation: 'h' 

      } 
     }; 

    if(!redraw){ 
     Plotly.plot(element, charData, layout); 
    } 
    else 
    { 
     Plotly.redraw(element,charData,layout); 
    } 
} 

scope.$watch('divisionId', function (newValue, oldValue) { 
    if (newValue) { 
     getData(newValue.id,true); 
    } 
}, true); 

getData(null,false); 

Welche der folgenden Tabelle erstellt:

enter image description here

Nun, wie Sie sehen können, ive eine watcher

  scope.$watch('divisionId', function (newValue, oldValue) { 
      if (newValue) { 
       getData(newValue.id,true); 
      } 
     }, true); 
hinzugefügt

Jetzt, wenn ich dies auslösen, sollte das Diagramm aktualisieren undaufrufen

Wenn dies jedoch geschieht, ändert sich das Diagramm überhaupt nicht. Es gibt keinen Fehler in der Konsole, also bin ich mir nicht sicher, was ich tun soll?

+1

Sind Sie plotly.js in einer Winkel Richtlinie verwendet? –

Antwort

13

Ich fand die Antwort auf die Frage.

apprently benötigt i zu verwenden:

Plotly.newPlot(element,charData,layout); 

statt redraw von

+0

Das funktioniert, aber ist wahrscheinlich nicht die beste Lösung, es muss einen anderen Weg geben. –

+1

Hmmm ... http://www.mzan.com/article/35946484-most-performant-way-to-update-graph-with-new-data-with-plotly.shtml –

+0

Dies wird den Speicher entleeren .. – giuseppe

12

Plotly.redraw(gd) der richtige Weg ist.
Aber Sie rufen Plotly.redraw falsch.
Der richtige Weg ist das data Objekt zu aktualisieren, anstatt neu ein data Objekt.

var data = [ { 
    x: ['VALUE 1'], // in reality I have more values... 
    y: [20], 
    type: 'bar' 
} 
]; 
Plotly.newPlot('PlotlyTest', data); function adjustValue1(value) { 
    data[0]['y'][0] = value; 
    Plotly.redraw('PlotlyTest'); 
} 

Ref: http://www.mzan.com/article/35946484-most-performant-way-to-update-graph-with-new-data-with-plotly.shtml