كيف ترسم رسما بيانيا خطيا متحركا بـ SVG؟
الرسم الخطي يوضح تغير الرقم عبر الزمن، وحين يُرسم الخط أمام العين يتتبعه القارئ مع القصة. لا نحتاج مكتبة: مصفوفة بيانات وبضعة أسطر تبني مسارا SVG ثم نرسمه بـ stroke-dashoffset.
الخطوات
- اكتب البيانات كمصفوفة، واحسب موضع كل نقطة (x وy) من قيمتها، ثم ابنِ المسار بـ M للنقطة الأولى وL لكل نقطة بعدها.
- خذ الطول الكامل للمسار بـ path.getTotalLength().
- اجعل stroke-dasharray وstroke-dashoffset يساويان الطول، ثم حرّك الإزاحة إلى 0 (هنا بـ Web Animations API).
- أضف الدوائر فوق النقاط بحركة pop متأخرة بعد انتهاء رسم الخط.
- أبقِ مساحة الرسم direction: ltr حتى لا يتبدل مكان الأرقام، وضع أسماء الأشهر بالعربية كنص SVG، وعند prefers-reduced-motion أظهر الخط جاهزا.
المصادر
- MDN: getTotalLength() تعيد الطول الكلي للمسار: https://developer.mozilla.org/en-US/docs/Web/API/SVGGeometryElement/getTotalLength
- MDN: stroke-dashoffset تزيح بداية نمط الشرطات: https://developer.mozilla.org/en-US/docs/Web/SVG/Reference/Attribute/stroke-dashoffset
- MDN: stroke-dasharray تحدد نمط الشرطات: https://developer.mozilla.org/en-US/docs/Web/SVG/Reference/Attribute/stroke-dasharray
- MDN: Element.animate() تشغّل حركة Web Animations API: https://developer.mozilla.org/en-US/docs/Web/API/Element/animate
- MDN: prefers-reduced-motion يكشف إعداد الجهاز لتقليل الحركة غير الضرورية، وقيمة reduce تعني أنه مفعّل: https://developer.mozilla.org/en-US/docs/Web/CSS/@media/prefers-reduced-motion
الكود
HTML
<!doctype html>
<html lang="ar" dir="rtl">
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1">
<style>
body { margin: 0; min-height: 100vh; display: grid; place-items: center; background: #0f172a; font-family: system-ui, "Segoe UI", Tahoma, sans-serif; }
svg { width: min(92vw, 640px); height: auto; direction: ltr; } /* the plot area keeps one fixed direction */
.grid { stroke: #334155; stroke-width: 1; }
.line { fill: none; stroke: #38bdf8; stroke-width: 3; stroke-linecap: round; stroke-linejoin: round; }
.dot { fill: #0f172a; stroke: #38bdf8; stroke-width: 3; transform-box: fill-box; transform-origin: center; animation: pop .35s ease-out backwards; animation-delay: calc(1.2s + var(--i) * .12s); }
.lbl { fill: #cbd5e1; font-size: 12px; text-anchor: middle; }
@keyframes pop { from { transform: scale(0); } }
@media (prefers-reduced-motion: reduce) { .dot { animation: none; } }
</style>
<svg id="chart" viewBox="0 0 400 220" role="img" aria-label="مبيعات ستة أشهر"></svg>
<script>
const data = [{ m: 'يناير', v: 30 }, { m: 'فبراير', v: 55 }, { m: 'مارس', v: 42 }, { m: 'أبريل', v: 78 }, { m: 'مايو', v: 64 }, { m: 'يونيو', v: 95 }];
const svg = document.getElementById('chart'), NS = 'http://www.w3.org/2000/svg';
const W = 400, H = 180, padX = 30, max = 100;
const x = i => padX + i * (W - 2 * padX) / (data.length - 1);
const y = v => H - v / max * (H - 20);
const el = (n, a, p = svg) => { const e = document.createElementNS(NS, n); for (const k in a) e.setAttribute(k, a[k]); p.appendChild(e); return e; };
[0, 50, 100].forEach(v => el('line', { class: 'grid', x1: padX, x2: W - padX, y1: y(v), y2: y(v) }));
const line = el('path', { class: 'line', d: data.map((p, i) => (i ? 'L' : 'M') + x(i) + ' ' + y(p.v)).join(' ') });
const len = line.getTotalLength(); // total path length in user units
const reduce = matchMedia('(prefers-reduced-motion: reduce)').matches;
if (!reduce) {
line.style.strokeDasharray = len;
line.style.strokeDashoffset = len;
line.animate([{ strokeDashoffset: len }, { strokeDashoffset: 0 }], { duration: 1200, fill: 'forwards', easing: 'ease-out' });
}
data.forEach((p, i) => {
el('circle', { class: 'dot', cx: x(i), cy: y(p.v), r: 5, style: `--i:${i}` });
const t = el('text', { class: 'lbl', x: x(i), y: H + 22 }); t.textContent = p.m;
});
</script>
</html>لم تجد الحركة التي تريدها؟ اطلبها من هنا
مقالات ذات صلة: