gpt4 book ai didi

javascript - D3 圆环图图例或悬停标签

转载 作者:行者123 更新时间:2023-11-28 08:02:53 25 4
gpt4 key购买 nike

我在这里使用 D3.js 创建了一个图表 ( http://jsfiddle.net/jkuchta/u57mt97c/ ),但我遇到了标签问题。我如何添加图例或让标签仅出现在悬停时?

// DRAW SLICE LABELS
var sliceLabel = label_group.selectAll("text")
.data(donut(data.pct));
sliceLabel.enter().append("svg:text")
.attr("class", "arcLabel")
.attr("transform", function(d) {return "translate(" + arc.centroid(d) + ")"; })
.attr("text-anchor", "middle")
.text(function(d, i) {return labels[i]; });

最佳答案

我最终使用下面的内容创建了图表。 fiddle (https://jsfiddle.net/dp50bLmo/)

<style>
.legend { margin: 0 0 20px 20px; }
.chartLabel { font: 14px sans-serif; fill: #000; }
.arcLabel { font: 11px sans-serif; fill: #fff; }
.bar { min-height: 100%; height: 156px !important; }
</style>

<script type="text/javascript">
function build_chart(data, labels, chart_selector) {
var total = 0;
for(i = 0; i < data.pct.length; i++) {
total += data.pct[i];
}
var w = 320,
h = 320,
r = Math.min(w, h) / 2,
dur = 750,
color = d3.scale.category10(),
donut = d3.layout.pie().sort(null),
arc = d3.svg.arc().innerRadius(r - 70).outerRadius(r - 20);
var svg = d3.select(chart_selector).append("svg:svg")
.attr("width", w).attr("height", h);
var arc_grp = svg.append("svg:g")
.attr("class", "arcGrp")
.attr("transform", "translate(" + (w / 2) + "," + (h / 2) + ")");
var label_group = svg.append("svg:g")
.attr("class", "lblGroup")
.attr("transform", "translate(" + (w / 2) + "," + (h / 2) + ")");
var center_group = svg.append("svg:g")
.attr("class", "ctrGroup")
.attr("transform", "translate(" + (w / 2) + "," + (h / 2) + ")");
var pieLabel = center_group.append("svg:text")
.attr("dy", ".35em").attr("class", "chartLabel")
.attr("text-anchor", "middle")
.text(data.label);
var arcs = arc_grp.selectAll("path")
.data(donut(data.pct));
arcs.enter().append("svg:path")
.attr("stroke", "white")
.attr("stroke-width", 0.5)
.attr("fill", function(d, i) {return color(i);})
.attr("d", arc)
.each(function(d) {this._current = d});
var sliceLabel = label_group.selectAll("text")
.data(donut(data.pct));
sliceLabel.enter().append("svg:text")
.attr("class", "arcLabel")
.attr("transform", function(d) {return "translate(" + arc.centroid(d) + ")"; })
.attr("text-anchor", "middle")
.text(function(d, i) {return (data.pct[i] / total * 100).toFixed(1) + "%"; });
var legend = d3.select(chart_selector).append("svg")
.attr("class", "legend")
.attr("width", w)
.attr("height", 22 * labels.length)
.selectAll("g")
.data(color.domain().slice())
.enter().append("g")
.attr("transform", function(d, i) { return "translate(0," + i * 20 + ")"; });
legend.append("rect")
.attr("width", 18)
.attr("height", 18)
.style("fill", color);
legend.append("text")
.attr("x", 24)
.attr("y", 9)
.attr("dy", ".35em")
.text(function(d, i) { return labels[i]; });
}
</script>

<script type="text/javascript">
$(function(){
var chart_selector = '#chart';
var data = { label: 'Test Chart', pct: [] };
var labels = ['Label 1', 'Label 2', 'Label 3', 'Label 4'];
data.pct = [.1, .3, .4, .2];

build_chart(data, labels, chart_selector);
});
</script>
<div id="chart"></div>

关于javascript - D3 圆环图图例或悬停标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25170519/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com