กราฟน้ำตก
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={["รายการ", "ล้านบาท"]}
/>| Prop | ค่าในตัวอย่างนี้ | ชนิดข้อมูล | คำอธิบาย |
|---|---|---|---|
| title* จำเป็น | "งบประมาณเปลี่ยนไปอย่างไรตลอดปี (ล้านบาท)" | string | The chart's caption. |
| ariaLabel* จำเป็น | "การเปลี่ยนแปลงของงบประมาณ" | string | Names the drawing for screen readers. |
| steps* จำเป็น | รายการ 5 รายการ[
{ 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" },
] | readonly WaterfallStep[] | The bars from left to right. Start and end with a total. |
| max* จำเป็น | 80 | number | Top of the value axis. At least the highest running balance. |
| ticks* จำเป็น | [0, 40, 80] | readonly number[] | Values for the axis grid lines, from 0 up to max. |
| legend* จำเป็น | { total: "ยอดคงเหลือ", up: "เพิ่ม", down: "ลด" } | { 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. |
props ทั้งหมด
ตัวอย่างด้านบนใช้ 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.
| ชื่อ | ชนิดข้อมูล | ค่าเริ่มต้น | คำอธิบาย |
|---|---|---|---|
| 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