D3.js中的线分割 [英] line segmentation in D3.js
问题描述
我已经创建了一个连接D3中各种数据点的线,作为音乐可视化项目的一部分:
I have created a line connecting various data points in D3, as part of a music visualization project:
但是,我需要对不同的线段进行不同的颜色。我通过创建多行而不是使用line()生成器(即每个段一行),但是我失去了我的最想要的插值方式:
However, I need to color various line segments differently. I did this by creating multiple lines instead of using the line() generator (that is, one line per segment), however I lost my most-wanted interpolation this way:
注意:笔触颜色不仅在开始时
Notice: The stroke color will be different not only at the beginning but also in other segments of the line, as well.
所以,我想问一下,是否有一种方法来组合这两个特征(不同的颜色笔画和步骤 - 内插后)。这里是我的代码的主要部分(d3.svg.line()示例),作为提示:
So, I would like to ask, whether there is a way to combine both features (different color strokes and step-after interpolation). Here is the main part of my code (d3.svg.line() example), as a hint:
<script>
var margin = {top: 20, right: 20, bottom: 30, left: 50},
width = 1200 - margin.left - margin.right,
height = 500 - margin.top - margin.bottom;
var x = d3.scale.linear()
.range([0, width]);
var y = d3.scale.linear()
.range([height, 100]);
var tickValuesArray = [0];
for (var i = 1; i < 27; i++) {
var newTick = i*16+1;
tickValuesArray.push(newTick);
}
var xAxis = d3.svg.axis()
.scale(x)
.orient("bottom")
.tickValues(tickValuesArray)
.tickFormat(function(d,i) {return i+1 })
.innerTickSize(-height)
.tickPadding(10);
var yAxis = d3.svg.axis()
.scale(y)
.orient("left")
.ticks(22)
.tickFormat(function(d) {
if (d == 0) {return "A"}
else if (d == 1) {return "B-"}
else if (d == 2) {return "B"}
else if (d == 3) {return "c"}
else if (d == 4) {return "c#"}
else if (d == 5) {return "d"}
else if (d == 6) {return "d#"}
else if (d == 7) {return "e"}
else if (d == 8) {return "f"}
else if (d == 9) {return "f#"}
else if (d == 10) {return "g"}
else if (d == 11) {return "g#"}
else if (d == 12) {return "a"}
else if (d == 13) {return "b-"}
else if (d == 14) {return "b"}
else if (d == 15) {return "cc"}
else if (d == 16) {return "cc#"}
else if (d == 17) {return "dd"}
else if (d == 18) {return "dd#"}
else if (d == 19) {return "ee"}
else if (d == 20) {return "ff"}
else if (d == 21) {return "ff#"}
else {return "gg"};
})
.innerTickSize(-width)
.tickPadding(10);
var line = d3.svg.line()
.interpolate("step-after")
.defined(function(d) { return !isNaN(d.pitch); })
.x(function(d) {
return x(d.times);
})
.y(function(d) {
return y(d.pitch);
});
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 + ")");
d3.tsv("linearAcomplete.tsv", type, function(error, data) {
if (error) throw error;
x.domain(d3.extent(data, function(d) { return d.times; }));
y.domain(d3.extent(data, function(d) { return d.pitch; }));
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("Pitch");
svg.append("path")
.datum(data)
.attr("class", "line")
.attr("d", line)
.attr("stroke", "green"); // can I be more "flexible" here?
});
function type(d) {
d.times = +d.times;
d.pitch = +d.pitch;
d.duration = +d.duration;
return d;
}
</script>
感谢您提供的任何建议!
Cheers - Ilias
Thanks for any advice you could provide! Cheers - Ilias
推荐答案
您可以使用线性渐变执行此操作,但需要计算开始/停止点的颜色百分比。下面是一个简单的例子:
You can do this with a linear gradient but it would require you to calculate start/stop points of a color as percentages. Here's a quick example:
<!DOCTYPE html>
<meta charset="utf-8">
<style>
.line {
fill: none;
}
.axis {
font: 10px sans-serif;
}
.axis path,
.axis line {
fill: none;
stroke: #000;
shape-rendering: crispEdges;
}
</style>
<body>
<script src="//d3js.org/d3.v3.min.js"></script>
<script>
var margin = {
top: 20,
right: 20,
bottom: 30,
left: 40
},
width = 960 - margin.left - margin.right,
height = 500 - margin.top - margin.bottom;
var x = d3.scale.linear()
.range([0, width]);
var y = d3.scale.linear()
.range([height, 0]);
var xAxis = d3.svg.axis()
.scale(x)
.orient("bottom");
var yAxis = d3.svg.axis()
.scale(y)
.orient("left");
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 + ")");
var grad = svg.append('defs')
.append('linearGradient')
.attr('id', 'grad');
grad.append('stop')
.attr('stop-color', 'green');
grad.append('stop')
.attr('stop-color', 'green')
.attr('offset', '25%');
grad.append('stop')
.attr('stop-color', 'red')
.attr('offset', '25%');
grad.append('stop')
.attr('stop-color', 'red')
.attr('offset', '50%');
grad.append('stop')
.attr('stop-color', 'green')
.attr('offset', '50%');
grad.append('stop')
.attr('stop-color', 'green')
.attr('offset', '100%');
var data = d3.range(100).map(function(d) {
return {
x: d,
y: Math.random() * 10
};
})
var line = d3.svg.line()
.interpolate("step-after")
.x(function(d) {
return x(d.x);
})
.y(function(d) {
return y(d.y);
});
x.domain([0, 100]);
y.domain([0, d3.max(data, function(d) {
return d.y;
}) * 1.1]);
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("path")
.datum(data)
.attr("class", "line")
.attr("d", line)
.attr("stroke", "url(#grad)");
</script>
这篇关于D3.js中的线分割的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!