Public
Edited
May 12
1 fork
Insert cell
Insert cell
Insert cell
countries = d3.json("https://gist.githubusercontent.com/emanueles/591172235e5c05c814752b7536155617/raw/8acfbc3b59204a9b7495a9a779012fadca811dce/countries2000.json")
Insert cell
Insert cell
comparePorLifeExpect = (a, b) => b.life_expect - a.life_expect
Insert cell
barras = {
const width = 600
const height = 1890

countries.sort(comparePorLifeExpect)
const svg = d3.select(DOM.svg(width, height))
svg.selectAll('rect')
.data(countries)
.enter()
.append('rect')
.attr('x', 10)
.attr('y', (d, i) => i * 30 + 20)
.attr('height', 20)
.attr('width', d => d.life_expect*3)
.attr('fill', "DarkCyan")
svg.selectAll('text') // Seleciona todos os retângulos filhos de svg (nesse caso, uma seleção vazia)
.data(countries) // Vincula arrayOfNumbers com DOM elementos <rect/>, produzindo seleções .enter(),.exit()
.enter()// Retorna a parte dos dados que é nova ("entering") e ainda não está vinculada aos elementos DOM
.append('text') // Para cada item de dado, adiciona um <rect /> ao svg selecionado
.attr('x', d => d.life_expect*3 + 15) // Configura a posição x de acordo com o índice do vetor
.attr('y', (d, i) => i * 30 + 20 + 15) // Configura a posição y de cada barra
.text((d) => `${d.life_expect}: ${d.country}`)
.style("font-family", "Arial")
.style("font-size", "14px")
return svg.node()
}
Insert cell
Insert cell
margin = {
//Definimos o objeto margin
let margin = {top: 40, right: 100, bottom: 40, left: 40};
return margin;
}
Insert cell
height = 600 - margin.top - margin.bottom
Insert cell
width = 800 - margin.left - margin.right
Insert cell
yScale = d3.scaleLinear()
.domain([40, d3.max(countries, d => d.life_expect)])
.range([height, 0])
Insert cell
xScale = d3.scaleLinear()
.domain([0, d3.max(countries, d => d.fertility)])
.range([0, width])
Insert cell
scatter = {
const outersvg = d3.select(DOM.svg(width + margin.left + margin.right,
height + margin.top + margin.bottom))
const svg = outersvg.append('g')
.attr("transform", "translate(" + margin.left + "," + margin.top + ")")
svg.selectAll('circle')
.data(countries)
.enter()
.append('circle')
.attr('cx', d => xScale(d.fertility))
.attr('cy', d => yScale(d.life_expect))
.attr('r', 5)

svg.selectAll('text')
.data(countries)
.enter()
.append('text')
.attr('x', d => xScale(d.fertility))
.attr('y', d => yScale(d.life_expect))
.attr('font-size', '11px')
.attr('font-family', 'sans-serif')
.attr('fill', 'red')
.text(d => d.country)

svg.append("g")
.attr("transform", "translate(0, " + height + ")")
.call(xAxis)

svg.append("g")
.call(yAxis)

svg.append("text")
.attr("transform", "translate(" + (width/2) + "," + (height +
margin.bottom) + ")")
.style("text-anchor", "middle")
.attr("font-family", "sans-serif")
.attr("font-size", "12px")
.text("Taxa de Fertilidade");

svg.append("text")
.attr("transform", "rotate(-90)")
.attr("y", 0 - margin.left)
.attr("x",0 - (height / 2))
.attr("dy", "1em")
.style("text-anchor", "middle")
.attr("font-family", "sans-serif")
.attr("font-size", "12px")
.text("Expectativa de Vida");
return outersvg.node()
}
Insert cell
xAxis = d3.axisBottom()
.scale(xScale)
.ticks(5)
Insert cell
yAxis = d3.axisLeft()
.scale(yScale)
.ticks(5)

Insert cell
Escreva sua resposta para as perguntas do exercício 2 aqui.
Insert cell
Insert cell
d3 = require('d3')
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