Public
Edited
Apr 19, 2023
1 star
Insert cell
Insert cell
Insert cell
chartBarTotal = {
const svg = d3.create("svg")
.attr("viewBox", [0, 0, totalWidth, height]);
svg.append("g")
.selectAll("rect")
.data([["Historical observations", totalHistorical]])
.join("rect")
.attr("x", d => xTotal(d[0]))
.attr("y", d => yYearly(d[1]))
.attr("height", d => yYearly(0) - yYearly(d[1]))
.attr("width", xTotal.bandwidth())
.attr("stroke", "black")
.attr("stroke-width", 3)
.attr("fill", colorLightWhiteAlpha)
.append("title")
.text(d => `The historical observed annual precipitations are on average: ${formatTotal(d[1])} mm.`);

svg.append("g")
.selectAll("rect")
.data([["Projected Mean", totalProjected]])
.join("rect")
.attr("x", d => xTotal(d[0]))
.attr("y", d => yYearly(d[1]))
.attr("height", d => yYearly(0) - yYearly(d[1]))
.attr("width", xTotal.bandwidth())
.attr("stroke", colorLightBlueAlpha)
.attr("stroke-width", 2)
.attr("fill", colorLightBlueAlpha)
.append("title")
.text(d => `The projected annual precipitations are: ${formatTotal(d[1])} mm.`);
svg.append("g")
.selectAll("rect")
.data([["Projected Mean", totalHistorical]])
.join("rect")
.attr("x", d => xTotal(d[0]))
.attr("y", d => yYearly(d[1]))
.attr("height", d => yYearly(0) - yYearly(d[1]))
.attr("width", xTotal.bandwidth())
.attr("stroke", "black")
.attr("stroke-width", 3)
.attr("fill", colorLightWhiteAlpha)
.append("title")
.text(d => `The projected annual precipitations are: ${formatTotal(totalProjected)} mm.`);

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

svg.append("g")
.call(yYearlyAxis);
// text label for the y axis
svg.append("g")
.append("text")
.attr("class", "indivAxis")
.attr("transform", "rotate(-90)")
.attr("y", margin.left * 0.25)
.attr("x",-height/2+margin.top)
.attr("dy", "1em")
.style("text-anchor", "middle")
.style("font-family", fontFamily)
.style("fill", fontFill)
.style("font-size", fontSize)
.text("Annual precipitations (mm)");

return svg.node();
}
Insert cell
Insert cell
chartBarMonthly = {
const svg = d3.create("svg")
.attr("viewBox", [0, 0, totalWidth, height]);
svg.append("g")
.selectAll("rect")
.data(data)
.join("rect")
.attr("x", d => x(d.Month))
.attr("y", d => yMonthly(d.Projected))
.attr("height", d => yMonthly(0) - yMonthly(d.Projected))
.attr("width", x.bandwidth())
.attr("stroke", colorLightBlueAlpha)
.attr("stroke-width", 2)
.attr("fill", colorLightBlueAlpha)//d => color(d.color))
.append("title")
.text(d => `The projected precipitations in ${d.Month} are: ${formatTotal(d.Projected)} mm.`);
svg.append("g")
.selectAll("rect")
.data(data)
.join("rect")
.attr("x", d => x(d.Month))
.attr("y", d => yMonthly(d.Historical))
.attr("height", d => yMonthly(0) - yMonthly(d.Historical))
.attr("width", x.bandwidth())
.attr("stroke", "black")
.attr("stroke-width", 3)
.attr("fill", colorLightWhiteAlpha)//d => color(d.color))
.append("title")
.text(d => `The historical observed precipitations in ${d.Month} are: ${formatTotal(d.Historical)} mm.`);

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

svg.append("g")
.call(yMonthlyAxis);
// text label for the y axis
svg.append("g")
.append("text")
.attr("class", "indivAxis")
.attr("transform", "rotate(-90)")
.attr("y", margin.left * 0.25)
.attr("x",-height/2+margin.top)
.attr("dy", "1em")
.style("text-anchor", "middle")
.style("font-family", fontFamily)
.style("fill", fontFill)
.style("font-size", fontSize)
.text("Monthly precipitations (mm)");

return svg.node();
}
Insert cell
Insert cell
chartBarPctChange = {
const svg = d3.create("svg")
.attr("viewBox", [0, 0, totalWidth, height*0.6]);
svg.append("g")
.selectAll("rect")
.data(data)
.join("rect")
.attr("x", d => x(d.Month))
.attr("y", d => yPctChange(Math.max(d.PctChange, 0)))
.attr("height", d => Math.abs(yPctChange(0) - yPctChange(d.PctChange)))
.attr("width", x.bandwidth())
.attr("stroke", d => d.PctChange < 0 ? "#CACACA" : colorLightBlueAlpha)
.attr("stroke-width", 2)
.attr("fill", d => d.PctChange < 0 ? colorLightWhiteAlpha : colorLightBlueAlpha)
.append("title")
.text(d => `The relative change between historal observations and projected mean is: ${formatRate(d.PctChange)}.`);

svg.append("g")
.call(xAxis)
.attr("transform", `translate(0,${yPctChange(0)})`);

svg.append("g")
.call(yPctChangeAxis);
// text label for the y axis
svg.append("g")
.append("text")
.attr("class", "indivAxis")
.attr("transform", "rotate(-90)")
.attr("y", margin.left * 0.25)
.attr("x",-height*0.6/2+margin.top)
.attr("dy", "1em")
.style("text-anchor", "middle")
.style("font-family", fontFamily)
.style("fill", fontFill)
.style("font-size", fontSize)
.text("Change in precipitations (mm)");

return svg.node();
}
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
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