Published
Edited
Feb 1, 2021
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
data = Object.assign(
d3
.csvParse(
await FileAttachment("starbucks-menu-nutrition-drinks@1.csv").text(),
({ Drink, Calories }) => ({ drink: Drink, value: +Calories })
)
.sort((a, b) => d3.descending(a.value, b.value)),
{ y: "↑ Calories" }
)
Insert cell
data2 = data.slice(0, 20)
Insert cell
//attachment = FileAttachment("starbucks-menu-nutrition-drinks@1.csv")
Insert cell
//text = attachment.text()
Insert cell
//data = d3.csvParse(text, d3.autoType).slice(0, 10)
Insert cell
render_data_table(data2)
Insert cell
viewof drinkselection = select({
title: "Drink Name",
description: "Please select a drink name.",
options: [
x_variable[0],
x_variable[1],
x_variable[2],
x_variable[3],
x_variable[4],
x_variable[5],
x_variable[6],
x_variable[7],
x_variable[8],
x_variable[9]
],
placeholder: "Search for a Starbucks drink . . .",
value: "Cool Lime Starbucks Refreshers™ Beverage"
})
Insert cell
x_variable = data2.map(d => d.drink)
Insert cell
y_variable = "Calories"
Insert cell
color_variable = "Calories"
Insert cell
Insert cell
render_data_table(data.slice(0, 20))
Insert cell
height = 500
Insert cell
margin = ({ top: 25, right: 20, bottom: 35, left: 40 })
Insert cell
color = "pink"
Insert cell
Insert cell
//XD = [d3.min(data, d => d.drink), d3.max(data, d => d.drink)]
Insert cell
//YD = [d3.min(data, d => d.value), d3.max(data, d => d.value)]
Insert cell
x = d3
.scaleBand()
.domain(d3.range(data.length))
.range([margin.left, width - margin.right])
.padding(0.1)
Insert cell
y = d3
.scaleLinear()
.domain([0, d3.max(data, d => d.value)])
.nice()
.range([height - margin.bottom, margin.top])
Insert cell
xAxis = g =>
g.attr("transform", `translate(0,${height - margin.bottom})`).call(
d3
.axisBottom(x)
.tickFormat(i => data[i].name)
.tickSizeOuter(0)
)
Insert cell
barX = d3
.scaleBand()
.domain(data.map(d => d.drink))
.range([margin.left, width - margin.right])
.padding(0.1)
Insert cell
barxAxis = g =>
g
.attr("transform", `translate(0,${height - margin.bottom})`)
.call(d3.axisBottom(barX).tickSizeOuter(0))
Insert cell
yAxis = g =>
g
.attr("transform", `translate(${margin.left},0)`)
.call(d3.axisLeft(y).ticks(null, data.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(data.y)
)
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
md`
- I don't think I understand scaling (specifically color scale and how to properly use it)
- I also did not know how to do the transition for the chart
- Also, my bars on the chart were displaying but now there not`
Insert cell
Insert cell
import {
assignment_instructions,
observable_challenges
} from "@uw-info474/utilities"
Insert cell
import { select } from "@jashkenas/inputs"
Insert cell
import { autoSelect } from "@jashkenas/inputs"
Insert cell
import { render_data_table, table_styles } from "@info474/utilities"
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