import { ChangeDetectionStrategy, Component, computed, input, signal } from '@angular/core'; import { CommonModule } from '@angular/common'; import { formatCount } from '../../utils/format.util'; export interface LinePoint { label: string; value: number; } interface LineGeometryPoint extends LinePoint { x: number; y: number; showLabel: boolean; } const WIDTH = 720; const HEIGHT = 260; const PADDING = { top: 26, right: 52, bottom: 36, left: 10 }; const TICK_COUNT = 4; @Component({ selector: 'app-line-chart', standalone: true, imports: [CommonModule], changeDetection: ChangeDetectionStrategy.OnPush, templateUrl: './line-chart.component.html', styleUrl: './line-chart.component.css', }) export class LineChartComponent { readonly points = input.required(); readonly seriesLabel = input('تعداد سوالات'); readonly unitLabel = input('سوال'); readonly emptyText = input('داده‌ای برای نمایش نیست'); readonly ariaLabel = input('نمودار روند'); readonly gradientId = input('lineAreaGradient'); readonly activeIndex = signal(null); readonly tooltipX = signal(0); readonly tooltipY = signal(0); readonly plotWidth = WIDTH - PADDING.left - PADDING.right; readonly plotHeight = HEIGHT - PADDING.top - PADDING.bottom; readonly viewBox = `0 0 ${WIDTH} ${HEIGHT}`; readonly plotRight = WIDTH - PADDING.right; readonly plotTop = PADDING.top; readonly baseline = HEIGHT - PADDING.bottom; readonly maxValue = computed(() => { const values = this.points().map((point) => point.value); return values.length ? Math.max(...values) : 0; }); readonly niceMax = computed(() => { const max = this.maxValue(); if (max <= 0) return 1; const magnitude = Math.pow(10, Math.floor(Math.log10(max))); return Math.ceil(max / magnitude) * magnitude; }); readonly ticks = computed(() => { const max = this.niceMax(); return Array.from({ length: TICK_COUNT + 1 }, (_, index) => (max / TICK_COUNT) * index); }); readonly geometry = computed(() => { const points = this.points(); const max = this.niceMax(); const lastIndex = Math.max(points.length - 1, 1); const labelStep = Math.max(Math.ceil(points.length / 6), 1); return points.map((point, index) => { // RTL: oldest point on the right, newest on the left. const x = WIDTH - PADDING.right - (index / lastIndex) * this.plotWidth; const y = PADDING.top + (1 - point.value / max) * this.plotHeight; return { ...point, x, y, showLabel: index % labelStep === 0 || index === points.length - 1 }; }); }); readonly linePath = computed(() => this.geometry() .map((point, index) => `${index === 0 ? 'M' : 'L'}${point.x} ${point.y}`) .join(' '), ); readonly areaPath = computed(() => { const points = this.geometry(); if (points.length === 0) return ''; const first = points[0]; const last = points[points.length - 1]; const baseline = HEIGHT - PADDING.bottom; return `M${first.x} ${first.y} ${points .slice(1) .map((point) => `L${point.x} ${point.y}`) .join(' ')} L${last.x} ${baseline} L${first.x} ${baseline} Z`; }); readonly activePoint = computed(() => { const index = this.activeIndex(); return index === null ? null : (this.geometry()[index] ?? null); }); formatValue(value: number): string { return formatCount(value); } tickY(tick: number): number { return this.plotTop + (1 - tick / this.niceMax()) * this.plotHeight; } onPointEnter(index: number, event: MouseEvent): void { const host = (event.currentTarget as Element).closest('.line-chart') as HTMLElement | null; if (host) { const rect = host.getBoundingClientRect(); this.tooltipX.set(event.clientX - rect.left); this.tooltipY.set(event.clientY - rect.top); } this.activeIndex.set(index); } onPointLeave(): void { this.activeIndex.set(null); } }