部分标签粗体 - 其余不粗体

why*_*heq 3 javascript css d3.js

我正在使用d3.js以下代码段在饼图周围呈现标签:

text.enter()
    .append("text")
    .attr("dy", ".35em")
    .style("opacity", 0.01)
    .text(function(d) {
        return (d.data.NAME + ": " + d.data.amount);
    });
Run Code Online (Sandbox Code Playgroud)

所以标签可能会读 Jimmy: 100

如何d.data.NAME以粗体风格渲染但d.data.amount不应该是粗体?

Ger*_*ado 5

一种解决方案是使用<tspan>具有不同元素font-weight为你的d.data.amount

检查演示:

var svg = d3.select("svg");
var text = svg.append("text")
  .attr("x", 10)
  .attr("y", 30)
  .attr("font-weight", 700)
  .text("This is bold...")
  .append("tspan")
  .attr("font-weight", 300)
  .text(" but this is not.")
Run Code Online (Sandbox Code Playgroud)
<script src="https://d3js.org/d3.v4.min.js"></script>
<svg></svg>
Run Code Online (Sandbox Code Playgroud)

在你的情况下,它应该是这样的:

//...
.style("font-weight", 700)
.text(function(d) {
    return d.data.NAME + ": ";
})
.append("tspan")
.style("font-weight", 300)
.text(function(d) {
    return d.data.amount;
});
Run Code Online (Sandbox Code Playgroud)