Published
Edited
Apr 8, 2019
1 fork
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
monthly_SP_chart = {

const svg = d3.select(DOM.svg(width_monthly_SP, height_monthly_SP));
// .style("width", "auto")
// .style("height", "100%")
// .style("margin", "20 0");

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

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

// const path = svg.append("g")
// .attr("fill", "none")
// .attr("stroke", "steelblue")
// .attr("stroke-width", 2)
// .attr("stroke-linejoin", "round")
// .attr("stroke-linecap", "round")
// .selectAll("path")
// .data(monthly_SP.series)
// .join("path")
// .style("mix-blend-mode", "multiply")
// .attr("d", d => line_monthly_SP(d.values))
// .attr("stroke", function(d, i){return z_monthly_SP(i);} )
// ;

// svg.call(hover, path);
// svg.append("path")
// .data(monthly_SP) // 10. Binds data to the line
// .attr("class", "line") // Assign a class for styling
// .attr("d", P_reference); // 11. Calls the line generator
svg.append("path")
.attr("fill", "none")
.attr("stroke", "steelblue")
.attr("stroke-width", 2)
.attr("stroke-linejoin", "round")
.attr("stroke-linecap", "round")
.data([monthly_SP])
.attr("class", "line")
.attr("d", line_P);
svg.append("path")
.attr("fill", "none")
.attr("stroke", "red")
.attr("stroke-width", 2)
.attr("stroke-linejoin", "round")
.attr("stroke-linecap", "round")
.data([monthly_SP])
.attr("class", "line")
.attr("d", line_S);
svg.append("path")
.attr("fill", "none")
.attr("stroke", "darkblue")
.attr("stroke-width", 1.5)
.attr("stroke-linejoin", "round")
.attr("stroke-linecap", "round")
.data([monthly_SP])
.attr("class", "line")
.attr("d", line_P_reference);

svg.append("path")
.attr("fill", "none")
.attr("stroke", "darkred")
.attr("stroke-width", 1.5)
.attr("stroke-linejoin", "round")
.attr("stroke-linecap", "round")
.data([monthly_SP])
.attr("class", "line")
.attr("d", line_S_reference);

return svg.node();

}
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
{
var margin = {top: 20, right: 30, bottom: 30, left: 60},
width = 800 - margin.left - margin.right,
height = 600 - margin.top - margin.bottom;
var data = [
{month: "Q1-2000", domestic: 69607, international: 35476},
{month: "Q2-2000", domestic: 226493, international: 97112},
{month: "Q3-2000", domestic: 189292, international: 126820},
{month: "Q4-2000", domestic: 26417, international: 6786},
{month: "Q1-2001", domestic: -96984, international: 42221},
{month: "Q2-2001", domestic: -473359, international: 36259},
{month: "Q3-2001", domestic: -387386, international: 96568},
{month: "Q4-2001", domestic: -396983, international: -137512},
{month: "Q1-2002", domestic: -1099842, international: -331636},
{month: "Q2-2002", domestic: -412967, international: -72752},
{month: "Q3-2002", domestic: -789387, international: -61523},
{month: "Q4-2002", domestic: -602512, international: -125041}
];

var series = d3.stack()
.keys(["domestic", "international"])
.offset(d3.stackOffsetDiverging)
(data);
var svg2 = d3.select("#profits_AA")
.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 + ")");

var x = d3.scaleBand()
.domain(data.map(function(d) { return d.month; }))
.rangeRound([margin.left, width - margin.right])
.padding(0.1);

var y = d3.scaleLinear()
.domain([d3.min(series, stackMin), d3.max(series, stackMax)])
.rangeRound([height - margin.bottom, margin.top]);

var z = d3.scaleOrdinal().domain(["domestic", "international"]).range(["steelblue", "lightblue"])

