Published
Edited
Jan 5, 2019
2 forks
5 stars
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
data = {
let options = {
"Cumulative": cumulativeData,
"Count": countData
}
return options[chartType];
}
Insert cell
line = d3.line()
.x(d => x(d.date))
.y(d => y(d.value))
.curve(d3.curveStepAfter)
Insert cell
area = d3.area()
.x(d => x(d.date))
.y1(d => y(d.value))
.y0(d => y(0))
.curve(d3.curveStepAfter)
Insert cell
x = d3.scaleTime()
.domain(d3.extent(data, d => d.date))
.range([margin, width - margin])
Insert cell
y = d3.scaleLinear()
.domain([0, d3.max(data, d => d.value)])
.range([height - margin, margin])
Insert cell
xAxis = g => g
.attr("transform", `translate(0,${height - margin})`)
.call(d3.axisBottom(x))
Insert cell
yAxis = g => g
.attr("transform", `translate(${margin},0)`)
.call(d3.axisLeft(y))
Insert cell
Insert cell
cumulativeData = {
let runningCount = 0;
let cumulativeByDate = [];
dateRange.forEach(function(date) {
let dateCount = dateLookup[dateFormatter(date)] || 0;
runningCount += dateCount;
let dateObj = {date: date, value: runningCount}
cumulativeByDate.push(dateObj);
});
return cumulativeByDate;
}
Insert cell
countData = {
let countByDate = [];
dateRange.forEach(function(date) {
let dateCount = dateLookup[dateFormatter(date)] || 0;
let dateObj = {date: date, value: dateCount}
countByDate.push(dateObj);
});
return countByDate;
}
Insert cell
dateFormatter = d3.timeFormat("%Y-%m-%d")
Insert cell
dateRange = filteredTweets.dateRange
Insert cell
dateLookup = groupbyDate.reduce((lookup, date) => (lookup[date.key] = date.value, lookup), {})
Insert cell
groupbyDate = d3.nest()
.key(d => d.dateString)
.rollup(d => d.length)
.entries(filteredTweets)
Insert cell
totalTweets = filteredTweets.length
Insert cell
filteredTweets = filter.length > 3 ? tweets.filter(t => t.contains(filter)) : tweets
Insert cell
Insert cell
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