2024-10-14 23:09:27 +02:00
|
|
|
import { Controller } from "@hotwired/stimulus";
|
|
|
|
import * as d3 from "d3";
|
2024-04-22 11:44:26 -06:00
|
|
|
|
|
|
|
export default class extends Controller {
|
|
|
|
static values = {
|
|
|
|
data: Object,
|
2024-04-29 14:56:38 +01:00
|
|
|
strokeWidth: { type: Number, default: 2 },
|
2024-04-22 11:44:26 -06:00
|
|
|
useLabels: { type: Boolean, default: true },
|
|
|
|
useTooltip: { type: Boolean, default: true },
|
2024-10-14 23:09:27 +02:00
|
|
|
};
|
2024-04-22 11:44:26 -06:00
|
|
|
|
2024-10-11 14:40:13 -04:00
|
|
|
_d3SvgMemo = null;
|
|
|
|
_d3GroupMemo = null;
|
|
|
|
_d3Tooltip = null;
|
|
|
|
_d3InitialContainerWidth = 0;
|
|
|
|
_d3InitialContainerHeight = 0;
|
|
|
|
_normalDataPoints = [];
|
2025-02-21 11:57:59 -05:00
|
|
|
_resizeObserver = null;
|
2024-04-22 11:44:26 -06:00
|
|
|
|
|
|
|
connect() {
|
2024-10-14 23:09:27 +02:00
|
|
|
this._install();
|
|
|
|
document.addEventListener("turbo:load", this._reinstall);
|
2025-02-21 11:57:59 -05:00
|
|
|
this._setupResizeObserver();
|
2024-04-22 11:44:26 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
disconnect() {
|
2024-10-14 23:09:27 +02:00
|
|
|
this._teardown();
|
|
|
|
document.removeEventListener("turbo:load", this._reinstall);
|
2025-02-21 11:57:59 -05:00
|
|
|
this._resizeObserver?.disconnect();
|
2024-04-22 11:44:26 -06:00
|
|
|
}
|
|
|
|
|
2024-10-11 14:40:13 -04:00
|
|
|
_reinstall = () => {
|
2024-10-14 23:09:27 +02:00
|
|
|
this._teardown();
|
|
|
|
this._install();
|
|
|
|
};
|
2024-04-22 11:44:26 -06:00
|
|
|
|
2024-10-11 14:40:13 -04:00
|
|
|
_teardown() {
|
2024-10-14 23:09:27 +02:00
|
|
|
this._d3SvgMemo = null;
|
|
|
|
this._d3GroupMemo = null;
|
|
|
|
this._d3Tooltip = null;
|
|
|
|
this._normalDataPoints = [];
|
2024-04-22 11:44:26 -06:00
|
|
|
|
2024-10-14 23:09:27 +02:00
|
|
|
this._d3Container.selectAll("*").remove();
|
2024-04-22 11:44:26 -06:00
|
|
|
}
|
|
|
|
|
2024-10-11 14:40:13 -04:00
|
|
|
_install() {
|
2024-10-14 23:09:27 +02:00
|
|
|
this._normalizeDataPoints();
|
|
|
|
this._rememberInitialContainerSize();
|
|
|
|
this._draw();
|
2024-04-22 11:44:26 -06:00
|
|
|
}
|
|
|
|
|
2024-10-11 14:40:13 -04:00
|
|
|
_normalizeDataPoints() {
|
|
|
|
this._normalDataPoints = (this.dataValue.values || []).map((d) => ({
|
2024-11-19 16:23:21 -05:00
|
|
|
date: new Date(`${d.date}T00:00:00Z`),
|
2025-02-21 11:57:59 -05:00
|
|
|
date_formatted: d.date_formatted,
|
|
|
|
trend: d.trend,
|
2024-10-14 23:09:27 +02:00
|
|
|
}));
|
2024-04-22 11:44:26 -06:00
|
|
|
}
|
|
|
|
|
2024-10-11 14:40:13 -04:00
|
|
|
_rememberInitialContainerSize() {
|
2024-10-14 23:09:27 +02:00
|
|
|
this._d3InitialContainerWidth = this._d3Container.node().clientWidth;
|
|
|
|
this._d3InitialContainerHeight = this._d3Container.node().clientHeight;
|
2024-04-22 11:44:26 -06:00
|
|
|
}
|
|
|
|
|
2024-10-11 14:40:13 -04:00
|
|
|
_draw() {
|
|
|
|
if (this._normalDataPoints.length < 2) {
|
2024-10-14 23:09:27 +02:00
|
|
|
this._drawEmpty();
|
2024-04-22 11:44:26 -06:00
|
|
|
} else {
|
2024-10-14 23:09:27 +02:00
|
|
|
this._drawChart();
|
2024-04-22 11:44:26 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-11 14:40:13 -04:00
|
|
|
_drawEmpty() {
|
2024-10-14 23:09:27 +02:00
|
|
|
this._d3Svg.selectAll(".tick").remove();
|
|
|
|
this._d3Svg.selectAll(".domain").remove();
|
2024-04-22 11:44:26 -06:00
|
|
|
|
2024-10-14 23:09:27 +02:00
|
|
|
this._drawDashedLineEmptyState();
|
|
|
|
this._drawCenteredCircleEmptyState();
|
2024-04-22 11:44:26 -06:00
|
|
|
}
|
|
|
|
|
2024-10-11 14:40:13 -04:00
|
|
|
_drawDashedLineEmptyState() {
|
|
|
|
this._d3Svg
|
2024-04-22 11:44:26 -06:00
|
|
|
.append("line")
|
2024-10-11 14:40:13 -04:00
|
|
|
.attr("x1", this._d3InitialContainerWidth / 2)
|
2024-04-22 11:44:26 -06:00
|
|
|
.attr("y1", 0)
|
2024-10-11 14:40:13 -04:00
|
|
|
.attr("x2", this._d3InitialContainerWidth / 2)
|
|
|
|
.attr("y2", this._d3InitialContainerHeight)
|
2025-02-12 11:22:30 -05:00
|
|
|
.attr("stroke", "var(--color-gray-300)")
|
2024-10-14 23:09:27 +02:00
|
|
|
.attr("stroke-dasharray", "4, 4");
|
2024-04-22 11:44:26 -06:00
|
|
|
}
|
|
|
|
|
2024-10-11 14:40:13 -04:00
|
|
|
_drawCenteredCircleEmptyState() {
|
|
|
|
this._d3Svg
|
2024-04-22 11:44:26 -06:00
|
|
|
.append("circle")
|
2024-10-11 14:40:13 -04:00
|
|
|
.attr("cx", this._d3InitialContainerWidth / 2)
|
|
|
|
.attr("cy", this._d3InitialContainerHeight / 2)
|
2024-04-22 11:44:26 -06:00
|
|
|
.attr("r", 4)
|
2025-02-12 11:22:30 -05:00
|
|
|
.style("fill", "var(--color-gray-400)");
|
2024-04-22 11:44:26 -06:00
|
|
|
}
|
|
|
|
|
2024-10-11 14:40:13 -04:00
|
|
|
_drawChart() {
|
2024-10-14 23:09:27 +02:00
|
|
|
this._drawTrendline();
|
2024-04-22 11:44:26 -06:00
|
|
|
|
|
|
|
if (this.useLabelsValue) {
|
2024-10-14 23:09:27 +02:00
|
|
|
this._drawXAxisLabels();
|
|
|
|
this._drawGradientBelowTrendline();
|
2024-04-22 11:44:26 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
if (this.useTooltipValue) {
|
2024-10-14 23:09:27 +02:00
|
|
|
this._drawTooltip();
|
|
|
|
this._trackMouseForShowingTooltip();
|
2024-04-22 11:44:26 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-11 14:40:13 -04:00
|
|
|
_drawTrendline() {
|
2024-10-14 23:09:27 +02:00
|
|
|
this._installTrendlineSplit();
|
2024-04-22 11:44:26 -06:00
|
|
|
|
2024-10-11 14:40:13 -04:00
|
|
|
this._d3Group
|
2024-04-22 11:44:26 -06:00
|
|
|
.append("path")
|
2024-10-11 14:40:13 -04:00
|
|
|
.datum(this._normalDataPoints)
|
2024-04-22 11:44:26 -06:00
|
|
|
.attr("fill", "none")
|
|
|
|
.attr("stroke", `url(#${this.element.id}-split-gradient)`)
|
2024-10-11 14:40:13 -04:00
|
|
|
.attr("d", this._d3Line)
|
2024-04-22 11:44:26 -06:00
|
|
|
.attr("stroke-linejoin", "round")
|
|
|
|
.attr("stroke-linecap", "round")
|
2024-10-14 23:09:27 +02:00
|
|
|
.attr("stroke-width", this.strokeWidthValue);
|
2024-04-22 11:44:26 -06:00
|
|
|
}
|
|
|
|
|
2024-10-11 14:40:13 -04:00
|
|
|
_installTrendlineSplit() {
|
|
|
|
const gradient = this._d3Svg
|
2024-04-22 11:44:26 -06:00
|
|
|
.append("defs")
|
|
|
|
.append("linearGradient")
|
|
|
|
.attr("id", `${this.element.id}-split-gradient`)
|
|
|
|
.attr("gradientUnits", "userSpaceOnUse")
|
2024-10-11 14:40:13 -04:00
|
|
|
.attr("x1", this._d3XScale.range()[0])
|
2024-10-14 23:09:27 +02:00
|
|
|
.attr("x2", this._d3XScale.range()[1]);
|
2024-04-22 11:44:26 -06:00
|
|
|
|
2024-10-14 23:09:27 +02:00
|
|
|
gradient
|
|
|
|
.append("stop")
|
2024-04-22 11:44:26 -06:00
|
|
|
.attr("class", "start-color")
|
|
|
|
.attr("offset", "0%")
|
2025-02-21 11:57:59 -05:00
|
|
|
.attr("stop-color", this.dataValue.trend.color);
|
2024-04-22 11:44:26 -06:00
|
|
|
|
2024-10-14 23:09:27 +02:00
|
|
|
gradient
|
|
|
|
.append("stop")
|
2024-04-22 11:44:26 -06:00
|
|
|
.attr("class", "middle-color")
|
|
|
|
.attr("offset", "100%")
|
2025-02-21 11:57:59 -05:00
|
|
|
.attr("stop-color", this.dataValue.trend.color);
|
2024-04-22 11:44:26 -06:00
|
|
|
|
2024-10-14 23:09:27 +02:00
|
|
|
gradient
|
|
|
|
.append("stop")
|
2024-04-22 11:44:26 -06:00
|
|
|
.attr("class", "end-color")
|
|
|
|
.attr("offset", "100%")
|
2025-02-12 11:22:30 -05:00
|
|
|
.attr("stop-color", "var(--color-gray-300)");
|
2024-04-22 11:44:26 -06:00
|
|
|
}
|
|
|
|
|
2024-10-11 14:40:13 -04:00
|
|
|
_setTrendlineSplitAt(percent) {
|
|
|
|
this._d3Svg
|
2024-04-22 11:44:26 -06:00
|
|
|
.select(`#${this.element.id}-split-gradient`)
|
|
|
|
.select(".middle-color")
|
2024-10-14 23:09:27 +02:00
|
|
|
.attr("offset", `${percent * 100}%`);
|
2024-04-22 11:44:26 -06:00
|
|
|
|
2024-10-11 14:40:13 -04:00
|
|
|
this._d3Svg
|
2024-04-22 11:44:26 -06:00
|
|
|
.select(`#${this.element.id}-split-gradient`)
|
|
|
|
.select(".end-color")
|
2024-10-14 23:09:27 +02:00
|
|
|
.attr("offset", `${percent * 100}%`);
|
2024-04-22 11:44:26 -06:00
|
|
|
|
2024-10-11 14:40:13 -04:00
|
|
|
this._d3Svg
|
2024-04-22 11:44:26 -06:00
|
|
|
.select(`#${this.element.id}-trendline-gradient-rect`)
|
2024-10-14 23:09:27 +02:00
|
|
|
.attr("width", this._d3ContainerWidth * percent);
|
2024-04-22 11:44:26 -06:00
|
|
|
}
|
|
|
|
|
2024-10-11 14:40:13 -04:00
|
|
|
_drawXAxisLabels() {
|
2024-04-22 11:44:26 -06:00
|
|
|
// Add ticks
|
2024-10-11 14:40:13 -04:00
|
|
|
this._d3Group
|
2024-04-22 11:44:26 -06:00
|
|
|
.append("g")
|
2024-10-11 14:40:13 -04:00
|
|
|
.attr("transform", `translate(0,${this._d3ContainerHeight})`)
|
2024-04-22 11:44:26 -06:00
|
|
|
.call(
|
|
|
|
d3
|
2024-10-11 14:40:13 -04:00
|
|
|
.axisBottom(this._d3XScale)
|
2024-10-14 23:09:27 +02:00
|
|
|
.tickValues([
|
|
|
|
this._normalDataPoints[0].date,
|
|
|
|
this._normalDataPoints[this._normalDataPoints.length - 1].date,
|
|
|
|
])
|
2024-04-22 11:44:26 -06:00
|
|
|
.tickSize(0)
|
2025-02-21 11:57:59 -05:00
|
|
|
.tickFormat(d3.utcFormat("%b %d, %Y")),
|
2024-04-22 11:44:26 -06:00
|
|
|
)
|
|
|
|
.select(".domain")
|
2024-10-14 23:09:27 +02:00
|
|
|
.remove();
|
2024-04-22 11:44:26 -06:00
|
|
|
|
|
|
|
// Style ticks
|
2024-10-14 23:09:27 +02:00
|
|
|
this._d3Group
|
|
|
|
.selectAll(".tick text")
|
2025-02-12 11:22:30 -05:00
|
|
|
.style("fill", "var(--color-gray-500)")
|
2024-04-22 11:44:26 -06:00
|
|
|
.style("font-size", "12px")
|
|
|
|
.style("font-weight", "500")
|
|
|
|
.attr("text-anchor", "middle")
|
|
|
|
.attr("dx", (_d, i) => {
|
|
|
|
// We know we only have 2 values
|
2024-10-14 23:09:27 +02:00
|
|
|
return i === 0 ? "5em" : "-5em";
|
2024-04-22 11:44:26 -06:00
|
|
|
})
|
2024-10-14 23:09:27 +02:00
|
|
|
.attr("dy", "0em");
|
2024-04-22 11:44:26 -06:00
|
|
|
}
|
|
|
|
|
2024-10-11 14:40:13 -04:00
|
|
|
_drawGradientBelowTrendline() {
|
2024-04-22 11:44:26 -06:00
|
|
|
// Define gradient
|
2024-10-11 14:40:13 -04:00
|
|
|
const gradient = this._d3Group
|
2024-04-22 11:44:26 -06:00
|
|
|
.append("defs")
|
|
|
|
.append("linearGradient")
|
|
|
|
.attr("id", `${this.element.id}-trendline-gradient`)
|
|
|
|
.attr("gradientUnits", "userSpaceOnUse")
|
|
|
|
.attr("x1", 0)
|
|
|
|
.attr("x2", 0)
|
2024-10-14 23:09:27 +02:00
|
|
|
.attr(
|
|
|
|
"y1",
|
2025-02-21 11:57:59 -05:00
|
|
|
this._d3YScale(d3.max(this._normalDataPoints, this._getDatumValue)),
|
2024-10-14 23:09:27 +02:00
|
|
|
)
|
|
|
|
.attr("y2", this._d3ContainerHeight);
|
2024-04-22 11:44:26 -06:00
|
|
|
|
|
|
|
gradient
|
|
|
|
.append("stop")
|
|
|
|
.attr("offset", 0)
|
2024-10-11 14:40:13 -04:00
|
|
|
.attr("stop-color", this._trendColor)
|
2024-10-14 23:09:27 +02:00
|
|
|
.attr("stop-opacity", 0.06);
|
2024-04-22 11:44:26 -06:00
|
|
|
|
|
|
|
gradient
|
|
|
|
.append("stop")
|
|
|
|
.attr("offset", 0.5)
|
2024-10-11 14:40:13 -04:00
|
|
|
.attr("stop-color", this._trendColor)
|
2024-10-14 23:09:27 +02:00
|
|
|
.attr("stop-opacity", 0);
|
2024-04-22 11:44:26 -06:00
|
|
|
|
|
|
|
// Clip path makes gradient start at the trendline
|
2024-10-11 14:40:13 -04:00
|
|
|
this._d3Group
|
2024-04-22 11:44:26 -06:00
|
|
|
.append("clipPath")
|
|
|
|
.attr("id", `${this.element.id}-clip-below-trendline`)
|
|
|
|
.append("path")
|
2024-10-11 14:40:13 -04:00
|
|
|
.datum(this._normalDataPoints)
|
2024-10-14 23:09:27 +02:00
|
|
|
.attr(
|
|
|
|
"d",
|
|
|
|
d3
|
|
|
|
.area()
|
|
|
|
.x((d) => this._d3XScale(d.date))
|
|
|
|
.y0(this._d3ContainerHeight)
|
2025-02-21 11:57:59 -05:00
|
|
|
.y1((d) => this._d3YScale(this._getDatumValue(d))),
|
2024-10-14 23:09:27 +02:00
|
|
|
);
|
2024-04-22 11:44:26 -06:00
|
|
|
|
|
|
|
// Apply the gradient + clip path
|
2024-10-11 14:40:13 -04:00
|
|
|
this._d3Group
|
2024-04-22 11:44:26 -06:00
|
|
|
.append("rect")
|
|
|
|
.attr("id", `${this.element.id}-trendline-gradient-rect`)
|
2024-10-11 14:40:13 -04:00
|
|
|
.attr("width", this._d3ContainerWidth)
|
|
|
|
.attr("height", this._d3ContainerHeight)
|
2024-04-22 11:44:26 -06:00
|
|
|
.attr("clip-path", `url(#${this.element.id}-clip-below-trendline)`)
|
2024-10-14 23:09:27 +02:00
|
|
|
.style("fill", `url(#${this.element.id}-trendline-gradient)`);
|
2024-04-22 11:44:26 -06:00
|
|
|
}
|
|
|
|
|
2024-10-11 14:40:13 -04:00
|
|
|
_drawTooltip() {
|
|
|
|
this._d3Tooltip = d3
|
2024-04-22 11:44:26 -06:00
|
|
|
.select(`#${this.element.id}`)
|
|
|
|
.append("div")
|
|
|
|
.style("position", "absolute")
|
|
|
|
.style("padding", "8px")
|
|
|
|
.style("font", "14px Inter, sans-serif")
|
2025-02-12 11:22:30 -05:00
|
|
|
.style("background", "var(--color-white)")
|
|
|
|
.style("border", "1px solid var(--color-alpha-black-100)")
|
2024-04-22 11:44:26 -06:00
|
|
|
.style("border-radius", "10px")
|
|
|
|
.style("pointer-events", "none")
|
2024-10-14 23:09:27 +02:00
|
|
|
.style("opacity", 0); // Starts as hidden
|
2024-04-22 11:44:26 -06:00
|
|
|
}
|
|
|
|
|
2024-10-11 14:40:13 -04:00
|
|
|
_trackMouseForShowingTooltip() {
|
2024-10-14 23:09:27 +02:00
|
|
|
const bisectDate = d3.bisector((d) => d.date).left;
|
2024-04-22 11:44:26 -06:00
|
|
|
|
2024-10-11 14:40:13 -04:00
|
|
|
this._d3Group
|
2024-04-22 11:44:26 -06:00
|
|
|
.append("rect")
|
2024-10-11 14:40:13 -04:00
|
|
|
.attr("width", this._d3ContainerWidth)
|
|
|
|
.attr("height", this._d3ContainerHeight)
|
2024-04-22 11:44:26 -06:00
|
|
|
.attr("fill", "none")
|
|
|
|
.attr("pointer-events", "all")
|
|
|
|
.on("mousemove", (event) => {
|
2024-10-14 23:09:27 +02:00
|
|
|
const estimatedTooltipWidth = 250;
|
|
|
|
const pageWidth = document.body.clientWidth;
|
|
|
|
const tooltipX = event.pageX + 10;
|
|
|
|
const overflowX = tooltipX + estimatedTooltipWidth - pageWidth;
|
|
|
|
const adjustedX =
|
|
|
|
overflowX > 0 ? event.pageX - overflowX - 20 : tooltipX;
|
|
|
|
|
|
|
|
const [xPos] = d3.pointer(event);
|
|
|
|
const x0 = bisectDate(
|
|
|
|
this._normalDataPoints,
|
|
|
|
this._d3XScale.invert(xPos),
|
|
|
|
1,
|
|
|
|
);
|
|
|
|
const d0 = this._normalDataPoints[x0 - 1];
|
|
|
|
const d1 = this._normalDataPoints[x0];
|
|
|
|
const d =
|
|
|
|
xPos - this._d3XScale(d0.date) > this._d3XScale(d1.date) - xPos
|
|
|
|
? d1
|
|
|
|
: d0;
|
|
|
|
const xPercent = this._d3XScale(d.date) / this._d3ContainerWidth;
|
|
|
|
|
|
|
|
this._setTrendlineSplitAt(xPercent);
|
2024-04-22 11:44:26 -06:00
|
|
|
|
|
|
|
// Reset
|
2024-10-14 23:09:27 +02:00
|
|
|
this._d3Group.selectAll(".data-point-circle").remove();
|
|
|
|
this._d3Group.selectAll(".guideline").remove();
|
2024-04-22 11:44:26 -06:00
|
|
|
|
|
|
|
// Guideline
|
2024-10-11 14:40:13 -04:00
|
|
|
this._d3Group
|
2024-04-22 11:44:26 -06:00
|
|
|
.append("line")
|
|
|
|
.attr("class", "guideline")
|
2024-10-11 14:40:13 -04:00
|
|
|
.attr("x1", this._d3XScale(d.date))
|
2024-04-22 11:44:26 -06:00
|
|
|
.attr("y1", 0)
|
2024-10-11 14:40:13 -04:00
|
|
|
.attr("x2", this._d3XScale(d.date))
|
|
|
|
.attr("y2", this._d3ContainerHeight)
|
2025-02-12 11:22:30 -05:00
|
|
|
.attr("stroke", "var(--color-gray-300)")
|
2024-10-14 23:09:27 +02:00
|
|
|
.attr("stroke-dasharray", "4, 4");
|
2024-04-22 11:44:26 -06:00
|
|
|
|
|
|
|
// Big circle
|
2024-10-11 14:40:13 -04:00
|
|
|
this._d3Group
|
2024-04-22 11:44:26 -06:00
|
|
|
.append("circle")
|
|
|
|
.attr("class", "data-point-circle")
|
2024-10-11 14:40:13 -04:00
|
|
|
.attr("cx", this._d3XScale(d.date))
|
2025-02-21 11:57:59 -05:00
|
|
|
.attr("cy", this._d3YScale(this._getDatumValue(d)))
|
2024-04-22 11:44:26 -06:00
|
|
|
.attr("r", 8)
|
2024-10-11 14:40:13 -04:00
|
|
|
.attr("fill", this._trendColor)
|
2024-04-22 11:44:26 -06:00
|
|
|
.attr("fill-opacity", "0.1")
|
2024-10-14 23:09:27 +02:00
|
|
|
.attr("pointer-events", "none");
|
2024-04-22 11:44:26 -06:00
|
|
|
|
|
|
|
// Small circle
|
2024-10-11 14:40:13 -04:00
|
|
|
this._d3Group
|
2024-04-22 11:44:26 -06:00
|
|
|
.append("circle")
|
|
|
|
.attr("class", "data-point-circle")
|
2024-10-11 14:40:13 -04:00
|
|
|
.attr("cx", this._d3XScale(d.date))
|
2025-02-21 11:57:59 -05:00
|
|
|
.attr("cy", this._d3YScale(this._getDatumValue(d)))
|
2024-04-22 11:44:26 -06:00
|
|
|
.attr("r", 3)
|
2024-10-11 14:40:13 -04:00
|
|
|
.attr("fill", this._trendColor)
|
2024-10-14 23:09:27 +02:00
|
|
|
.attr("pointer-events", "none");
|
2024-04-22 11:44:26 -06:00
|
|
|
|
|
|
|
// Render tooltip
|
2024-10-11 14:40:13 -04:00
|
|
|
this._d3Tooltip
|
|
|
|
.html(this._tooltipTemplate(d))
|
2024-04-22 11:44:26 -06:00
|
|
|
.style("opacity", 1)
|
2024-04-23 17:05:18 +01:00
|
|
|
.style("z-index", 999)
|
2024-10-14 23:09:27 +02:00
|
|
|
.style("left", `${adjustedX}px`)
|
|
|
|
.style("top", `${event.pageY - 10}px`);
|
2024-04-22 11:44:26 -06:00
|
|
|
})
|
|
|
|
.on("mouseout", (event) => {
|
2024-10-14 23:09:27 +02:00
|
|
|
const hoveringOnGuideline =
|
|
|
|
event.toElement?.classList.contains("guideline");
|
2024-04-22 11:44:26 -06:00
|
|
|
|
|
|
|
if (!hoveringOnGuideline) {
|
2024-10-14 23:09:27 +02:00
|
|
|
this._d3Group.selectAll(".guideline").remove();
|
|
|
|
this._d3Group.selectAll(".data-point-circle").remove();
|
|
|
|
this._d3Tooltip.style("opacity", 0);
|
2024-04-22 11:44:26 -06:00
|
|
|
|
2024-10-14 23:09:27 +02:00
|
|
|
this._setTrendlineSplitAt(1);
|
2024-04-22 11:44:26 -06:00
|
|
|
}
|
2024-10-14 23:09:27 +02:00
|
|
|
});
|
2024-04-22 11:44:26 -06:00
|
|
|
}
|
|
|
|
|
2024-10-11 14:40:13 -04:00
|
|
|
_tooltipTemplate(datum) {
|
2024-10-14 23:09:27 +02:00
|
|
|
return `
|
2025-02-12 11:22:30 -05:00
|
|
|
<div style="margin-bottom: 4px; color: var(--color-gray-500);">
|
2025-02-21 11:57:59 -05:00
|
|
|
${datum.date_formatted}
|
2024-04-22 11:44:26 -06:00
|
|
|
</div>
|
|
|
|
|
|
|
|
<div style="display: flex; align-items: center; gap: 16px;">
|
|
|
|
<div style="display: flex; align-items: center; gap: 8px;">
|
|
|
|
<svg width="10" height="10">
|
|
|
|
<circle
|
|
|
|
cx="5"
|
|
|
|
cy="5"
|
|
|
|
r="4"
|
2025-02-21 11:57:59 -05:00
|
|
|
stroke="${datum.trend.color}"
|
2024-04-22 11:44:26 -06:00
|
|
|
fill="transparent"
|
|
|
|
stroke-width="1"></circle>
|
|
|
|
</svg>
|
|
|
|
|
2025-02-21 11:57:59 -05:00
|
|
|
${this._extractFormattedValue(datum.trend.current)}
|
2024-04-22 11:44:26 -06:00
|
|
|
</div>
|
|
|
|
|
2024-10-14 23:09:27 +02:00
|
|
|
${
|
2025-02-21 11:57:59 -05:00
|
|
|
datum.trend.value === 0
|
|
|
|
? `<span style="width: 80px;"></span>`
|
2024-10-14 23:09:27 +02:00
|
|
|
: `
|
2025-02-21 11:57:59 -05:00
|
|
|
<span style="color: ${datum.trend.color};">
|
|
|
|
${this._extractFormattedValue(datum.trend.value)} (${datum.trend.percent_formatted})
|
2024-04-22 11:44:26 -06:00
|
|
|
</span>
|
2024-10-14 23:09:27 +02:00
|
|
|
`
|
|
|
|
}
|
2024-04-22 11:44:26 -06:00
|
|
|
</div>
|
2024-10-14 23:09:27 +02:00
|
|
|
`;
|
2024-04-22 11:44:26 -06:00
|
|
|
}
|
|
|
|
|
2025-02-21 11:57:59 -05:00
|
|
|
_getDatumValue = (datum) => {
|
|
|
|
return this._extractNumericValue(datum.trend.current);
|
|
|
|
};
|
2024-04-22 11:44:26 -06:00
|
|
|
|
2025-02-21 11:57:59 -05:00
|
|
|
_extractNumericValue = (numeric) => {
|
|
|
|
if (typeof numeric === "object" && "amount" in numeric) {
|
|
|
|
return Number(numeric.amount);
|
2024-04-22 11:44:26 -06:00
|
|
|
}
|
2025-02-21 11:57:59 -05:00
|
|
|
return Number(numeric);
|
|
|
|
};
|
2024-04-22 11:44:26 -06:00
|
|
|
|
2025-02-21 11:57:59 -05:00
|
|
|
_extractFormattedValue = (numeric) => {
|
|
|
|
if (typeof numeric === "object" && "formatted" in numeric) {
|
|
|
|
return numeric.formatted;
|
2024-04-22 11:44:26 -06:00
|
|
|
}
|
2025-02-21 11:57:59 -05:00
|
|
|
return numeric;
|
|
|
|
};
|
2024-04-22 11:44:26 -06:00
|
|
|
|
2024-10-11 14:40:13 -04:00
|
|
|
_createMainSvg() {
|
|
|
|
return this._d3Container
|
2024-04-22 11:44:26 -06:00
|
|
|
.append("svg")
|
2024-10-11 14:40:13 -04:00
|
|
|
.attr("width", this._d3InitialContainerWidth)
|
|
|
|
.attr("height", this._d3InitialContainerHeight)
|
2024-10-14 23:09:27 +02:00
|
|
|
.attr("viewBox", [
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
this._d3InitialContainerWidth,
|
|
|
|
this._d3InitialContainerHeight,
|
|
|
|
]);
|
2024-04-22 11:44:26 -06:00
|
|
|
}
|
|
|
|
|
2024-10-11 14:40:13 -04:00
|
|
|
_createMainGroup() {
|
|
|
|
return this._d3Svg
|
2024-04-22 11:44:26 -06:00
|
|
|
.append("g")
|
2024-10-14 23:09:27 +02:00
|
|
|
.attr("transform", `translate(${this._margin.left},${this._margin.top})`);
|
2024-04-22 11:44:26 -06:00
|
|
|
}
|
|
|
|
|
2024-10-11 14:40:13 -04:00
|
|
|
get _d3Svg() {
|
2024-10-14 23:09:27 +02:00
|
|
|
if (!this._d3SvgMemo) {
|
|
|
|
this._d3SvgMemo = this._createMainSvg();
|
2024-04-22 11:44:26 -06:00
|
|
|
}
|
2024-10-14 23:09:27 +02:00
|
|
|
return this._d3SvgMemo;
|
2024-04-22 11:44:26 -06:00
|
|
|
}
|
|
|
|
|
2024-10-11 14:40:13 -04:00
|
|
|
get _d3Group() {
|
2024-10-14 23:09:27 +02:00
|
|
|
if (!this._d3GroupMemo) {
|
|
|
|
this._d3GroupMemo = this._createMainGroup();
|
2024-04-22 11:44:26 -06:00
|
|
|
}
|
2024-10-14 23:09:27 +02:00
|
|
|
return this._d3GroupMemo;
|
2024-04-22 11:44:26 -06:00
|
|
|
}
|
|
|
|
|
2024-10-11 14:40:13 -04:00
|
|
|
get _margin() {
|
2024-04-22 11:44:26 -06:00
|
|
|
if (this.useLabelsValue) {
|
2025-02-25 17:28:40 -05:00
|
|
|
return { top: 20, right: 0, bottom: 10, left: 0 };
|
2024-04-22 11:44:26 -06:00
|
|
|
}
|
2024-10-14 23:09:27 +02:00
|
|
|
return { top: 0, right: 0, bottom: 0, left: 0 };
|
2024-04-22 11:44:26 -06:00
|
|
|
}
|
|
|
|
|
2024-10-11 14:40:13 -04:00
|
|
|
get _d3ContainerWidth() {
|
2024-10-14 23:09:27 +02:00
|
|
|
return (
|
|
|
|
this._d3InitialContainerWidth - this._margin.left - this._margin.right
|
|
|
|
);
|
2024-04-22 11:44:26 -06:00
|
|
|
}
|
|
|
|
|
2024-10-11 14:40:13 -04:00
|
|
|
get _d3ContainerHeight() {
|
2024-10-14 23:09:27 +02:00
|
|
|
return (
|
|
|
|
this._d3InitialContainerHeight - this._margin.top - this._margin.bottom
|
|
|
|
);
|
2024-04-22 11:44:26 -06:00
|
|
|
}
|
|
|
|
|
2024-10-11 14:40:13 -04:00
|
|
|
get _d3Container() {
|
2024-10-14 23:09:27 +02:00
|
|
|
return d3.select(this.element);
|
2024-04-22 11:44:26 -06:00
|
|
|
}
|
|
|
|
|
2024-10-11 14:40:13 -04:00
|
|
|
get _trendColor() {
|
2025-02-21 11:57:59 -05:00
|
|
|
return this.dataValue.trend.color;
|
2024-04-22 11:44:26 -06:00
|
|
|
}
|
|
|
|
|
2024-10-11 14:40:13 -04:00
|
|
|
get _d3Line() {
|
2024-04-22 11:44:26 -06:00
|
|
|
return d3
|
|
|
|
.line()
|
2024-10-14 23:09:27 +02:00
|
|
|
.x((d) => this._d3XScale(d.date))
|
2025-02-21 11:57:59 -05:00
|
|
|
.y((d) => this._d3YScale(this._getDatumValue(d)));
|
2024-04-22 11:44:26 -06:00
|
|
|
}
|
|
|
|
|
2024-10-11 14:40:13 -04:00
|
|
|
get _d3XScale() {
|
2024-04-22 11:44:26 -06:00
|
|
|
return d3
|
|
|
|
.scaleTime()
|
2024-10-11 14:40:13 -04:00
|
|
|
.rangeRound([0, this._d3ContainerWidth])
|
2024-10-14 23:09:27 +02:00
|
|
|
.domain(d3.extent(this._normalDataPoints, (d) => d.date));
|
2024-04-22 11:44:26 -06:00
|
|
|
}
|
|
|
|
|
2024-10-11 14:40:13 -04:00
|
|
|
get _d3YScale() {
|
2024-11-04 20:27:31 -05:00
|
|
|
const reductionPercent = this.useLabelsValue ? 0.3 : 0.05;
|
2025-02-21 11:57:59 -05:00
|
|
|
const dataMin = d3.min(this._normalDataPoints, this._getDatumValue);
|
|
|
|
const dataMax = d3.max(this._normalDataPoints, this._getDatumValue);
|
2024-10-14 23:09:27 +02:00
|
|
|
const padding = (dataMax - dataMin) * reductionPercent;
|
2024-04-22 11:44:26 -06:00
|
|
|
|
|
|
|
return d3
|
|
|
|
.scaleLinear()
|
2024-10-11 14:40:13 -04:00
|
|
|
.rangeRound([this._d3ContainerHeight, 0])
|
2024-10-14 23:09:27 +02:00
|
|
|
.domain([dataMin - padding, dataMax + padding]);
|
2024-04-22 11:44:26 -06:00
|
|
|
}
|
2025-02-21 11:57:59 -05:00
|
|
|
|
|
|
|
_setupResizeObserver() {
|
|
|
|
this._resizeObserver = new ResizeObserver(() => {
|
|
|
|
this._reinstall();
|
|
|
|
});
|
|
|
|
this._resizeObserver.observe(this.element);
|
|
|
|
}
|
2024-10-14 23:09:27 +02:00
|
|
|
}
|