The personal finance app for everyone (by everyone)
Ruby
import { Controller } from "@hotwired/stimulus";
import * as d3 from "d3";
import { CHART_TOOLTIP_CLASSES } from "utils/chart_tooltip";
import SettledResize from "utils/settled_resize";
// Grouped bar chart used by the dashboard "money flow" widget — each month
// shows an expense bar (red) and an income bar (green) side by side.
// Modeled after time_series_chart_controller's lifecycle (install/teardown,
// ResizeObserver, turbo:load reinstall, page-relative tooltip positioning)
// but with scaleBand/scaleLinear instead of a line.
// Breathing room between neighbouring month labels before they read as touching.
const LABEL_GAP_PX = 8;
const MARGIN = { top: 16, right: 4, bottom: 24, left: 4 };
const BAND_PADDING = 0.3;
export default class extends Controller {
static values = {
data: Array,
currency: { type: String, default: "USD" },
incomeLabel: { type: String, default: "Income" },
expenseLabel: { type: String, default: "Expenses" },
};
_settledResize = null;
connect() {
this._install();
document.addEventListener("turbo:load", this._reinstall);
// The month labels step down (full, short, every other) as the chart
// narrows: a resize across one of those steps redraws then, rather than
// stretch labels that no longer fit, or stay cut back, until the end.
this._settledResize = new SettledResize(this.element, this._reinstall, {
layoutChanges: (from, to) =>
this._labelFit(from.width) !== this._labelFit(to.width),
margins: { left: MARGIN.left, right: MARGIN.right },
});
this._settledResize.start();
}
disconnect() {
this._teardown();
document.removeEventListener("turbo:load", this._reinstall);
this._settledResize?.stop();
}
_reinstall = () => {
this._teardown();
this._install();
this._settledResize?.drew();
};
_teardown() {
d3.select(this.element).selectAll("*").remove();
}
_install() {
const width = this.element.clientWidth;
const height = this.element.clientHeight;
const data = this.dataValue || [];
if (width < 50 || height < 50 || data.length === 0) return;
const margin = MARGIN;
const innerWidth = width - margin.left - margin.right;
const innerHeight = height - margin.top - margin.bottom;
const svg = d3
.select(this.element)
.append("svg")
.attr("width", width)
.attr("height", height)
.attr("viewBox", [0, 0, width, height]);
const group = svg
.append("g")
.attr("transform", `translate(${margin.left},${margin.top})`);
const series = ["income", "expense"];
const seriesColor = { expense: "var(--color-gray-400)", income: "var(--color-success)" };
const x0 = d3
.scaleBand()
.domain(data.map((d) => d.label))
.range([0, innerWidth])
.padding(BAND_PADDING);
const x1 = d3.scaleBand().domain(series).range([0, x0.bandwidth()]).padding(0.15);
const maxValue = d3.max(data, (d) => Math.max(d.income, d.expense)) || 1;
const y = d3.scaleLinear().domain([0, maxValue * 1.1]).range([innerHeight, 0]);
// Floor tiny-but-nonzero bars (e.g. an in-progress month) at 2px so they stay visible.
const barHeight = (v) => (v > 0 ? Math.max(2, innerHeight - y(v)) : 0);
const tooltip = d3
.select(this.element)
.append("div")
.attr("class", `${CHART_TOOLTIP_CLASSES} opacity-0 top-0`);
const showTooltip = (event, month, key) => {
const estimatedTooltipWidth = 200;
const pageWidth = document.body.clientWidth;
const tooltipX = event.pageX + 10;
const overflowX = tooltipX + estimatedTooltipWidth - pageWidth;
const adjustedX = overflowX > 0 ? event.pageX - overflowX - 20 : tooltipX;
tooltip
.html(this._tooltipTemplate(month, key))
.style("opacity", 1)
.style("left", `${adjustedX}px`)
.style("top", `${event.pageY - 10}px`);
};
const hideTooltip = () => tooltip.style("opacity", 0);
const monthGroups = group
.selectAll("g.month")
.data(data)
.join("g")
.attr("class", "month")
.attr("transform", (d) => `translate(${x0(d.label)},0)`);
monthGroups
.selectAll("rect")
.data((d) => series.map((key) => ({ key, value: d[key], month: d })))
.join("rect")
.attr("x", (d) => x1(d.key))
.attr("y", (d) => innerHeight - barHeight(d.value))
.attr("width", x1.bandwidth())
.attr("height", (d) => barHeight(d.value))
.attr("rx", 3)
.attr("fill", (d) => seriesColor[d.key])
// In-progress month (period capped at today) reads as provisional.
.attr("fill-opacity", (d) => (d.month.partial ? 0.5 : 1))
.on("mousemove", (event, d) => showTooltip(event, d.month, d.key))
.on("mouseleave", hideTooltip);
const axisLabels = group
.append("g")
.attr("transform", `translate(0,${innerHeight})`)
.call(d3.axisBottom(x0).tickSize(0))
.call((g) => g.select(".domain").remove())
.selectAll("text")
.attr("class", (_d, i) => (data[i].highlighted ? "text-primary fill-current" : "text-secondary fill-current"))
.style("font-size", "12px")
.style("font-weight", (_d, i) => (data[i].highlighted ? 600 : 500));
this._fitAxisLabels(axisLabels, data, x0.step());
}
// The month labels are sized by the locale, not by the chart: "Mar 2026" fits
// a phone, "Mar de 2026" (ca/es/pt) does not and overlaps its neighbours.
// Measure what actually rendered and step down until it fits — full label,
// then the abbreviated month, then every other tick. Measuring beats guessing
// at a character width, which varies by locale, font and zoom.
_fitAxisLabels(labels, data, step) {
this._labelWidths = null;
if (labels.empty()) return;
// Both widths, so _labelFit can tell the step at any other chart width.
const widest = () => d3.max(labels.nodes(), (node) => node.getComputedTextLength()) || 0;
const full = widest();
labels.text((_d, i) => data[i].short_label ?? data[i].label);
this._labelWidths = { full, short: widest(), count: data.length };
const fit = this._labelFit(this.element.clientWidth, step);
if (fit === "full") {
labels.text((_d, i) => data[i].label);
return;
}
if (fit === "short") return;
// Still too wide (a very narrow column): thin out rather than overlap.
// The parity is taken from the highlighted month rather than fixed at even,
// so that month survives without being an exception to the pattern — it
// sits last (build_money_flow_data counts down to the selected month), and
// keeping it on top of every even index left the final two labels one step
// apart, the very spacing that had just been measured as too tight.
const highlightedIndex = data.findIndex((d) => d.highlighted);
const keepParity = highlightedIndex >= 0 ? highlightedIndex % 2 : 0;
labels.style("display", (_d, i) => (i % 2 === keepParity ? null : "none"));
}
// Which labels fit a chart this wide: "full", "short" or "alternate" (every
// other short label). Null before the labels were measured.
_labelFit(width, step = null) {
const widths = this._labelWidths;
if (!widths) return null;
const bandStep =
step ??
d3
.scaleBand()
.domain(d3.range(widths.count))
.range([0, width - MARGIN.left - MARGIN.right])
.padding(BAND_PADDING)
.step();
const room = bandStep - LABEL_GAP_PX;
if (widths.full <= room) return "full";
if (widths.short <= room) return "short";
return "alternate";
}
_tooltipTemplate(month, key) {
const label = key === "income" ? this.incomeLabelValue : this.expenseLabelValue;
// Match the bar/legend palette — expenses render gray, not destructive red.
const color = key === "income" ? "var(--color-success)" : "var(--color-gray-400)";
return `
<div class="text-xs text-secondary mb-1">${month.label}</div>
<div class="flex items-center gap-1.5 text-primary font-medium tabular-nums">
<span class="inline-block w-2 h-2 rounded-full" style="background-color: ${color};"></span>
${label}: ${this._formatCurrency(month[key])}
</div>
`;
}
_formatCurrency(value) {
try {
return new Intl.NumberFormat(undefined, {
style: "currency",
currency: this.currencyValue,
maximumFractionDigits: 0,
}).format(value);
} catch {
return value;
}
}
}