2024-03-06 09:56:59 -05:00
|
|
|
import { Controller } from "@hotwired/stimulus";
|
|
|
|
import tailwindColors from "@maybe/tailwindcolors";
|
|
|
|
import * as d3 from "d3";
|
|
|
|
|
|
|
|
export default class extends Controller {
|
2024-03-19 09:10:40 -04:00
|
|
|
static values = { series: Object };
|
2024-03-06 09:56:59 -05:00
|
|
|
|
|
|
|
connect() {
|
|
|
|
this.renderChart(this.seriesValue);
|
2024-03-11 22:42:18 +01:00
|
|
|
document.addEventListener("turbo:load", this.renderChart);
|
2024-03-06 09:56:59 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
disconnect() {
|
2024-03-11 22:42:18 +01:00
|
|
|
document.removeEventListener("turbo:load", this.renderChart);
|
2024-03-06 09:56:59 -05:00
|
|
|
}
|
|
|
|
|
2024-03-11 22:42:18 +01:00
|
|
|
renderChart = () => {
|
2024-03-19 09:10:40 -04:00
|
|
|
const data = this.prepareData(this.seriesValue);
|
|
|
|
this.drawChart(data);
|
|
|
|
};
|
2024-03-06 09:56:59 -05:00
|
|
|
|
2024-03-19 09:10:40 -04:00
|
|
|
prepareData(series) {
|
|
|
|
return series.values.map((d) => ({
|
2024-03-06 09:56:59 -05:00
|
|
|
date: new Date(d.date + "T00:00:00"),
|
2024-04-18 10:32:36 -04:00
|
|
|
value: d.value.amount ? +d.value.amount : +d.value,
|
2024-03-06 09:56:59 -05:00
|
|
|
}));
|
2024-03-19 09:10:40 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
drawChart(data) {
|
2024-03-06 09:56:59 -05:00
|
|
|
const chartContainer = d3.select(this.element);
|
|
|
|
chartContainer.selectAll("*").remove();
|
|
|
|
const initialDimensions = {
|
|
|
|
width: chartContainer.node().clientWidth,
|
|
|
|
height: chartContainer.node().clientHeight,
|
|
|
|
};
|
|
|
|
|
|
|
|
const svg = chartContainer
|
|
|
|
.append("svg")
|
|
|
|
.attr("width", initialDimensions.width)
|
|
|
|
.attr("height", initialDimensions.height)
|
|
|
|
.attr("viewBox", [
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
initialDimensions.width,
|
|
|
|
initialDimensions.height,
|
|
|
|
])
|
|
|
|
.attr("style", "max-width: 100%; height: auto; height: intrinsic;");
|
|
|
|
|
|
|
|
const margin = { top: 0, right: 0, bottom: 0, left: 0 };
|
|
|
|
const width = initialDimensions.width - margin.left - margin.right;
|
|
|
|
const height = initialDimensions.height - margin.top - margin.bottom;
|
|
|
|
|
|
|
|
const isLiability = this.classificationValue === "liability";
|
2024-03-19 09:10:40 -04:00
|
|
|
const trendDirection = data[data.length - 1].value - data[0].value;
|
2024-03-06 09:56:59 -05:00
|
|
|
let lineColor;
|
|
|
|
|
|
|
|
if (trendDirection > 0) {
|
|
|
|
lineColor = isLiability
|
|
|
|
? tailwindColors.error
|
|
|
|
: tailwindColors.green[500];
|
|
|
|
} else if (trendDirection < 0) {
|
|
|
|
lineColor = isLiability
|
|
|
|
? tailwindColors.green[500]
|
|
|
|
: tailwindColors.error;
|
|
|
|
} else {
|
|
|
|
lineColor = tailwindColors.gray[500];
|
|
|
|
}
|
|
|
|
|
|
|
|
const xScale = d3
|
|
|
|
.scaleTime()
|
|
|
|
.rangeRound([0, width])
|
|
|
|
.domain(d3.extent(data, (d) => d.date));
|
|
|
|
|
|
|
|
const PADDING = 0.05;
|
2024-03-19 09:10:40 -04:00
|
|
|
const dataMin = d3.min(data, (d) => d.value);
|
|
|
|
const dataMax = d3.max(data, (d) => d.value);
|
2024-03-06 09:56:59 -05:00
|
|
|
const padding = (dataMax - dataMin) * PADDING;
|
|
|
|
|
|
|
|
const yScale = d3
|
|
|
|
.scaleLinear()
|
|
|
|
.rangeRound([height, 0])
|
|
|
|
.domain([dataMin - padding, dataMax + padding]);
|
|
|
|
|
|
|
|
const line = d3
|
|
|
|
.line()
|
|
|
|
.x((d) => xScale(d.date))
|
2024-03-19 09:10:40 -04:00
|
|
|
.y((d) => yScale(d.value));
|
2024-03-06 09:56:59 -05:00
|
|
|
|
|
|
|
svg
|
|
|
|
.append("path")
|
|
|
|
.datum(data)
|
|
|
|
.attr("fill", "none")
|
|
|
|
.attr("stroke", lineColor)
|
|
|
|
.attr("stroke-width", 2)
|
|
|
|
.attr("d", line);
|
|
|
|
}
|
|
|
|
}
|