chart = {
const svg = d3.create("svg").attr("viewBox", [0, 0, width, height]);
svg
.append("text")
.attr("x", width / 2)
.attr("y", margin.top / 1.5)
.attr("text-anchor", "middle")
.style("font-size", "16px")
.text("Soccer Team's Position's Average Salary per Team");
svg
.append('g')
.attr('transform', `translate(0, ${height - margin.bottom})`)
.call(g =>
g
.attr("transform", `translate(0,${height - margin.bottom})`)
.call(d3.axisBottom(x_scale))
)
.selectAll('text')
.style('text-anchor', 'end')
.attr('dx', "-0.8em")
.attr("dy", "0.15em")
.attr('transform', "rotate(-45)");
svg
.append("text")
.attr('text-anchor', 'middle')
.attr('x', (width + margin.left) / 2)
.attr('y', height - 10)
.text('Player Position');
svg
.append('g')
.call(g =>
g
.attr("transform", `translate(${margin.left},0)`)
.call(d3.axisLeft(y_scale))
);
svg
.append("text")
.attr("y", margin.left / 2 - 20)
.attr("x", -(height - margin.bottom) / 2)
.style("text-anchor", "middle")
.text("Average Salary")
.attr('transform', "rotate(-90)");
const selection = svg.selectAll('.bar');
const bars = selection.data(chart_data.filter(d => d.club == team));
bars
.join(
enter =>
enter
.append('rect')
.style('opacity', 1)
.attr('x', d => x_scale(d.x))
.attr('y', height - margin.bottom)
.attr('width', x_scale.bandwidth())
.attr('fill', d => color_scale(d.x))
.style('opacity', 1),
exit => exit.remove()
)
.transition()
.attr('height', d => height - margin.bottom - y_scale(d.y))
.attr('y', d => y_scale(d.y))
.duration(2000);
return svg.node();
}