D3 Click Event Example

A basic demo to show how you can listen for a click event within an SVG element and then draw a circle at the event mouse.origin.

by GordyD

HTML

<svg class="plot">
</svg>

CSS

.plot {
    background-color: #000;
    width: 500px;
    height: 500px;
}

.click-circle {
    fill: steelblue;
}

JavaScript

(function() {
    var svg = d3.select('svg');
    
    function getRandom(min, max) {
      return Math.floor(Math.random() * (max - min) + min);
    }
    
    function drawCircle(x, y, size) {
        console.log('Drawing circle at', x, y, size);
        svg.append("circle")
            .attr('class', 'click-circle')
            .attr("cx", x)
            .attr("cy", y)
            .attr("r", size);
    }
    
    svg.on('click', function() {
        var coords = d3.mouse(this);
        console.log(coords);
        drawCircle(coords[0], coords[1], getRandom(5,50));
    });
    
    
})();