Skip to content

Commit

Permalink
refactor(graph): update createSVGElement signature
Browse files Browse the repository at this point in the history
  • Loading branch information
xHeaven committed Apr 4, 2024
1 parent 3f4d6fd commit 41696a4
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 10 deletions.
51 changes: 48 additions & 3 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ export type SetAttrs = (
attributes: Record<string, string>,
) => void;
export type RemoveAttrs = (element: Element, ...attributes: string[]) => void;
export type CreateSVGElement = (
export type CreateSVGElement = <T extends SVGElement>(
element: string,
container?: Element,
container?: SVGElement | null,
attributes?: Record<string, string>,
) => Element;
) => T;
export type GenerateLegendBackground = (
color: string | string[],
direction?: "horizontal" | "vertical",
Expand All @@ -24,3 +24,48 @@ export type AreEqual = <T>(
value: NestedArray<T>,
newValue: NestedArray<T>,
) => boolean;

export type CreateCurves = (
x1: number,
y1: number,
x2: number,
y2: number,
) => string;
export type CreateVerticalCurves = (
x1: number,
y1: number,
x2: number,
y2: number,
) => string;
export type CreatePath = (
index: number,
X: number[],
Y: number[],
YNext: number[],
) => string;
export type CreateVerticalPath = (
index: number,
X: number[],
XNext: number[],
Y: number[],
) => string;

export interface FunnelGraphData {
colors?: string[];
labels?: string[];
subLabels?: string[];
values?: number[] | number[][];
}

export interface FunnelGraphOptions {
container: HTMLElement | string;
data: FunnelGraphData;
direction?: "horizontal" | "vertical";
displayPercent?: boolean;
formatter?: (value: number) => string;
gradientDirection?: "horizontal" | "vertical";
height?: number;
precision?: number;
subLabelValue?: "percent" | "value";
width?: number;
}
17 changes: 10 additions & 7 deletions src/utils/graph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,18 +35,21 @@ export const removeAttrs: RemoveAttrs = (element, ...attributes) => {
});
};

export const createSVGElement: CreateSVGElement = (
element,
container,
attributes,
) => {
const el = document.createElementNS("http://www.w3.org/2000/svg", element);
export const createSVGElement: CreateSVGElement = <T extends SVGElement>(
element: string,
container?: SVGElement | null,
attributes?: Record<string, string>,
): T => {
const el = document.createElementNS(
"http://www.w3.org/2000/svg",
element,
) as T;

if (typeof attributes === "object") {
setAttrs(el, attributes);
}

if (typeof container !== "undefined") {
if (container !== undefined && container !== null) {
container.appendChild(el);
}

Expand Down

0 comments on commit 41696a4

Please sign in to comment.