svg2.append("g")
.selectAll("g")
.data(series)
.enter().append("g")
.attr("fill", function(d) { return z(d.key); })
.selectAll("rect")
.data(function(d) { return d; })
.enter().append("rect")
.attr("width", x.bandwidth)
.attr("x", function(d) { return x(d.data.month); })
.attr("y", function(d) { return y(d[1]); })
.attr("height", function(d) { return y(d[0]) - y(d[1]); })

svg2.append("g")
.attr("transform", "translate(0," + y(0) + ")")
.call(d3.axisBottom(x))
.call(g => g.select(".tick:last-of-type text").clone()
.attr("x",-20)
.attr("y",-10)
.attr("text-anchor", "start")
.attr("font-weight", "bold")
.text("Date"));

svg2.append("g")
.attr("transform", "translate(" + margin.left + ",0)")
.call(d3.axisLeft(y))
.call(g => g.select(".tick:last-of-type text").clone()
.attr("x", 3)
.attr("y", -30)
.attr("text-anchor", "start")
.attr("font-weight", "bold")
.text("Profits"));

function stackMin(serie) {
return d3.min(serie, function(d) { return d[0]; });
}

function stackMax(serie) {
return d3.max(serie, function(d) { return d[1]; });
}
}
Insert cell
Insert cell
Insert cell
Insert cell
y_profits_AA = d3.scaleLinear()
.domain([d3.min(profits_AA, function(d) {
return Math.min(d.Domestic, d.International)}), d3.max(profits_AA, function(d) {
return Math.max(d.Domestic, d.International); })]).nice()
.range([height_monthly_SP - margin_monthly_SP.bottom, margin_monthly_SP.top])
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
monthly_SP = d3.csv("https://raw.githubusercontent.com/caz2114/datavis-hw5/master/data/monthly_seats_passengers.csv").then(
function(data) {
data.forEach(function(d) {
d.Month = parseTime(d.Month);
d.Passengers = +d.Passengers;
d.Seats = +d.Seats;
});
return data;
}
)


Insert cell
Insert cell
Insert cell
Insert cell
x_monthly_SP = d3.scaleTime()
.domain(d3.extent(monthly_SP, function(d) { return d.Month; }))
// .domain([new Date("2001-01"), new Date("2004-12")])
.range([margin_monthly_SP.left, width_monthly_SP - margin_monthly_SP.right])
Insert cell
y_monthly_SP = d3.scaleLinear()
.domain([0, d3.max(monthly_SP, function(d) {
return Math.max(d.Passengers, d.Seats); })]).nice()
.range([height_monthly_SP - margin_monthly_SP.bottom, margin_monthly_SP.top])
Insert cell
Insert cell
xAxis_monthly_SP = g => g
.attr("transform", `translate(0,${height_monthly_SP - margin_monthly_SP.bottom})`)
.call(d3.axisBottom(x_monthly_SP).ticks(width_monthly_SP / 80).tickSizeOuter(0))
.call(g => g.select(".tick:last-of-type text").clone()
.attr("x",-30)
.attr("y",-10)
.attr("text-anchor", "start")
.attr("font-weight", "bold")
.text("Date"))
Insert cell
Insert cell
line_monthly_SP = d3.line()
.defined(d => !isNaN(d))
.x((d, i) => x_monthly_SP(monthly_SP.dates[i]))
.y(d => y_monthly_SP(d))
Insert cell
line_P = d3.line()
.x(function(d) { return x_monthly_SP(d.Month); })
.y(function(d) { return y_monthly_SP(d.Passengers); });

Insert cell
Insert cell
// P_reference_data = {
// var parseTime = d3.timeParse("%B %Y");
// const data = [];
// for (var i = 0; i < pdata.length; i++){
// const temp = {Month: parseTime(pdata[i]["Month"]) , Passengers: pdata[8]["Passengers"]};
// data.push(temp);
// }
// return {
// data: data
// }

// }
Insert cell
Insert cell
line_P_reference = d3.line()
.x(function(d) { return x_monthly_SP(d.Month); })
.y(function(d) { return y_monthly_SP(397011); });
Insert cell
Insert cell
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