my question is that i will added a polyline in D3 pie chart

My problem is that my polylines in my chart are not correct positioning on the exact data on my slices which are given in my code

This is my code of slices and polylines when yu convert this code into html this will run and place on W3 school or place this code on a function and call on html on click

for reference the whole code
https://observablehq.com/@mast4461/d3-donut-chart-labels
this is a donut but exact polylines will place on their correct positioning and my chart is pie chart in 3D-view i will attach my UI in image

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code> var slices = vis
.append("svg:g")
.attr("transform", "translate(" + x + "," + y + ")")
.attr("class", "slice");
slices
.selectAll(".innerSlice")
.data(_data)
.enter()
.append("path")
.attr("class", "innerSlice")
.style("fill", function (d) {
return d3.hsl(d.data.color).darker(0.7);
})
.attr("d", function (d) {
return pieInner(d, rx + 0.5, ry + 0.5, h, ir);
})
.each(function (d) {
this._current = d;
});
slices
.selectAll(".topSlice")
.data(_data)
.enter()
.append("path")
.attr("class", "topSlice")
.style("fill", function (d) {
return d.data.color;
})
.style("stroke", function (d) {
return d.data.color;
})
.attr("d", function (d) {
return pieTop(d, rx, ry, ir);
})
.each(function (d) {
this._current = d;
});
slices
.selectAll(".outerSlice")
.data(_data)
.enter()
.append("path")
.attr("class", "outerSlice")
.style("fill", function (d) {
return d3.hsl(d.data.color).darker(0.7);
})
.attr("d", function (d) {
return pieOuter(d, rx - 0.5, ry - 0.5, 40, 5, 50, 100, 0.02);
})
.each(function (d) {
this._current = d;
});
slices
.selectAll(".percent")
.data(_data)
.enter()
.append("text")
.attr("class", "percent")
.attr("x", function (d) {
return 0.6 * rx * Math.cos(0.5 * (d.startAngle + d.endAngle));
})
.attr("y", function (d) {
return 0.6 * ry * Math.sin(0.5 * (d.startAngle + d.endAngle));
})
.text(getPercent)
.each(function (d) {
this._current = d;
});
function pieInner(d, rx, ry, h, ir) {
var startAngle = d.startAngle < Math.PI ? Math.PI : d.startAngle;
var endAngle = d.endAngle < Math.PI ? Math.PI : d.endAngle;
var sx = ir * rx * Math.cos(startAngle),
sy = ir * ry * Math.sin(startAngle),
ex = ir * rx * Math.cos(endAngle),
ey = ir * ry * Math.sin(endAngle);
var ret = [];
ret.push(
"M",
sx,
sy,
"A",
ir * rx,
ir * ry,
"0 0 1",
ex,
ey,
"L",
ex,
h + ey,
"A",
ir * rx,
ir * ry,
"0 0 0",
sx,
h + sy,
"z"
);
return ret.join(" ");
}
function pieTop(d, rx, ry, ir) {
if (d.endAngle - d.startAngle == 0) return "M 0 0";
var sx = rx * Math.cos(d.startAngle),
sy = ry * Math.sin(d.startAngle),
ex = rx * Math.cos(d.endAngle),
ey = ry * Math.sin(d.endAngle);
var ret = [];
ret.push(
"M",
sx,
sy,
"A",
rx,
ry,
"0",
d.endAngle - d.startAngle > Math.PI ? 1 : 0,
"1",
ex,
ey,
"L",
ir * ex,
ir * ey
);
ret.push(
"A",
ir * rx,
ir * ry,
"0",
d.endAngle - d.startAngle > Math.PI ? 1 : 0,
"0",
ir * sx,
ir * sy,
"z"
);
return ret.join(" ");
}
function pieOuter(d, rx, ry, h) {
var startAngle = d.startAngle > Math.PI ? Math.PI : d.startAngle;
var endAngle = d.endAngle > Math.PI ? Math.PI : d.endAngle;
var sx = rx * Math.cos(startAngle),
sy = ry * Math.sin(startAngle),
ex = rx * Math.cos(endAngle),
ey = ry * Math.sin(endAngle);
var ret = [];
ret.push(
"M",
sx,
h + sy,
"A",
rx,
ry,
"0 0 1",
ex,
h + ey,
"L",
ex,
ey,
"A",
rx,
ry,
"0 0 0",
sx,
sy,
"z"
);
return ret.join(" ");
}
var radius = 200;
var arc = d3
.arc()
.innerRadius(radius - 100)
.outerRadius(radius - 50);
var outerArc = d3
.arc()
.innerRadius(radius * 0.9)
.outerRadius(radius * 0.9);
// Add the polylines between chart and labels:
slices
.selectAll("allPolylines")
.data(_data)
.enter()
.append("polyline")
.attr("stroke", "black")
.style("fill", "none")
.attr("stroke-width", 1)
.attr("points", function (d) {
console.log("checkkkshdfkh", d);
var posA = arc.centroid(d) // line insertion in the slice
var posB = outerArc.centroid(d) // line break: we use the other arc generator that has been built only for that
var posC = outerArc.centroid(d); // Label position = almost the same as posB
let tempX= 0.6 * rx * Math.cos(0.5 * (d.startAngle + d.endAngle));
let tempY= 0.6 * ry * Math.sin(0.5 * (d.startAngle + d.endAngle))
var midangle = d.startAngle + (d.endAngle - d.startAngle) / 2 // we need the angle to see if the X position will be at the extreme right or extreme left
posC[0] = radius * 0.95 * (midangle < Math.PI ? 1 : -1);
console.log("checknskdnfknsdf",posA,[tempX,tempY])
return [posA, posB, posC]
});
</code>
<code> var slices = vis .append("svg:g") .attr("transform", "translate(" + x + "," + y + ")") .attr("class", "slice"); slices .selectAll(".innerSlice") .data(_data) .enter() .append("path") .attr("class", "innerSlice") .style("fill", function (d) { return d3.hsl(d.data.color).darker(0.7); }) .attr("d", function (d) { return pieInner(d, rx + 0.5, ry + 0.5, h, ir); }) .each(function (d) { this._current = d; }); slices .selectAll(".topSlice") .data(_data) .enter() .append("path") .attr("class", "topSlice") .style("fill", function (d) { return d.data.color; }) .style("stroke", function (d) { return d.data.color; }) .attr("d", function (d) { return pieTop(d, rx, ry, ir); }) .each(function (d) { this._current = d; }); slices .selectAll(".outerSlice") .data(_data) .enter() .append("path") .attr("class", "outerSlice") .style("fill", function (d) { return d3.hsl(d.data.color).darker(0.7); }) .attr("d", function (d) { return pieOuter(d, rx - 0.5, ry - 0.5, 40, 5, 50, 100, 0.02); }) .each(function (d) { this._current = d; }); slices .selectAll(".percent") .data(_data) .enter() .append("text") .attr("class", "percent") .attr("x", function (d) { return 0.6 * rx * Math.cos(0.5 * (d.startAngle + d.endAngle)); }) .attr("y", function (d) { return 0.6 * ry * Math.sin(0.5 * (d.startAngle + d.endAngle)); }) .text(getPercent) .each(function (d) { this._current = d; }); function pieInner(d, rx, ry, h, ir) { var startAngle = d.startAngle < Math.PI ? Math.PI : d.startAngle; var endAngle = d.endAngle < Math.PI ? Math.PI : d.endAngle; var sx = ir * rx * Math.cos(startAngle), sy = ir * ry * Math.sin(startAngle), ex = ir * rx * Math.cos(endAngle), ey = ir * ry * Math.sin(endAngle); var ret = []; ret.push( "M", sx, sy, "A", ir * rx, ir * ry, "0 0 1", ex, ey, "L", ex, h + ey, "A", ir * rx, ir * ry, "0 0 0", sx, h + sy, "z" ); return ret.join(" "); } function pieTop(d, rx, ry, ir) { if (d.endAngle - d.startAngle == 0) return "M 0 0"; var sx = rx * Math.cos(d.startAngle), sy = ry * Math.sin(d.startAngle), ex = rx * Math.cos(d.endAngle), ey = ry * Math.sin(d.endAngle); var ret = []; ret.push( "M", sx, sy, "A", rx, ry, "0", d.endAngle - d.startAngle > Math.PI ? 1 : 0, "1", ex, ey, "L", ir * ex, ir * ey ); ret.push( "A", ir * rx, ir * ry, "0", d.endAngle - d.startAngle > Math.PI ? 1 : 0, "0", ir * sx, ir * sy, "z" ); return ret.join(" "); } function pieOuter(d, rx, ry, h) { var startAngle = d.startAngle > Math.PI ? Math.PI : d.startAngle; var endAngle = d.endAngle > Math.PI ? Math.PI : d.endAngle; var sx = rx * Math.cos(startAngle), sy = ry * Math.sin(startAngle), ex = rx * Math.cos(endAngle), ey = ry * Math.sin(endAngle); var ret = []; ret.push( "M", sx, h + sy, "A", rx, ry, "0 0 1", ex, h + ey, "L", ex, ey, "A", rx, ry, "0 0 0", sx, sy, "z" ); return ret.join(" "); } var radius = 200; var arc = d3 .arc() .innerRadius(radius - 100) .outerRadius(radius - 50); var outerArc = d3 .arc() .innerRadius(radius * 0.9) .outerRadius(radius * 0.9); // Add the polylines between chart and labels: slices .selectAll("allPolylines") .data(_data) .enter() .append("polyline") .attr("stroke", "black") .style("fill", "none") .attr("stroke-width", 1) .attr("points", function (d) { console.log("checkkkshdfkh", d); var posA = arc.centroid(d) // line insertion in the slice var posB = outerArc.centroid(d) // line break: we use the other arc generator that has been built only for that var posC = outerArc.centroid(d); // Label position = almost the same as posB let tempX= 0.6 * rx * Math.cos(0.5 * (d.startAngle + d.endAngle)); let tempY= 0.6 * ry * Math.sin(0.5 * (d.startAngle + d.endAngle)) var midangle = d.startAngle + (d.endAngle - d.startAngle) / 2 // we need the angle to see if the X position will be at the extreme right or extreme left posC[0] = radius * 0.95 * (midangle < Math.PI ? 1 : -1); console.log("checknskdnfknsdf",posA,[tempX,tempY]) return [posA, posB, posC] }); </code>
    var slices = vis
      .append("svg:g")
      .attr("transform", "translate(" + x + "," + y + ")")
      .attr("class", "slice");

    slices
      .selectAll(".innerSlice")
      .data(_data)
      .enter()
      .append("path")
      .attr("class", "innerSlice")
      .style("fill", function (d) {
        return d3.hsl(d.data.color).darker(0.7);
      })
      .attr("d", function (d) {
        return pieInner(d, rx + 0.5, ry + 0.5, h, ir);
      })
      .each(function (d) {
        this._current = d;
      });

    slices
      .selectAll(".topSlice")
      .data(_data)
      .enter()
      .append("path")
      .attr("class", "topSlice")
      .style("fill", function (d) {
        return d.data.color;
      })
      .style("stroke", function (d) {
        return d.data.color;
      })
      .attr("d", function (d) {
        return pieTop(d, rx, ry, ir);
      })
      .each(function (d) {
        this._current = d;
      });

    slices
      .selectAll(".outerSlice")
      .data(_data)
      .enter()
      .append("path")
      .attr("class", "outerSlice")
      .style("fill", function (d) {
        return d3.hsl(d.data.color).darker(0.7);
      })
      .attr("d", function (d) {
        return pieOuter(d, rx - 0.5, ry - 0.5, 40, 5, 50, 100, 0.02);
      })
      .each(function (d) {
        this._current = d;
      });

    slices
      .selectAll(".percent")
      .data(_data)
      .enter()
      .append("text")
      .attr("class", "percent")
      .attr("x", function (d) {
        return 0.6 * rx * Math.cos(0.5 * (d.startAngle + d.endAngle));
      })
      .attr("y", function (d) {
        return 0.6 * ry * Math.sin(0.5 * (d.startAngle + d.endAngle));
      })
      .text(getPercent)
      .each(function (d) {
        this._current = d;
      });

    function pieInner(d, rx, ry, h, ir) {
      var startAngle = d.startAngle < Math.PI ? Math.PI : d.startAngle;
      var endAngle = d.endAngle < Math.PI ? Math.PI : d.endAngle;

      var sx = ir * rx * Math.cos(startAngle),
        sy = ir * ry * Math.sin(startAngle),
        ex = ir * rx * Math.cos(endAngle),
        ey = ir * ry * Math.sin(endAngle);

      var ret = [];
      ret.push(
        "M",
        sx,
        sy,
        "A",
        ir * rx,
        ir * ry,
        "0 0 1",
        ex,
        ey,
        "L",
        ex,
        h + ey,
        "A",
        ir * rx,
        ir * ry,
        "0 0 0",
        sx,
        h + sy,
        "z"
      );
      return ret.join(" ");
    }

    function pieTop(d, rx, ry, ir) {
      if (d.endAngle - d.startAngle == 0) return "M 0 0";
      var sx = rx * Math.cos(d.startAngle),
        sy = ry * Math.sin(d.startAngle),
        ex = rx * Math.cos(d.endAngle),
        ey = ry * Math.sin(d.endAngle);

      var ret = [];
      ret.push(
        "M",
        sx,
        sy,
        "A",
        rx,
        ry,
        "0",
        d.endAngle - d.startAngle > Math.PI ? 1 : 0,
        "1",
        ex,
        ey,
        "L",
        ir * ex,
        ir * ey
      );
      ret.push(
        "A",
        ir * rx,
        ir * ry,
        "0",
        d.endAngle - d.startAngle > Math.PI ? 1 : 0,
        "0",
        ir * sx,
        ir * sy,
        "z"
      );
      return ret.join(" ");
    }

    function pieOuter(d, rx, ry, h) {
      var startAngle = d.startAngle > Math.PI ? Math.PI : d.startAngle;
      var endAngle = d.endAngle > Math.PI ? Math.PI : d.endAngle;

      var sx = rx * Math.cos(startAngle),
        sy = ry * Math.sin(startAngle),
        ex = rx * Math.cos(endAngle),
        ey = ry * Math.sin(endAngle);

      var ret = [];
      ret.push(
        "M",
        sx,
        h + sy,
        "A",
        rx,
        ry,
        "0 0 1",
        ex,
        h + ey,
        "L",
        ex,
        ey,
        "A",
        rx,
        ry,
        "0 0 0",
        sx,
        sy,
        "z"
      );
      return ret.join(" ");
    }
    var radius = 200;
    var arc = d3
      .arc()
      .innerRadius(radius - 100)
      .outerRadius(radius - 50);

    var outerArc = d3
      .arc()
      .innerRadius(radius * 0.9)
      .outerRadius(radius * 0.9);
    // Add the polylines between chart and labels:
    slices
      .selectAll("allPolylines")
      .data(_data)
      .enter()
      .append("polyline")
      .attr("stroke", "black")
      .style("fill", "none")
      .attr("stroke-width", 1)
      .attr("points", function (d) {
        console.log("checkkkshdfkh", d);
      
        var posA = arc.centroid(d) // line insertion in the slice
        var posB = outerArc.centroid(d) // line break: we use the other arc generator that has been built only for that
        var posC = outerArc.centroid(d); // Label position = almost the same as posB
        let tempX= 0.6 * rx * Math.cos(0.5 * (d.startAngle + d.endAngle));
        let tempY= 0.6 * ry * Math.sin(0.5 * (d.startAngle + d.endAngle))
        var midangle = d.startAngle + (d.endAngle - d.startAngle) / 2 // we need the angle to see if the X position will be at the extreme right or extreme left
        posC[0] = radius * 0.95 * (midangle < Math.PI ? 1 : -1);
        console.log("checknskdnfknsdf",posA,[tempX,tempY])
        return [posA, posB, posC]

      });

My UI looks like

I try https://observablehq.com/@mast4461/d3-donut-chart-labels and i try most of the code but it didn’t work

I expect that my polylines are place on the exact slice based on the data and also

New contributor

Janvi tyagi is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật