Sto usando d3js per trovare la larghezza di uno SVG elemento il codice riportato qui di seguito:Come ottengo la larghezza di un elemento svg usando d3js?
<script>
var body = d3.select("body");
console.log(body);
var svg = body.select("svg");
console.log(svg);
console.log(svg.style("width"));
</script>
<svg class="svg" height="3300" width="2550">
<image x="0" y="0" height="3300" width="2550" xlink:href="1.jpg"></image>
<rect class="word" id="15" x="118" y="259" width="182" height="28"
text="Substitute"></rect>
</svg>
Ma ritornato questo errore:
Uncaught TypeError: Cannot call method 'getPropertyValue' of null
credo, la variabile svg è un array null.
Come posso ottenere la larghezza di un elemento svg usando d3js?
Oppure eseguirlo dopo aver caricato la pagina. – vittore
svg.style ("width") restituisce il valore con "px" aggiunto che non è conveniente. – user2846569
Puoi sempre togliere 'px' usando qualcosa di simile a questo: var widthString = svg.style ("width"); var width = widthString.length> 2? Number (widthString.substring (0, widthString.length - 2)): 0; –