Add VT2-examples: all 6 chapters of 'Visualize This' (110 files) — data scraping, ETL, trends, categorical, multivariate, spatial, distribution analysis
This commit is contained in:
1 parent
088771a9cf
commit
b42b866a8c
110 files changed
+400895
No files matched your search
@@ -0,0 +1,156 @@
|
||||
// Dimensions of chart.
|
||||
let margin = { top: 30, right: 10, bottom: 30, left: 45 },
|
||||
// width = parseInt(d3.select('#chart').style('width'), 10) - margin.left - margin.right,
|
||||
width = 600 - margin.left - margin.right,
|
||||
height = width*.8 - margin.top - margin.bottom;
|
||||
|
||||
let svg = d3.select("#chart").append("svg")
|
||||
.attr("width", width + margin.left + margin.right)
|
||||
.attr("height", height + margin.top + margin.bottom)
|
||||
.append("g")
|
||||
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
|
||||
|
||||
|
||||
// Scales: x and y
|
||||
let x = d3.scaleLinear()
|
||||
.domain([1909, 2017])
|
||||
.range([0, width]);
|
||||
let y = d3.scaleLinear()
|
||||
.domain([0, 100])
|
||||
.range([height, 0]);
|
||||
|
||||
|
||||
// Geometries
|
||||
let area = d3.area()
|
||||
.x(d => x(d.year))
|
||||
.y1(d => y(d.amt));
|
||||
let line = d3.line()
|
||||
.x(d => x(d.year))
|
||||
.y(d => y(d.amt));
|
||||
|
||||
|
||||
let xAxis, yAxis, xAxisEl, yAxisEL;
|
||||
|
||||
let meats, beef, chicken;
|
||||
|
||||
|
||||
// Load data
|
||||
const meatData = Promise.all([
|
||||
d3.csv("data/beef-vs-chicken-difference.csv", d3.autoType)
|
||||
]);
|
||||
meatData.then(function(data) {
|
||||
|
||||
meats = data[0];
|
||||
console.log(meats);
|
||||
|
||||
svg.datum(meats);
|
||||
|
||||
// Initialize chart now that data is loaded.
|
||||
initChart();
|
||||
});
|
||||
|
||||
|
||||
|
||||
function initChart() {
|
||||
|
||||
// x-axis
|
||||
xAxis = d3.axisBottom(x)
|
||||
.ticks(4)
|
||||
.tickFormat(d => d)
|
||||
.tickSize(-height-8)
|
||||
.tickPadding(5);
|
||||
xAxisEl = svg.append("g")
|
||||
.attr("class", "x axis bottom")
|
||||
.attr("transform", "translate(0,"+(height+8)+")");
|
||||
xAxisEl.call(xAxis);
|
||||
|
||||
// y-axis
|
||||
yAxis = d3.axisLeft(y)
|
||||
// .ticks(7)
|
||||
.ticks(5)
|
||||
.tickSize(-width-8)
|
||||
.tickPadding(5);
|
||||
yAxisEl = svg.append("g")
|
||||
.attr("class", "y axis left")
|
||||
.attr("transform", "translate("+(-8)+",0)");
|
||||
yAxisEl.append("text")
|
||||
.attr("class", "axistitle")
|
||||
.attr("x", 0).attr("y", 0)
|
||||
.attr("dx", "-2.25em")
|
||||
.attr("dy", "-1.5em")
|
||||
.style("text-anchor", "start")
|
||||
.text("Pounds per capita");
|
||||
yAxisEl.call(yAxis);
|
||||
|
||||
// Area clipping paths
|
||||
svg.append("clipPath")
|
||||
.attr("id", "clip-above")
|
||||
.append("path")
|
||||
.attr("d", d3.area()
|
||||
.x(d => x(d.Year))
|
||||
.y0(0)
|
||||
.y1(d => y(d.Chicken)));
|
||||
svg.append("clipPath")
|
||||
.attr("id", "clip-below")
|
||||
.append("path")
|
||||
.attr("d", d3.area()
|
||||
.x(d => x(d.Year))
|
||||
.y0(height)
|
||||
.y1(d => y(d.Chicken)));
|
||||
|
||||
// Area differences
|
||||
svg.append("path")
|
||||
.attr("class", "area above")
|
||||
.attr("clip-path", "url(#clip-above)")
|
||||
.attr("fill", "#fdad94")
|
||||
.attr("d", d3.area()
|
||||
.x(d => x(d.Year)).y0(height).y1(d => y(d.Beef)));
|
||||
svg.append("path")
|
||||
.attr("class", "area below")
|
||||
.attr("fill", "#f9e59f")
|
||||
.attr("clip-path", "url(#clip-below)")
|
||||
.attr("d", d3.area()
|
||||
.x(d => x(d.Year)).y0(0).y1(d => y(d.Beef)));
|
||||
|
||||
// Lines
|
||||
svg.append("path")
|
||||
.attr("class", "line chicken")
|
||||
.attr("fill", "none")
|
||||
.attr("stroke", "#bf980d")
|
||||
.attr("stroke-width", "2px")
|
||||
.attr("d", d3.line()
|
||||
.x(d => x(d.Year))
|
||||
.y(d => y(d.Chicken)));
|
||||
svg.append("path")
|
||||
.attr("class", "line beef")
|
||||
.attr("fill", "none")
|
||||
.attr("stroke", "#fb470e")
|
||||
.attr("stroke-width", "2px")
|
||||
.attr("d", d3.line()
|
||||
.x(d => x(d.Year))
|
||||
.y(d => y(d.Beef)));
|
||||
|
||||
|
||||
setAnnotation();
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
function setAnnotation() {
|
||||
svg.append("text")
|
||||
.attr("class", "mainlab")
|
||||
.attr("fill", "#fb470e")
|
||||
.attr("x", x(1962))
|
||||
.attr("y", y(40))
|
||||
.text("BEEF");
|
||||
|
||||
svg.append("text")
|
||||
.attr("text-anchor", "end")
|
||||
.attr("class", "mainlab")
|
||||
.attr("fill", "#bf980d")
|
||||
.attr("x", x(2017))
|
||||
.attr("y", y(65))
|
||||
.text("CHICKEN");
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user