Published
Edited
May 13, 2021
1 fork
1 star
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
// chart = {
// const svg = d3.create("svg").attr("viewBox", [0, 0, 800, 800]);

// svg
// .append("g")
// .attr("fill", "steelblue")
// .selectAll("rect")
// .data(sorted_top_ten)
// .join("rect")
// .attr("x", (d, i) => x(i))
// .attr("y", d => y(d.value))
// .attr("height", d => y(0) - y(d.value))
// .attr("width", x.bandwidth());

// svg
// .append("g")
// .call(xAxis)
// .selectAll("text")
// .style("text-anchor", "end")
// .attr("transform", function(d) {
// return "rotate(-30)";
// });

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

// return svg.node();
// }
Insert cell
// x = d3
// .scaleBand()
// .domain(d3.range(sorted_top_ten.length))
// .range([margin.left, width - margin.right])
// .padding(0.2)
Insert cell
// xAxis = g =>
// g.attr("transform", `translate(0,${height - margin.bottom})`).call(
// d3
// .axisBottom(x)
// .tickFormat(i => sorted_top_ten[i].name)
// .tickSizeOuter(0)
// )
Insert cell
// y = d3
// .scaleLinear()
// .domain([0, d3.max(sorted_top_ten, d => d.value)])
// .nice()
// .range([height - margin.bottom, margin.top])
Insert cell
// yAxis = g =>
// g
// .attr("transform", `translate(${margin.left},0)`)
// .call(d3.axisLeft(y).ticks(null, sorted_top_ten.format))
// .call(g => g.select(".domain").remove())
// .call(g =>
// g
// .append("text")
// .attr("x", -margin.left)
// .attr("y", 10)
// .attr("fill", "currentColor")
// .attr("text-anchor", "start")
// .text(sorted_top_ten.y)
// )
Insert cell
Insert cell
Insert cell
comments.comments.forEach(
obj => (obj.timestamp = new Date(obj.timestamp * 1000))
)
Insert cell
comments_last_year = {
let comments_last_year = [];
let currDate = new Date();
const oneyear = 31556952000;
for (let i = 0; i < comments.comments.length; i++) {
let data = comments.comments[i];
let date = data.timestamp;
if (currDate - date < oneyear) {
comments_last_year.push(data);
}
}
yield comments_last_year;
}
Insert cell
comments_last_year.forEach(
obj =>
(obj.timestamp = new Date(
obj.timestamp.getFullYear(),
obj.timestamp.getMonth(),
obj.timestamp.getDate()
))
)
Insert cell
comment_map = d3.rollup(
comments_last_year,
v => d3.count(v, d => d.timestamp),
d => d.timestamp
)
Insert cell
testDate = new Date("2021-04-25T00:00")
Insert cell
comment_map.get(testDate)
Insert cell
comment_data = []
Insert cell
comment_map.forEach((value, key) => {
comment_data.push({ key, value });
})
Insert cell
import { TimeChart, TimeAxis } from "@observablehq/timechart"
Insert cell
import { disposal } from "@observablehq/inputs"
Insert cell
interval = d3.utcMinute.every(200)
Insert cell
stop = new Date("2021-04-25T00:00")
Insert cell
start = new Date("2020-05-04T00:00")
Insert cell
TimeAxis({ interval, start, stop })
Insert cell
Insert cell
md`## Bar Chart - Facebook Reactions`
Insert cell
Insert cell
Insert cell
reactions_data.reactions.forEach(
obj => (obj.timestamp = new Date(obj.timestamp * 1000))
)
Insert cell
reactions_data.reactions[0]
Insert cell
reactions_last_year = {
let reactions_last_year = [];
let currDate = new Date();
const oneyear = 31556952000;
for (let i = 0; i < reactions_data.reactions.length; i++) {
let data = reactions_data.reactions[i];
let date = data.timestamp;
if (currDate - date < oneyear) {
reactions_last_year.push(data);
}
}
yield reactions_last_year;
}
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
chart = {
const svg = d3.create("svg").attr("viewBox", [0, 0, 800, 800]);

svg
.append("g")
.attr("fill", "steelblue")
.selectAll("rect")
.data(reaction_count)
.join("rect")
.attr("x", (d, i) => x(i))
.attr("y", d => y(d.value))
.attr("height", d => y(0) - y(d.value))
.attr("width", x.bandwidth());

svg
.append("g")
.call(xAxis)
.selectAll("text")
.style("text-anchor", "end")
.attr("transform", function(d) {
return "rotate(-30)";
});

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

return svg.node();
}
Insert cell
Insert cell
Insert cell
margin = ({ top: 30, right: 0, bottom: 30, left: 40 })
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