chart = {
var w = width,
h = height,
margin = { top: 40, right: 20, bottom: 20, left: 40 },
radius = 6;
const svg = d3.create("svg")
.attr("viewBox", [0, 0, width, height]);
var dataset = [
{ x: 100, y: 110 },
{ x: 83, y: 43 },
{ x: 92, y: 28 },
{ x: 49, y: 74 },
{ x: 51, y: 10 },
{ x: 25, y: 98 },
{ x: 77, y: 30 },
{ x: 20, y: 83 },
{ x: 11, y: 63 },
{ x: 4, y: 55 },
{ x: 0, y: 0 },
{ x: 85, y: 100 },
{ x: 60, y: 40 },
{ x: 70, y: 80 },
{ x: 10, y: 20 },
{ x: 40, y: 50 },
{ x: 25, y: 31 }
];
var xScale = d3.scaleLinear()
.domain([0, d3.max(dataset, function (d) { return d.x + 10; })])
.range([margin.left, w - margin.right]);
var yScale = d3.scaleLinear()
.domain([0, d3.max(dataset, function (d) { return d.y + 10; })])
.range([margin.top, h - margin.bottom]);
var xAxis = d3.axisTop(xScale);
var yAxis = d3.axisLeft(yScale);
var circleInitialAttrs = {
cx: xScale(0),
cy: yScale(0),
r: 1
};
var circleAttrs = {
cx: function(d) { return xScale(d.x); },
cy: function(d) { return yScale(d.y); },
r: radius
};
var xAxisGroup = svg.append("g")
.attr("class", "axis")
.attr("transform", "translate(" + [0, margin.top] + ")")
.call(xAxis);
var yAxisGroup = svg.append("g")
.attr("class", "axis")
.attr("transform", "translate(" + [margin.left, 0] + ")")
.call(yAxis);
var circles = svg.selectAll("circle")
.data(dataset)
.enter()
.append("circle")
.attr("cx", xScale(0))
.attr("cy", yScale(0))
.attr("r", 1)
.on("mouseover", handleMouseOver)
.on("mouseout", handleMouseOut);
circles.transition()
.delay(function (d, i){
return i * 100;
})
.duration(1000)
.ease(d3.easeElastic.period(0.4))
.attr("cx", (d) => xScale(d.x))
.attr("cy", (d) => yScale(d.y))
.attr("r", radius);
svg.on("click", function(event) {
let coords = d3.pointer(event);
var newData= {
x: Math.round( xScale.invert(coords[0])),
y: Math.round( yScale.invert(coords[1]))
};
dataset.push(newData);
xScale.domain([0, d3.max(dataset, function (d) { return d.x + 10; })])
yScale.domain([0, d3.max(dataset, function (d) { return d.y + 10; })])
xAxisGroup.transition().call(xAxis);
yAxisGroup.transition().call(yAxis);
let c = svg.selectAll("circle");
c.transition()
.ease(d3.easeElastic.period(0.4))
.attr("cx", (d) => xScale(d.x))
.attr("cy", (d) => yScale(d.y))
.attr("r", radius);
c = svg.selectAll("circle")
.data(dataset)
.enter()
.append("circle")
.attr("cx", xScale(0))
.attr("cy", yScale(0))
.attr("r", 1)
.on("mouseover", handleMouseOver)
.on("mouseout", handleMouseOut);
c.transition()
.duration(1000)
.ease(d3.easeElastic.period(0.4))
.attr("cx", (d) => xScale(d.x))
.attr("cy", (d) => yScale(d.y))
.attr("r", radius);
})
function handleMouseOver(event, d, i, index) {
d3.select(this)
.attr("fill", "orange")
.attr("r", radius * 2);
svg.append("text")
.attr("id", function() { return "t" + d.x + "-" + d.y + "-" + index})
.attr("x", function() { return xScale(d.x) - 30; })
.attr("y", function() { return yScale(d.y) - 15; })
.text(function() {
return `${d.x}, ${d.y}`;
});
}
function handleMouseOut(event, d, i, index) {
d3.select(this)
.attr("fill", "black")
.attr("r", radius);
d3.select("#t" + d.x + "-" + d.y + "-" + index).remove();
}
return svg.node();
}