Published
Edited
Aug 11, 2020
Insert cell
Insert cell
Insert cell
Insert cell
chart = {
const svg = d3.create("svg")
.attr("viewBox", [0, 0, width, height]);

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

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

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

const colorId = DOM.uid("color");

svg.append("linearGradient")
.attr("id", colorId.id)
.attr("gradientUnits", "userSpaceOnUse")
.attr("x1", 0)
.attr("x2", width)
.selectAll("stop")
.data(data)
.join("stop")
.attr("offset", d => x(d.year) / width)
.attr("stop-color", d => color(d.balbudgt));

svg.append("path")
.datum(data)
.attr("fill", "none")
.attr("stroke", colorId)
.attr("stroke-width", 5)
.attr("stroke-linejoin", "round")
.attr("stroke-linecap", "round")
.attr("d", line);
svg.append("text") // text label for the x axis
.attr("x", 50 )
.attr("y", 10)
.style("text-anchor", "middle")
.style("font-size", "11px")
.style("font-family", "sans-serif")
.text("Expenditure Gap");

return svg.node();
}
Insert cell
filteredData = dataset.filter(d => d.country==countryselected)
Insert cell
Insert cell
Insert cell
function arrayToCSV(objArray) {
const array = typeof objArray !== 'object' ? JSON.parse(objArray) : objArray;
let str = `${Object.keys(array[0]).map(value => `"${value}"`).join(",")}` + '\r\n';

return array.reduce((str, next) => {
str += `${Object.values(next).map(value => `"${value}"`).join(",")}` + '\r\n';
return str;
}, str);
}
Insert cell
filt = arrayToCSV(filteredData)
Insert cell
d3.csvParse(filt, d3.autoType)
Insert cell
data = Object.assign(d3.csvParse(filt, d3.autoType),
{
//y: "GDP Growth",
conditions: ["No Balanced Budget Provision", "Balanced Budget Provision"],
//labels: ["Balanced", "Not Balanced"],
colors: ["#aaaaaa", "deepskyblue"]
});
Insert cell
d3.min(data, d => d.ex_gap)
Insert cell
dataset = {
const parseDate = d3.utcParse("%Y-%m-%d %H:%M");
return Object.assign(d3.csvParse(await FileAttachment("bbvsexg.csv").text(), d3.autoType),
{
//y: "GDP Growth",
conditions: ["No Balanced Budget Provision", "Balanced Budget Provision"],
//labels: ["Balanced", "Not Balanced"],
colors: ["deepskyblue", "#aaaaaa"]
});
}
Insert cell
line = d3.line()
.curve(d3.curveStep)
.x(d => x(d.year))
.y(d => y(d.ex_gap))
Insert cell
x = d3.scaleLinear()
.domain(d3.extent(data, d => d.year))
.rangeRound([margin.left, width - margin.right])
Insert cell
y = d3.scaleLinear()
.domain(d3.extent(data, d => d.ex_gap)).nice()
.rangeRound([height - margin.bottom, margin.top])
Insert cell
d3.extent(data, d => d.ex_gap)
Insert cell
height
Insert cell
color = d3.scaleOrdinal(
data.conditions === undefined ? data.map(d => d.balbudgt) : data.conditions,
data.colors === undefined ? d3.schemeCategory10 : data.colors
).unknown("black")
Insert cell
xAxis = g => g
.attr("transform", `translate(0,${height - margin.bottom})`)
.call(d3.axisBottom(x).ticks(width / 80)
.tickSizeOuter(0)
.tickFormat(formatDate))
//.call(g => g.select(".domain").remove())
Insert cell
formatDate = d=> d
Insert cell
yAxis = g => g
.attr("transform", `translate(${margin.left},0)`)
.call(d3.axisLeft(y))
//.call(g => g.select(".domain").remove())
.call(g => g.select(".tick:last-of-type text").append("tspan").text(data.y))
Insert cell
grid = g => g
.attr("stroke", "currentColor")
.attr("stroke-opacity", 0.1)
.call(g => g.append("g")
.selectAll("line")
.data(x.ticks())
.join("line")
.attr("x1", d => 0.5 + x(d))
.attr("x2", d => 0.5 + x(d))
.attr("y1", margin.top)
.attr("y2", height - margin.bottom))
.call(g => g.append("g")
.selectAll("line")
.data(y.ticks())
.join("line")
.attr("y1", d => 0.5 + y(d))
.attr("y2", d => 0.5 + y(d))
.attr("x1", margin.left)
.attr("x2", width - margin.right));
Insert cell
margin = ({top: 25, right: 20, bottom: 30, left: 40})
Insert cell
height = 500
Insert cell
d3 = require("d3@5")
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more