试试这个。
var width = 400, height = 200; var x = d3.scale.linear().range([0, width]), y = d3.scale.linear().range([height, 0]); var svg = d3.select("body") .append("svg") .attr("width", width) .attr("height", height);var dataSource = 'data.tsv',dataSource2 = 'data2.tsv';function updateChart(sourcefile) { d3.tsv(sourcefile, function(error, data) { if (error) console.warn(error); x.domain(d3.extent(data, function(q) {return q.xCoord;})); y.domain(d3.extent(data, function(q) {return q.yCoord;})); svg.selectAll(".dot") .data(data) .enter().append("circle") .attr("r", 10) .attr("cx", function(d) { return x(d.xCoord); }) .attr("cy", function(d) { return y(d.yCoord); }) }); }updateChart(dataSource);//here is where you change the data..d3.select(#button).on("click", function() {updateChart(dataSource2)})


