scatter4 = {
const margin = {top: 40, right: 40, bottom: 40, left: 90};
const visWidth = 700;
const visHeight = 400;
const svg = d3.create('svg')
.attr('width', visWidth + margin.left + margin.right)
.attr('height', visHeight + margin.top + margin.bottom);
const g = svg.append('g')
.attr('transform', `translate(${margin.left},${margin.top})`)
.attr('font-family', 'sans-serif');
svg.node().drawData = function(input_state) {
const x = (d => (input_state == 'Sepal') ? d.sepal_length : d.petal_length)
const y = (d => (input_state == 'Sepal') ? d.sepal_width : d.petal_width)
const xs = d3.scaleLinear()
.domain(d3.extent(irisData, d => x(d)))
.range([0, visWidth]);
const ys = d3.scaleLinear()
.domain(d3.extent(irisData, d => y(d)))
.range([visHeight, 0]);
const xAxis = d3.axisBottom(xs);
const yAxis = d3.axisLeft(ys);
g.append('g')
.attr("class", "xAxis")
.attr('transform', `translate(0,${visHeight})`)
g.append('g')
.attr("class", "yAxis")
g.selectAll('circle')
.data(irisData)
.join(enter => enterFunc(enter),
update => updateFunc(update),
exit => exitFunc(exit))
function enterFunc(enterSel){
enterSel.append('circle')
.transition().duration(1000)
.style('opacity', 0.75)
.attr("fill", function(d) {
if (d.species == 'setosa') {return '#1b9e77'}
else if (d.species == 'virginica') {return '#d95f02'}
else {return '#7570b3'}
})
.attr('cx', (d) => xs(x(d)))
.attr('cy', (d) => ys(y(d)))
.attr("r", 15)
}
function updateFunc(toUpdate) {
g.select(".xAxis")
.call(xAxis)
.call(
g => g.selectAll('.tick line').clone()
.attr('y1', 0)
.attr('y2', -visHeight)
.attr('stroke', 'grey')
.attr('stroke-width', 1)
)
.call(g => g.select('.domain').remove())
.append('text')
.attr('x', visWidth / 2)
.attr('y', 30)
.attr('fill', 'black')
.attr('text-anchor', 'center')
.text('length');
g.select('.yAxis')
.call(yAxis)
.call(
g => g.selectAll('.tick line').clone()
.attr('x1', 0)
.attr('x2', visWidth)
.attr('stroke', 'grey')
.attr('stroke-width', 1)
)
.call(g => g.select('.domain').remove())
.append('text')
.attr('x', -35)
.attr('y', visHeight / 2)
.attr('fill', 'black')
.attr('dominant-baseline', 'center')
.attr('text-anchor', 'end')
.text('width');
toUpdate.transition().duration(1000)
.attr("fill", function(d) {
if (d.species == 'setosa') {return '#1b9e77'}
else if (d.species == 'virginica') {return '#d95f02'}
else {return '#7570b3'}
})
.attr('cx', (d) => xs(x(d)))
.attr('cy', (d) => ys(y(d)))
.attr("r", 15)
}
function exitFunc(dissapearingEls){
dissapearingEls
.transition().duration(1000)
.style("opacity", 0)
.remove()
}
}
return svg.node();
}