Javascript 如何实时更新D3图形?

Javascript 如何实时更新D3图形?,javascript,d3.js,Javascript,D3.js,我有一个显示两个不同数据集的折线+面积图。我不想添加新数据,而是希望经常更改图表值。我该怎么做 当我得到新的数据时,我会相应地更新比例和轴,并尝试更新线,但我最终得到的不是很多旧点,就是没有 这是我的尝试: 这个例子可能会有所帮助,但它似乎对“点”不起作用。:/Check out以完全理解数据联接在d3中的工作方式。您不应该调用selection.data().enter()来更新现有的数据绑定。相反,只需将selection.data()返回的更新选择用于updatesStill。如果我只是

我有一个显示两个不同数据集的折线+面积图。我不想添加新数据,而是希望经常更改图表值。我该怎么做

当我得到新的数据时,我会相应地更新比例和轴,并尝试更新线,但我最终得到的不是很多旧点,就是没有

这是我的尝试:


这个例子可能会有所帮助,但它似乎对“点”不起作用。:/Check out以完全理解数据联接在d3中的工作方式。您不应该调用selection.data().enter()来更新现有的数据绑定。相反,只需将selection.data()返回的更新选择用于updatesStill。如果我只是这样更新,我就不能创建新元素。
var data =  [
    [{'x':0,'y':10},{'x':1,'y':12},{'x':2,'y':17},{'x':3,'y':9},{'x':4,'y':10},{'x':5,'y':13},{'x':6,'y':13},{'x':7,'y':15},{'x':8,'y':16},{'x':9,'y':16}, {'x':10,'y':17}, {'x':11,'y':17}, {'x':12,'y':0}], 
  [{'x':0,'y':1},{'x':1,'y':1},{'x':2,'y':2},{'x':3,'y':3},{'x':4,'y':2},{'x':5,'y':8},{'x':6,'y':6},{'x':7,'y':4},{'x':8,'y':6},{'x':9,'y':7}, {'x':10,'y':9}, {'x':11,'y':5}, {'x':12,'y':3}]
];

var colors = [
    '#ec4863', 'blue'
]


//************************************************************
// Create Margins and Axis
//************************************************************
var margin = {top: 20, right: 30, bottom: 30, left: 50},
    width = 700 - margin.left - margin.right,
    height = 300 - margin.top - margin.bottom;

var x = d3.scale.linear()
    .domain([1, 8])
    .range([0, width]);

var y = d3.scale.linear()
    .domain([0, 20])
    .range([height, 0]);

var xAxis = d3.svg.axis()
    .scale(x)
    .tickSize(0)
    .tickPadding(15)    
    .tickSubdivide(true)    
    .orient("bottom")
    .tickValues(d3.range(1, 8, 1)); 

var yAxis = d3.svg.axis()
    .scale(y)
    .tickPadding(15)
    .tickSize(-width)
    .tickSubdivide(true)    
    .orient("left")
.tickValues(d3.range(0, 21, 5));

var area = d3.svg.area()
    .x(function(d) { return x(d.date); })
    .y0(height)
    .y1(function(d) { return y(d.close); });    





//************************************************************
// Generate our SVG object
//************************************************************  
var svg = d3.select("body").append("svg")
    .attr("width", width + margin.left + margin.right)
    .attr("height", height + margin.top + margin.bottom)
    .append("g")
    .attr("transform", "translate(" + margin.left + "," + margin.top + ")");

svg.append("g")
    .attr("class", "x axis")
    .attr("transform", "translate(0," + height + ")")
    .call(xAxis);

svg.append("g")
    .attr("class", "y axis")
    .call(yAxis);

svg.append("g")
    .attr("class", "y axis")
    .append("text")
    .attr("class", "axis-label")
    .attr("transform", "rotate(-90)")
    .attr("y", (-margin.left) + 10)
    .attr("x", -height/2);

svg.append("clipPath")
    .attr("id", "clip")
    .append("rect")
    .attr("width", width + 10)
    .attr("height", height);





//************************************************************
// Create D3 line object and draw data on our SVG object
//************************************************************
var line = d3.svg.area()
    .interpolate("monotone")    
    .x(function(d) { return x(d.x); })
    .y0(height)
    .y1(function(d) { return y(d.y); });        

svg.selectAll('.line')
    .data(data)
    .enter()
    .append("path")
    .attr("clip-path", "url(#clip)")
    .attr('stroke', function(d,i){          
  console.log(i);
        return colors[i%colors.length];
    })
.attr('fill', function(d,i){            
        return colors[i % colors.length];
    })
    .attr("d", line);       


//************************************************************
// Draw points on SVG object based on the data given
//************************************************************
var points = svg.selectAll('.dots')
    .data(data)
    .enter()
    .append("g")
    .attr("class", "dots")
    .attr("clip-path", "url(#clip)");   

points.selectAll('.dot')
    .data(function(d, index){       
        var a = [];
        d.forEach(function(point,i){
            a.push({'index': index, 'point': point});
        });     
        return a;
    })
    .enter()
    .append('circle')
    .attr('class','dot')
    .attr("r", 4)
    .attr('fill', function(d,i){    
        return colors[d.index%colors.length];
    })  
    .attr("transform", function(d) { 
        return "translate(" + x(d.point.x) + "," + y(d.point.y) + ")"; }
    );


function update() {

  var newValue = Math.random() * 20;

  var arrSize = data[0].length;

  data[0][arrSize-1].y = newValue;

  console.log(data);

  svg.selectAll('.dots').data(data).enter();
}




setInterval(function() {
  update();
}, 1500);