我正在尝试创建一个多线图d3编辑器。代码是
var margin = {
top: 20,
right: 80,
bottom: 30,
left: 50
},
width = 960 - margin.left - margin.right,
height = 500 - margin.top - margin.bottom;
var parseDate = d3.time.format("%Y%m%d").parse;
var x = d3.time.scale()
.range([0, width]);
var y = d3.scale.linear()
.range([height, 0]);
var color = d3.scale.category10();
var xAxis = d3.svg.axis()
.scale(x)
.orient("bottom");
var yAxis = d3.svg.axis()
.scale(y)
.orient("left");
var line = d3.svg.line()
//.interpolate("basis")
.x(function(d) {
return x(d.date);
})
.y(function(d) {
return y(d.temperature);
})
.interpolate("monotone");
var dragged = null,
selected = null;
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("rect")
.attr("width", width)
.attr("height", height)
.attr("fill", "none");
dataurl = "https://gist.githubusercontent.com/ChrisJamesC/5896521/raw/83d7c5c04f7d031d3c71b4d6fd5b5193366d9fed/data.tsv";
count = 0;
var cities;
d3.tsv(dataurl, function(error, data) {
console.log('count:', count++);
color.domain(d3.keys(data[0]).filter(function(key) {
return key !== "date";
}));
data.forEach(function(d) {
d.date = parseDate(d.date);
});
cities = color.domain().map(function(name) {
return {
name: name,
values: data.map(function(d) {
return {
date: d.date,
temperature: +d[name]
};
})
};
});
x.domain(d3.extent(data, function(d) {
return d.date;
}));
console.log("cities", cities);
y.domain([
d3.min(cities, function(c) {
return d3.min(c.values, function(v) {
return v.temperature;
});
}),
d3.max(cities, function(c) {
return d3.max(c.values, function(v) {
return v.temperature;
});
})
]);
svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + height + ")")
.call(xAxis);
svg.append("g")
.attr("class", "y axis")
.call(yAxis)
.append("text")
.attr("transform", "rotate(-90)")
.attr("y", 6)
.attr("dy", ".71em")
.style("text-anchor", "end")
.text("Temperature (ºF)");
var city = svg.selectAll(".city")
.data(cities)
.enter().append("g")
.attr("class", "city");
city.append("path")
.attr("class", "line")
.attr("d", function(d) {
return line(d.values);
})
.style("stroke", function(d) {
return color(d.name);
})
.call(redraw);
city.node().focus();
console.log(city.node());
function redraw() {
city.select("path").attr("d", function(d) {
return line(d.values);
});
var circle = city.append("g").selectAll("circle")
.data(function(d) {
return d.values
})
.enter();
circle
.append("circle")
.attr("r", 2)
.attr("cx", function(dd) {
return x(dd.date)
})
.attr("cy", function(dd) {
return y(dd.temperature)
})
.attr("fill", "none")
.attr("stroke", function(d) {
return color(this.parentNode.__data__.name)
})
.on("drag", function(d) {
console.log('ondrag', d);
selected = dragged = d;
redraw();
})
.call(drag)
.transition()
.duration(750)
.ease("elastic")
.attr("r", 6.5);
circle
.classed("dragging", function(d) {
console.log('draginng update', d, d === selected);
return d === selected;
})
.attr("cx", function(d) {
return x(d.x);
})
.attr("cy", function(d) {
return y(d.y);
});
circle.exit().remove();
};
city.append("text")
.datum(function(d) {
return {
name: d.name,
value: d.values[d.values.length - 1]
};
})
.attr("transform", function(d) {
return "translate(" + x(d.value.date) + "," + y(d.value.temperature) + ")";
})
.attr("x", 3)
.attr("dy", ".35em")
.text(function(d) {
return d.name;
});
var drag = d3.behavior.drag()
.origin(function(d) {
console.log('origin', d);
return d;
})
.on("dragstart", dragstarted)
.on("drag", dragged)
.on("dragend", dragended);
function dragstarted(d) {
console.log('Drag started');
d3.event.sourceEvent.stopPropagation();
d3.event.sourceEvent.preventDefault();
d3.select(this).classed("dragging", true);
}
function dragged(d) {
selected = dragged = d3.mouse(city.node());
d.x = x.invert(Math.min(width, dragged[0]));
d.y = y.invert(Math.min(height, dragged[1]));
console.log("node", d3.mouse(city.node()));
console.log("d3.event", d3.event, d3.event.dx, d3.event.dy, d3.event.x, d3.event.y, d, d.x, d.y);
redraw();
}
function dragended(d) {
console.log('Drag started');
d3.select(this).classed("dragging", false);
dragged = null;
}
});

body {
font: 10px sans-serif;
}
.axis path,
.axis line {
fill: none;
stroke: #000;
shape-rendering: crispEdges;
}
.x.axis path {
display: none;
}
.line {
fill: none;
stroke: steelblue;
stroke-width: 1.5px;
}

<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.4.11/d3.min.js"></script>
&#13;
类似于http://bl.ocks.org/mbostock/4342190,但对于多行。 但是线条是正在渲染的圆形,但是通过选择圆形来编辑样条线是不起作用的。拖动功能和事件不会被触发。任何人都可以提供帮助。