Published
Edited
Jul 2, 2020
Insert cell
Insert cell
Insert cell
Insert cell
chart = {
const svg = d3.create("svg")
.attr("viewBox", [0, 0, width, height])
const tooltip = d3.select("body").append("div")
.attr("class", "svg-tooltip")
.style("position", "absolute")
.style("visibility", "hidden")
// Three function that change the tooltip when user hover / move / leave a cell
const mouseover = function(d) {
tooltip.style("visibility", "visible")
d3.selectAll("rect")
.filter((e) => {
return d.crime === e.crime })
.style("stroke", "gray")
.style("stroke-width", 2)
.style("opacity", 1)
}
const mousemove = function(d) {
tooltip.style("visibility", "visible")
.style("top", (d3.event.pageY-10)+"px").style("left", (d3.event.pageX+10)+"px")
.html(d.crime)
}
const mouseleave = function(d) {
tooltip.style("visibility", "hidden");
d3.selectAll("rect")
.filter((e) => {
return d.crime === e.crime })
.style("stroke", "none")
.style("opacity", 0.8)
}
const g = svg.append("g")
.selectAll("g")
.data(augmentedData)
.join("g")
.selectAll("rect")
.data(d => d)
.join("rect")
.attr("fill", d => colorCrimes(d.crime))
.attr("x", (d, i) => x(d.data.area))
.attr("y", d => y(d[1]))
.attr("height", d => y(d[0]) - y(d[1]))
.attr("width", x.bandwidth())
.on("mouseover", mouseover)
.on("mousemove", mousemove)
.on("mouseleave", mouseleave)

svg.append("g")
.call(xAxis)
svg.append("g")
.call(yAxis)
svg.append("text")
.attr("x", (width/2))
.attr("y", margin.top)
.attr("text-anchor", "middle")
.attr("font-size", "24px")
.attr("font-family", "sans-serif")
.text(`Top 5 Crimes Per LA Police Area, ${currentYearData[0].year_rptd}`)
svg.append("text")
.attr("x", (width/2))
.attr("y", height - margin.bottom/3)
.attr("text-anchor", "middle")
.attr("font-size", "16px")
.attr("font-family", "sans-serif")
.text("Neighborhoods")
svg.append("text")
.attr("transform", "rotate(-90)")
.attr("x", -height/2)
.attr("y", margin.left-40)
.attr("text-anchor", "middle")
.attr("font-size", "16px")
.attr("font-family", "sans-serif")
.text("Number of Incidents")
svg.selectAll(".tick")
.attr("font-size", "8px")
return svg.node()
}
Insert cell
currentYearData = top5.filter((obj) => obj.year_rptd == yearSelection)
Insert cell
Insert cell
Insert cell
sortedData = groupedData.sort((a,b) => niceReduce(b) - niceReduce(a))
Insert cell
Insert cell
Insert cell
augmentedData = stackedData.slice(0).map(
(layer) => {
layer.map((d) => {
let diff = d[1] - d[0]
for (const prop in d.data) {
if ((prop !== 'area') && (d.data[prop].total == diff)) {
d.crime = d.data[prop].crimeName
d.data.crime = d.crime
}
}
return d
})
return layer
}
)
Insert cell
niceReduce = function(a) {
return a.reduce((acc, curr) => acc + curr.total, 0)
}
Insert cell
Insert cell
Insert cell
color = d3.scaleOrdinal()
.domain(stackedData.map(d => d.key))
.range(d3.schemePurples[stackedData.length])
.unknown("#ccc")
Insert cell
uniqueCrimes = Array.from(new Set(currentYearData.map(d => d.crime)))
Insert cell
colorCrimes = d3.scaleOrdinal()
.domain(uniqueCrimes)
.range(d3.schemeSet3)
Insert cell
uniqueCrimes.map(d => colorCrimes(d))
Insert cell
x = d3.scaleBand()
.domain(formattedData.map(d => d.area))
.range([margin.left, width - margin.right])
.padding(0.1)
Insert cell
y = d3.scaleLinear()
.domain([0, 7000])
.rangeRound([height - margin.bottom, margin.top])
Insert cell
xAxis = g => g
.attr("transform", `translate(0,${height - margin.bottom})`)
.call(d3.axisBottom(x).tickSizeOuter(0))
.call(g => g.selectAll(".domain").remove())
Insert cell
yAxis = g => g
.attr("transform", `translate(${margin.left},0)`)
.call(d3.axisLeft(y).ticks(null, "s"))
.call(g => g.selectAll(".domain").remove())
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
import {swatches} from "@d3/color-legend"
Insert cell
styles = html`
<style>

.svg-tooltip {
font-family: -apple-system, system-ui, BlinkMacSystemFont, "Segoe UI", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol";
background: rgba(69,77,93,.9);
border-radius: .1rem;
color: #fff;
display: block;
font-size: 11px;
max-width: 320px;
padding: .2rem .4rem;
position: absolute;
text-overflow: ellipsis;
white-space: pre;
z-index: 300;
visibility: hidden;
}
</style>`
Insert cell

One platform to build and deploy the best data apps

Experiment and prototype by building visualizations in live JavaScript notebooks. Collaborate with your team and decide which concepts to build out.
Use Observable Framework to build data apps locally. Use data loaders to build in any language or library, including Python, SQL, and R.
Seamlessly deploy to Observable. Test before you ship, use automatic deploy-on-commit, and ensure your projects are always up-to-date.
Learn more