ข้ามไปยังเนื้อหา

กราฟน้ำตก

WaterfallChart

How a balance changed: totals stand on the baseline, and each change floats from the running balance.

ใช้อธิบายว่ายอดหนึ่งเปลี่ยนจากต้นงวดถึงปลายงวดได้อย่างไร เช่น งบตั้งต้น โอนเข้า โอนออก ใช้จ่าย และคงเหลือ ถ้าดูยอดสะสมตามเวลา ใช้ LineChart ถ้าเทียบยอดของหลายกลุ่ม ใช้ BarChart

import { WaterfallChart } from "@bsru/ui";

ตัวอย่าง

งบประมาณตลอดปี

ยอด total ตั้งบนฐาน ส่วน up และ down ลอยจากยอดสะสมก่อนหน้า ค่าของ down ต้องติดลบ

งบประมาณเปลี่ยนไปอย่างไรตลอดปี (ล้านบาท)
ยอดคงเหลือเพิ่มลด
ดูข้อมูลเป็นตาราง
รายการล้านบาท
ตั้งต้น72
โอนเข้า8
โอนออก-3
ใช้จ่าย-64.8
คงเหลือ12.2
<WaterfallChart
  title="งบประมาณเปลี่ยนไปอย่างไรตลอดปี (ล้านบาท)"
  ariaLabel="การเปลี่ยนแปลงของงบประมาณ"
  steps={[
    { label: "ตั้งต้น", value: 72, kind: "total" },
    { label: "โอนเข้า", value: 8, kind: "up" },
    { label: "โอนออก", value: -3, kind: "down" },
    { label: "ใช้จ่าย", value: -64.8, kind: "down" },
    { label: "คงเหลือ", value: 12.2, kind: "total" },
  ]}
  max={80}
  ticks={[0, 40, 80]}
  legend={{ total: "ยอดคงเหลือ", up: "เพิ่ม", down: "ลด" }}
  tableHead={["รายการ", "ล้านบาท"]}
/>

props ทั้งหมด

ตัวอย่างด้านบนใช้ props เพียงบางตัว ตารางนี้แสดงทุกตัวที่คอมโพเนนต์รับ พร้อมค่าเริ่มต้นและค่าที่ใช้ได้

WaterfallChart props
ชื่อชนิดข้อมูลค่าเริ่มต้นคำอธิบาย
title* จำเป็นstring—The chart's caption.
ariaLabel* จำเป็นstring—Names the drawing for screen readers.
steps* จำเป็นreadonly WaterfallStep[]—The bars from left to right. Start and end with a total.
max* จำเป็นnumber—Top of the value axis. At least the highest running balance.
ticks* จำเป็นreadonly number[]—Values for the axis grid lines, from 0 up to max.
legend* จำเป็น{ total: string; up: string; down: string }—Legend words for totals, increases and decreases.
tableHead* จำเป็นreadonly [string, string]—Table headers for the label and value columns.

ชนิดข้อมูล

WaterfallStep

One bar of a waterfall chart.

WaterfallStep fields
ชื่อชนิดข้อมูลค่าเริ่มต้นคำอธิบาย
label* จำเป็นstring—Under the bar, such as "โอนเข้า".
value* จำเป็นnumber—A total is the balance itself. A change is signed: positive for up, negative for down.
kind* จำเป็น"total" | "up" | "down"—total stands on the baseline. up and down float from the running balance.

ข้อควรปฏิบัติ

  • เริ่มและจบด้วย total และ total สุดท้ายต้องเท่ากับยอดตั้งต้นบวกการเปลี่ยนแปลงทั้งหมด กราฟไม่คำนวณให้
  • up ใช้ค่าบวก down ใช้ค่าลบ
  • max ต้องไม่น้อยกว่ายอดสะสมสูงสุด และ ticks เริ่มที่ 0 ถึง max
  • ไม่ควรเกินประมาณแปดขั้น เพื่อให้ป้ายใต้แท่งไม่ชนกัน
  • หน้าที่มีกราฟต้องวาง <ChartPaints /> หนึ่งครั้ง ไม่เช่นนั้นแท่งจะไม่มีสี
  • ส่งข้อความทุกตัวในภาษาของหน้าจออยู่แล้ว แอปแปลเองด้วยไฟล์ข้อความของแอป เช่น t() ของ next-intl ส่วนคำของกราฟเอง เช่น ดูข้อมูลเป็นตาราง เปลี่ยนตาม LangProvider
  • ข้อมูลในตัวอย่างและการทดสอบต้องเป็นข้อมูลสมมติ

ซอร์สโค้ด: ui/src/charts/waterfall.tsx