{
const margin = {top: 10, right: 10, bottom: 50, left: 100};
const visWidth = 510 - margin.left - margin.right;
const visHeight = 460 - margin.top - margin.bottom;
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})`);
const x = d3.scaleLinear()
.domain(d3.extent(cars, d => d.horsepower)).nice()
.range([0, visWidth]);
const y = d3.scaleLinear()
.domain(d3.extent(cars, d => d.weight)).nice()
.range([visHeight, 0]);
const xAxis = d3.axisBottom(x);
g.append("g")
.attr('transform', `translate(0, ${visHeight})`)
.call(xAxis)
.call(g => g.selectAll('.domain').remove())
.append('text')
.attr('x', visWidth / 2)
.attr('y', 40)
.attr('fill', 'black')
.attr('text-anchor', 'middle')
.text('horsepower');
const yAxis = d3.axisLeft(y);
g.append('g')
.call(yAxis)
.call(g => g.selectAll('.domain').remove())
.append('text')
.attr('x', -40)
.attr('y', visHeight / 2)
.attr('fill', 'black')
.attr('dominant-baseline', 'middle')
.text('weight (lbs)');
const grid = g.append('g')
.attr('class', 'grid');
grid.append('g')
.selectAll('line')
.data(y.ticks())
.join('line')
.attr('stroke', lightgray)
.attr('x1', 0)
.attr('x2', visWidth)
.attr('y1', d => 0.5 + y(d))
.attr('y2', d => 0.5 + y(d));
grid.append('g')
.selectAll('line')
.data(x.ticks())
.join('line')
.attr('stroke', lightgray)
.attr('x1', d => 0.5 + x(d))
.attr('x2', d => 0.5 + x(d))
.attr('y1', d => 0)
.attr('y2', d => visHeight);
g.selectAll('circle')
.data(cars)
.join('circle')
.attr('cx', d => x(d.horsepower))
.attr('cy', d => y(d.weight))
.attr('fill', d => color6(d.origin))
.attr('opacity', 1)
.attr('r', 3);
return svg.node();
}