Published
Edited
Apr 27, 2020
Insert cell
md`# Corona Line and Scatter plot in Same Chart [Log Scale]`
Insert cell
chart = {
const svg= d3.create('svg')
.attr('viewBox',[0,0,width,height]);
svg.append('g')
.call(xAxis);
svg.append('g')
.call(yAxis);
svg.append('path')
.datum(data)
.attr('fill','none')
.attr('stroke', color)
.attr('stroke-width','1')
.attr("opacity", "0.5")
.attr('stroke-linejoin','round')
.attr('stroke-linecap','round')
.attr('d',line);

svg.selectAll("dot")
.data(data)
.enter().append("circle")
.attr("class", "dot")
.attr("r", function(d) { return r(d.close)} )
.attr("cx", function(d) { return x(d.date) })
.attr("cy", function(d) { return y(d.close) })
.attr("opacity", "0.7")
.attr("fill", color);

return svg.node();
}
Insert cell
parseTime = d3.timeParse("%m/%_d/%Y")
Insert cell
data_raw=d3.csvParse(await FileAttachment("covid@12.csv").text())
Insert cell
data={
data_raw.map(function(d){
d.close= +d.total_deaths
return d;
});
var df = d3.nest()
.key(function(d) { return d.date; })
.rollup(function(v) { return d3.sum(v, function(d) { return +d.close; }); })
.entries(data_raw);
var dt=df;
dt.forEach(function(d) {
d.date=parseTime(d.key);
d.close = +d.value;
});
dt.sort((b,a) => a.date - b.date);
var data=dt;
return data;
}
Insert cell
colorFunction=function color(array){if (array[array.length-1].close < array[0].close) {return '#ff1e00';} else {return '#02d139';}}
Insert cell
color=colorFunction(data)
Insert cell
margin = ({top: 50, right: 30, bottom: 30, left: 50})
Insert cell
height=300;
Insert cell
width=900;
Insert cell
d3.extent(data, d=> d.date)
Insert cell
x=d3.scaleUtc()
.domain(d3.extent(data, d=> d.date))
.range([margin.left,width-margin.right])
Insert cell
xAxis= g => g
.attr('transform',`translate(0,${height-margin.bottom})`)
.attr("color","steelblue")
.call(
d3.axisBottom(x).ticks(7).tickSize(3))

Insert cell
yDomain=d3.extent(data, d=> d.close)
Insert cell
y=d3.scaleLog()
.domain([1, yDomain[1]])
.range([height- margin.bottom ,margin.top])
Insert cell
yAxis= g => g
.attr('transform',`translate(${margin.left},0)`)
.attr("color","steelblue")
.call(d3.axisLeft(y).ticks(4).tickSize(-width+margin.left+margin.right).tickFormat(d3.format(".0s")) )
.call(g => g.select(".domain").remove())
.call(g => g.select(".tick:last-of-type text").clone()
.attr("x", width/2-margin.left)
.attr("y",-10)
.attr("text-anchor", "middle")
.attr("font-size","20px")
.attr("font-weight", "bold")
.text("Deaths due to COVID-19 worldwide"))
Insert cell
r=d3.scaleLinear()
.domain(d3.extent(data, d=> d.close)).nice()
.range([0 ,10])
Insert cell
line=d3.line()
.curve(d3.curveMonotoneX)
.defined( d => !isNaN(d.close))
.x(d => x(d.date))
.y(d => y(d.close))
Insert cell
styles = html`
<style>
svg{
background-color:#111;
}

</style>
`
Insert cell
d3=require('d3@5')
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