I am having trouble with a simple task in d3 as I am new to the library.
Using the gradient example, I've inserted a linear gradient into the footer div element:
#footer {
position: absolute;
z-index: 10;
bottom: 10px;
left: 50%;
width: 300px;
margin-left: -150px;
height: 20px;
border: 2px solid black;
background: rgba(12, 12, 12, 0.8);
color: #eee;
}
var svg = d3.select("footer")
.append("svg:svg")
.attr("width", 300)//canvasWidth)
.attr("height", 20);
svg.append("rect")
.attr("width", 300)
.attr("height", 20)
.style("fill", "url(#linear-gradient)");
var defs = svg.append("defs");
var linearGradient = defs.append("linearGradient")
.attr("id", "linear-gradient");
linearGradient.append("stop")
.attr("offset", "0%")
.attr("stop-color", "#ffa474");
linearGradient.append("stop")
.attr("offset", "100%")
.attr("stop-color", "#8b0000");
how would one insert text "a" and "b" on either side of the gradient so that the text is within the bar and aligned to the left and right sides, while appearing above the color? I have tried adding text in the div element but this only "pushes" aside the gradient bar
An easy solution to center text horizontally and vertically in SVG: Set the position of the text to the absolute center of the element in which you want to center it: If it's the parent, you could just do x="50%" y ="50%" .
The text-anchor attribute is used to align (start-, middle- or end-alignment) a string of pre-formatted text or auto-wrapped text where the wrapping area is determined from the inline-size property relative to a given point. This attribute is not applicable to other types of auto-wrapped text.
You can position your text elements using text-anchor
. For the first text, set text-anchor
to start. For the last one, set text-anchor
to end:
svg.append("text")
.attr("text-anchor", "start")
.attr("x", 4)//padding of 4px
.attr("y", 14)
.text("a");
svg.append("text")
.attr("text-anchor", "end")
.attr("x", 296)//padding of 4px
.attr("y", 14)
.text("b");
Here is a demo:
var svg = d3.select("#footer")
.append("svg:svg")
.attr("width", 300)//canvasWidth)
.attr("height", 20);
var defs = svg.append("defs");
var linearGradient = defs.append("linearGradient")
.attr("id", "linear-gradient");
linearGradient.append("stop")
.attr("offset", "0%")
.attr("stop-color", "#ffa474");
linearGradient.append("stop")
.attr("offset", "100%")
.attr("stop-color", "#8b0000");
svg.append("rect")
.attr("width", 300)
.attr("height", 20)
.style("fill", "url(#linear-gradient)");
svg.append("text")
.attr("text-anchor", "start")
.attr("x", 4)
.attr("y", 14)
.text("a");
svg.append("text")
.attr("text-anchor", "end")
.attr("x", 296)
.attr("y", 14)
.text("b");
#footer {
position: absolute;
z-index: 10;
bottom: 10px;
left: 50%;
width: 300px;
margin-left: -150px;
height: 20px;
border: 2px solid black;
background: rgba(12, 12, 12, 0.8);
color: #eee;
}
<script src="https://d3js.org/d3.v4.min.js"></script>
<div id="footer"></div>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With