Public
Edited
Feb 14, 2023
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
barchartData = OxCGRTdata.filter(d => dateCreator(d.Date).getTime() === thirtyDaysAgo.getTime() && d.RegionName === "")
.map(d => ({
cname : d.CountryName,
code : d.CountryCode,
cases : +d.ConfirmedCases,
deaths : +d.ConfirmedDeaths
})
)
.filter(d => d.deaths > minimumDeaths )
.sort((a,b) => b.cases - a.cases);
Insert cell
Insert cell
Insert cell
Insert cell
barchartData.map(d => d.code);
Insert cell
xScale('FRA');
Insert cell
xScale.bandwidth()
Insert cell
xAxis = g => g
.attr("transform", `translate(0,${height - margin.bottom})`)
.call(d3.axisBottom(xScale).tickSizeOuter(0))
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
<button onclick=${() => mutable nticks++}>Click me</button>
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
barchartwithhover = {
const svg = d3.create("svg")
.attr("width", width)
.attr("height", height)
.attr("viewBox",[0, 0, width, height]);
svg.append("g")
.call(xAxis);

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

svg.selectAll('rect')
.data(barchartData)
.join('rect')
.attr('x', d => xScale(d.code))
.attr('y', d => yScale(d.cases))
.attr('width', xScale.bandwidth())
.attr('height', d => yScale(0) - yScale(d.cases))
.attr('fill', 'purple')
.on("mouseover", function(d) {
d3.select(this).attr("fill", "steelblue");})
.on("mouseout", function(d) {
d3.select(this).attr("fill", "purple");});
// Specific to Observable
return svg.node();
}
Insert cell
Insert cell
Insert cell
Insert cell
barchartwithtooltip = {
const svg = d3.create("svg")
.attr("width", width)
.attr("height", height)
.attr("viewBox",[0, 0, width, height]);
svg.append("g")
.call(xAxis);

svg.append("g")
.call(yAxis);
const tooltip = d3.select('body')
.append('div')
.attr('id', 'barchart-tooltip')
.style('position', 'absolute')
.style('z-index', '1')
.style('visibility', 'hidden')
.style('padding', '10px')
.style('background', 'rgba(0,0,0,0.6)')
.style('border-radius', '4px')
.style('color', '#fff');
svg.selectAll('rect')
.data(barchartData)
.join('rect')
.attr('x', d => xScale(d.code))
.attr('y', d => yScale(d.cases))
.attr('width', xScale.bandwidth())
.attr('height', d => yScale(0) - yScale(d.cases))
.attr('fill', 'purple')
.on("mouseover", function(e,d) {
const tooltipWidth = tooltip.node().offsetWidth;
const tooltipHeight = tooltip.node().offsetHeight;
tooltip
.style("left", e.pageX - tooltipWidth/2 +'px')
.style("top", e.pageY-tooltipHeight - 10+'px')
.style('visibility', 'visible')
.html(`<b>Country</b>: ${d.cname} <br/><b>Cases</b>: ${d.cases}`);
d3.select(this).attr("fill", "steelblue");})
.on('mousemove', function(e) {
const tooltipWidth = tooltip.node().offsetWidth;
const tooltipHeight = tooltip.node().offsetHeight;
tooltip
.style("left", e.pageX - tooltipWidth/2 +'px')
.style("top", e.pageY-tooltipHeight - 10+'px')
})
.on("mouseout", function(e,d) {
tooltip
.style('visibility', 'hidden')
d3.select(this).attr("fill", "purple");});
// Specific to Observable
return svg.node();
}
Insert cell
Insert cell
number = 12345.678
Insert cell
number.toLocaleString('en-US', {minimumFractionDigits: 2, maximumFractionDigits: 2});
Insert cell
Insert cell
Insert cell
Insert cell
barchartwithzoom = {
const svg = d3.create("svg")
.attr("width", width)
.attr("height", height)
.attr("viewBox",[0, 0, width, height]);
// Clipping
svg.append('defs')
.append('clipPath')
.attr('id', 'clip')
.append('rect')
.attr('x', margin.left)
.attr('y', margin.top)
.attr('width', width-margin.right)
.attr('height', height-margin.top);

const main = svg.append('g')
.attr('class', 'main')
.attr('clip-path', 'url(#clip)');

const tooltip = d3.select('body')
.append('div')
.attr('id', 'barchart-tooltip')
.style('position', 'absolute')
.style('z-index', '1')
.style('visibility', 'hidden')
.style('padding', '10px')
.style('background', 'rgba(0,0,0,0.6)')
.style('border-radius', '4px')
.style('color', '#fff');
main.selectAll('rect')
.data(barchartData)
.join('rect')
.attr('x', d => xScale(d.code))
.attr('y', d => yScale(d.cases))
.attr('width', xScale.bandwidth())
.attr('height', d => yScale(0) - yScale(d.cases))
.attr('fill', 'purple')
.on("mouseover", function(e,d,i) {
let tooltipWidth = tooltip.node().offsetWidth;
let tooltipHeight = tooltip.node().offsetHeight;
tooltip
.style("left", e.pageX - tooltipWidth/2 +'px')
.style("top", e.pageY-tooltipHeight - 10+'px')
.style('visibility', 'visible')
.html(`<b>Country</b>: ${d.cname} <br/><b>Cases</b>: ${d.cases}`);
d3.select(this).attr("fill", "steelblue");})
.on('mousemove', function (e) {
let tooltipWidth = tooltip.node().offsetWidth;
let tooltipHeight = tooltip.node().offsetHeight;
tooltip
.style("left", e.pageX - tooltipWidth/2 +'px')
.style("top", e.pageY-tooltipHeight - 10+'px')
})
.on("mouseout", function(e,d) {
tooltip
.style('visibility', 'hidden')
d3.select(this).attr("fill", "purple");});

const gx = main.append("g").call(xAxis);

const gy = svg.append("g").call(yAxis);
const extent = [[margin.left, margin.top], [width - margin.right, height - margin.top]];

svg.call(d3.zoom()
.scaleExtent([1, 8])
.translateExtent(extent)
.extent(extent)
.on("zoom", zoomed));

function zoomed(event) {
xScale.range([event.transform.applyX(margin.left), event.transform.applyX(width - margin.right)]);
main.selectAll('rect').attr("x", d => xScale(d.code)).attr("width", xScale.bandwidth());
gx.call(xAxis);
}

// Specific to Observable
return svg.node();
}
Insert cell
Insert cell
Insert cell
barchartwithbrush = {
const svg = d3.create("svg")
.attr("width", width)
.attr("height", height+ minibarHeight)
.attr("viewBox",[0, 0, width, height+ minibarHeight]);
// Clipping
svg.append('defs')
.append('clipPath')
.attr('id', 'clip')
.append('rect')
.attr('x', margin.left)
.attr('y', margin.top)
.attr('width', width)
.attr('height', height);

const main = svg.append('g')
.attr('class', 'main')
.attr('clip-path', 'url(#clip)');
const tooltip = d3.select('body')
.append('div')
.attr('id', 'barchart-tooltip')
.style('position', 'absolute')
.style('z-index', '1')
.style('visibility', 'hidden')
.style('padding', '10px')
.style('background', 'rgba(0,0,0,0.6)')
.style('border-radius', '4px')
.style('color', '#fff');
const mainBarsGroup = main.append("g")
.attr("class", "mainBarsGroup");

const miniGroup = svg.append("g")
.attr("class", "miniGroup")
.attr("transform", "translate(0," + height + ")");

mainBarsGroup.selectAll('rect')
.data(barchartData2)
.join('rect')
.attr('x', d => xScaleBrush(d.code))
.attr('y', d => yScale(d.cases))
.attr('width', xScaleBrush.bandwidth())
.attr('height', d => yScale(0) - yScale(d.cases))
.attr('fill', 'purple')
.on("mouseover", function(e,d,i) {
let tooltipWidth = tooltip.node().offsetWidth;
let tooltipHeight = tooltip.node().offsetHeight;
tooltip
.style("left", e.pageX - tooltipWidth/2 +'px')
.style("top", e.pageY-tooltipHeight - 10+'px')
.style('visibility', 'visible')
.html(`<b>Country</b>: ${d.cname} <br/><b>Cases</b>: ${d.cases}`);
d3.select(this).attr("fill", "steelblue");})
.on('mousemove', function (e) {
let tooltipWidth = tooltip.node().offsetWidth;
let tooltipHeight = tooltip.node().offsetHeight;
tooltip
.style("left", e.pageX - tooltipWidth/2 +'px')
.style("top", e.pageY-tooltipHeight - 10+'px')
})
.on("mouseout", function(e,d) {
tooltip
.style('visibility', 'hidden')
d3.select(this).attr("fill", "purple");});

const gx = mainBarsGroup.append("g").call(mainXAxis);

const gy = svg.append("g").attr("fill", "white").call(yAxis);
miniGroup.selectAll('rect')
.data(barchartData2)
.join('rect')
.attr('x', d => miniX(d.code))
.attr('y', d => miniY(d.cases))
.attr('width', miniX.bandwidth())
.attr('height', d => miniY(0) - miniY(d.cases))
.attr('fill', 'steelblue');
const brush = d3.brushX()
.extent([[minimargin.left, height + minimargin.top],
[width - minimargin.right, height + minibarHeight- minimargin.bottom]])
.on("start brush end", brushed);

svg.append("g")
.call(brush)
.call(brush.move, [barchartData2[0].code,barchartData2[15].code].map(miniX)); //Initially the first 15 are selected

function brushed({selection}) {
if (selection === null) {
miniGroup.attr("stroke", null);
} else {
const originalRange = mainXZoom.range();
mainXZoom.domain(selection);
xScaleBrush.range([mainXZoom(originalRange[0]), mainXZoom(originalRange[1])]);
mainBarsGroup.selectAll('rect')
.data(barchartData2)
.join('rect')
.attr('x', d => xScaleBrush(d.code))
.attr('y', d => yScale(d.cases))
.attr('width', xScaleBrush.bandwidth())
.attr('height', d => yScale(0) - yScale(d.cases))
.attr('fill', 'purple')
gx.call(mainXAxis);
}
}

// Specific to Observable
return svg.node();
}
Insert cell
barchartData2 = OxCGRTdata.filter(d => dateCreator(d.Date).getTime() === thirtyDaysAgo.getTime() && d.RegionName === "")
.map(d => {return {
cname : d.CountryName,
code : d.CountryCode,
cases : +d.ConfirmedCases,
deaths : +d.ConfirmedDeaths
};}
)
.filter(d => d.deaths > 10000 )
.sort((a,b) => b.cases - a.cases);
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
barchartwithzoomtransition = {
const svg = d3.create("svg")
.attr("width", width)
.attr("height", height)
.attr("viewBox",[0, 0, width, height]);
// Clipping
svg.append('defs')
.append('clipPath')
.attr('id', 'clip')
.append('rect')
.attr('x', margin.left)
.attr('y', margin.top)
.attr('width', width)
.attr('height', height);

const main = svg.append('g')
.attr('class', 'main')
.attr('clip-path', 'url(#clip)');
const tooltip = d3.select('body')
.append('div')
.attr('id', 'barchart-tooltip')
.style('position', 'absolute')
.style('z-index', '1')
.style('visibility', 'hidden')
.style('padding', '10px')
.style('background', 'rgba(0,0,0,0.6)')
.style('border-radius', '4px')
.style('color', '#fff');
main.selectAll('rect')
.data(barchartData)
.join('rect')
.attr('x', (d, i) => xScale(i))
.attr('y', d => yScale(d.cases))
.attr('width', xScale.bandwidth())
.attr('height', d => yScale(0) - yScale(d.cases))
.attr('fill', 'purple')
.on("mouseover", function(e,d,i) {
let tooltipWidth = tooltip.node().offsetWidth;
let tooltipHeight = tooltip.node().offsetHeight;
tooltip
.style("left", e.pageX - tooltipWidth/2 +'px')
.style("top", e.pageY-tooltipHeight - 10+'px')
.style('visibility', 'visible')
.html(`<b>Country</b>: ${d.cname} <br/><b>Cases</b>: ${d.cases}`);
d3.select(this)
.attr("fill", "CornflowerBlue")
.transition()
.duration(400)
.attr("fill", "steelblue");})
.on('mousemove', function (e) {
let tooltipWidth = tooltip.node().offsetWidth;
let tooltipHeight = tooltip.node().offsetHeight;
tooltip
.style("left", e.pageX - tooltipWidth/2 +'px')
.style("top", e.pageY-tooltipHeight - 10+'px')
})
.on("mouseout", function(e,d) {
tooltip
.style('visibility', 'hidden')
d3.select(this).transition()
.duration(400)
.attr("fill", "purple");});

const gx = main.append("g").call(xAxis);

const gy = svg.append("g").call(yAxis);
const extent = [[margin.left, margin.top], [width - margin.right, height - margin.top]];

svg.call(d3.zoom()
.scaleExtent([0.5, 8])
.translateExtent(extent)
.extent(extent)
.on("zoom", zoomed));

function zoomed(event) {
//xScale.range([margin.left, width - margin.right].map(d => event.transform.applyX(d)));
xScale.range([event.transform.applyX(margin.left), event.transform.applyX(width - margin.right)]);
main.selectAll('rect')
.attr("x", (d, i) => xScale(i))
.attr("width", xScale.bandwidth());
gx.call(xAxis);
}

// Specific to Observable
return svg.node();
}
Insert cell
Insert cell
Insert cell
Insert cell
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