Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using it's URL and the proper URL extention.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                
<script src="https://d3js.org/d3.v5.min.js"></script>
<script src="https://d3js.org/topojson.v1.min.js"></script>
<div id="svgDiv"></div>
<div class="ui">
    <span id=count></span>
    <input id=dateInput type="range" style="width:calc(100vw - 220px)" min=0 value=0 oninput=update() >
    <span id=date></span>
</div>



              
            
!

CSS

              
                body, html {
	margin: 0;
	overflow: hidden;
    background-color: #222;
}

.ocean {
	fill: steelblue;
}

.land {
	fill: #eee;
}

.boundary {
	fill: none;
	stroke: #000;
	stroke-linejoin: round;
	stroke-linecap: round;
	vector-effect: non-scaling-stroke;
}

.mark{
/*     animation: pulse 1s infinite; */
    stroke: black;
    cursor: pointer;
}

@keyframes pulse {
    0%{r:2;fill:red}
    50%{r:10;fill:pink}
    100%{r:2;fill:red}
}

.ui {
    position: absolute;
    top:0;
    color:white;
    background: #0007;
}

.ui span {
    display: inline-block;
    width:100px;
    font-size:30px;
}
              
            
!

JS

              
                var locations = {}
var w, h, scl, gpos0, o0, gpos1, o1; 
var svg = d3.select("#svgDiv").append("svg")
var projection = d3.geoOrthographic().rotate([-60,-20]);
var path = d3.geoPath().projection(projection);
var map = svg.append("g");
var count = d3.select('#count');
var date = d3.select('#date');

svg.call(d3.drag()
            .on("start", dragstarted)
            .on("drag", dragged))
    .call(d3.zoom()
            .on("zoom", zoomed));

d3.json("https://gist.githubusercontent.com/sarah37/dcca42b936545d9ee9f0bc8052e03dbd/raw/550cfee8177df10e515d82f7eb80bce4f72c52de/world-110m.json")
    .then(json => {
    
	map.append("path")
        .datum({type: "Sphere"})
        .attr("class", "ocean")
        .attr("d", path);

	map.append("path")
        .datum(topojson.merge(json,json.objects.countries.geometries))
        .attr("class", "land")
        .attr("d", path);

	map.append("path")
        .datum(topojson.mesh(json,json.objects.countries,(a,b)=>a!==b))
        .attr("class", "boundary")
        .attr("d", path);
});

d3.csv('https://raw.githack.com/CSSEGISandData/COVID-19/master/csse_covid_19_data/csse_covid_19_time_series/time_series_19-covid-Confirmed.csv').then(csv => {
    locations = csv;
    dateInput.setAttribute('max', Object.keys(locations[0]).length-5);
    update();
})

let resize = () => {
    w = window.innerWidth;
    h = window.innerHeight;
    scl = Math.min(w, h)/2.5;
    projection.translate([ w/2, h/2 ]);
    svg.attr("width", w).attr("height", h);
    map.selectAll("path").attr("d", path);
    update();
}

resize();

d3.select(window).on('resize', resize)

setInterval(() => {
    let v =((+dateInput.getAttribute('value')+1)||0)%+dateInput.getAttribute('max');
    dateInput.setAttribute('value', v);
    update();
}, 40)

function dragstarted() {
	gpos0 = projection.invert(d3.mouse(this));
    c0 = projection.center();
}

function dragged() {
    gpos1 = projection.invert(d3.mouse(this));
    o0 = projection.rotate();
    o1 = eulerAngles(gpos0, gpos1, o0);
    o1 && projection.rotate(o1);
	map.selectAll("path").attr("d", path);
    update();    
}

function zoomed() {
	projection.scale(d3.event.transform.translate(projection).k * scl)
	map.selectAll("path").attr("d", path);
    update();
}


