2014-07-11 13 views
6

我有一個折線圖,每次頁面刷新都會改變數據,這很好,但我需要通過用戶單擊進行刷新。這是因爲頁面上最終會有其他輸入字段,刷新頁面會破壞當前會話。NVD3 - 如何刷新數據功能以產生新的點擊數據

的jsfiddle - http://jsfiddle.net/darcyvoutt/dXtv2/

下面是代碼的設置來創建行:

function economyData() { 

    // Rounds 
    var numRounds = 10; 

    // Stability of economy 
    var stable = 0.2; 
    var unstable = 0.6; 
    var stability = unstable; 

    // Type of economy 
    var boom = 0.02; 
    var flat = 0; 
    var poor = -0.02; 
    var economyTrend = boom; 

    // Range  
    var start = 1; 
    var max = start + stability; 
    var min = start - stability; 

    // Arrays 
    var baseLine = []; 
    var economy = []; 

    // Loop 
    for (var i = 0; i < numRounds + 1; i++) {  

     baseLine.push({x: i, y: 1}); 

     if (i == 0) { 

     economyValue = 1; 

     } else { 

     var curve = Math.min(Math.max(start + ((Math.random() - 0.5) * stability), min), max);   

     economyValue = Math.round(((1 + (economyTrend * i)) * curve) * 100)/100; 

     } 

     economy.push({x: i, y: economyValue}); 

    } 

    return [ 
     { 
     key: 'Base Line', 
     values: baseLine 
     }, 
     { 
     key: 'Economy', 
     values: economy 
     } 
    ]; 
    } 

這裏是我試着寫,但更新失敗:

function update() { 
     sel = svg.selectAll(".nv-line") 
     .datum(data); 

     sel 
     .exit() 
     .remove(); 

     sel 
     .enter() 
     .append('path') 
      .attr('class','.nv-line'); 

     sel 
     .transition().duration(1000); 

    }; 

    d3.select("#update").on("click", data); 
+0

在你的小提琴中,它給svg未定義在線 sel = svg.selectAll(「。nv-line」) 但svg沒有任何聲明或定義。 看看這個。 –

回答

15

這裏是什麼我對你的代碼做了不同的處理。

// Maintian an instance of the chart 
var chart; 

// Maintain an Instance of the SVG selection with its data 
var chartData; 

nv.addGraph(function() { 
    chart = nv.models.lineChart().margin({ 
     top : 5, 
     right : 10, 
     bottom : 38, 
     left : 10 
    }).color(["lightgrey", "rgba(242,94,34,0.58)"]) 
     .useInteractiveGuideline(false) 
     .transitionDuration(350) 
     .showLegend(true).showYAxis(false) 
     .showXAxis(true).forceY([0.4, 1.6]); 

    chart.xAxis.tickFormat(d3.format('d')).axisLabel("Rounds"); 
    chart.yAxis.tickFormat(d3.format('0.1f')); 

    var data = economyData(); 

    // Assign the SVG selction 
    chartData = d3.select('#economyChart svg').datum(data); 
    chartData.transition().duration(500).call(chart); 

    nv.utils.windowResize(chart.update); 

    return chart; 
}); 

這裏是update()函數的樣子:

function update() { 
    var data = economyData(); 

    // Update the SVG with the new data and call chart 
    chartData.datum(data).transition().duration(500).call(chart); 
    nv.utils.windowResize(chart.update); 
}; 

// Update the CHART 
d3.select("#update").on("click", update); 

這裏是你的代碼的working version的鏈接。

希望它有幫助。

+0

謝謝!點。甚至使用我寫的一些新代碼。 – dvoutt

+2

注意:我無法獲得該示例的工作。克隆和本地玩,我得到一個關於transitionDuration的錯誤(看起來[語法已經改變](https://github.com/nvd3-community/nvd3/blob/gh-pages/examples/lineChart.html#L50 -L53))。即使刪除了,我也沒有得到任何提琴(儘管在本地工作)。 – bwarren2

+2

@ bwarren2我已經更新了shabeer90的代碼,使其再次工作。查看它在:http://jsfiddle.net/eu26dLcx/ – ajaybc