Public
Edited
Jun 29, 2023
1 fork
Insert cell
viz = {
const viz = d3.create("div").attr("class","viz")
const svg = viz.append("svg")
.attr("width",w)
.attr("height",h)
.attr("viewBox",[0,0,w,h])
const size = 600;
const squareSize = 60;
const authorTooltip = d3.select("body")
.append("div")
.attr("class","writerTooltip")
.style("visibility","hidden")
const scale = d3.scaleLinear()
.domain([0,10])
.range([0,size]);
const swatch = swatches({
colour: d3.scaleOrdinal(["Autores","","Revistas","Lugares",
"Críticos","Artístas","Cursos"],
["#fed7af", "#00ffff", "#89065b", "#ffea47", "#e94f37",
"#6477b9", "#fbd1fb", "#ffa50a", "#ffffff"]),
swatchRadius: 10,})

const author = svg.append("g").attr("class","legend")
.attr("transform",`translate(${w/2-310},100)`)
.append(()=>swatch);
const waffle = svg.append("g")
.attr("transform",`translate(${w/2-290},200)`)
.selectAll("rect")
.data(data)
.enter()
.append("a")
.attr("xlink:href",d=>d.Work)
.attr("target","_blank")
.append("rect")
.attr("class","waffle")
.attr("x",(d,i)=>{
const n = i % 10
return scale(n)
})
.attr("y",(d,i)=>{
const n = Math.floor(i/10)
return scale(n)
})
.attr("width",squareSize-10)
.attr("height",squareSize-10)
.attr("stroke-width",1.5)
.attr("stroke",d=>colorScale(d.Type))
.style("fill",d=> {if (d.Awards!="-"){return pattern.url()}
else {return "transparent"}})
.on("mousemove",function (event,d){
d3.select(this).style("opacity",1).attr("stroke-width",3)
authorTooltip.transition()
.duration(200)
.style("visibility","visible")
authorTooltip.html("<h1>"+d.Name+"("+d.YearWritten+")"+"</h1><br/>"+d.About)
.style("transform","translateY(-55%)")
.style("left",(event.x)+10+"px")
.style("top",(event.y)-15+"px")
})
.on("mouseout",function (event,d){
d3.select(this).style("opacity",1).attr("stroke-width",2)
authorTooltip.transition().style("visibility","hidden")
})
svg.call(pattern)
return viz.node()
}
Insert cell
timeline = {
const viz = d3.create("div")
const svg = viz.append("svg").attr("id","svg2")
.attr("width",w)
.attr("height",h2)
.attr("viewBox",[0,0,w,h2])
const timeline = svg.append("g")
.attr("transform",`translate(${w/2-320},50)`)
.selectAll("timeline")
.data(timelineData)
.join("rect")
.attr("class","timeline")
.attr("x",(d,i)=>timelineScale(d.YearWritten))
.attr("y",(d,i)=>i*140)
.attr("width",50)
.attr("height",50)
.attr("stroke-width",1.5)
.attr("stroke",d=>colorScale(d.Type))
.style("fill",pattern.url())
return viz.node()
}
Insert cell
writerTimeline = {
timeline
const annotations = timelineData.map((d,i)=>{
return{
x:(timelineScale(d.YearWritten)<=250)?250:350,
y:i*140+25,
dy:0,
dx: timelineScale(d.YearWritten)<=250? -70:70,
color:"white",
note:{
label:d.Awards,
title:d.Name +" "+"("+ d.YearWritten +")"
}
}})
const type = d3.annotationCustomType(d3.annotationLabel,
{"className":"custom",
"connector":{"end":"dot"},
"note":{"align":"middle",
"orientation":"leftRight",
"lineType":"vertical"}})
const makeAnnotations = d3.annotation()
.notePadding(10)
.type(type)
.textWrap(250)
.annotations(annotations)
d3.select("#svg2").append("g")
.attr("transform",`translate(${w/2-320},50)`)
.call(makeAnnotations)
}
Insert cell
timelineScale = d3.scaleOrdinal()
.domain(timelineData.map(d=>d.YearWritten))
.range([250,300])
Insert cell
timelineData = data.filter(d=>d.Awards!="-")
Insert cell
authorLegend = d3.scaleOrdinal()
.domain(["Autores","Lugares","Críticos","Revistas","Cursos","Artístas","poet","publisher","scholar","sociologist"])
.range(["#393e41","cyan","#89065b","#ffea47","#e94f37","#cfc9c9","#6477b9","#ffa50a","white","#0acdff"]);
Insert cell
colorScale = d3.scaleOrdinal()
.domain(["Autores","Lugares","Críticos","Revistas",
"Cursos","Artístas","publisher","scholar","sociologist"])
.range(color)
Insert cell
color = ["#fed7af",
"#00ffff",
"#89065b",
"#ffea47",
"#e94f37",
"#6477b9",
"#fbd1fb",
"#ffa50a",
"#ffffff"]
Insert cell
pattern = textures.lines()
.size(7)
.strokeWidth(0.9)
.background("transparent")
.stroke("#6b3395")
Insert cell
h2 = 6200
Insert cell
h = 900;
Insert cell
w = width;
Insert cell
prisma@2.csv
Type Table, then Shift-Enter. Ctrl-space for more options.

Insert cell
data = FileAttachment("prisma@2.csv").csv({typed:true})
Insert cell
textures = require('textures@1.2.0/dist/textures.js')
Insert cell
import {swatches} from "@bayre/svg-swatches"
Insert cell
d3 = require('d3@6','d3-svg-annotation')
Insert cell
html`<style>
.vizTitle{
font-size:40px;
font-family:times;
}


svg {
background-color: rgb(189, 161, 161);
margin:auto;
display:flex;
}

.writerTooltip{
text-align: center;
max-width: 300px;
position: fixed;
background-color: white;
padding: 5px;
pointer-events: none;
/*left:50%;
transform: translate(-50%);*/
border:solid;
}

.annotation-note-bg{
fill:white;
fill-opacity: 0.2;
}


</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