GitHub Stars
elements/github-starsFreeBlock
Display repository star history with area chart visualization
Install it
npx shadcn@latest add https://www.tryelements.dev/r/github-stars.jsonSource
1"use client";23import { useEffect, useId, useMemo, useState } from "react";45import { cn } from "@/lib/utils";67interface GitHubStarsProps extends React.HTMLAttributes<HTMLDivElement> {8 owner: string;9 repo: string;10 days?: number;11 staticStars?: number;12 staticData?: number[];13}1415function formatNumber(num: number): string {16 if (num >= 1000000) {17 return `${(num / 1000000).toFixed(1)}M`;18 }19 if (num >= 1000) {20 return `${(num / 1000).toFixed(1)}K`;21 }22 return num.toString();23}2425function generateLinePath(26 data: number[],27 width: number,28 height: number,29 offsetY: number = 0,30): string {31 if (data.length === 0) return "";3233 const max = Math.max(...data);34 const min = Math.min(...data);35 const range = max - min || 1;36 const normalized = data.map((v) => (v - min) / range);3738 const points = normalized.map((value, i) => {39 const x = (i / (data.length - 1)) * width;40 const y = height - value * height + offsetY;41 return { x, y };42 });4344 return points.map((p, i) => `${i === 0 ? "M" : "L"}${p.x},${p.y}`).join(" ");45}4647function generateAreaPath(48 data: number[],49 width: number,50 height: number,51): string {52 if (data.length === 0) return "";5354 const linePath = generateLinePath(data, width, height);55 return `${linePath} L${width},${height} L0,${height} Z`;56}5758function generateHatchLines(59 width: number,60 height: number,61 spacing: number,62): string {63 const lines: string[] = [];64 const diagonal = Math.sqrt(width * width + height * height);65 const count = Math.ceil(diagonal / spacing) * 2;6667 for (let i = -count; i < count; i++) {68 const offset = i * spacing;69 lines.push(70 `M${offset - height},${height + 10} L${offset + width + 10},-10`,71 );72 }7374 return lines.join(" ");75}7677interface AreaChartProps {78 data: number[];79 className?: string;80}8182const AREA_CHART_VIEWBOX_WIDTH = 300;83const AREA_CHART_VIEWBOX_HEIGHT = 200;8485function AreaChart({ data, className }: AreaChartProps) {86 const id = useId();87 const gradientId = `gradient-${id}`;88 const maskId = `mask-${id}`;8990 const width = AREA_CHART_VIEWBOX_WIDTH;91 const height = AREA_CHART_VIEWBOX_HEIGHT;9293 const mainLinePath = useMemo(94 () => generateLinePath(data, width, height),95 [data, height, width],96 );9798 const areaPath = useMemo(99 () => generateAreaPath(data, width, height),100 [data, height, width],101 );102103 const hatchPath = useMemo(104 () => generateHatchLines(width, height, 10),105 [height, width],106 );107108 return (109 <svg110// … truncated
Files it writes
More from elements
All 359 items| Component | Registry | Kind | Access | Installs | Command |
|---|---|---|---|---|---|
| Agentic Codingagentic-coding | elements | Blocks | Free | no deps | |
| AgentMail Logoagentmail-logo | elements | Blocks | Free | no deps | |
| AI Badgeai-badge | elements | Blocks | Free | 2 deps · 2 files | |
| AI Servicesai-services | elements | Blocks | Free | no deps | |
| Astro Logoastro-logo | elements | Blocks | Free | no deps | |
| AWS All Servicesaws-all | elements | Blocks | Free | no deps | |
| AWS Analyticsaws-analytics | elements | Blocks | Free | no deps | |
| AWS Computeaws-compute | elements | Blocks | Free | no deps |
