-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
197 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,3 +13,9 @@ | |
.m-1 { | ||
margin: 1rem; | ||
} | ||
.p-1 { | ||
padding: 1rem; | ||
} | ||
.outline { | ||
outline: 1px solid #aaa; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,4 @@ | ||
export * from './lib/cartesian'; | ||
export * from './lib/charts'; | ||
export * from './lib/layout'; | ||
export * from './lib/types'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,44 @@ | ||
import type { Signal } from '@angular/core'; | ||
import type { Signal, WritableSignal } from '@angular/core'; | ||
import type { Primitive } from 'd3-array'; | ||
import type { GapInput, InnerBounds } from '../types'; | ||
|
||
/** | ||
* Base cartesian chart requirements | ||
*/ | ||
export abstract class CartesianChart { | ||
/** | ||
* Data Source | ||
*/ | ||
public readonly data!: Signal<Record<string, Primitive>[]>; | ||
|
||
/** | ||
* Initial height of the chart | ||
*/ | ||
public readonly height!: Signal<number>; | ||
/** | ||
* Initial width of the chart | ||
*/ | ||
public readonly width!: Signal<number>; | ||
public readonly data!: Signal<Record<string, Primitive>[]>; | ||
/** | ||
* These are used to allow the chart to be resized dynamically. | ||
* | ||
* @internal | ||
*/ | ||
public readonly _height!: WritableSignal<number>; | ||
/** | ||
* These are used to allow the chart to be resized dynamically. | ||
* | ||
* @internal | ||
*/ | ||
public readonly _width!: WritableSignal<number>; | ||
/** | ||
* Optional. Gap between the chart and the edges of the container | ||
* | ||
* @default 10 | ||
*/ | ||
public readonly gap!: Signal<GapInput>; | ||
/** | ||
* @internal | ||
*/ | ||
public readonly innerBounds!: Signal<InnerBounds>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
import { GapInput } from '../types'; | ||
|
||
export const DEFAULT_GAPS: GapInput = { top: 10, right: 10, bottom: 10, left: 10 }; | ||
export const DEFAULT_GAPS: GapInput = { top: 12, right: 12, bottom: 12, left: 12 }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './responsive-container.component'; |
71 changes: 71 additions & 0 deletions
71
apps/ng-vz/src/lib/layout/responsive-container.component.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import { DOCUMENT } from '@angular/common'; | ||
import { | ||
ChangeDetectionStrategy, | ||
Component, | ||
computed, | ||
contentChild, | ||
effect, | ||
ElementRef, | ||
inject, | ||
input, | ||
} from '@angular/core'; | ||
import { toSignal } from '@angular/core/rxjs-interop'; | ||
import { debounceTime, fromEvent } from 'rxjs'; | ||
import { LineChart } from '../charts'; | ||
import { CssNumberValue } from '../types/css'; | ||
|
||
@Component({ | ||
selector: 'vz-responsive-container', | ||
template: ` | ||
<ng-content></ng-content> | ||
`, | ||
host: { | ||
'[style]': 'hostStyle()', | ||
}, | ||
changeDetection: ChangeDetectionStrategy.OnPush, | ||
}) | ||
export class ResponsiveContainer { | ||
private readonly element = inject(ElementRef); | ||
private readonly document = inject(DOCUMENT); | ||
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion, @typescript-eslint/no-non-null-asserted-optional-chain | ||
private readonly window = this.document?.defaultView!; | ||
|
||
private readonly chart = contentChild(LineChart); | ||
|
||
public readonly height = input<CssNumberValue>('auto'); | ||
public readonly width = input<CssNumberValue>('auto'); | ||
public readonly debounceTime = input(0); | ||
|
||
private readonly resizingEvent = toSignal(fromEvent(this.window, 'resize').pipe(debounceTime(this.debounceTime()))); | ||
|
||
hostStyle = computed(() => { | ||
const height = this.height(); | ||
const width = this.width(); | ||
|
||
return { | ||
display: 'inline-block', | ||
position: 'absolute', | ||
boxSizing: 'content-box !important', // TODO: Remove this once we have a better way to handle this | ||
height, | ||
width, | ||
}; | ||
}); | ||
|
||
constructor() { | ||
effect(() => { | ||
const chart = this.chart(); | ||
const _height = this.height(); | ||
const _width = this.width(); | ||
const _resizingEvent = this.resizingEvent(); | ||
|
||
if (chart) { | ||
const element = this.element.nativeElement.parentElement; | ||
const style = this.window?.getComputedStyle(element); | ||
if (!style) return; | ||
|
||
chart._height.set(element.clientHeight - parseFloat(style.paddingTop) - parseFloat(style.paddingBottom)); | ||
chart._width.set(element.clientWidth - parseFloat(style.paddingLeft) - parseFloat(style.paddingRight)); | ||
} | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export type GapInput = Record<'top' | 'right' | 'bottom' | 'left', number>; | ||
export type InnerBounds = { innerWidth: number; innerHeight: number }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
type CssNumberUnit = 'px' | 'em' | 'rem' | 'ex' | 'ch' | 'vw' | 'vh' | 'vmin' | 'vmax' | '%'; | ||
export type CssNumberValue = `${number}${CssNumberUnit}` | 'auto'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
export type GapInput = Record<'top' | 'right' | 'bottom' | 'left', number>; | ||
export type InnerBounds = { innerWidth: number; innerHeight: number }; | ||
export * from './chart'; | ||
export * from './css'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './strip-css-units.util'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
export const stripCssUnits = (cssValue: string) => { | ||
const match = cssValue.match(/^(-?\d*\.?\d+)([a-z%]*)$/i); | ||
return match ? parseFloat(match[1]) : NaN; | ||
}; |