我愿意更改不同学院的线路属性(如虚线,虚线等),并在其相应行的末尾添加学院名称。我使用嵌套函数将每个学院的所有数据分组。任何帮助将不胜感激。这是我的代码-
// Set the ranges
var x = d3.scaleLinear().range([0 , width]);
var y = d3.scaleLinear().range([0, height]);
// Define the line
var priceline = d3.line()
.x(function(d) { return x(d.year); })
.y(function(d) { return y(d.item_score); });
// Adds the svg canvas
var svg = d3.select("body")
.append("svg")
.attr("width", width + margin.left + margin.right+200)
.attr("height", height + margin.top + margin.bottom)
.append("g")
.attr("transform",
"translate(" + margin.left + "," + margin.top + ")");
// Get the data
d3.json("data.php", function(error, data) {
data.forEach(function(d) {
d.date = parseDate(d.year);
d.price = +d.item_score;
});
// Scale the range of the data
x.domain(d3.extent(data, function(d) { return d.year; }));
max = d3.max(data, function(d) { return d.price; })
y.domain([1, max]);
// Nest the entries by symbol
var dataNest = d3.nest()
.key(function(d) {return d.item_name;})
.entries(data);
// set the colour scale
var color = d3.scaleOrdinal(d3.schemeCategory10);
legendSpace = width/dataNest.length; // spacing for the legend
// Loop through each symbol / key
dataNest.forEach(function(d,i) {
svg.append("path")
.attr("class", "line")
.style("stroke", function() { // Add the colours dynamically
return d.color = color(d.key); })
.attr("id", 'tag'+d.key.replace(/\s+/g, '')) // assign an ID
.attr("d", priceline(d.values));
// Add the Legend
svg.append("text")
.attr("x", width+(margin.right)) // space legend
.attr("y", (legendSpace/2)+i*legendSpace)
.attr("class", "legend")
.attr("text-anchor", "end")// style the legend
.style("fill", function() { // Add the colours dynamically
return d.color = color(d.key); })
.text(d.key);
});
});