I’m trying to do a stacked bar chart to show cash flow. My attempt here kinda works
async function cashflow() {
const data = await d3.csv("cashflow.csv");
const options = {
marginLeft: 50,
title: "Cash Flow",
subtitle: htl.html`<h4><span style="color:green">Operating</span> + <span style="color:gold">Investing</span> + <span style="color:red">Financing</span></h4>`,
caption: "Black line is net cash",
x: {
transform: (d) => new Date(d),
grid: true
},
y: {
transform: (d) => +d,
line: true,
grid: true
},
marks: [
Plot.barY(data, {x: "FY", y1: "Investing", y2: "Operating", fill: "green", opacity: 0.7}),
Plot.barY(data, {x: "FY", y1: "Financing", y2: "Investing", fill: "gold", opacity: 0.7}),
Plot.barY(data, {x: "FY", y: "Financing", fill: "red", opacity: 0.7}),
Plot.barY(data, {x: "FY", y: (d) => (+d.Operating + +d.Investing + +d.Financing), stroke: "black"}),
Plot.lineY(data, {x: "FY", y: (d) => (+d.Operating + +d.Investing + +d.Financing), stroke: "black"}),
Plot.ruleY([0])
]
};
const plot = Plot.plot(options);
const div = document.querySelector("#cashflow");
div.append(plot);
}
cashflow();
My example data looks like
FY | Operating | Investing | Financing |
---|---|---|---|
2015/03/31 | 159 | -552 | 551 |
2016/03/31 | 78 | -1384 | 1892 |
2017/03/31 | -40 | 2814 | -411 |
2018/03/31 | -158 | 7853 | -388 |
2019/03/31 | 410 | -7326 | -2043 |
2020/03/31 | -196 | 2316 | 6 |
2021/03/31 | 73 | -731 | 53 |
2022/03/31 | -702 | 5906 | 894 |
2023/03/31 | -177 | 12555 | -12096 |
While I get a graph along the lines I want, it comes with this warning
Warning: some data associated with the x scale are dates. Dates are typically associated with a “utc” or “time” scale rather than a “band” scale. If you are using a bar mark, you probably want a rect mark with the interval option instead; if you are using a group transform, you probably want a bin transform instead. If you want to treat this data as ordinal, you can specify the interval of the x scale (e.g., d3.utcDay), or you can suppress this warning by setting the type of the x scale to “band”.
What I’ve tried is
Plot.rectY(data, {x: "FY", y1: "Investing", y2: "Operating", fill: "green", opacity: 0.7, interval: d3.utcYear}),
Plot.rectY(data, {x: "FY", y1: "Financing", y2: "Investing", fill: "gold", opacity: 0.7, interval: d3.utcYear}),
Plot.rectY(data, {x: "FY", y: "Financing", fill: "red", opacity: 0.7, interval: d3.utcYear}),
Plot.rectY(data, {x: "FY", y: (d) => (+d.Operating + +d.Investing + +d.Financing), stroke: "black"}),
which doesn’t work (no errors, just no output).
If I use areaY instead of barY, the error message about the dates goes, but I want a bar chart.