Гант: не рисовать зависимости, следующие из цепочки других (транзитивная редукция)
Some checks failed
ci / test (push) Successful in 23s
ci / deploy (push) Has been cancelled

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-10-17 13:29:10 +03:00
parent 1a27060706
commit f56c19598d
2 changed files with 35 additions and 1 deletions

View File

@@ -148,6 +148,32 @@ export function placeLabels(items: Array<{ x: number; row: ProcessRow }>): { lab
return { labels, levels: labels.length ? Math.max(...labels.map((l) => l.level)) + 1 : 0 };
}
/** Зависимости, которые следуют из цепочки других зависимостей (A→B, если B достижим из A через другие шаги): на Ганте не рисуются, смысл графа не меняется. Ключ - `от>к`. */
export function redundantDependencies(rows: ProcessRow[]): Set<string> {
const ids = new Set(rows.map((r) => r.id));
const succ = new Map<string, string[]>();
for (const r of rows) for (const d of r.dependsOn) if (ids.has(d)) succ.set(d, [...(succ.get(d) ?? []), r.id]);
const reachable = (from: string, to: string): boolean => {
const stack = [from];
const seen = new Set<string>();
while (stack.length) {
const cur = stack.pop()!;
for (const next of succ.get(cur) ?? []) {
if (cur === from && next === to) continue;
if (next === to) return true;
if (!seen.has(next)) {
seen.add(next);
stack.push(next);
}
}
}
return false;
};
const out = new Set<string>();
for (const r of rows) for (const d of r.dependsOn) if (ids.has(d) && reachable(d, r.id)) out.add(`${d}>${r.id}`);
return out;
}
export function renderGantt(model: ProcessModel, schedule: Schedule, opts: GanttOptions = {}): string {
const px = clampPx(opts.px);
const collapsedSet = new Set(opts.collapsed ?? []);
@@ -247,9 +273,11 @@ export function renderGantt(model: ProcessModel, schedule: Schedule, opts: Gantt
};
if (opts.deps !== false) {
const arrows: string[] = [];
const redundant = redundantDependencies(model.rows);
for (const g of groups) {
for (const { row } of g.rows) {
for (const dep of row.dependsOn) {
if (redundant.has(`${dep}>${row.id}`)) continue;
const from = anchor(dep);
const to = anchor(row.id);
if (!from || !to) continue;

View File

@@ -1,7 +1,7 @@
import { describe, expect, it } from "vitest";
import { renderPage } from "./html.js";
import { renderAris } from "./aris.js";
import { clampPx, placeLabels, renderGantt, stageOrder, summarizeGroup } from "./gantt.js";
import { clampPx, placeLabels, redundantDependencies, renderGantt, stageOrder, summarizeGroup } from "./gantt.js";
import { buildApi } from "./http-api.js";
import { buildModel, describeResponsible, loadModel, mapRow, type ProcessModel } from "./model.js";
import { computeSchedule, parseConstraint, parseDuration, parseDurationInfo } from "./schedule.js";
@@ -332,6 +332,12 @@ describe("Гант: группы, вехи, свёртывание, масшта
expect(arrows(renderGantt(ms, sch, {}))).toBeGreaterThan(arrows(shut));
});
it("зависимость, следующая из цепочки других, не рисуется стрелкой (A→C при A→B→C)", () => {
const rows = [row({ id: "A" }), row({ id: "B", depends_on: "A" }), row({ id: "C", depends_on: "B", depends_on2: "A" })];
const redundant = redundantDependencies(rows);
expect([...redundant]).toEqual(["A>C"]);
});
it("подпись полосы - название работы (внутри полосы или над ней), id справа снизу, без дат", () => {
const svg = renderGantt(ms, sch, { px: 20 });
expect(svg).toContain('fill="#fff">Работа</text>'); // название внутри длинной полосы