Published
Edited
Dec 16, 2020
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
data = d3.csvParse(await FileAttachment("Pokemon.csv").text())
Insert cell
pokemon1 = data
.map(data => {
return {
x: +data["Attack"],
y: +data["Speed"],
color: data["Type 1"]
};
})
.slice(0, 100)
Insert cell
pokemon2 = data
.map(data => {
return {
x: data["Name"],
y: +data["Defense"],
color: data["Type 1"]
};
})
.slice(0, 50)
Insert cell
Insert cell
margin = ({ top: 100, right: 40, bottom: 50, left: 50})
Insert cell
height = 750
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
md `CHART 2`
Insert cell
uniqX = _.uniqBy(pokemon2.map(d => d.x))
Insert cell
y_domain2 = [d3.min(pokemon2, d => +d.y), d3.max(pokemon2, d => +d.y)]
Insert cell
color_domain2 = _.uniqBy(pokemon2.map(d => d.color))
Insert cell
xScale2 = d3
.scaleBand()
.domain(uniqX)
.range([margin.left, width - margin.right])
.padding(0.1)
Insert cell
yScale2 = d3
.scaleLinear()
.domain(y_domain2)
.range([height - margin.bottom, margin.top])
Insert cell
colorScale2 = d3.scaleOrdinal(color_domain2, d3.schemeCategory10)
Insert cell
Insert cell
swatches({
color: d3.scaleOrdinal(color_domain2, d3.schemeCategory10)
})
Insert cell
y_scale = {
const svg = d3.create("svg")
.attr("width", width)
.attr("height", height)
const g = svg.append('g')
.call(d3.axisRight()
.scale(yScale1))
return svg.node();
}
Insert cell
x_scale = {
const svg = d3.create("svg")
.attr("width", width)
.attr("height", height)
.attr('margin', margin)
.attr("transform", "translate(0, 380)")
const g = svg.append('g')
.call(d3.axisBottom()
.scale(xScale1))
return svg.node();
}
Insert cell
{
const svg = d3.create("svg")
.attr("viewBox", [0, 0, width, height]);

svg.append("g")
.attr("transform", `translate(0,${height - margin.bottom})`)
.call(d3.axisBottom(xScale1));

svg.append("g")
.attr("transform", `translate(${margin.left},0)`)
.call(d3.axisLeft(yScale1));

return svg.node();
}
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
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
circle_chart = {
const svg = d3
.create("svg")
.attr("width", width)
.attr("height", height);

svg
.selectAll("circle")
.data(pokemon1)
.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
x_chart = {
const svg = d3
.create("svg")
.attr("width", width)
.attr("height", height);

svg
.selectAll("circle")
.data(pokemon1)
.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.5)
.attr("r", 8)
.attr("transform", "translate(350,0)");

return svg.node();
}
Insert cell
y_chart = {
const svg = d3
.create("svg")
.attr("width", width)
.attr("height", height);

svg
.selectAll("circle")
.data(pokemon1)
.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.5)
.attr("r", 8)
.attr("transform", "translate(0,300)");

return svg.node();
}
Insert cell
chart2_mark = {
const svg = d3.create("svg").attr("width", width).attr("height", height + 150);

svg
.selectAll('rect')
.data(pokemon2)
.join(enter => enter.append('rect'))
.attr('x', d => xScale2(d.x))
.attr('y', d => yScale2(15))
.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(20) - yScale2(d.y));
return svg.node()
}
Insert cell
Insert cell
chart1 = {
const svg = d3
.create("svg")
.attr("width", width + 50)
.attr("height", height + 100);

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("Attack Power");
svg
.append("text")
.attr("class", "y label")
.attr("text-anchor", "end")
.attr("x", -height / 2.75)
.attr("y", 6)
.attr("transform", "rotate(-90)")
.text("Speed");

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

svg
.selectAll("circle")
.data(pokemon1)
.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);
svg
.append("g")
.attr("transform", "translate(100,30)")
.append(() =>
legend({
color: color,
width: width - 2 * (margin.left + margin.right),
title: "Type of Pokemon"
})
);

return svg.node();
}
Insert cell
chart2 = {
const svg = d3.create("svg").attr("width", width).attr("height", height + 150);

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

svg
.selectAll('rect')
.data(pokemon2)
.join(enter => enter.append('rect'))
.attr('x', d => xScale2(d.x))
.attr('y', d => yScale2(15))
.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(20) - 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("Defense");

svg
.append("text")
.text("Name Vs Overall Score")
.style("text-anchor", "middle")
.attr("transform", `translate(${width / 2}, 100)`)
.append(() =>
legend({
color: colorScale2,
width: width - 2 * (margin.left + margin.right),
title: "Type"
})
);
return svg.node();
}
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
import { swatches, legend } from "@d3/color-legend"
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