Files
faq_chatbot_frontend/src/app/shared/ui/charts/line-chart.component.ts
T
Peyman Honarjoo 0b5a882ecb feat(reports): implement reports page and data visualization components
Implement a comprehensive reports dashboard including user question
summaries, AI performance metrics, and usage trends. This includes
the creation of several reusable chart components and supporting
services.

- Add `ReportsPage` with tabbed navigation for questions and performance
- Implement `ReportsService` with mock data support for rapid development
- Create reusable UI components: `BarChartComponent`, `DoughnutChartComponent`, and `LineChartComponent`
- Add `format.util.ts` for Persian digit and number formatting
- Integrate 'Lahze' font family and add font assets
- Add unit tests for `ReportsPage` and `ReportsService`
2026-10-09 17:48:18 +03:30

125 lines
4.0 KiB
TypeScript

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<LinePoint[]>();
readonly seriesLabel = input<string>('تعداد سوالات');
readonly unitLabel = input<string>('سوال');
readonly emptyText = input<string>('داده‌ای برای نمایش نیست');
readonly ariaLabel = input<string>('نمودار روند');
readonly gradientId = input<string>('lineAreaGradient');
readonly activeIndex = signal<number | null>(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<LineGeometryPoint[]>(() => {
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<LineGeometryPoint | null>(() => {
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);
}
}