2020-06-23 09:25:28 +02:00
|
|
|
// Copyright: Ankitects Pty Ltd and contributors
|
|
|
|
// License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
|
|
|
|
|
|
|
|
/* eslint
|
|
|
|
@typescript-eslint/no-explicit-any: "off",
|
|
|
|
*/
|
|
|
|
|
2022-02-04 09:36:34 +01:00
|
|
|
import type { Bin, ScaleLinear, ScaleSequential } from "d3";
|
2021-01-30 01:13:47 +01:00
|
|
|
import {
|
2022-02-04 09:36:34 +01:00
|
|
|
area,
|
2021-01-30 01:13:47 +01:00
|
|
|
axisBottom,
|
|
|
|
axisLeft,
|
|
|
|
axisRight,
|
2022-02-04 09:36:34 +01:00
|
|
|
cumsum,
|
2021-01-30 01:13:47 +01:00
|
|
|
curveBasis,
|
2022-02-04 09:36:34 +01:00
|
|
|
max,
|
|
|
|
pointer,
|
|
|
|
scaleLinear,
|
|
|
|
select,
|
2021-01-30 01:13:47 +01:00
|
|
|
} from "d3";
|
2022-02-04 09:36:34 +01:00
|
|
|
|
2021-12-29 06:04:15 +01:00
|
|
|
import { localizedNumber } from "../lib/i18n";
|
2020-11-01 05:26:58 +01:00
|
|
|
import { GraphBounds, setDataAvailable } from "./graph-helpers";
|
2021-03-21 13:47:52 +01:00
|
|
|
import { clickableClass } from "./graph-styles";
|
2022-02-04 09:36:34 +01:00
|
|
|
import { hideTooltip, showTooltip } from "./tooltip";
|
2020-06-23 09:25:28 +02:00
|
|
|
|
|
|
|
export interface HistogramData {
|
|
|
|
scale: ScaleLinear<number, number>;
|
|
|
|
bins: Bin<number, number>[];
|
|
|
|
total: number;
|
2020-06-24 01:41:07 +02:00
|
|
|
hoverText: (
|
2021-01-30 02:08:01 +01:00
|
|
|
bin: Bin<number, number>,
|
2020-06-24 01:41:07 +02:00
|
|
|
cumulative: number,
|
2021-10-19 01:06:00 +02:00
|
|
|
percent: number,
|
2020-06-24 01:41:07 +02:00
|
|
|
) => string;
|
2021-01-25 17:36:04 +01:00
|
|
|
onClick: ((data: Bin<number, number>) => void) | null;
|
2020-06-23 12:43:19 +02:00
|
|
|
showArea: boolean;
|
|
|
|
colourScale: ScaleSequential<string>;
|
2020-06-26 11:25:02 +02:00
|
|
|
binValue?: (bin: Bin<any, any>) => number;
|
2020-08-21 04:58:02 +02:00
|
|
|
xTickFormat?: (d: any) => string;
|
2020-06-23 09:25:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
export function histogramGraph(
|
|
|
|
svgElem: SVGElement,
|
|
|
|
bounds: GraphBounds,
|
2021-10-19 01:06:00 +02:00
|
|
|
data: HistogramData | null,
|
2020-06-23 09:25:28 +02:00
|
|
|
): void {
|
|
|
|
const svg = select(svgElem);
|
|
|
|
const trans = svg.transition().duration(600) as any;
|
2021-12-29 06:04:15 +01:00
|
|
|
const axisTickFormat = (n: number): string => localizedNumber(n);
|
2020-06-23 09:25:28 +02:00
|
|
|
|
2020-07-06 06:01:49 +02:00
|
|
|
if (!data) {
|
|
|
|
setDataAvailable(svg, false);
|
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
setDataAvailable(svg, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
const binValue = data.binValue ?? ((bin: any): number => bin.length as number);
|
|
|
|
|
2020-06-23 09:25:28 +02:00
|
|
|
const x = data.scale.range([bounds.marginLeft, bounds.width - bounds.marginRight]);
|
2021-04-02 05:25:38 +02:00
|
|
|
svg.select<SVGGElement>(".x-ticks")
|
|
|
|
.call((selection) =>
|
|
|
|
selection.transition(trans).call(
|
|
|
|
axisBottom(x)
|
|
|
|
.ticks(7)
|
|
|
|
.tickSizeOuter(0)
|
2021-12-29 06:04:15 +01:00
|
|
|
.tickFormat((data.xTickFormat ?? axisTickFormat) as any),
|
2021-10-19 01:06:00 +02:00
|
|
|
),
|
2021-03-21 13:47:52 +01:00
|
|
|
)
|
2021-04-02 05:25:38 +02:00
|
|
|
.attr("direction", "ltr");
|
2020-06-23 09:25:28 +02:00
|
|
|
|
|
|
|
// y scale
|
|
|
|
|
2020-06-26 11:25:02 +02:00
|
|
|
const yMax = max(data.bins, (d) => binValue(d))!;
|
2020-06-23 09:25:28 +02:00
|
|
|
const y = scaleLinear()
|
|
|
|
.range([bounds.height - bounds.marginBottom, bounds.marginTop])
|
2020-08-05 06:49:57 +02:00
|
|
|
.domain([0, yMax])
|
|
|
|
.nice();
|
2021-04-02 05:25:38 +02:00
|
|
|
svg.select<SVGGElement>(".y-ticks")
|
|
|
|
.call((selection) =>
|
|
|
|
selection.transition(trans).call(
|
|
|
|
axisLeft(y)
|
|
|
|
.ticks(bounds.height / 50)
|
2021-12-29 06:04:15 +01:00
|
|
|
.tickSizeOuter(0)
|
|
|
|
.tickFormat(axisTickFormat as any),
|
2021-10-19 01:06:00 +02:00
|
|
|
),
|
2021-03-21 13:47:52 +01:00
|
|
|
)
|
2021-04-02 05:25:38 +02:00
|
|
|
.attr("direction", "ltr");
|
2020-06-23 09:25:28 +02:00
|
|
|
|
|
|
|
// x bars
|
|
|
|
|
2021-01-30 02:08:01 +01:00
|
|
|
function barWidth(d: Bin<number, number>): number {
|
|
|
|
const width = Math.max(0, x(d.x1!) - x(d.x0!) - 1);
|
|
|
|
return width ?? 0;
|
2020-06-23 09:25:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const updateBar = (sel: any): any => {
|
|
|
|
return sel
|
|
|
|
.attr("width", barWidth)
|
2020-06-23 12:43:19 +02:00
|
|
|
.transition(trans)
|
2020-06-23 09:25:28 +02:00
|
|
|
.attr("x", (d: any) => x(d.x0))
|
2020-06-26 11:25:02 +02:00
|
|
|
.attr("y", (d: any) => y(binValue(d))!)
|
2020-09-29 14:13:25 +02:00
|
|
|
.attr("height", (d: any) => y(0)! - y(binValue(d))!)
|
2020-06-24 01:41:07 +02:00
|
|
|
.attr("fill", (d) => data.colourScale(d.x1));
|
2020-06-23 09:25:28 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
svg.select("g.bars")
|
|
|
|
.selectAll("rect")
|
|
|
|
.data(data.bins)
|
|
|
|
.join(
|
|
|
|
(enter) =>
|
|
|
|
enter
|
|
|
|
.append("rect")
|
|
|
|
.attr("rx", 1)
|
2020-09-29 14:13:25 +02:00
|
|
|
.attr("x", (d: any) => x(d.x0)!)
|
|
|
|
.attr("y", y(0)!)
|
2020-06-23 09:25:28 +02:00
|
|
|
.attr("height", 0)
|
|
|
|
.call(updateBar),
|
|
|
|
(update) => update.call(updateBar),
|
|
|
|
(remove) =>
|
|
|
|
remove.call((remove) =>
|
2021-10-19 01:06:00 +02:00
|
|
|
remove.transition(trans).attr("height", 0).attr("y", y(0)!),
|
|
|
|
),
|
2020-06-23 09:25:28 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
// cumulative area
|
|
|
|
|
2020-06-26 11:25:02 +02:00
|
|
|
const areaCounts = data.bins.map((d) => binValue(d));
|
2020-06-23 09:25:28 +02:00
|
|
|
areaCounts.unshift(0);
|
|
|
|
const areaData = cumsum(areaCounts);
|
2020-08-05 06:49:57 +02:00
|
|
|
const yAreaScale = y.copy().domain([0, data.total]).nice();
|
2020-06-23 09:25:28 +02:00
|
|
|
|
2020-07-01 05:32:01 +02:00
|
|
|
if (data.showArea && data.bins.length && areaData.slice(-1)[0]) {
|
2021-04-02 05:25:38 +02:00
|
|
|
svg.select<SVGGElement>(".y2-ticks")
|
|
|
|
.call((selection) =>
|
|
|
|
selection.transition(trans).call(
|
|
|
|
axisRight(yAreaScale)
|
|
|
|
.ticks(bounds.height / 50)
|
2021-12-29 06:04:15 +01:00
|
|
|
.tickSizeOuter(0)
|
|
|
|
.tickFormat(axisTickFormat as any),
|
2021-10-19 01:06:00 +02:00
|
|
|
),
|
2021-03-21 13:47:52 +01:00
|
|
|
)
|
2021-04-02 05:25:38 +02:00
|
|
|
.attr("direction", "ltr");
|
2020-08-05 06:49:57 +02:00
|
|
|
|
2021-05-22 02:19:13 +02:00
|
|
|
svg.select("path.cumulative-overlay")
|
2020-06-23 12:43:19 +02:00
|
|
|
.datum(areaData as any)
|
|
|
|
.attr(
|
|
|
|
"d",
|
|
|
|
area()
|
2020-06-26 11:28:19 +02:00
|
|
|
.curve(curveBasis)
|
2021-01-08 14:22:20 +01:00
|
|
|
.x((_d, idx) => {
|
2020-06-23 12:43:19 +02:00
|
|
|
if (idx === 0) {
|
2020-09-29 14:13:25 +02:00
|
|
|
return x(data.bins[0].x0!)!;
|
2020-06-23 12:43:19 +02:00
|
|
|
} else {
|
2020-09-29 14:13:25 +02:00
|
|
|
return x(data.bins[idx - 1].x1!)!;
|
2020-06-23 12:43:19 +02:00
|
|
|
}
|
|
|
|
})
|
|
|
|
.y0(bounds.height - bounds.marginBottom)
|
2021-10-19 01:06:00 +02:00
|
|
|
.y1((d: any) => yAreaScale(d)!) as any,
|
2020-06-23 12:43:19 +02:00
|
|
|
);
|
|
|
|
}
|
2020-06-23 09:25:28 +02:00
|
|
|
|
2021-05-26 01:21:33 +02:00
|
|
|
const hoverData: [Bin<number, number>, number][] = data.bins.map(
|
2021-10-19 01:06:00 +02:00
|
|
|
(bin: Bin<number, number>, index: number) => [bin, areaData[index + 1]],
|
2021-05-26 01:21:33 +02:00
|
|
|
);
|
2021-01-30 02:08:01 +01:00
|
|
|
|
2020-06-23 09:25:28 +02:00
|
|
|
// hover/tooltip
|
2021-01-08 14:28:38 +01:00
|
|
|
const hoverzone = svg
|
2021-03-21 10:58:39 +01:00
|
|
|
.select("g.hover-columns")
|
2020-06-23 09:25:28 +02:00
|
|
|
.selectAll("rect")
|
2021-01-30 02:08:01 +01:00
|
|
|
.data(hoverData)
|
2020-06-23 09:25:28 +02:00
|
|
|
.join("rect")
|
2021-01-30 02:08:01 +01:00
|
|
|
.attr("x", ([bin]) => x(bin.x0!))
|
|
|
|
.attr("y", () => y(yMax))
|
|
|
|
.attr("width", ([bin]) => barWidth(bin))
|
|
|
|
.attr("height", () => y(0) - y(yMax))
|
|
|
|
.on("mousemove", (event: MouseEvent, [bin, area]) => {
|
2021-01-30 02:35:33 +01:00
|
|
|
const [x, y] = pointer(event, document.body);
|
2021-01-30 02:08:01 +01:00
|
|
|
const pct = data.showArea ? (area / data.total) * 100 : 0;
|
|
|
|
showTooltip(data.hoverText(bin, area, pct), x, y);
|
2020-06-23 09:25:28 +02:00
|
|
|
})
|
2021-01-08 14:22:20 +01:00
|
|
|
.on("mouseout", hideTooltip);
|
|
|
|
|
2021-01-25 16:33:18 +01:00
|
|
|
if (data.onClick) {
|
2021-01-26 13:48:36 +01:00
|
|
|
hoverzone
|
2021-01-30 02:46:26 +01:00
|
|
|
.filter(([bin]) => bin.length > 0)
|
2021-03-21 13:47:52 +01:00
|
|
|
.attr("class", clickableClass)
|
2021-01-30 02:46:26 +01:00
|
|
|
.on("click", (_event, [bin]) => data.onClick!(bin));
|
2021-01-08 14:22:20 +01:00
|
|
|
}
|
2020-06-23 09:25:28 +02:00
|
|
|
}
|