如果您使用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