D3 trigger click

by jarandaf

HTML

<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.16/d3.min.js"></script>

JavaScript

'use strict';

const svg = d3.select('body')
  .append('svg')
  .attr('height', 500)
  .attr('width', 500)
  .style('background-color', 'black');
  
const data = d3.range(5);

const circles = svg.selectAll('circle').data(data);

circles.enter()
  .append('circle')
  .attr('cx', () => Math.random()*500)
  .attr('cy', () => Math.random()*500)
  .attr('r', 5)
  .style('fill', 'orange')
  .on('click', function (d) { console.log('circle ' + d); });
  
// Trigger click on first selected circle
const circle = d3.select('circle');
circle.on('click').call(circle.node(), circle.datum());