Published
Edited
Jun 24, 2019
Importers
Insert cell
md`# BarChart-Names-Exercise`
Insert cell
Insert cell
unisexNamesChartAlt = (unisexSelector) => {
const height = 700
const margin = ({top: 100, right: 10, bottom: 100, left: 40})
const colors = {"males": "#63a2cd", "females": "#e293ac"}
const data = processNamesData(unisexSelector)
const svg = d3.select(DOM.svg(width, height))
const columns = Object.keys(unisexNames[0]).slice(2)
const series = d3.stack().keys(columns)(data)
const yMax = d3.max(series, d => d3.max(d, d => d[1]))
// ====== Create chart Title ======
svg.append("text")
.attr("class", "chart-title")
.attr("x", (width / 2) - (margin.left /2) )
.attr("y", margin.right * 2 )
.style("font-size", "20pt")
.style("font-family", "Segoe UI, Tahoma, Geneva, Verdana, sans-serif")
.text("Top Unisex Names")
const x = d3.scaleBand()
.domain(data.map(d => d.name))
.range([margin.left, width - margin.right])
.padding(0.1)
const y = d3.scaleLinear()
.domain([0, yMax])
.rangeRound([height - margin.bottom, margin.top])

const xAxis = g => g
.attr("transform", `translate(0,${height - margin.bottom})`)
.call(d3.axisBottom(x).tickSizeOuter(0))
.call(g => g.selectAll(".domain").remove())
const yAxis = g => g
.attr("transform", `translate(${margin.left},0)`)
.call(d3.axisLeft(y).ticks(null, "s"))
.call(g => g.selectAll(".domain").remove())
// ALTERNATIVE: JOIN
svg.append("g")
.selectAll("g")
.data(series)
.join("g")
.attr("fill", d => colors[d.key])
.selectAll("rect")
.data(d => d)
.join("rect")
.attr("x", (d, i) => x(d.data.name))
.attr("y", d => y(d[1]))
.attr("height", d => y(d[0]) - y(d[1]))
.attr("width", x.bandwidth());

svg.append("g")
.attr("class", "xAxis")
.call(xAxis);

svg.append("g")
.attr("class", "yAxis")
.call(yAxis);

return svg.node()
}
Insert cell
unisexNamesChart = (unisexSelector) =>{ const height = 700
const margin = ({top: 100, right: 10, bottom: 100, left: 40})
const colors = {"males": "#63a2cd", "females": "#e293ac"}
const data = processNamesData(unisexSelector)
const svg = d3.select(DOM.svg(width, height))
const columns = Object.keys(unisexNames[0]).slice(2)
const series = d3.stack().keys(columns)(data)
const yMax = d3.max(series, d => d3.max(d, d => d[1]))
// ====== Create chart Title ======
svg.append("text")
.attr("class", "chart-title")
.attr("x", (width / 2) - (margin.left /2) )
.attr("y", margin.right * 2 )
.style("font-size", "20pt")
.style("font-family", "Segoe UI, Tahoma, Geneva, Verdana, sans-serif")
.text("Top Unisex Names")
const x = d3.scaleBand()
.domain(data.map(d => d.name))
.range([margin.left, width - margin.right])
.padding(0.1)
const y = d3.scaleLinear()
.domain([0, yMax])
.rangeRound([height - margin.bottom, margin.top])

const xAxis = g => g
.attr("transform", `translate(0,${height - margin.bottom})`)
.call(d3.axisBottom(x).tickSizeOuter(0))
.call(g => g.selectAll(".domain").remove())
const yAxis = g => g
.attr("transform", `translate(${margin.left},0)`)
.call(d3.axisLeft(y).ticks(null, "s"))
.call(g => g.selectAll(".domain").remove())
// Data Format:
// seriesData = [seriesDataItem[], key: males | females, index: number]
// seriesDataItem = [yEnd: number, yStart: number, data: {name: string, rank: number, males: number, females: number}]
svg.append("g")
.selectAll("g")
.data(series)
.enter().append("g")
.attr("fill", (d, i) => colors[d.key])
.selectAll("rect")
.data(d => d)
.enter()
.append("rect")
.attr("x", (d, i) => x(d.data.name))
.attr("y", d => y(d[1]))
.attr("height", d => y(d[0]) - y(d[1]))
.attr("width", x.bandwidth())
svg.append("g")
.attr("class", "xAxis")
.call(xAxis);

svg.append("g")
.attr("class", "yAxis")
.call(yAxis);

return svg.node()
}
Insert cell
processNamesData = (selector) => {
const startIndex = selector === "Most Popular" ? 0 : (unisexNames.length -1) - 10
const endIndex = selector === "Most Popular" ? 10 : (unisexNames.length -1)
const data = unisexNames.slice(startIndex, endIndex)
return data
}
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