_plotPolygons(polygon, container) {
const idList = [];
polygon.coordinates.forEach((coords) => {
const id = generateId();
idList.push(id);
// 使用 curveLinearClosed 确保路径闭合,决定了数据缩放和绘制方式。
const lineGenerator = d3.line()
.x((d) => d[0] * this._zx)
.y((d) => d[1] * this._zy)
.curve(d3.curveLinearClosed); // 自动闭合路径
//如果使用d3.curveLinear就首尾不闭合
const path = container
.append('path')
.data([coords])
.attr('id', id)
// 使用修改后的 lineGenerator
.attr('d', lineGenerator)
.attr('stroke', polygon.properties.color ?? "red")
.attr('stroke-width', 2)
.classed('polygon-path', true);
if (polygon.properties.type === 'CarBoundary') {
path.attr('fill', 'none');
} else {
path
.attr('fill', 'white')
.attr('fill-opacity', 0.5);
}
});
}
D3j——line的绘制
于 2024-08-22 15:35:58 首次发布