Published
Edited
Sep 2, 2020
Insert cell
md`# Day Six, 30 Aug 2020`
Insert cell
d3 = require("d3@6")
Insert cell
dataset = [
[5,20], [480,90], [250,50], [100,33], [330,95],
[410,12], [475,44], [25,67], [85,21], [220,88]
];
Insert cell
d3.max(dataset, function(d){
return d[0];
})
Insert cell
width = 600
Insert cell
height = 400
Insert cell
xScale = d3.scaleLinear()
.domain([0, d3.max(dataset, function(d) { return d[0]; })])
.range([0, width]);
Insert cell
yScale = d3.scaleLinear().domain(
[0, d3.max(dataset, function(d) { return d[1]; })]).range(
[0,height]);
Insert cell
md`Functions and methods are the same, basically. They take in our codes and give us outputs.

\`D3\` is an object. We need to reference it first before we can access its functions, like \`select\`, \`append()\` and \`text()\`.

Scott was using \`append("p")\`. I tried to replace \`"p"\` with anything I liked, and they also worked. Try yourself. **I don't know why they worked, though.**


`
Insert cell
md` **anonymous function** and **named function**
`
Insert cell
template = html`
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>D3 Page Template</title>
<script type="text/javascript" src="d3.js"></script>
</head>
<body>
<script type="text/javascript">
</script>
</body>
</html>
`
Insert cell
dataset_2 = [5, 10, 15, 20, 25];
Insert cell
md` In \`.text(function(d){return d;})\`, the anonymous function will run first.
`
Insert cell
md`## Chapter 5 Tasks:
- generate a \`dataset\` made up of five numbers: 5, 10, 15, 20, 25;
- generate five lines of texts, each line of text being "New paragraph!";
- generate five lines of texts: "I can count up to 5", "10", ...
- make all the five lines red;
- make only lines in which the number is above 15 red, and all else lines black;
`
Insert cell
md `## Chapter Six
`
Insert cell
Type JavaScript, then Shift-Enter. Ctrl-space for more options. Arrow ↑/↓ to switch modes.

Insert cell

One platform to build and deploy the best data apps

Experiment and prototype by building visualizations in live JavaScript notebooks. Collaborate with your team and decide which concepts to build out.
Use Observable Framework to build data apps locally. Use data loaders to build in any language or library, including Python, SQL, and R.
Seamlessly deploy to Observable. Test before you ship, use automatic deploy-on-commit, and ensure your projects are always up-to-date.
Learn more