Published
Edited
Feb 28, 2019
3 forks
3 stars
Insert cell
Insert cell
chart = draw()
Insert cell
filteredTweets = tweets
Insert cell
Insert cell
margin = 40
Insert cell
height = 500
Insert cell
lineColor = "#fdc032"
Insert cell
areaColor = "#fff3d6"
Insert cell
lineWidth = 1.5
Insert cell
lineCurve = "curveStepAfter"
Insert cell
circleRadius = 5
Insert cell
Insert cell
Insert cell
d3 = require("d3@5")
Insert cell
Insert cell
x = d3.scaleTime()
.domain(d3.extent(cumulativeByDate, d => d.date))
.range([margin, width - margin])
Insert cell
Insert cell
y = d3.scaleLinear()
.domain([0, filteredTweets.length])
.range([height - margin, margin])
Insert cell
Insert cell
line = d3.line()
.x(d => x(d.date))
.y(d => y(d.value))
.curve(d3[lineCurve])
Insert cell
Insert cell
area = d3.area()
.x(d => x(d.date))
.y1(d => y(d.value))
.y0(d => y(0))
.curve(d3[lineCurve])
Insert cell
Insert cell
xAxis = g => g
.attr("transform", `translate(0,${height - margin})`)
.call(d3.axisBottom(x))
Insert cell
Insert cell
yAxis = g => g
.attr("transform", `translate(${margin},0)`)
.call(d3.axisLeft(y))
Insert cell
Insert cell
function draw() {
// Create a blank SVG object
const svg = d3.select(DOM.svg(width, height));
// Draw an area plot
svg.append("path")
.datum(cumulativeByDate)
.attr("fill", areaColor)
.attr("d", area);

// Draw a line on top of the area p lot
svg.append("path")
.datum(cumulativeByDate)
.attr("fill", "none")
.attr("stroke", lineColor)
.attr("stroke-width", lineWidth)
.attr("d", line);

// Plot a circle marking the final tweet
svg.append("circle")
.attr("cx", x(filteredTweets.endDate))
.attr("cy", y(filteredTweets.length))
.attr("fill", lineColor)
.attr("r", circleRadius);
// Draw the x axis
svg.append("g")
.call(xAxis);

// Draw the y axis
svg.append("g")
.call(yAxis);
// Return the chart for rendering
return svg.node();
}
Insert cell
Insert cell
Insert cell
import {tweets} from "@palewire/trump-tweets"
Insert cell
Insert cell
groupByDate = d3.nest()
.key(d => d.dateString) // Groups on day
.rollup(d => d.length) // Counts how many records are in each date group
.entries(filteredTweets) // Passes in the current tweet list for grouping and counting
Insert cell
Insert cell
dateLookup = groupByDate.reduce((lookup, date) => (lookup[date.key] = date.value, lookup), {})
Insert cell
Insert cell
dateFormatter = d3.timeFormat("%Y-%m-%d")
Insert cell
Insert cell
cumulativeByDate = {
// Start the running count at zero.
let runningCount = 0;
// Loop through all the dates in the current filtered Tweet list
return filteredTweets.dateRange.map(d => {
// Use the lookup and the date formatter to pull the count for each day.
let dateCount = dateLookup[dateFormatter(d)] || 0; // <-- If it isn't in the lookup, set it to zero.
// Add each day's tally to the running count
runningCount += dateCount;
// Return a new datum for this date with the cumulative tally instead of that day's count.
return {date: d, value: runningCount};
});
}
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