Data Visualization with D3: Add Labels to D3 Elements

D3 lets you label a graph element, such as a bar, using the SVG text element.
Like the rect element, a text element needs to have x and y attributes, to place it on the SVG canvas. It also needs to access the data to display those values.
D3 gives you a high level of control over how you label your bars.
<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) => 3 * d)
       .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)
  </script>
<body>

Output:

3 comments:

  1. This was a really great contest and hopefully I can attend the next one. It was alot of fun and I really enjoyed myself.. labels

    ReplyDelete
  2. Data lakeare a popular choice for many companies looking for a way to store and access a massive amount of data. When you’re dealing with petabytes of data, a relational database may no longer be sufficient. This is especially true for companies that want to analyze data quickly, as well as those with strict compliance requirements. A data lake gives you the ability to store data from a variety of sources, including different formats and different data lakes.

    ReplyDelete

Powered by Blogger.