javascript - 如何在两点之间创建中点?

标签 javascript d3.js svg

我正在尝试在三 Angular 形的任意两点之间创建点。如何在两点之间创建中点?例如,我将如何创建一个点 在坐标 (150, 0) 和 (0, 200) 之间?

var points = [
      {"x": 150, "y": 0},
       {"x": 0, "y": 200},
       {"x": 300, "y": 200},  
       {"x": 150, "y": 0}
      ];

//创建 SVG 容器

    var svg = d3.select("body").append("svg")
        .attr("width", 500)
        .attr("height", 500)
        .style('fill', '#113F8C')
        .style('padding-left', '20px')
        .style('padding-top', '5px');

//绘制路径

    var path = svg.append("path")
        .data([lineData])
        .attr("stroke", "black")
        .attr("stroke-width", 3)
        .attr("d", d3.line());

//设置点

    svg.selectAll("circle")
      .data(points)
      .enter().append("circle")
      .attr("cx", function(d) {
        console.log(d.x+ " hello");

        for (var i = 0; i < points.length; i++) {
          //console.log(points[i]);
          x[i] = d.x;
          console.log(d.x);
        };

         return d.x; 

    })
      .attr("cy", function(d) {console.log(d.y+ " hello"); return d.y; })
      .attr("r", 5);

      // THE X TEST
      x[0];

最佳答案

由于您的 points 数据在三 Angular 形中具有冗余的第四个点(与第一个点相同),因此我们可以简单地删除数据的第四个对象:

.data(points.slice(0, points.length-1))

甚至不用担心最后数据的范围误差:

.attr("cx", (d,i)=>(d.x + points[i+1].x)/2)
.attr("cy", (d,i)=>(d.y + points[i+1].y)/2);

这是一个演示。蓝点是数据数组的点,红点是中点。

var svg = d3.select("body")
	.append("svg")
	.attr("width", 400)
	.attr("height", 300);
	
var points = [
      {"x": 150, "y": 10},
       {"x": 10, "y": 200},
       {"x": 300, "y": 200},  
       {"x": 150, "y": 10}
      ];
			
var circles = svg.selectAll(".circles")
	.data(points)
	.enter()
	.append("circle")
	.attr("r", 5)
	.attr("fill", "blue")
	.attr("cx", d=>d.x)
	.attr("cy", d=>d.y);
	
var midPoints = svg.selectAll(".midPoints")
	.data(points.slice(0, points.length-1))
	.enter()
	.append("circle")
	.attr("r", 5)
	.attr("fill", "brown")
	.attr("cx", (d,i)=>(d.x + points[i+1].x)/2)
	.attr("cy", (d,i)=>(d.y + points[i+1].y)/2);
<script src="https://d3js.org/d3.v4.min.js"></script>

相同的片段,每个三 Angular 形都有线:

var svg = d3.select("body")
	.append("svg")
	.attr("width", 400)
	.attr("height", 300);
	
var points = [
      {"x": 150, "y": 10},
       {"x": 10, "y": 200},
       {"x": 300, "y": 200},  
       {"x": 150, "y": 10}
      ];

var line = d3.line()
  .x(d=>d.x)
  .y(d=>d.y);

var lineMid = d3.line()
  .x((d,i)=>(d.x + points[i+1].x)/2)
  .y((d,i)=>(d.y + points[i+1].y)/2)
  .curve(d3.curveLinearClosed);

var lineCircles = svg.append("path")
  .attr("d", line(points))
  .attr("fill", "none")
  .attr("stroke", "blue")
  .attr("opacity", 0.4);

var lineMidPoints = svg.append("path")
  .attr("d", lineMid(points.slice(0, points.length-1)))
  .attr("fill", "none")
  .attr("stroke", "brown")
  .attr("opacity", 0.4);
			
var circles = svg.selectAll(".circles")
	.data(points)
	.enter()
	.append("circle")
	.attr("r", 5)
	.attr("fill", "blue")
	.attr("cx", d=>d.x)
	.attr("cy", d=>d.y);
	
var midPoints = svg.selectAll(".midPoints")
	.data(points.slice(0, points.length-1))
	.enter()
	.append("circle")
	.attr("r", 5)
	.attr("fill", "brown")
	.attr("cx", (d,i)=>(d.x + points[i+1].x)/2)
	.attr("cy", (d,i)=>(d.y + points[i+1].y)/2);
<script src="https://d3js.org/d3.v4.min.js"></script>

关于javascript - 如何在两点之间创建中点?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40978831/

相关文章:

javascript - Datamap - 根据给定的 x 和 y 坐标检索国家/地区代码

javascript - 在 D3 中收集网络图的一些数据。我应该如何格式化它?

svg - 使用 Bokeh 生成高分辨率图形?

javascript - d3.js 动画线条绘制发生在与预期相反的方向

javascript - vuejs 延迟加载组件,无需路由器

javascript - 如何在 Google Closure Compiler 中为对象类型指定 @param @return?

javascript - D3 图 : how may i assign images inside a node

javascript - 响应式 D3 条形图在调整大小时附加新的 <text>

javascript - 如何更新数据库条目而不是创建新条目

javascript - 将包含数据的 csv 文件写入不同的列