Published
Edited
Dec 6, 2021
Insert cell
# Change in online shopping behaviors
Insert cell
yMargin = 40
Insert cell
height = 400
Insert cell
xMargin = 120
Insert cell
d3 = require("d3@7")
Insert cell
TimeSpentFile = FileAttachment("time_spent@2.csv")
Insert cell
TimeSpentText = TimeSpentFile.text()
Insert cell
TimeSpentData = d3
.csvParse(TimeSpentText, d3.autoType)
.slice(1)
.map((d) => ({ ...d, difference: d[2020] - d[2019] }))
Insert cell
xScale = d3.scaleLinear()
.domain([0, TimeSpentData.length])
.range([0, chartWidth])
Insert cell
yScale = d3
.scaleLinear()
.domain([0, d3.max(TimeSpentData)])
.range([0, chartHeight])
Insert cell
chartWidth = 800;
Insert cell
{
const data = { 20: 100, state: 14, il: 50, ny: 20 };

const state = "ny";

return data[20];
}
Insert cell
chartHeight = 500
Insert cell
Plot = {
const height = 500;
const margin = 20;

const svg = d3.create("svg").attr("width", width).attr("height", height);

svg
.append("rect")
.attr("width", width)
.attr("height", width)
.attr("fill", "#aaaaaa");

const xScale = d3
.scaleBand()
.domain(TimeSpentData.map((d) => d.Activity))
.range([margin, width - margin * 2])
.paddingInner(0.2);

const yScale = d3
.scaleLinear()
.domain(d3.extent(TimeSpentData, (d) => d.difference))
.range([0, height - margin * 2]);
svg
.selectAll(".bars")
.data(TimeSpentData)
.enter()
.append("rect")
.attr("x", (d) => xScale(d.Activity))
.attr("y", (d) => height - margin - yScale(d.difference))
.attr("width", xScale.bandwidth)
.attr("height", (d) => yScale(d.difference))
.attr("fill", "blue");

// svg
// .append("text")
// .attr("y", 0)
// .attr("x", i * rowHeight + margin + rowHeight)

// .attr("font-size", 8)
// .text(TimeSpentData[i].name);

return svg.node();
}
Insert cell
// map = {
// //create SVG artboard
// const svg = d3.create("svg").attr("width", width).attr("height", height);

// //determine which two data keys from the census we should show
// const visualizationKeys =
// vizTopic == "beforecovid vs aftercovid"
// ? ["", ""]
// : ["", ""];

Insert cell
Inputs.table(TimeSpentData)
Insert cell
TimeSpentData.map(d=>d.Activity)
Insert cell
d3.range(margin,chartHeight-margin,(chartHeight - (margin*2))/(TimeSpentData.length-1)).concat(chartHeight-margin)
Insert cell
margin=30
Insert cell
dbYScale = d3.scaleOrdinal()
.domain(TimeSpentData.map(d=>d.Activity))
.range(d3.range(margin,chartHeight-margin,chartHeight/TimeSpentData.length))
Insert cell
dbXScale = d3.scaleLinear()
.domain([0,10])
.range([margin,width-margin])
Insert cell
Plot2 = {
const height = 500;
const margin = 20;

const svg = d3.create("svg").attr("width", width).attr("height", height);

svg
.append("rect")
.attr("width", width)
.attr("height", width)
.attr("fill", "#aaaaaa");

svg
.selectAll(".dots2019")
.data(TimeSpentData)
.enter()
.append("circle")
.attr("cx", (d) => dbXScale(d[2019]))
.attr("cy", (d) => dbYScale(d.Activity))
.attr("r", 3)
.attr('class','dots2019')
.attr("fill", "blue")
}
Insert cell
Plot3 = {
const height = 500;
const margin = 20;

const svg = d3.create("svg").attr("width", width).attr("height", height);

svg.append('defs').append('marker').attr('id','arrowhead').attr('markerWidth',10).attr('markerHeight',7).attr('refX',10).attr('refY',3.5).attr('orient','auto').append('polygon').attr('points','0 0, 10 3.5, 0 7')
svg
.append("rect")
.attr("width", width)
.attr("height", width)
.attr("fill", "#aaaaaa");

svg
.selectAll(".lines")
.data(TimeSpentData)
.enter()
.append("line")
.attr("x1", (d) => dbXScale(d[2019]))
.attr("x2", (d) => dbXScale(d[2019]))
.attr("y1", (d) => dbYScale(d.Activity))
.attr("y2", (d) => dbYScale(d.Activity))
.attr('class','lines')
.attr('stroke-width',3)
.attr("stroke", "purple")
.attr("marker-end", "url(#endarrow)")
;

svg
.selectAll(".dots2020")
.data(TimeSpentData)
.enter()
.append("circle")
.attr("cx", (d) => dbXScale(d[2019]))
.attr("cy", (d) => dbYScale(d.Activity))
.attr('class','dots2020')
.attr("r", 3)
.attr("fill", "red");
svg
.selectAll(".dots2019")
.data(TimeSpentData)
.enter()
.append("circle")
.attr("cx", (d) => dbXScale(d[2019]))
.attr("cy", (d) => dbYScale(d.Activity))
.attr("r", 3)
.attr('class','dots2019')
.attr("fill", "blue")

svg.append('rect')
.attr('x',width - 8*margin)
.attr('y',height/2)
.attr('width',100)
.attr('height',20)
.attr('fill','coral')
.on('click', (e,d) => {


svg
.selectAll(".dots2020")
.data(TimeSpentData)
.transition()
.duration(2000)
.attr("r", 6)
.transition()
.duration(2000)
.attr("cx", (d) => dbXScale(d[2020]))
.transition()
.duration(2000)
.attr("r", 3)

svg
.selectAll(".lines")
.data(TimeSpentData)
.transition()
.delay(2000)
.duration(2000)
.attr("x2", (d) => dbXScale(d[2020]))
// .transition()
// .duration(2000)
//.attr("marker-end", "url(#arrowhead)")

})
// svg
// .selectAll(".lines")
// .data(TimeSpentData)
// .enter()
// .append("line")
// .attr("x1", (d) => dbXScale(d[2019]))
// .attr("x2", (d) => dbXScale(d[2020]))

// .attr("y1", (d) => dbYScale(d.Activity))
// .attr("y2", (d) => dbYScale(d.Activity))

// .attr('class','dots2020')

// .attr("stroke", "purple");
// svg
// .append("text")
// .attr("y", 0)
// .attr("x", i * rowHeight + margin + rowHeight)

// .attr("font-size", 8)
// .text(TimeSpentData[i].name);

return svg.node();
}
Insert cell
//http://thenewcode.com/1068/Making-Arrows-in-SVG
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