Data Visualization with D3: Style D3 Labels

D3 methods can add styles to the bar labels. The fill attribute sets the color of the text for a text node. The style() method sets CSS rules for other styles, such as "font-family" or "font-size".

In this tutorial,Set the font-size of the text elements to 25px, and the color of the text to red.

<body>
  <script>
    const dataset = [12312217251829149];

    const w = 500;
    const h = 100;

    const svg = d3.select("body")
                  .append("svg")
                  .attr("width"w)
                  .attr("height"h);

    svg.selectAll("rect")
       .data(dataset)
       .enter()
       .append("rect")
       .attr("x", (di) => i * 30)
       .attr("y", (di) => h - 3 * d)
       .attr("width"25)
       .attr("height", (di) => d * 3)
       .attr("fill""navy");

    svg.selectAll("text")
       .data(dataset)
       .enter()
       .append("text")
       .text((d) => d)
       .attr("x", (di) => i * 30)
       .attr("y", (di) => h - (3 * d) - 3)
       .attr("fill","red")
       .style("font-size","25px")
  </script>
</body>
Output:

No comments

Powered by Blogger.