如果您使用D3.js,而你用线工作,内置的内插()函数是要走的路。
这里是D3的line.interpolate的工作示例()使用 “红衣主教” 平滑:
下面的代码:
var margin = {
top: 30,
right: 20,
bottom: 30,
left: 50
},
width = 600 - margin.left - margin.right,
height = 270 - margin.top - margin.bottom;
// Parse the date/time
var parseDate = d3.time.format("%d-%b-%y").parse;
// Set the ranges
var x = d3.time.scale().range([0, width]);
var y = d3.scale.linear().range([height, 0]);
// Define the axes
var xAxis = d3.svg.axis().scale(x).orient("bottom").ticks(5);
var yAxis = d3.svg.axis().scale(y).orient("left").ticks(5);
// Define the line
var valueline = d3.svg.line()
.interpolate("cardinal")
.x(function(d) {
return x(d.date);
})
.y(function(d) {
return y(d.close);
});
// Adds the svg canvas
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 + ")");
// Get the data
d3.json('https://api.myjson.com/bins/175jl', function(error, data) {
data.forEach(function(d) {
d.date = parseDate(d.date);
d.close = +d.close;
});
// Scale the range of the data
// Starting with a basic graph 14
x.domain(d3.extent(data, function(d) {
return d.date;
}));
y.domain([0, d3.max(data, function(d) {
return d.close;
})]);
// Add the valueline path.
svg.append("path")
.attr("class", "line")
.attr("d", valueline(data));
// Add the X Axis
svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + height + ")")
.call(xAxis);
// Add the Y Axis
svg.append("g")
.attr("class", "y axis")
.call(yAxis);
});
相关文章[使SVG路径像一条流畅的线条而不是破烂不前](http://stackoverflow.com/questions/28638327/) – Hugolpz 2015-04-01 19:30:01
任何时候你需要连续平滑线条的所有决议,你的选择基本上归结为大文件或SVG。我期望这个解决方案将采取简化的数据并将其转换为SVG,并在角落四舍五入来实现预期的效果。 – brichins 2015-04-07 21:14:31
此外,作为一名土木工程师,我首先会问为什么这是必需的 - 它意味着数据集中不存在的准确性水平,对于我处理的大多数问题,夸大准确性是一个严重的问题问题。根据您的使用情况,如果这可能会误导用户并导致错误的结论,则这可能是不好的做法。我个人比较喜欢显示(例如)一些块状图,以便在视觉上代表“+/- 5英尺”,而不是完美的,可能落在属性角落错误一侧的0宽度线。 – brichins 2015-04-07 21:19:41