首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >NVD3 -如何刷新数据函数以在单击时生成新数据

NVD3 -如何刷新数据函数以在单击时生成新数据
EN

Stack Overflow用户
提问于 2014-07-11 10:14:23
回答 1查看 19.6K关注 0票数 6

我有一个折线图,每次页面刷新时,它都会更改数据,这很棒,但我需要通过用户单击刷新。这是因为页面上最终会有其他输入字段,刷新页面会破坏它们的当前会话。

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

下面是创建行的代码设置:

代码语言:javascript
复制
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  
      }
    ];
  }

以下是我尝试编写但更新失败的内容:

代码语言:javascript
复制
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);  
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-07-11 18:12:13

以下是我对您的代码所做的不同之处。

代码语言:javascript
复制
// 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()函数的外观:

代码语言:javascript
复制
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);

下面是您代码的的链接。

希望能有所帮助。

票数 17
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/24689157

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档