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 = [12, 31, 22, 17, 25, 18, 29, 14, 9];
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", (d, i) => i * 30)
.attr("y", (d, i) => h - 3 * d)
.attr("width", 25)
.attr("height", (d, i) => 3 * d)
.attr("fill", "navy");
svg.selectAll("text")
.data(dataset)
.enter()
.append("text")
.text((d) => d)
.attr("x", (d, i) => i * 30)
.attr("y", (d, i) => h - (3 * d) - 3)
</script>
<body>
Output:
Mojja Sani. Valnagse.
ReplyDeleteThis was a really great contest and hopefully I can attend the next one. It was alot of fun and I really enjoyed myself.. labels
ReplyDeleteData 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