/** * Simple Line Chart Component * Lightweight line chart for displaying trends * Modular and extensible for Pro version enhancements */ import React from "react"; import { __ } from "../../lib/i18n"; interface DataPoint { label: string; value: number; } interface SimpleLineChartProps { data: DataPoint[]; title: string; height?: number; color?: string; showGrid?: boolean; } /** * Simple Line Chart Component * Renders a basic line chart using SVG */ export const SimpleLineChart: React.FC = ({ data, title, height = 200, color = "#3b82f6", showGrid = true, }) => { if (!data || data.length === 0) { return (
{__("No data available", "yatra")}
); } const maxValue = Math.max(...data.map((d) => d.value), 1); const minValue = Math.min(...data.map((d) => d.value), 0); const range = maxValue - minValue || 1; const width = 100; const chartHeight = height - 40; const padding = 10; // Calculate points for the line const points = data.map((point, index) => { const x = padding + (index * (width - 2 * padding)) / (data.length - 1 || 1); const y = chartHeight - padding - ((point.value - minValue) / range) * (chartHeight - 2 * padding); return { x, y, value: point.value, label: point.label }; }); // Create path for the line const pathData = points .map((point, index) => { return `${index === 0 ? "M" : "L"} ${point.x} ${point.y}`; }) .join(" "); // Create area path (for fill) const areaPath = `${pathData} L ${points[points.length - 1].x} ${chartHeight - padding} L ${padding} ${chartHeight - padding} Z`; return (
{title && (

{title}

)} {/* Grid lines - subtle */} {showGrid && ( {[0, 0.25, 0.5, 0.75, 1].map((ratio) => { const y = chartHeight - padding - ratio * (chartHeight - 2 * padding); return ( ); })} )} {/* Area fill - subtle */} {/* Line */} {/* Data points */} {points.map((point, index) => ( {`${point.label}: ${point.value}`} ))} {/* X-axis labels */} {points.map((point, index) => { // Show all labels but space them better const shouldShow = index === 0 || index === data.length - 1 || index % Math.max(1, Math.floor(data.length / 4)) === 0; if (shouldShow) { // Split long labels (like "MayJun" -> "May" / "Jun") const labelParts = point.label.length > 5 ? point.label.match(/.{1,3}/g) || [point.label] : [point.label]; return ( {labelParts.map((part, partIndex) => ( {part} ))} ); } return null; })}
); }; export default SimpleLineChart;