Published
Edited
Nov 1, 2020
3 forks
Insert cell
Insert cell
Insert cell
Insert cell
date1 = new Date(); //returns the current date/time
Insert cell
Insert cell
Insert cell
date2 = new Date("09/13/2020");
Insert cell
md`Date: ${date2.toDateString()}`
Insert cell
Insert cell
strDate = "20200913";
Insert cell
date3wrong = new Date("20200913");
Insert cell
dt1 = parseInt(strDate.substring(6,8))

Insert cell
strDate.substring(6,8)

Insert cell
date3init = {
var dt1 = parseInt(strDate.substring(6,8));
var mon1 = parseInt(strDate.substring(4,6));
var yr1 = parseInt(strDate.substring(0,4));
var date = new Date(yr1, mon1-1, dt1); //Warning with the month!!!!
return date;
}
Insert cell
dateCreator = function(str){
var dt1 = parseInt(str.substring(6,8));
var mon1 = parseInt(str.substring(4,6));
var yr1 = parseInt(str.substring(0,4));
var date = new Date(yr1, mon1-1, dt1);
return date;
};
Insert cell
date3 = dateCreator(strDate);
Insert cell
md`Date: ${date3.toDateString()}`
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
desiredData = OxCGRTdata.filter(d => d.CountryName === "Italy")
.map(d => {return {
date : dateCreator(d.Date),
cases : +d.ConfirmedCases,
deaths : +d.ConfirmedDeaths
};}
);
Insert cell
Insert cell
Insert cell
x = d3.scaleUtc()
.domain(d3.extent(desiredData, d => d.date))
.range([margin.left, width - margin.right])
Insert cell
x2 = d3.scaleTime()
.domain(d3.extent(desiredData, d => d.date))
.range([margin.left, width - margin.right])
Insert cell
Insert cell
Insert cell
Insert cell
p = d3.line()([[10, 60], [40, 90], [60, 10], [190, 10]]);
Insert cell
Insert cell
Insert cell
lineCases = d3.line()
.defined(d => !isNaN(d.date) && !isNaN(d.cases))
.x(d => x(d.date))
.y(d => y(d.cases))
Insert cell
lineDeaths = d3.line()
.defined(d => !isNaN(d.date))
.x(d => x(d.date))
.y(d => y(d.deaths))
Insert cell
y = d3.scaleLinear()
.domain([0, d3.max(desiredData, d => d.cases)]).nice()
.range([height - margin.bottom, margin.top])
Insert cell
Insert cell
temporalchart = {
const svg = d3.create("svg")
.attr("width", width)
.attr("height", height);

svg.append("path")
.datum(desiredData)
.attr("fill", "none")
.attr("stroke", "purple")
.attr("stroke-width", 1.5)
.attr("stroke-linejoin", "round")
.attr("stroke-linecap", "round")
.attr("d", lineCases);

svg.append("path")
.datum(desiredData)
.attr("fill", "none")
.attr("stroke", "steelblue")
.attr("stroke-width", 1.5)
.attr("stroke-linejoin", "round")
.attr("stroke-linecap", "round")
.attr("d", lineDeaths);

return svg.node();
}
Insert cell
Insert cell
Insert cell
line = key => {return d3.line()
.defined(d => !isNaN(d.date) && !isNaN(d[key]) )
.x(d => x(d.date)) //.x(d => x(d['date']))
.y(d => y(d[key]))}
Insert cell
{
const svg = d3.create("svg")
.attr("width", width)
.attr("height", height);

svg.append("path")
.datum(desiredData)
.attr("fill", "none")
.attr("stroke", "purple")
.attr("stroke-width", 1.5)
.attr("stroke-linejoin", "round")
.attr("stroke-linecap", "round")
.attr("d", line('cases'));

svg.append("path")
.datum(desiredData)
.attr("fill", "none")
.attr("stroke", "steelblue")
.attr("stroke-width", 1.5)
.attr("stroke-linejoin", "round")
.attr("stroke-linecap", "round")
.attr("d", line('deaths'));

return svg.node();
}
Insert cell
Insert cell
Insert cell
xAxis = g => g
.attr("transform", `translate(0,${height - margin.bottom})`)
.call(d3.axisBottom(x)
.ticks(width / 80)
.tickSizeOuter(0))
Insert cell
yAxis = g => g
.attr("transform", `translate(${margin.left},0)`)
.call(d3.axisLeft(y))
.call(g => g.select(".domain").remove())
.call(g => g.select(".tick:last-of-type text").clone()
.attr("x", 3)
.attr("text-anchor", "start")
.attr("font-weight", "bold")
.text("Total number of cases/deaths"))
Insert cell
temporalchartwithaxis = {
const svg = d3.create("svg")
.attr("width", width)
.attr("height", height);

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

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

svg.append("path")
.datum(desiredData)
.attr("fill", "none")
.attr("stroke", "purple")
.attr("stroke-width", 1.5)
.attr("stroke-linejoin", "round")
.attr("stroke-linecap", "round")
.attr("d", line('cases'));

svg.append("path")
.datum(desiredData)
.attr("fill", "none")
.attr("stroke", "steelblue")
.attr("stroke-width", 1.5)
.attr("stroke-linejoin", "round")
.attr("stroke-linecap", "round")
.attr("d", line('deaths'));

return svg.node();
}
Insert cell
Insert cell
Insert cell
area = d3.area()
.x(d => x(d.date))
.y0(d => y(0))
.y1(d => y(d.deaths))
Insert cell
Insert cell
areachart = {
const svg = d3.create("svg")
.attr("width", width)
.attr("height", height);

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

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

svg.append("path")
.datum(desiredData)
.attr("fill", "none")
.attr("stroke", "purple")
.attr("stroke-width", 1.5)
.attr("stroke-linejoin", "round")
.attr("stroke-linecap", "round")
.attr("d", line('cases'));

svg.append("path")
.datum(desiredData)
.attr("fill", 'steelblue')
.attr("d", area)

return svg.node();
}
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
dataGroup = d3.groups(desiredCountryData, d => d.country)
Insert cell
Insert cell
xmultiAxis = g => g
.attr("transform", `translate(0,${height - margin.bottom})`)
.call(d3.axisBottom(xmulti).ticks(width / 80).tickSizeOuter(0))
Insert cell
ymultiAxis = g => g
.attr("transform", `translate(${margin.left},0)`)
.call(d3.axisLeft(ymulti))
.call(g => g.select(".domain").remove())
.call(g => g.select(".tick:last-of-type text").clone()
.attr("x", 3)
.attr("text-anchor", "start")
.attr("font-weight", "bold")
.text("Total number of cases"))
Insert cell
xmulti = d3.scaleTime()
.domain(d3.extent(desiredCountryData, d => d.date))
.range([margin.left, width - margin.right])
Insert cell
ymulti = d3.scaleLinear()
.domain([0, d3.max(desiredCountryData, d => d.cases)]).nice()
.range([height - margin.bottom, margin.top])
Insert cell
Insert cell
d3.schemeCategory10
Insert cell
colorScale = d3.scaleOrdinal(dataGroup.map(d=> d[0]), d3.schemeCategory10)
Insert cell
dataGroup.map(d=> d[0])
Insert cell
colorScale("Canada")
Insert cell
lineMultiCases = d3.line()
.defined(d => !isNaN(d.date))
.x(d => xmulti(d.date))
.y(d => ymulti(d.cases))
Insert cell
multilinechart = {
const svg = d3.create("svg")
.attr("width", width)
.attr("height", height);

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

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

svg.selectAll('.line')
.data(dataGroup)
.join('path')
.attr("class", "line")
.attr("d", d => lineMultiCases(d[1]))
.attr("stroke", d => colorScale(d[0]))
.style("fill", "none")
.style("stroke-width", 1);

let legend = svg.selectAll(".legend")
.data(dataGroup)
.enter();
legend
.append('text')
.text(d => d[0])
.attr('x', legendOrigin[0] + labelHeight * 1.2)
.attr('y', (d,i) => legendOrigin[1] + labelHeight + labelHeight * 1.2 *i)
.style('font-family', 'sans-serif')
.style('font-size', `${labelHeight-3}px`);
legend
.append('rect')
.attr('x', legendOrigin[0])
.attr('y', (d,i) => legendOrigin[1] + labelHeight * 1.2 * i)
.attr('width', labelHeight)
.attr('height', labelHeight)
.attr('fill', d => colorScale(d[0]))
.attr('stroke', 'grey')
.style('stroke-width', '1px');
return svg.node();
}
Insert cell
Insert cell
legendOrigin = [100,50];
Insert cell
labelHeight = 15;
Insert cell
Insert cell
Insert cell
weather = (await require('vega-datasets@1'))['weather.csv']()
Insert cell
weather[2000]
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
margin = ({top: 20, right: 30, bottom: 30, left: 60})
Insert cell
Insert cell
Insert cell
d3 = require("d3@6");
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