0

I am a beginner to d3 js. I have a div that contains a object tag having svg file.
I need to create circle with texts over that svg file and bind click functions over them using d3 js.
Thanx in advance. Below is my html code:

<div class="span8 canvasDiv">
    <object data="img/floor.svg" type="image/svg+xml" id="floorCanvas"></object>
</div>

JS Code:

var svgContainer = d3.select("#floorCanvas");
var circle = svgContainer.append("circle")
                         .attr("cx", 30)
                         .attr("cy", 30)
                         .attr("r", 20);
Andrey Korneyev
  • 26,353
  • 15
  • 70
  • 71
Rahul Munjal
  • 2,551
  • 2
  • 15
  • 35

1 Answers1

0

youll need to create a 'g' tag then append both the circle and the text to that 'g' as you cant append text directly to an svg shape so something like this (not tested) :

HTML :

 <div class="span8 canvasDiv">
        <object data="img/floor.svg" type="image/svg+xml" id="floorCanvas"></object>
        </div>

JS Code:-

 var svgContainer = d3.select("#floorCanvas").append('svg').append('g').attr('id', 'circleWithText');
 var circleWithText = d3.select("#circleWithText");

 circleWithText .append("circle")
                .attr("cx", 30)
                .attr("cy", 30)
                .attr("r", 20);

 circleWithText.append('text')
                .attr('class', 'text')
                .attr("dx", "0")
                .attr("dy", "0")
                .text('what ever you want here');

CSS

.text{
    text-anchor: middle;
    color: steelblue;
    position:relative;
    font: 14px Calibri;
}

Something along those lines :)

Rahul Munjal
  • 2,551
  • 2
  • 15
  • 35
thatOneGuy
  • 9,977
  • 7
  • 48
  • 90