Published
Edited
Oct 20, 2020
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
data = d3.csvParse(await FileAttachment("data@1.csv").text())
Insert cell
Insert cell
fifa1 = data
.map(data => {
return {
x: +data["Age"],
y: +data["Overall"],
color: data["Position"]
};
})
.slice(0, 100)
Insert cell
Insert cell
fifa2 = {
const data = await FileAttachment("data@1.csv").text();

return d3
.csvParse(data, ({ Name, Overall, Nationality }) => ({
x: Name,
y: +Overall,
color: Nationality
}))
.slice(0, 50);
}
Insert cell
Insert cell
Insert cell
minX1 = d3.min(fifa1, d => d.x)
Insert cell
maxX1 = d3.max(fifa1, d => d.x)
Insert cell
minY1 = d3.min(fifa1, d => d.y)
Insert cell
maxY1 = d3.max(fifa1, d => d.y)
Insert cell
uniqColor1 = _.uniqBy(fifa1, d => d.color).map(d => d.color)
Insert cell
xScale1 = d3
.scaleLinear()
.domain([minX1, maxX1 + 1])
.range([margin.left, width - margin.right])
Insert cell
yScale1 = d3
.scaleLinear()
.domain([minY1, maxY1 + 1])
.range([height - margin.bottom, margin.top])
Insert cell
colorScale1 = d3.scaleOrdinal(uniqColor1, d3.schemeCategory10)
Insert cell
Insert cell
minY2 = d3.min(fifa2, d => d.y)
Insert cell
maxY2 = d3.max(fifa2, d => d.y)
Insert cell
uniqColor2 = _.uniqBy(fifa2, d => d.color).map(d => d.color)
Insert cell
uniqX = _.uniqBy(fifa2, d => d.x).map(d => d.x)
Insert cell
xScale2 = d3
.scaleBand()
.domain(uniqX)
.range([margin.left, width - margin.right])
.padding(0.1)
Insert cell
yDomain2 = [minY2 - 1, maxY2 + 1]
Insert cell
yScale2 = d3
.scaleLinear()
.domain(yDomain2)
.range([height - margin.bottom, margin.top])
Insert cell
colorScale2 = d3.scaleOrdinal(uniqColor2, d3.schemeCategory10)
Insert cell
Insert cell
Insert cell
xAxis1 = g =>
g
.attr("transform", `translate(0,${height - margin.bottom})`)
.call(d3.axisBottom(xScale1))
Insert cell
yAxis1 = g =>
g.attr("transform", `translate(${margin.left}, 0)`).call(d3.axisLeft(yScale1))
Insert cell
colorLegend1 = () =>
swatches({
color: colorScale1
})
Insert cell
Insert cell
xAxis2 = g =>
g
.attr("transform", `translate(0,${height - margin.bottom})`)
.call(d3.axisBottom(xScale2))
.selectAll("text")
.style("text-anchor", "end")
.attr("dx", "-.8em")
.attr("dy", ".15em")
.attr("transform", "rotate(-90)")
Insert cell
yAxis2 = g =>
g.attr("transform", `translate(${margin.left}, 0)`).call(d3.axisLeft(yScale2))
Insert cell
colorLegend2 = () =>
swatches({
color: colorScale2
})
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
chart1 = {
const svg = d3
.create("svg")
.attr("viewBox", [-50, 0, width + 100, height + 50]);

svg.append("g").call(xAxis1);
svg.append("g").call(yAxis1);

svg.append("g").attr("transform", "translate(600,390)");

svg
.append("text")
.attr("class", "x label")
.attr("text-anchor", "middle")
.attr("x", width / 2)
.attr("y", height + 30)
.text("Age");
svg
.append("text")
.attr("class", "y label")
.attr("text-anchor", "end")
.attr("x", -height / 2.75)
.attr("y", 6)
.attr("transform", "rotate(-90)")
.text("Overall Score");

svg
.append("text")
.text("Age Vs Overall Score of Players in Fifa")
.style("text-anchor", "middle")
.attr("transform", `translate(${width / 2}, 40)`)
.style("font-size", "1.5em");

svg
.selectAll("circle")
.data(fifa1)
.join(enter => enter.append("circle"))
.transition()
.duration(2000)
.attr("cx", d => xScale1(d.x))
.attr("cy", d => yScale1(d.y))
.attr("fill", d => colorScale1(d.color))
.style("opacity", 0.7)
.attr("r", 8);

return svg.node();
}
Insert cell
Insert cell
Insert cell
Insert cell
chart2 = {
const svg = d3.create("svg").attr("viewBox", [-50, 0, width, height + 70]);

svg.append("g").call(yAxis2);
svg.append("g").call(xAxis2);

svg
.selectAll('rect')
.data(fifa2)
.join(enter => enter.append('rect'))
.attr('x', d => xScale2(d.x))
.attr('y', d => yScale2(minY2))
.attr('width', xScale2.bandwidth())
.attr('height', d => 0)
.transition()
.duration(1000)
.attr('fill', d => colorScale2(d.color))
.attr("y", d => yScale2(d.y))
.attr("height", d => yScale2(minY2 - 1) - yScale2(d.y));

svg
.append("text")
.attr("class", "x label")
.attr("text-anchor", "middle")
.attr("x", width / 2)
.attr("y", height + 60)
.text("Name");
svg
.append("text")
.attr("class", "y label")
.attr("text-anchor", "end")
.attr("x", -height / 2.75)
.attr("y", 6)
.attr("transform", "rotate(-90)")
.text("Overall Score");

svg
.append("text")
.text("Name Vs Overall Score")
.style("text-anchor", "middle")
.attr("transform", `translate(${width / 2}, 100)`)
.style("font-size", "1.5em");

return svg.node();
}
Insert cell
Insert cell
Insert cell
margin = ({ top: 100, right: 50, bottom: 30, left: 40 })
Insert cell
height = 750
Insert cell
import { swatches, legend } from "@d3/color-legend"
Insert cell
Insert cell
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more