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`
This commit is contained in:
@@ -0,0 +1,137 @@
|
||||
.bar-chart {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 1.1rem;
|
||||
margin: 0;
|
||||
padding: 0;
|
||||
list-style: none;
|
||||
}
|
||||
|
||||
.bar-row {
|
||||
position: relative;
|
||||
}
|
||||
|
||||
.bar-head {
|
||||
display: flex;
|
||||
align-items: baseline;
|
||||
justify-content: space-between;
|
||||
gap: 0.75rem;
|
||||
margin-bottom: 0.4rem;
|
||||
}
|
||||
|
||||
.bar-label {
|
||||
font-size: 0.8125rem;
|
||||
color: var(--text-secondary);
|
||||
overflow: hidden;
|
||||
text-overflow: ellipsis;
|
||||
white-space: nowrap;
|
||||
}
|
||||
|
||||
.bar-metrics {
|
||||
display: flex;
|
||||
align-items: baseline;
|
||||
gap: 0.5rem;
|
||||
flex-shrink: 0;
|
||||
}
|
||||
|
||||
.bar-value {
|
||||
font-size: 0.8125rem;
|
||||
font-weight: 600;
|
||||
color: var(--text-primary);
|
||||
}
|
||||
|
||||
.bar-share {
|
||||
font-size: 0.6875rem;
|
||||
color: var(--text-muted);
|
||||
}
|
||||
|
||||
.bar-track {
|
||||
position: relative;
|
||||
height: 12px;
|
||||
border-radius: 9999px;
|
||||
background: var(--bar-track-bg, rgba(100, 116, 139, 0.16));
|
||||
cursor: default;
|
||||
}
|
||||
|
||||
.bar-fill {
|
||||
height: 100%;
|
||||
border-radius: 9999px;
|
||||
background: var(--bar-color);
|
||||
transform-origin: right center;
|
||||
animation: barGrow 0.7s cubic-bezier(0.22, 1, 0.36, 1) both;
|
||||
}
|
||||
|
||||
.bar-tooltip {
|
||||
position: absolute;
|
||||
bottom: calc(100% + 8px);
|
||||
z-index: 20;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 0.15rem;
|
||||
min-width: max-content;
|
||||
max-width: 260px;
|
||||
padding: 0.55rem 0.75rem;
|
||||
border: 1px solid var(--card-border);
|
||||
border-radius: 0.75rem;
|
||||
background: var(--surface-bg);
|
||||
box-shadow: 0 12px 32px rgba(15, 23, 42, 0.18);
|
||||
transform: translateX(50%);
|
||||
pointer-events: none;
|
||||
animation: fadeIn 0.15s ease-out;
|
||||
}
|
||||
|
||||
.bar-tooltip-title {
|
||||
font-size: 0.8125rem;
|
||||
font-weight: 600;
|
||||
color: var(--text-primary);
|
||||
line-height: 1.5;
|
||||
}
|
||||
|
||||
.bar-tooltip-row {
|
||||
font-size: 0.75rem;
|
||||
color: var(--text-secondary);
|
||||
}
|
||||
|
||||
.bar-tooltip-hint {
|
||||
font-size: 0.6875rem;
|
||||
color: var(--text-muted);
|
||||
}
|
||||
|
||||
.chart-empty {
|
||||
padding: 2.5rem 1rem;
|
||||
text-align: center;
|
||||
font-size: 0.875rem;
|
||||
color: var(--text-muted);
|
||||
}
|
||||
|
||||
@keyframes barGrow {
|
||||
from {
|
||||
transform: scaleX(0);
|
||||
}
|
||||
to {
|
||||
transform: scaleX(1);
|
||||
}
|
||||
}
|
||||
|
||||
@keyframes fadeIn {
|
||||
from {
|
||||
opacity: 0;
|
||||
}
|
||||
to {
|
||||
opacity: 1;
|
||||
}
|
||||
}
|
||||
|
||||
@media (prefers-reduced-motion: reduce) {
|
||||
.bar-fill {
|
||||
animation: none;
|
||||
}
|
||||
}
|
||||
|
||||
:host-context([data-bs-theme='dark']) .bar-track {
|
||||
background: rgba(148, 163, 184, 0.22);
|
||||
}
|
||||
|
||||
:host-context([data-bs-theme='dark']) .bar-tooltip {
|
||||
box-shadow: 0 12px 32px rgba(0, 0, 0, 0.5);
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
@if (items().length) {
|
||||
<ul class="bar-chart" role="list" [attr.aria-label]="ariaLabel()">
|
||||
@for (item of items(); track item.label; let index = $index) {
|
||||
<li class="bar-row">
|
||||
<div class="bar-head">
|
||||
<span class="bar-label" [title]="item.label">{{ item.label }}</span>
|
||||
<span class="bar-metrics">
|
||||
<span class="bar-value">{{ format(item.value) }}</span>
|
||||
<span class="bar-share">{{ share(item.value) }}</span>
|
||||
</span>
|
||||
</div>
|
||||
|
||||
<div
|
||||
class="bar-track"
|
||||
[style.--bar-color]="item.color"
|
||||
(mouseenter)="onTrackMove(index, $event)"
|
||||
(mousemove)="onTrackMove(index, $event)"
|
||||
(mouseleave)="onTrackLeave()"
|
||||
>
|
||||
<div class="bar-fill" [style.width]="barWidth(item.value)"></div>
|
||||
</div>
|
||||
|
||||
@if (activeIndex() === index) {
|
||||
<div
|
||||
class="bar-tooltip"
|
||||
role="tooltip"
|
||||
[style.inset-inline-start]="tooltipX() + 'px'"
|
||||
>
|
||||
<strong class="bar-tooltip-title">{{ item.label }}</strong>
|
||||
<span class="bar-tooltip-row">تعداد: {{ format(item.value) }}</span>
|
||||
<span class="bar-tooltip-row">سهم: {{ share(item.value) }}</span>
|
||||
@if (item.hint) {
|
||||
<span class="bar-tooltip-hint">{{ item.hint }}</span>
|
||||
}
|
||||
</div>
|
||||
}
|
||||
</li>
|
||||
}
|
||||
</ul>
|
||||
} @else {
|
||||
<p class="chart-empty">{{ emptyText() }}</p>
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
import { ChangeDetectionStrategy, Component, computed, input, signal } from '@angular/core';
|
||||
import { CommonModule } from '@angular/common';
|
||||
import { formatCount, formatPercent } from '../../utils/format.util';
|
||||
|
||||
export interface BarChartItem {
|
||||
label: string;
|
||||
value: number;
|
||||
color: string;
|
||||
hint?: string;
|
||||
}
|
||||
|
||||
@Component({
|
||||
selector: 'app-bar-chart',
|
||||
standalone: true,
|
||||
imports: [CommonModule],
|
||||
changeDetection: ChangeDetectionStrategy.OnPush,
|
||||
templateUrl: './bar-chart.component.html',
|
||||
styleUrl: './bar-chart.component.css',
|
||||
})
|
||||
export class BarChartComponent {
|
||||
readonly items = input.required<BarChartItem[]>();
|
||||
readonly valueSuffix = input<string>('');
|
||||
readonly emptyText = input<string>('دادهای برای نمایش نیست');
|
||||
readonly ariaLabel = input<string>('نمودار میلهای');
|
||||
|
||||
readonly activeIndex = signal<number | null>(null);
|
||||
readonly tooltipX = signal(0);
|
||||
|
||||
readonly total = computed(() => this.items().reduce((sum, item) => sum + item.value, 0));
|
||||
readonly max = computed(() => {
|
||||
const values = this.items().map((item) => item.value);
|
||||
return values.length ? Math.max(...values) : 0;
|
||||
});
|
||||
|
||||
barWidth(value: number): string {
|
||||
return this.max() > 0 ? `${(value / this.max()) * 100}%` : '0%';
|
||||
}
|
||||
|
||||
share(value: number): string {
|
||||
return this.total() > 0 ? formatPercent((value / this.total()) * 100) : '۰٪';
|
||||
}
|
||||
|
||||
format(value: number): string {
|
||||
return `${formatCount(value)}${this.valueSuffix()}`;
|
||||
}
|
||||
|
||||
onTrackMove(index: number, event: MouseEvent): void {
|
||||
const track = event.currentTarget as HTMLElement;
|
||||
const rect = track.getBoundingClientRect();
|
||||
this.tooltipX.set(event.clientX - rect.left);
|
||||
this.activeIndex.set(index);
|
||||
}
|
||||
|
||||
onTrackLeave(): void {
|
||||
this.activeIndex.set(null);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,181 @@
|
||||
.doughnut-wrapper {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
align-items: center;
|
||||
gap: 1.25rem;
|
||||
}
|
||||
|
||||
.doughnut-chart {
|
||||
position: relative;
|
||||
width: 200px;
|
||||
height: 200px;
|
||||
flex-shrink: 0;
|
||||
}
|
||||
|
||||
.doughnut-svg {
|
||||
width: 100%;
|
||||
height: 100%;
|
||||
}
|
||||
|
||||
.doughnut-base {
|
||||
fill: none;
|
||||
stroke: var(--doughnut-base-bg, rgba(100, 116, 139, 0.14));
|
||||
stroke-width: 22;
|
||||
}
|
||||
|
||||
.doughnut-segment {
|
||||
fill: none;
|
||||
stroke-width: 22;
|
||||
stroke-linecap: butt;
|
||||
cursor: default;
|
||||
transition: stroke-width 0.2s ease, opacity 0.2s ease;
|
||||
animation: doughnutDraw 0.8s cubic-bezier(0.22, 1, 0.36, 1) both;
|
||||
}
|
||||
|
||||
.doughnut-segment-active {
|
||||
stroke-width: 27;
|
||||
}
|
||||
|
||||
.doughnut-center {
|
||||
position: absolute;
|
||||
inset: 0;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
gap: 0.15rem;
|
||||
pointer-events: none;
|
||||
text-align: center;
|
||||
padding: 0 2.2rem;
|
||||
}
|
||||
|
||||
.doughnut-center-value {
|
||||
font-size: 1.5rem;
|
||||
font-weight: 700;
|
||||
color: var(--text-primary);
|
||||
line-height: 1.2;
|
||||
}
|
||||
|
||||
.doughnut-center-caption {
|
||||
font-size: 0.6875rem;
|
||||
color: var(--text-muted);
|
||||
line-height: 1.4;
|
||||
}
|
||||
|
||||
.doughnut-legend {
|
||||
display: grid;
|
||||
grid-template-columns: repeat(2, minmax(0, 1fr));
|
||||
gap: 0.5rem 1rem;
|
||||
width: 100%;
|
||||
margin: 0;
|
||||
padding: 0;
|
||||
list-style: none;
|
||||
}
|
||||
|
||||
.legend-item {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 0.5rem;
|
||||
padding: 0.35rem 0.5rem;
|
||||
border-radius: 0.5rem;
|
||||
font-size: 0.75rem;
|
||||
color: var(--text-secondary);
|
||||
transition: background 0.2s ease;
|
||||
cursor: default;
|
||||
}
|
||||
|
||||
.legend-item-active,
|
||||
.legend-item:hover {
|
||||
background: var(--sidebar-hover);
|
||||
}
|
||||
|
||||
.legend-dot {
|
||||
width: 0.625rem;
|
||||
height: 0.625rem;
|
||||
border-radius: 50%;
|
||||
flex-shrink: 0;
|
||||
}
|
||||
|
||||
.legend-label {
|
||||
overflow: hidden;
|
||||
text-overflow: ellipsis;
|
||||
white-space: nowrap;
|
||||
}
|
||||
|
||||
.legend-value {
|
||||
margin-inline-start: auto;
|
||||
font-weight: 600;
|
||||
color: var(--text-primary);
|
||||
}
|
||||
|
||||
.legend-percent {
|
||||
min-width: 2.75rem;
|
||||
text-align: end;
|
||||
color: var(--text-muted);
|
||||
}
|
||||
|
||||
.chart-tooltip {
|
||||
position: absolute;
|
||||
z-index: 20;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 0.15rem;
|
||||
min-width: max-content;
|
||||
max-width: 240px;
|
||||
padding: 0.55rem 0.75rem;
|
||||
border: 1px solid var(--card-border);
|
||||
border-radius: 0.75rem;
|
||||
background: var(--surface-bg);
|
||||
box-shadow: 0 12px 32px rgba(15, 23, 42, 0.18);
|
||||
transform: translate(50%, -110%);
|
||||
pointer-events: none;
|
||||
animation: tooltipFade 0.15s ease-out;
|
||||
}
|
||||
|
||||
.chart-tooltip-title {
|
||||
font-size: 0.8125rem;
|
||||
font-weight: 600;
|
||||
color: var(--text-primary);
|
||||
}
|
||||
|
||||
.chart-tooltip-row {
|
||||
font-size: 0.75rem;
|
||||
color: var(--text-secondary);
|
||||
}
|
||||
|
||||
.chart-empty {
|
||||
padding: 2.5rem 1rem;
|
||||
text-align: center;
|
||||
font-size: 0.875rem;
|
||||
color: var(--text-muted);
|
||||
}
|
||||
|
||||
@keyframes doughnutDraw {
|
||||
from {
|
||||
opacity: 0;
|
||||
stroke-dasharray: 0 377;
|
||||
}
|
||||
}
|
||||
|
||||
@keyframes tooltipFade {
|
||||
from {
|
||||
opacity: 0;
|
||||
}
|
||||
to {
|
||||
opacity: 1;
|
||||
}
|
||||
}
|
||||
|
||||
@media (prefers-reduced-motion: reduce) {
|
||||
.doughnut-segment {
|
||||
animation: none;
|
||||
}
|
||||
}
|
||||
|
||||
:host-context([data-bs-theme='dark']) .doughnut-base {
|
||||
stroke: rgba(148, 163, 184, 0.18);
|
||||
}
|
||||
|
||||
:host-context([data-bs-theme='dark']) .chart-tooltip {
|
||||
box-shadow: 0 12px 32px rgba(0, 0, 0, 0.5);
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
@if (segments().length) {
|
||||
<div class="doughnut-wrapper">
|
||||
<div class="doughnut-chart" role="img" [attr.aria-label]="ariaLabel()">
|
||||
<svg viewBox="0 0 160 160" class="doughnut-svg">
|
||||
<circle class="doughnut-base" cx="80" cy="80" [attr.r]="60" />
|
||||
<g transform="rotate(-90 80 80)">
|
||||
@for (arc of arcs(); track arc.label; let index = $index) {
|
||||
<circle
|
||||
class="doughnut-segment"
|
||||
[class.doughnut-segment-active]="activeIndex() === index"
|
||||
[style.stroke]="arc.color"
|
||||
[style.stroke-dasharray]="arc.dashArray"
|
||||
[style.stroke-dashoffset]="arc.dashOffset"
|
||||
cx="80"
|
||||
cy="80"
|
||||
r="60"
|
||||
(mouseenter)="onSegmentEnter(index, $event)"
|
||||
(mouseleave)="onSegmentLeave()"
|
||||
/>
|
||||
}
|
||||
</g>
|
||||
</svg>
|
||||
|
||||
<div class="doughnut-center" aria-hidden="true">
|
||||
<span class="doughnut-center-value">{{ centerValue() }}</span>
|
||||
<span class="doughnut-center-caption">{{ centerCaption() }}</span>
|
||||
</div>
|
||||
|
||||
@if (activeIndex() !== null) {
|
||||
<div
|
||||
class="chart-tooltip"
|
||||
role="tooltip"
|
||||
[style.inset-inline-start]="tooltipX() + 'px'"
|
||||
[style.top]="tooltipY() + 'px'"
|
||||
>
|
||||
<strong class="chart-tooltip-title">{{ arcs()[activeIndex()!].label }}</strong>
|
||||
<span class="chart-tooltip-row">تعداد: {{ formatValue(arcs()[activeIndex()!].value) }}</span>
|
||||
<span class="chart-tooltip-row">سهم: {{ formatShare(arcs()[activeIndex()!].value) }}</span>
|
||||
</div>
|
||||
}
|
||||
</div>
|
||||
|
||||
<ul class="doughnut-legend" role="list">
|
||||
@for (arc of arcs(); track arc.label; let index = $index) {
|
||||
<li
|
||||
class="legend-item"
|
||||
[class.legend-item-active]="activeIndex() === index"
|
||||
(mouseenter)="activeIndex.set(index)"
|
||||
(mouseleave)="activeIndex.set(null)"
|
||||
>
|
||||
<span class="legend-dot" [style.background]="arc.color"></span>
|
||||
<span class="legend-label">{{ arc.label }}</span>
|
||||
<span class="legend-value">{{ formatValue(arc.value) }}</span>
|
||||
<span class="legend-percent">{{ formatShare(arc.value) }}</span>
|
||||
</li>
|
||||
}
|
||||
</ul>
|
||||
</div>
|
||||
} @else {
|
||||
<p class="chart-empty">{{ emptyText() }}</p>
|
||||
}
|
||||
@@ -0,0 +1,95 @@
|
||||
import { ChangeDetectionStrategy, Component, computed, input, signal } from '@angular/core';
|
||||
import { CommonModule } from '@angular/common';
|
||||
import { formatCount, formatPercent } from '../../utils/format.util';
|
||||
|
||||
export interface DoughnutSegment {
|
||||
label: string;
|
||||
value: number;
|
||||
color: string;
|
||||
}
|
||||
|
||||
interface DoughnutArc extends DoughnutSegment {
|
||||
dashArray: string;
|
||||
dashOffset: number;
|
||||
percent: number;
|
||||
}
|
||||
|
||||
const RADIUS = 60;
|
||||
const CIRCUMFERENCE = 2 * Math.PI * RADIUS;
|
||||
const SEGMENT_GAP = 3;
|
||||
|
||||
@Component({
|
||||
selector: 'app-doughnut-chart',
|
||||
standalone: true,
|
||||
imports: [CommonModule],
|
||||
changeDetection: ChangeDetectionStrategy.OnPush,
|
||||
templateUrl: './doughnut-chart.component.html',
|
||||
styleUrl: './doughnut-chart.component.css',
|
||||
})
|
||||
export class DoughnutChartComponent {
|
||||
readonly segments = input.required<DoughnutSegment[]>();
|
||||
readonly centerLabel = input<string>('کل سوالات');
|
||||
readonly emptyText = input<string>('دادهای برای نمایش نیست');
|
||||
readonly ariaLabel = input<string>('نمودار دایرهای');
|
||||
|
||||
readonly activeIndex = signal<number | null>(null);
|
||||
readonly tooltipX = signal(0);
|
||||
readonly tooltipY = signal(0);
|
||||
|
||||
readonly total = computed(() => this.segments().reduce((sum, segment) => sum + segment.value, 0));
|
||||
|
||||
readonly arcs = computed<DoughnutArc[]>(() => {
|
||||
const total = this.total();
|
||||
let cumulative = 0;
|
||||
|
||||
return this.segments().map((segment) => {
|
||||
const fraction = total > 0 ? segment.value / total : 0;
|
||||
const dashLength = Math.max(fraction * CIRCUMFERENCE - SEGMENT_GAP, 0);
|
||||
|
||||
const arc: DoughnutArc = {
|
||||
...segment,
|
||||
dashArray: `${dashLength} ${CIRCUMFERENCE}`,
|
||||
dashOffset: -cumulative,
|
||||
percent: fraction * 100,
|
||||
};
|
||||
|
||||
cumulative += fraction * CIRCUMFERENCE;
|
||||
return arc;
|
||||
});
|
||||
});
|
||||
|
||||
readonly centerValue = computed(() => {
|
||||
const active = this.activeIndex();
|
||||
if (active === null) return this.formatValue(this.total());
|
||||
return this.arcs()[active] ? this.formatValue(this.arcs()[active].value) : '۰';
|
||||
});
|
||||
|
||||
readonly centerCaption = computed(() => {
|
||||
const active = this.activeIndex();
|
||||
if (active === null) return this.centerLabel();
|
||||
const arc = this.arcs()[active];
|
||||
return arc ? `${arc.label} — ${formatPercent(arc.percent)}` : this.centerLabel();
|
||||
});
|
||||
|
||||
formatValue(value: number): string {
|
||||
return formatCount(value);
|
||||
}
|
||||
|
||||
formatShare(value: number): string {
|
||||
return this.total() > 0 ? formatPercent((value / this.total()) * 100) : '۰٪';
|
||||
}
|
||||
|
||||
onSegmentEnter(index: number, event: MouseEvent): void {
|
||||
const host = (event.currentTarget as Element).closest('.doughnut-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);
|
||||
}
|
||||
|
||||
onSegmentLeave(): void {
|
||||
this.activeIndex.set(null);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,137 @@
|
||||
.line-chart {
|
||||
position: relative;
|
||||
width: 100%;
|
||||
}
|
||||
|
||||
.line-svg {
|
||||
width: 100%;
|
||||
height: auto;
|
||||
overflow: visible;
|
||||
}
|
||||
|
||||
.line-grid line {
|
||||
stroke: var(--line-grid-color, rgba(100, 116, 139, 0.18));
|
||||
stroke-width: 1;
|
||||
stroke-dasharray: 4 6;
|
||||
}
|
||||
|
||||
.line-tick-label {
|
||||
font-size: 6px;
|
||||
fill: var(--text-muted);
|
||||
}
|
||||
|
||||
.line-x-label {
|
||||
font-size: 6px;
|
||||
fill: var(--text-muted);
|
||||
}
|
||||
|
||||
.line-area {
|
||||
animation: areaFade 0.8s ease-out both;
|
||||
}
|
||||
|
||||
.line-path {
|
||||
stroke-dasharray: 2000;
|
||||
animation: lineDraw 1.1s cubic-bezier(0.22, 1, 0.36, 1) both;
|
||||
}
|
||||
|
||||
.line-hit-area {
|
||||
fill: transparent;
|
||||
cursor: default;
|
||||
}
|
||||
|
||||
.line-point {
|
||||
fill: var(--surface-bg);
|
||||
stroke: var(--color-primary);
|
||||
stroke-width: 2;
|
||||
transition: r 0.15s ease;
|
||||
}
|
||||
|
||||
.line-point-active {
|
||||
r: 4;
|
||||
fill: var(--color-primary);
|
||||
}
|
||||
|
||||
.line-guide {
|
||||
stroke: var(--color-primary);
|
||||
stroke-width: 1.5;
|
||||
stroke-dasharray: 3 4;
|
||||
opacity: 0.7;
|
||||
animation: fadeIn 0.15s ease-out;
|
||||
}
|
||||
|
||||
.chart-tooltip {
|
||||
position: absolute;
|
||||
z-index: 20;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 0.15rem;
|
||||
min-width: max-content;
|
||||
padding: 0.55rem 0.75rem;
|
||||
border: 1px solid var(--card-border);
|
||||
border-radius: 0.75rem;
|
||||
background: var(--surface-bg);
|
||||
box-shadow: 0 12px 32px rgba(15, 23, 42, 0.18);
|
||||
transform: translate(50%, -110%);
|
||||
pointer-events: none;
|
||||
animation: fadeIn 0.15s ease-out;
|
||||
}
|
||||
|
||||
.chart-tooltip-title {
|
||||
font-size: 0.8125rem;
|
||||
font-weight: 600;
|
||||
color: var(--text-primary);
|
||||
}
|
||||
|
||||
.chart-tooltip-row {
|
||||
font-size: 0.75rem;
|
||||
color: var(--text-secondary);
|
||||
}
|
||||
|
||||
.chart-empty {
|
||||
padding: 2.5rem 1rem;
|
||||
text-align: center;
|
||||
font-size: 0.875rem;
|
||||
color: var(--text-muted);
|
||||
}
|
||||
|
||||
@keyframes lineDraw {
|
||||
from {
|
||||
stroke-dashoffset: 2000;
|
||||
}
|
||||
to {
|
||||
stroke-dashoffset: 0;
|
||||
}
|
||||
}
|
||||
|
||||
@keyframes areaFade {
|
||||
from {
|
||||
opacity: 0;
|
||||
}
|
||||
to {
|
||||
opacity: 1;
|
||||
}
|
||||
}
|
||||
|
||||
@keyframes fadeIn {
|
||||
from {
|
||||
opacity: 0;
|
||||
}
|
||||
to {
|
||||
opacity: 1;
|
||||
}
|
||||
}
|
||||
|
||||
@media (prefers-reduced-motion: reduce) {
|
||||
.line-path,
|
||||
.line-area {
|
||||
animation: none;
|
||||
}
|
||||
}
|
||||
|
||||
:host-context([data-bs-theme='dark']) .line-grid line {
|
||||
stroke: rgba(148, 163, 184, 0.2);
|
||||
}
|
||||
|
||||
:host-context([data-bs-theme='dark']) .chart-tooltip {
|
||||
box-shadow: 0 12px 32px rgba(0, 0, 0, 0.5);
|
||||
}
|
||||
@@ -0,0 +1,88 @@
|
||||
@if (points().length) {
|
||||
<div class="line-chart" role="img" [attr.aria-label]="ariaLabel()">
|
||||
<svg [attr.viewBox]="viewBox" class="line-svg" preserveAspectRatio="xMidYMid meet">
|
||||
<defs>
|
||||
<linearGradient [attr.id]="gradientId()" x1="0" y1="0" x2="0" y2="1">
|
||||
<stop offset="0%" stop-color="var(--color-primary)" stop-opacity="0.32" />
|
||||
<stop offset="100%" stop-color="var(--color-primary)" stop-opacity="0.02" />
|
||||
</linearGradient>
|
||||
</defs>
|
||||
|
||||
@for (tick of ticks(); track tick) {
|
||||
<g class="line-grid">
|
||||
<line
|
||||
[attr.x1]="10"
|
||||
[attr.y1]="tickY(tick)"
|
||||
[attr.x2]="plotRight"
|
||||
[attr.y2]="tickY(tick)"
|
||||
/>
|
||||
<text
|
||||
class="line-tick-label"
|
||||
[attr.x]="plotRight + 8"
|
||||
[attr.y]="tickY(tick) + 4"
|
||||
>{{ formatValue(tick) }}</text>
|
||||
</g>
|
||||
}
|
||||
|
||||
<path class="line-area" [attr.d]="areaPath()" [attr.fill]="'url(#' + gradientId() + ')'" />
|
||||
<path
|
||||
class="line-path"
|
||||
[attr.d]="linePath()"
|
||||
fill="none"
|
||||
stroke="var(--color-primary)"
|
||||
stroke-width="2.5"
|
||||
stroke-linecap="round"
|
||||
stroke-linejoin="round"
|
||||
/>
|
||||
|
||||
@for (point of geometry(); track point.label; let index = $index) {
|
||||
<g class="line-point-group">
|
||||
@if (point.showLabel) {
|
||||
<text class="line-x-label" [attr.x]="point.x" [attr.y]="baseline + 18" text-anchor="middle">
|
||||
{{ point.label }}
|
||||
</text>
|
||||
}
|
||||
<circle
|
||||
class="line-hit-area"
|
||||
[attr.cx]="point.x"
|
||||
[attr.cy]="point.y"
|
||||
r="14"
|
||||
(mouseenter)="onPointEnter(index, $event)"
|
||||
(mouseleave)="onPointLeave()"
|
||||
/>
|
||||
<circle
|
||||
class="line-point"
|
||||
[class.line-point-active]="activeIndex() === index"
|
||||
[attr.cx]="point.x"
|
||||
[attr.cy]="point.y"
|
||||
r="2.5"
|
||||
/>
|
||||
</g>
|
||||
}
|
||||
|
||||
@if (activePoint()) {
|
||||
<line
|
||||
class="line-guide"
|
||||
[attr.x1]="activePoint()!.x"
|
||||
[attr.y1]="plotTop"
|
||||
[attr.x2]="activePoint()!.x"
|
||||
[attr.y2]="baseline"
|
||||
/>
|
||||
}
|
||||
</svg>
|
||||
|
||||
@if (activePoint()) {
|
||||
<div
|
||||
class="chart-tooltip"
|
||||
role="tooltip"
|
||||
[style.inset-inline-start]="tooltipX() + 'px'"
|
||||
[style.top]="tooltipY() + 'px'"
|
||||
>
|
||||
<strong class="chart-tooltip-title">{{ activePoint()!.label }}</strong>
|
||||
<span class="chart-tooltip-row">{{ seriesLabel() }}: {{ formatValue(activePoint()!.value) }} {{ unitLabel() }}</span>
|
||||
</div>
|
||||
}
|
||||
</div>
|
||||
} @else {
|
||||
<p class="chart-empty">{{ emptyText() }}</p>
|
||||
}
|
||||
@@ -0,0 +1,124 @@
|
||||
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);
|
||||
}
|
||||
}
|
||||
@@ -178,7 +178,7 @@
|
||||
border-radius: 0.5rem;
|
||||
color: var(--sidebar-muted);
|
||||
text-decoration: none;
|
||||
font-size: 1rem;
|
||||
font-size: 0.875rem;
|
||||
font-weight: 500;
|
||||
transition: background 0.2s;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user