Published
Edited
May 14, 2020
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
md`# Chart 1`
Insert cell
md` Loading Data`
Insert cell
data1 = Object.assign(
d3.csvParse(
await FileAttachment("ProductInfo2.csv").text(),
({
Product_Name,
Product_Price: x,
Quantity_Sold: y,
Customer_Rating
}) => ({
Product_Name,
x: +x,
y: +y,
rating: Customer_Rating
})
),
{ x: "Product Price →", y: "↑ Product Quantity" }
)
Insert cell
md`Scales`
Insert cell
width = 800
Insert cell
height = 800
Insert cell
margin = ({ top: 25, right: 20, bottom: 35, left: 40 })
Insert cell
color(5)
Insert cell
md`Axes`
Insert cell
x = d3
.scaleLinear()
.domain(d3.extent(data1, d => d.x))
.nice()
.range([margin.left, width - margin.right])
Insert cell
y = d3
.scaleLinear()
.domain(d3.extent(data1, d => d.y))
.nice()
.range([height - margin.bottom, margin.top])
Insert cell
xAxis = g =>
g
.attr("transform", `translate(0,${height - margin.bottom})`)
.call(d3.axisBottom(x).ticks(width / 80))
.call(g => g.select(".domain").remove())
.call(g =>
g
.append("text")
.attr("x", width)
.attr("y", margin.bottom - 4)
.attr("fill", "currentColor")
.attr("text-anchor", "end")
.text(data1.x)
)
Insert cell
yAxis = g =>
g
.attr("transform", `translate(${margin.left},0)`)
.call(d3.axisLeft(y))
.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(data1.y)
)
Insert cell
color = d3.scaleSequential(
d3.extent(data1, d => d.rating),
d3.interpolateSpectral
)
Insert cell
md`Marks`
Insert cell
//modified from basic bar chart notebook
grid = g =>
g
.attr("stroke", "currentColor")
.attr("stroke-opacity", 0.1)
.call(g =>
g
.append("g")
.selectAll("line")
.data(x.ticks())
.join("line")
.attr("x1", d => 0.5 + x(d))
.attr("x2", d => 0.5 + x(d))
.attr("y1", margin.top)
.attr("y2", height - margin.bottom)
)
.call(g =>
g
.append("g")
.selectAll("line")
.data(y.ticks())
.join("line")
.attr("y1", d => 0.5 + y(d))
.attr("y2", d => 0.5 + y(d))
.attr("x1", margin.left)
.attr("x2", width - margin.right)
)
Insert cell
md`Chart`
Insert cell
import { legend } from "@d3/color-legend"
Insert cell
chart = {
const svg = d3.create("svg").attr("viewBox", [0, 0, width, height]);
svg
.append("g")
.attr("transform", "translate(500,20)")
.append(() =>
legend({
color,
width: 300
})
);
svg.append("g").call(xAxis);

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

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

svg
.append("g")
.attr("stroke", "none")
.attr("stroke-width", 1.5)
.attr("fill", "none")
.selectAll("circle")
.data(data1)
.join("circle")
.attr("cx", d => x(d.x))
.attr("cy", d => y(d.y))
.attr("r", 3)
.attr("fill", d => color(d.rating));

svg
.append("g")
.attr("font-family", "sans-serif")
.attr("font-size", 10)
.selectAll("text")
.data(data1)
.join("text")
.attr("dy", "0.35em")
.attr("x", d => x(d.x) + 7)
.attr("y", d => y(d.y))
.text(d => d.name);

svg
.append("g")
.attr("font-family", "sans-serif")
.attr("font-size", 10)
.selectAll("text")
.data(data1)
.join("text")
.attr("x", d => x(d.x) + 25)
.attr("y", d => y(d.y))
.text(d => d.Product_Name);

return svg.node();
}
Insert cell
md`# Chart 2`
Insert cell
md`Loading Data`
Insert cell
data2 = Object.assign(
d3.csvParse(
await FileAttachment("weightOverTime2.csv").text(),
({ Week: x, Weight: y }) => ({
week: +x,
weight: +y
})
),
{ format: "Weeks", y: "↑ Weight (IBs)" }
)
Insert cell
md`Scales`
Insert cell
x1 = d3
.scaleBand()
.domain(data2.map(d => d.week))
.range([margin.left, width - margin.right])
.padding(0.1)
Insert cell
y1 = d3
.scaleLinear()
.domain([0, d3.max(data2, d => d.weight)])
.nice()
.range([height - margin.bottom, margin.top])
Insert cell
color2 = d3.scaleSequential(
d3.extent(data2, d => d.weight),
d3.interpolateSpectral
)
Insert cell
md`Axes/ Marks`
Insert cell
xAxis2 = d3.axisBottom(x1).tickSizeOuter(0)
Insert cell
yAxis2 = d3.axisLeft(y1).tickSizeOuter(0)
Insert cell
md`Chart`
Insert cell
bars = {
const container = html`<svg width="900" height="850"/>`;

const svg = d3.select(container);

svg
.append('g')
.attr('class', 'bars')
.selectAll('rect')
.data(data2)
.join('rect')
.attr('class', 'bar')
.attr('x', d => x1(d.week))
.attr('y', d => y1(d.weight))
.attr('width', x1.bandwidth())
.attr('height', d => y1(0) - y1(d.weight))
.attr("fill", d => color2(d.weight));

svg
.append('g')
.attr('class', 'x-axis')
.attr('transform', `translate(0,${height - margin.bottom})`)
.call(xAxis2);

svg
.append('g')
.attr('class', 'y-axis')
.attr('transform', `translate(${margin.left},0)`)
.call(yAxis2);
svg
.append("text")
.attr("dx", width / 2 + 20)
.attr("dy", height - margin.top + 30)
.attr("text-anchor", "middle")
.text("Weeks");

svg
.append("text")
.attr("dx", margin.left - 440)
.attr("dy", height - margin.top - 763)
.style("text-anchor", "middle")
.attr("transform", "rotate(-90)")
.text("Weight");

return container;
}
Insert cell
Insert cell
md`This assignment was decent hwen I did it the first time, and felt good this second time, I tried a couple different things I have been seeing from looking through notebooks. The sizing was a bit weird at times but everything else was fine for the most part. Took a while to get the bar chart to have a numeric bottom. `
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