Published
Edited
Jul 6, 2020
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
// TODO: apply the color scale from task 3
//This Map object is to set each attribute a color scale
//https://observablehq.com/@d3/sequential-scales
//https://github.com/d3/d3-scale-chromatic
//https://www.d3indepth.com/scales/

colorScale = {
let colorScale = new Map();
attributes.forEach(function(attribute){
//https://observablehq.com/@d3/d3-scaleordinal
//For discrete set
if(attribute === "origin"){
colorScale.set(
attribute,
d3.scaleOrdinal(d3.schemeTableau10)
.domain(data.map(d => d.origin))
//.interpolator(d3.interpolateRainbow)
);
} else {
// For continuous data set
colorScale.set(
attribute,
d3.scaleSequential()
.domain(d3.extent(data, d => d[attribute]))
.interpolator(d3.interpolateViridis)
);
}
});
return colorScale;
}
Insert cell
console.log("data.map(d => d.origin):" + data.map(d => d.origin));
Insert cell
Insert cell
Insert cell
Insert cell
console.log("colorAttribute: " + colorAttribute);
Insert cell
Insert cell
Insert cell
Insert cell
console.log();
Insert cell
md`## Appendix`
Insert cell
height = 500
Insert cell
margin = ({ top: 10, right: 30, bottom: 10, left: 10 })
Insert cell
data = d3.csvParse(await FileAttachment("testCustom.csv").text(), d3.autoType)
Insert cell
attributes = data.columns.filter(d => d !== "name")
Insert cell
shortAttributeNames = new Map(
Object.entries({
mpg: "MPG",
cylinders: "CYL",
displacement: "DPL",
horsepower: "HP",
weight: "WGT",
acceleration: "ACL",
year: "YEAR",
origin: "OGN"
})
)
Insert cell
import { select } from "@jashkenas/inputs"
Insert cell
d3 = require("d3@5")
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