score:9
If you are familiar with JQuery then D3 should have a friendly feel to it, as it shares a similar API. Specifically in regards to .on(action, fn)
syntax for attaching an event listener to a DOM selection.
If you check out the jsFiddle I have created which implements a very basic implementation of what you are after then you can see this in motion in 21 lines of JS.
(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));
});
})();
The most important aspects of this snippet are on lines 18-20 (.on(...)
and d3.mouse(this)
). The on('click',..)
event is attached to the svg element. When a click occurs d3.mouse
is called with the current scope as its argument. It then returns an array with x and y coords of the mouse event. This information is then passed to drawCircle
, along with a random radius to draw a circle on the current SVG canvas.
I advise you to take the jsFiddle and have a play!
Source: stackoverflow.com
Related Query
- D3: Simple Interaction - Circle with a click
- On button click open a new window and draw a circle with D3
- Generate a shadow on circle click with D3.js
- On button click open a new window and draw a large circle with D3
- How can I bring a circle to the front with d3?
- Draw simple timeline with D3
- drawing circle with radius specified in meters on a map
- Draw a D3 circle with gradient colours
- Calling two functions on same click event with d3.js
- Circle clip and projection with D3 orthographic
- How to change a circle into a square with d3.js
- Make simple bar chart using C3 with separate columns on the x axis
- Half filled circle with d3.js
- Possible to use a circle pack layout in d3.js with fixed circle sizes?
- d3 simple linear chart with jquery ui range slider
- Angular 4 + D3v4 : dragging circle not working correctly in conjunction with zooming
- How to add a simple arc with D3
- Zoomable Circle Packing with Automatic Text Sizing in D3.js
- D3 Circle Pack Layout with a horizontal arrangement
- Circle points are not aligned with area shape
- Draw D3 Simple Line chart With an Array
- Prevent mouseout action after click on an element with D3.js
- Ho do I capture ctrl + click on MacOS with d3js
- Initialize element and start dragging with just one click
- Drawing simple lines with d3js
- Circle approximation that is a regular polygon with N corners
- Circle packing with static size
- d3.js, click action to another URL encoding with array of variables of sunburst
- Wrap text in a circle with D3 v4 and D3plus
- Error in A Simple D3 Line chart with Legend and Tooltips for D3v 3.5.13
More Query from same tag
- Data is not showing on Radial Dendrogram
- SVG g hides when parent g is set to a height
- A d3.select... equivalent to jQuery.children()
- D3: Convert scale level of the time scale to time period, like: day, week or month?
- zoom/pan with d3.js and svg not working
- d3 - Shift color scheme to pastels
- JavaScript , D3 bar graph error using CSV
- d3 drag blurring/jittery handle
- Creating dynamic barchart with D3js and Angularjs
- How to select nested data elements in d3
- D3 does not maintain object constancy even when there is a key function
- Efficient way of Using D3 to create hierarchical content from a CSV file
- forceNetwork not displaying, no code errors returned
- Nodes of a radial force layout only take up a quarter of the circle
- How to load data from Django to a variable for a D3 graph
- Gauge charts with sub value indicators and label
- dc.js with crossfilter and d3, how to load multiple csv files at once, without knowing how many files the user will upload
- Preserve a Transform in an SVG element
- d3js move a node without dragging
- dimple.js/d3.js Toggle between Normal and log scale
- javascript grouping linked nodes
- GeoStream is not assignable to parameter of type 'GeoRawProjection' in d3 API version 4
- Wheel event is not fired on a SVG group element in Safari
- How to bundle d3 v4 using webpack
- Amending D3 circle animation
- dc.js reductio helper function, how to get output in object format
- D3 Bubble Example: What does bubble.nodes() do?
- How to overwrite the array inside a function?
- How to send this D3.js element to another element's on-click function in this recursive Javascript
- D3.js - Width of xAxis for a bar chart too short