diff --git a/src/ancestor-chart.ts b/src/ancestor-chart.ts index bd7ea1e..e036fdc 100644 --- a/src/ancestor-chart.ts +++ b/src/ancestor-chart.ts @@ -80,11 +80,7 @@ export class AncestorChart if (!fam) { continue; } - const [father, mother] = - entry.family!.id === this.options.startFam && - this.options.swapStartSpouses - ? [fam.getMother(), fam.getFather()] - : [fam.getFather(), fam.getMother()]; + const [father, mother] = this.getParents(fam, entry); if (!father && !mother) { continue; } @@ -134,4 +130,29 @@ export class AncestorChart this.util.updateSvgDimensions(info); return Object.assign(info, { animationPromise }); } + + private getParents(fam: Fam, entry: TreeNode) { + let [father, mother] = [fam.getFather(), fam.getMother()]; + if(!father && !mother) { + // get indis whose FAMS contains this family + const res: any[] = Array.from((this.options.data as any).indis.entries()) + .filter((e: any[]) => e[1]?.json?.fams && e[1].json.fams.includes(entry.family?.id)); + const getFromRes = function(pRes: any, sex: "M" | "F") { + const r = pRes.find((e: any) => e[1].json.sex === sex); + return r.length === 2 + ? r[1].json?.id || null + : null; + } + father = getFromRes(res, "M"); + mother = getFromRes(res, "F"); + } + + if(this.options.swapStartSpouses) { + const pivot = father; + father = mother; + mother = pivot; + } + + return [father || null, mother || null]; + } } diff --git a/tests/ancestor-chart.spec.ts b/tests/ancestor-chart.spec.ts index 146c741..5364882 100644 --- a/tests/ancestor-chart.spec.ts +++ b/tests/ancestor-chart.spec.ts @@ -56,10 +56,36 @@ describe('Ancestor chart', () => { const data = new JsonDataProvider(json); const chart = new AncestorChart({ data, - startFam: 'F1', + startFam: json.fams[0].id, renderer: new FakeRenderer(), svgSelector: 'svg', }); chart.render(); }); + + it('should work with FAM values only', () => { + // I1+I2 + // F1 + // | + // I3 + const json: JsonGedcomData = { + fams: [ + {id: 'F1', children: []}, + ], + indis: [ + {id: 'I1', fams: ['F1'], sex: "M"}, + {id: 'I2', fams: ['F1'], sex: "F"}, + {id: 'I3', famc: 'F1', sex: "F"}, + ], + }; + const data = new JsonDataProvider(json); + const chart = new AncestorChart({ + data, + startFam: json.fams[0].id, + renderer: new FakeRenderer(), + svgSelector: 'svg', + }); + chart.render(); + }); + });