function update() {
    if (!locations[0])
        return;
    
    let day = +dateInput.value;
    let lat = -projection.rotate()[1];
    let lon = -projection.rotate()[0];
    
    let selection = svg.selectAll(".mark")
        .data(Object.values(locations).filter(loc => loc.Long)).join("circle").attr('class',"mark")
    .attr('title', d => "1")
        .attr('fill', d => `hsl(0deg,55%,${100-5*Math.log(getDay(d, day)[1])}%`)
        .attr('r', d => Math.max(Math.log(getDay(d, day)[1]),0));
    
    svg.selectAll(".mark")
        .attr("opacity", d => {
            var dist = d3.geoDistance([lon,lat], [+d.Long, +d.Lat]);
	        return dist > Math.PI/2 ? 0.1 : 1;
        })
        .attr("transform", d => `translate(${projection([+d.Long, +d.Lat])})`);
    
    
     if (locations[0]){ 
date.html(getDay(locations[0], day)[0]);
         
count.html(locations.reduce((a,e)=>a+ +Object.values(e)[4+day]||0,0));
}
}

function getDay(d, day){
    return Object.entries(d)[4+day]
}

// controls

var to_radians = Math.PI / 180;
var to_degrees = 180 / Math.PI;

function cross(v0, v1) {
    return [v0[1] * v1[2] - v0[2] * v1[1], v0[2] * v1[0] - v0[0] * v1[2], v0[0] * v1[1] - v0[1] * v1[0]];
}

function dot(v0, v1) {
    for (var i = 0, sum = 0; v0.length > i; ++i) sum += v0[i] * v1[i];
    return sum;
}

function lonlat2xyz( coord ){

	var lon = coord[0] * to_radians;
	var lat = coord[1] * to_radians;

	var x = Math.cos(lat) * Math.cos(lon);

	var y = Math.cos(lat) * Math.sin(lon);

	var z = Math.sin(lat);

	return [x, y, z];
}

function quaternion(v0, v1) {

	if (v0 && v1) {
		
	    var w = cross(v0, v1),  // vector pendicular to v0 & v1
	        w_len = Math.sqrt(dot(w, w)); // length of w     

        if (w_len == 0)
        	return;

        var theta = .5 * Math.acos(Math.max(-1, Math.min(1, dot(v0, v1)))),

	        qi  = w[2] * Math.sin(theta) / w_len; 
	        qj  = - w[1] * Math.sin(theta) / w_len; 
	        qk  = w[0]* Math.sin(theta) / w_len;
	        qr  = Math.cos(theta);

	    return theta && [qr, qi, qj, qk];
	}
}

function euler2quat(e) {

	if(!e) return;
    
    var roll = .5 * e[0] * to_radians,
        pitch = .5 * e[1] * to_radians,
        yaw = .5 * e[2] * to_radians,

        sr = Math.sin(roll),
        cr = Math.cos(roll),
        sp = Math.sin(pitch),
        cp = Math.cos(pitch),
        sy = Math.sin(yaw),
        cy = Math.cos(yaw),

        qi = sr*cp*cy - cr*sp*sy,
        qj = cr*sp*cy + sr*cp*sy,
        qk = cr*cp*sy - sr*sp*cy,
        qr = cr*cp*cy + sr*sp*sy;

    return [qr, qi, qj, qk];
}

function quatMultiply(q1, q2) {
	if(!q1 || !q2) return;

    var a = q1[0],
        b = q1[1],
        c = q1[2],
        d = q1[3],
        e = q2[0],
        f = q2[1],
        g = q2[2],
        h = q2[3];

    return [
     a*e - b*f - c*g - d*h,
     b*e + a*f + c*h - d*g,
     a*g - b*h + c*e + d*f,
     a*h + b*g - c*f + d*e];

}

function quat2euler(t){

	if(!t) return;

	return [ Math.atan2(2 * (t[0] * t[1] + t[2] * t[3]), 1 - 2 * (t[1] * t[1] + t[2] * t[2])) * to_degrees, 
			 Math.asin(Math.max(-1, Math.min(1, 2 * (t[0] * t[2] - t[3] * t[1])))) * to_degrees, 
			 Math.atan2(2 * (t[0] * t[3] + t[1] * t[2]), 1 - 2 * (t[2] * t[2] + t[3] * t[3])) * to_degrees
			]
}

function eulerAngles(v0, v1, o0) {
	var t = quatMultiply( euler2quat(o0), quaternion(lonlat2xyz(v0), lonlat2xyz(v1) ) );
	return quat2euler(t);	
}







              
            
!
999px

Console