anki/ts/graphs/histogram-graph.ts

175 lines
5.3 KiB
TypeScript
Raw Normal View History

// Copyright: Ankitects Pty Ltd and contributors
// License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
/* eslint
@typescript-eslint/no-non-null-assertion: "off",
@typescript-eslint/no-explicit-any: "off",
*/
import "d3-transition";
import { select, mouse } from "d3-selection";
import { cumsum, max, Bin } from "d3-array";
2020-06-24 01:41:07 +02:00
import { scaleLinear, ScaleLinear, ScaleSequential } from "d3-scale";
import { axisBottom, axisLeft, axisRight } from "d3-axis";
2020-06-26 11:28:19 +02:00
import { area, curveBasis } from "d3-shape";
import { showTooltip, hideTooltip } from "./tooltip";
import { GraphBounds, setDataAvailable } from "./graph-helpers";
export interface HistogramData {
scale: ScaleLinear<number, number>;
bins: Bin<number, number>[];
total: number;
2020-06-24 01:41:07 +02:00
hoverText: (
data: HistogramData,
binIdx: number,
cumulative: number,
percent: number
) => string;
2021-01-07 12:22:50 +01:00
makeQuery?: (
data: HistogramData,
binIdx: number,
) => string;
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;
}
export function histogramGraph(
svgElem: SVGElement,
bounds: GraphBounds,
2021-01-07 12:22:50 +01:00
data: HistogramData | null,
dispatch: any,
): void {
const svg = select(svgElem);
const trans = svg.transition().duration(600) as any;
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);
const x = data.scale.range([bounds.marginLeft, bounds.width - bounds.marginRight]);
svg.select<SVGGElement>(".x-ticks")
.transition(trans)
2020-08-21 04:58:02 +02:00
.call(
axisBottom(x)
.ticks(7)
.tickSizeOuter(0)
.tickFormat((data.xTickFormat ?? null) as any)
);
// y scale
2020-06-26 11:25:02 +02:00
const yMax = max(data.bins, (d) => binValue(d))!;
const y = scaleLinear()
.range([bounds.height - bounds.marginBottom, bounds.marginTop])
.domain([0, yMax])
.nice();
svg.select<SVGGElement>(".y-ticks")
.transition(trans)
.call(
axisLeft(y)
2020-07-16 03:50:04 +02:00
.ticks(bounds.height / 50)
.tickSizeOuter(0)
);
// x bars
function barWidth(d: any): number {
const width = Math.max(0, x(d.x1)! - x(d.x0)! - 1);
return width ? width : 0;
}
const updateBar = (sel: any): any => {
return sel
.attr("width", barWidth)
2020-06-23 12:43:19 +02:00
.transition(trans)
.attr("x", (d: any) => x(d.x0))
2020-06-26 11:25:02 +02:00
.attr("y", (d: any) => y(binValue(d))!)
.attr("height", (d: any) => y(0)! - y(binValue(d))!)
2020-06-24 01:41:07 +02:00
.attr("fill", (d) => data.colourScale(d.x1));
};
svg.select("g.bars")
.selectAll("rect")
.data(data.bins)
.join(
(enter) =>
enter
.append("rect")
.attr("rx", 1)
.attr("x", (d: any) => x(d.x0)!)
.attr("y", y(0)!)
.attr("height", 0)
.call(updateBar),
(update) => update.call(updateBar),
(remove) =>
remove.call((remove) =>
remove.transition(trans).attr("height", 0).attr("y", y(0)!)
)
);
// cumulative area
2020-06-26 11:25:02 +02:00
const areaCounts = data.bins.map((d) => binValue(d));
areaCounts.unshift(0);
const areaData = cumsum(areaCounts);
const yAreaScale = y.copy().domain([0, data.total]).nice();
if (data.showArea && data.bins.length && areaData.slice(-1)[0]) {
svg.select<SVGGElement>(".y2-ticks")
.transition(trans)
.call(
axisRight(yAreaScale)
.ticks(bounds.height / 50)
.tickSizeOuter(0)
);
2020-06-23 12:43:19 +02:00
svg.select("path.area")
.datum(areaData as any)
.attr(
"d",
area()
2020-06-26 11:28:19 +02:00
.curve(curveBasis)
.x((_d, idx) => {
2020-06-23 12:43:19 +02:00
if (idx === 0) {
return x(data.bins[0].x0!)!;
2020-06-23 12:43:19 +02:00
} else {
return x(data.bins[idx - 1].x1!)!;
2020-06-23 12:43:19 +02:00
}
})
.y0(bounds.height - bounds.marginBottom)
.y1((d: any) => yAreaScale(d)!) as any
2020-06-23 12:43:19 +02:00
);
}
// hover/tooltip
const hoverzone = svg.select("g.hoverzone")
.selectAll("rect")
.data(data.bins)
.join("rect")
.attr("x", (d: any) => x(d.x0)!)
.attr("y", () => y(yMax!)!)
.attr("width", barWidth)
.attr("height", () => y(0)! - y(yMax!)!)
.on("mousemove", function (this: any, _d: any, idx: number) {
const [x, y] = mouse(document.body);
2020-06-23 12:43:19 +02:00
const pct = data.showArea ? (areaData[idx + 1] / data.total) * 100 : 0;
2020-06-24 01:41:07 +02:00
showTooltip(data.hoverText(data, idx, areaData[idx + 1], pct), x, y);
})
.on("mouseout", hideTooltip);
if (data.makeQuery) {
hoverzone
.attr("class", "clickable")
.on("click", function (this: any, _d: any, idx: number) {
dispatch("search", { query: data.makeQuery!(data, idx) })
});
}
}