reactivechart = {
const margin = {top: 40, right: 40, bottom: 40, left: 90};
const visWidth = 500;
const visHeight = 500;
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) {
g.append('text')
.attr("class", "title")
const x = d3.scaleLinear()
.domain(d3.extent(irisData, d => d[input_state + "_length"]))
.range([0, visWidth]);
const y = d3.scaleLinear()
.domain(d3.extent(irisData, d => d[input_state + "_width"]))
.range([visHeight, 0]);
const xAxis = d3.axisBottom(x);
const yAxis = d3.axisLeft(y);
// x-axis and vertical grid lines
g.append('g')
.attr('transform', `translate(0,${visHeight})`)
.attr("class", "xAxis")
.append("text")
.attr("class", "xtext")
// y-axis and horizontal grid lines
g.append('g')
.attr("class", "yAxis")
.append("text")
.attr("class", "ytext")
g.selectAll('circle')
.data(irisData)
.join(enter => enterFunc(enter),
update => updateFunc(update),
exit => exitFunc(exit))
function enterFunc(enterSel){
enterSel.append('circle')
.transition().duration(1000)
.attr('cx', d => x(d[input_state + "_length"]))
.attr('cy', d => y(d[input_state + "_width"]))
.attr('r', 5)
.attr('fill', "red");
}
function updateFunc(toUpdate) {
g.select('.title')
.attr('x', visWidth / 2)
.attr('y', -10)
.attr('font-size', 14)
.attr('text-anchor', 'middle')
.text(input_state + ' length vs ' + input_state + ' width')
g.select('.xAxis')
.call(xAxis)
.call(
// add grid lines
g => g.selectAll('.tick line').clone()
.attr('y1', 0)
.attr('y2', -visHeight)
.attr('stroke', 'gray')
.attr('stroke-width', 1)
)
.call(g => g.select('.domain').remove())
.select('.xtext')
.attr('x', visWidth / 2)
.attr('y', 30)
.attr('fill', 'black')
.attr('text-anchor', 'center')
.text(input_state + ' length');
// y-axis and horizontal grid lines
g.select('.yAxis')
.call(yAxis)
.call(
// add grid lines
g => g.selectAll('.tick line').clone()
.attr('x1', 0)
.attr('x2', visWidth)
.attr('stroke', 'gray')
.attr('stroke-width', 1)
)
.call(g => g.select('.domain').remove())
.select('.ytext')
.attr('x', -35)
.attr('y', visHeight / 2)
.attr('fill', 'black')
.attr('dominant-baseline', 'center')
.attr('text-anchor', 'end')
.text(input_state + ' width');
toUpdate.transition().duration(1000)
.attr('cx', d => x(d[input_state + "_length"]))
.attr('cy', d => y(d[input_state + "_width"]))
}
function exitFunc(dissapearingEls){
dissapearingEls
.transition().duration(1000)
.style("opacity", 0)
.remove()
}
}
return svg.node();
}