From bfe96d7066cf6e1371308ea4eacf66e75cb7038d Mon Sep 17 00:00:00 2001 From: Jiajing Wang Date: Wed, 19 Dec 2018 14:26:59 -0800 Subject: [PATCH 1/4] Add typings for venn.js --- types/venn/index.d.ts | 105 +++++++++++++++++++++++++++++++++++++++ types/venn/tsconfig.json | 24 +++++++++ types/venn/tslint.json | 6 +++ types/venn/venn-tests.ts | 71 ++++++++++++++++++++++++++ 4 files changed, 206 insertions(+) create mode 100644 types/venn/index.d.ts create mode 100644 types/venn/tsconfig.json create mode 100644 types/venn/tslint.json create mode 100644 types/venn/venn-tests.ts diff --git a/types/venn/index.d.ts b/types/venn/index.d.ts new file mode 100644 index 0000000000..68054206d3 --- /dev/null +++ b/types/venn/index.d.ts @@ -0,0 +1,105 @@ +// Type definitions for venn 0.2.16 +// Project: https://github.com/benfred/venn.js/ +// Definitions by: Jiajing Wang +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped +// TypeScript Version: 2.8 + +import * as d3 from 'd3'; + +/** Typing for Point object. */ +export interface Point { + x: number; + y: number; +} + +/** Typing for Circle object. */ +export interface Circle { + x: number; + y: number; + radius: number; +} + +export interface Overlap { + sets: number[]; + size: number; + weight?: number; +} + +/** Typing for Area object. */ +export interface Area { + sets: string[]; + size: number; +} + +/** Typing for layout function parameter. */ +export interface LayoutParameter { + lossFunction?: (sets: {[key: string]: Circle}, overlaps: Overlap[]) => number; + restarts?: number; +} + +/** Typing for the VennDiagram instance. */ +export interface VennDiagram { + (selection: d3.Selection): {}; + width: (w: number) => VennDiagram; + height: (h: number) => VennDiagram; + duration: (d: number) => VennDiagram; + layoutFunction: + (f: (areas: Area[], parameters: LayoutParameter) => void) => VennDiagram; + lossFunction: + (f: (sets: {[key: string]: Circle}, + overlaps: Overlap[]) => number) => VennDiagram; + orientationOrder: + (f: (a: {setid: string}, b: {setid: string}) => number) => VennDiagram; +} + +/** + * Typing for venn.intersectionArea that returns the intersection area of a + * bunch of circles (where each circle is an object having an x,y and radius + * property). + */ +export function intersectionArea( + circles: Array<{}>, stats?: {}): number; + +/** + * Typing for venn.distance that returns euclidean distance between two points. + */ +export function distance(p1: Point, p2: Point): number; + +/** + * Typing for venn.circleOverlap that returns the overlap area of two circles + * of radius r1 and r2 - that have their centers separated by distance d. + * Simpler faster circle intersection for only two circles. + */ +export function circleOverlap( + r1: number, r2: number, d: number): number; + +/** + * Typing for venn.circleFromPath that returns a circle object from an svg path. + */ +export function circleFromPath(path: string): Circle; + +/** + * Typing for venn.sortAreas that sorts all areas in the venn diagram, so that + * a particular area is on top (relativeTo) - and all other areas are so that + * the smallest areas are on top. + */ +export function sortAreas( + div: d3.Selection, + relativeTo: Area): void; + +/** + * Typing for venn.bestInitialLayout that takes the best working variant of + * either constrained MDS or greedy. + */ +export function bestInitialLayout( + areas: Area[], params: LayoutParameter): Circle[]; + +/** + * Typing for venn.venn, which given a list of set objects, and their + * corresponding overlaps, updates the (x, y, radius) attribute on each set such + * that their positions roughly correspond to the desired overlaps. + */ +export function venn(areas: Area[], parameters: {}): Circle[]; + +/** Typing for the constructor. */ +export function VennDiagram(): VennDiagram; diff --git a/types/venn/tsconfig.json b/types/venn/tsconfig.json new file mode 100644 index 0000000000..9ccb88daea --- /dev/null +++ b/types/venn/tsconfig.json @@ -0,0 +1,24 @@ +{ + "compilerOptions": { + "module": "commonjs", + "lib": [ + "es6", + "dom" + ], + "noImplicitAny": true, + "noImplicitThis": true, + "strictNullChecks": true, + "strictFunctionTypes": true, + "baseUrl": "../", + "typeRoots": [ + "../" + ], + "types": [], + "noEmit": true, + "forceConsistentCasingInFileNames": true + }, + "files": [ + "index.d.ts", + "venn-tests.ts" + ] +} diff --git a/types/venn/tslint.json b/types/venn/tslint.json new file mode 100644 index 0000000000..c77c9bc13d --- /dev/null +++ b/types/venn/tslint.json @@ -0,0 +1,6 @@ +{ + "extends": "dtslint/dt.json", + "rules": { + "dt-header": false + } +} diff --git a/types/venn/venn-tests.ts b/types/venn/venn-tests.ts new file mode 100644 index 0000000000..3d88030ae3 --- /dev/null +++ b/types/venn/venn-tests.ts @@ -0,0 +1,71 @@ +/** + * Created by marcwjj on 12/19/2018. + */ + +import venn = require('venn'); + +const vennDiagram = venn.VennDiagram() + .width(100) + .height(200) + .duration(300); + +vennDiagram.orientationOrder( + (a: {setid: string}, b: {setid: string}) => + a['setid'].localeCompare(b['setid'])); + +function lossFunction( + sets: {[key: string]: venn.Circle}, + overlaps: venn.Overlap[]): number { + let output = 0; + for (const area of overlaps) { + let overlapSize; + if (area.sets.length === 1) { + continue; + } else { + if (area.sets.length === 2) { + const left = sets[area.sets[0]]; + const right = sets[area.sets[1]]; + overlapSize = venn.circleOverlap( + left.radius, right.radius, venn.distance(left, right)); + } else { + overlapSize = + venn.intersectionArea(area.sets.map((i: number) => sets[i])); + } + } + if (overlapSize < 100 && area.size > 0) { + return Number.MAX_VALUE; + } + + // Penalizes rendering overlapping circles when there is no actual overlap. + if (area.size === 0 && overlapSize > 0) { + output += (overlapSize - area.size) * (overlapSize - area.size) * 100000; + } + const weight = area.weight !== undefined ? area.weight : 1; + output += weight * (overlapSize - area.size) * (overlapSize - area.size); + } + return output; +} + +vennDiagram.layoutFunction( + (areas: venn.Area[], parameters: venn.LayoutParameter) => { + return venn.venn( + areas, { + initialLayout: (areas: venn.Area[]) => { + const circles: {[key: string]: venn.Circle} = {}; + let circleCount = 0; + areas.filter((area) => area.sets.length === 1) + .forEach((area) => { + circles[area.sets[0]] = { + x: 0, + y: 0, + radius: Math.sqrt(area.size / Math.PI), + }; + circleCount += 1; + }); + return venn.bestInitialLayout(areas, {lossFunction}); + }, + ...parameters, + }); + }); + +vennDiagram.lossFunction(lossFunction); From b1671d7abe154002279f501b39f5bcb9581106a9 Mon Sep 17 00:00:00 2001 From: Jiajing Wang Date: Wed, 19 Dec 2018 14:36:52 -0800 Subject: [PATCH 2/4] Fix indentation --- types/venn/tsconfig.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/types/venn/tsconfig.json b/types/venn/tsconfig.json index 9ccb88daea..1c2dcb77e6 100644 --- a/types/venn/tsconfig.json +++ b/types/venn/tsconfig.json @@ -8,7 +8,7 @@ "noImplicitAny": true, "noImplicitThis": true, "strictNullChecks": true, - "strictFunctionTypes": true, + "strictFunctionTypes": true, "baseUrl": "../", "typeRoots": [ "../" From 76ffd421f3c7292dddd30bb02326fcf19e1fcb66 Mon Sep 17 00:00:00 2001 From: Jiajing Wang Date: Wed, 19 Dec 2018 17:21:32 -0800 Subject: [PATCH 3/4] Fix one of the typing, add a missing jsdoc. --- types/venn/index.d.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/types/venn/index.d.ts b/types/venn/index.d.ts index 68054206d3..a71fa2edc0 100644 --- a/types/venn/index.d.ts +++ b/types/venn/index.d.ts @@ -19,8 +19,9 @@ export interface Circle { radius: number; } +/** Typing for Overlap object. */ export interface Overlap { - sets: number[]; + sets: string[]; size: number; weight?: number; } From 97e6287fefa303096f95e2be19114dc9dea27f99 Mon Sep 17 00:00:00 2001 From: Jiajing Wang Date: Wed, 19 Dec 2018 17:28:29 -0800 Subject: [PATCH 4/4] Update venn-tests.ts --- types/venn/venn-tests.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/types/venn/venn-tests.ts b/types/venn/venn-tests.ts index 3d88030ae3..8e7e6ce515 100644 --- a/types/venn/venn-tests.ts +++ b/types/venn/venn-tests.ts @@ -29,7 +29,7 @@ function lossFunction( left.radius, right.radius, venn.distance(left, right)); } else { overlapSize = - venn.intersectionArea(area.sets.map((i: number) => sets[i])); + venn.intersectionArea(area.sets.map((i: string) => sets[i])); } } if (overlapSize < 100 && area.size > 0) {