กรอบกราฟ
Chart
The frame every chart sits in: title, optional controls and legend, the SVG drawn at the canvas's real width (at least 240px), a tooltip for any mark with data-tip, an optional footer and the table view.
Chart คือกรอบที่กราฟทุกแบบใช้ร่วมกัน ได้แก่ ชื่อกราฟ คำอธิบายสี ตัวชี้ค่าเมื่อชี้ และมุมมองตาราง ใช้เมื่อสร้างกราฟแบบใหม่ในไลบรารีนี้เท่านั้น หน้าจอทั่วไปให้ใช้กราฟสำเร็จรูป เช่น LineChart หรือ BarChart ChartPaints ต้องวางหนึ่งครั้งต่อหน้าเมื่อหน้านั้นมีกราฟ
import { Chart, ChartLegend, ChartKey, ChartPaints } from "@bsru/ui";ตัวอย่าง
กราฟแบบใหม่บนกรอบ Chart
draw รับความกว้างจริงเป็น px และคืน SVG ส่วนที่มี data-tip จะแสดงค่าเมื่อชี้ table ให้ข้อมูลเดียวกันเป็นตาราง
ดูข้อมูลเป็นตาราง
| หน่วยงาน | คำขอ |
|---|---|
| หน่วยงาน ก | 12 |
| หน่วยงาน ข | 7 |
| หน่วยงาน ค | 15 |
| หน่วยงาน ง | 4 |
// Once per page, such as in the app layout.
<ChartPaints />
<Chart
title={t("คำขอที่ค้างตามหน่วยงาน", "Open requests by unit")}
legend={<ChartLegend items={[{ label: t("คำขอที่ค้าง", "Open requests"), paint: 0 }]} />}
table={{
cols: [t("หน่วยงาน", "Unit"), t("คำขอ", "Requests")],
rows: rows.map((d) => [tx(d.label), d.value]),
}}
draw={(w) => (
<svg height={132} viewBox={`0 0 ${w} 132`} role="img" aria-label={t("คำขอที่ค้างตามหน่วยงาน", "Open requests by unit")}>
{rows.map((d, i) => (
<circle
key={i}
cx={84 + (d.value / 20) * (w - 100)}
cy={18 + i * 30}
r="6"
fill="url(#gv-c1)"
data-tip={`${tx(d.label)}: ${d.value}`}
/>
))}
</svg>
)}
/>| Prop | ค่าในตัวอย่างนี้ | ชนิดข้อมูล | คำอธิบาย |
|---|---|---|---|
| title* จำเป็น | รายการ 2 รายการ["คำขอที่ค้างตามหน่วยงาน", "Open requests by unit"] | ReactNode | The chart's caption, already in the screen language. |
| legend | รายการ 1 รายการ[{ label: ["คำขอที่ค้าง", "Open requests"], paint: 0 }] | ReactNode | Shown between the title and the chart, such as a ChartLegend. |
| rows | รายการ 4 รายการ[
{ label: ["หน่วยงาน ก", "Unit A"], value: 12 },
{ label: ["หน่วยงาน ข", "Unit B"], value: 7 },
{ label: ["หน่วยงาน ค", "Unit C"], value: 15 },
{ label: ["หน่วยงาน ง", "Unit D"], value: 4 },
] | array | — |
| max | 20 | number | — |
| draw* จำเป็น | (w) => ( <svg height={132} viewBox={`0 0 ${w} 132`} role="img" aria-label={t("คำขอที่ค้างตามหน่วยงาน", "Open requests by unit")}> {rows.map((d, i) => ( <circle key={i} cx={84 + (d.value / 20) * (w - 100)} cy={18 + i * 30} r="6" fill="url(#gv-c1)" data-tip={`${tx(d.label)}: ${d.value}`} /> ))} </svg> ) | (width: number) => ReactNode | Returns the SVG for a width in px. |
คำอธิบายสีตามลำดับชุดข้อมูล
paint คือสีของชุดข้อมูล 0 ถึง 3 ใช้เลขเดียวกับที่กราฟใช้วาด ข้อความต้องแปลเป็นภาษาของหน้าจอก่อนส่ง
<ChartLegend
items={[
{ label: "งบแผ่นดิน", paint: 0 },
{ label: "เงินรายได้", paint: 1 },
{ label: "เงินกองทุน", paint: 2 },
{ label: "เงินอุดหนุน", paint: 3 },
]}
/>| Prop | ค่าในตัวอย่างนี้ | ชนิดข้อมูล | คำอธิบาย |
|---|---|---|---|
| items* จำเป็น | รายการ 4 รายการ[
{ label: "งบแผ่นดิน", paint: 0 },
{ label: "เงินรายได้", paint: 1 },
{ label: "เงินกองทุน", paint: 2 },
{ label: "เงินอุดหนุน", paint: 3 },
] | readonly { label: string; paint: number }[] | One entry per series: its text, already in the screen language, and its series colour from 0 to 3. |
คำอธิบายสีที่ไม่ใช่สีชุดข้อมูล
ChartKey ใช้เมื่อสีไม่เรียงตามชุดข้อมูล เช่น ยอดคงเหลือในกราฟน้ำตก className คือคลาสสีของแนวทาง จึงใช้ในคอมโพเนนต์กราฟเท่านั้น ไม่ใช่ในหน้าจอ
<div className="chart-legend">
<ChartKey className="bg-ink-600">{t("ยอดคงเหลือ", "Balance")}</ChartKey>
<ChartKey className="bg-c1">{t("เพิ่ม", "Increase")}</ChartKey>
<ChartKey className="bg-c2">{t("ลด", "Decrease")}</ChartKey>
</div>props ทั้งหมด
ตัวอย่างด้านบนใช้ props เพียงบางตัว ตารางนี้แสดงทุกตัวที่คอมโพเนนต์รับ พร้อมค่าเริ่มต้นและค่าที่ใช้ได้
Chart
The frame every chart sits in: title, optional controls and legend, the SVG drawn at the canvas's real width (at least 240px), a tooltip for any mark with data-tip, an optional footer and the table view.
| ชื่อ | ชนิดข้อมูล | ค่าเริ่มต้น | คำอธิบาย |
|---|---|---|---|
| title* จำเป็น | ReactNode | — | The chart's caption, already in the screen language. |
| controls | ReactNode | — | Shown beside the title, such as a chart style switch. |
| legend | ReactNode | — | Shown between the title and the chart, such as a ChartLegend. |
| table | ChartTableData | — | Rows for the "View as a table" view. Leave it out only when the chart has no figures to read. |
| footer | ReactNode | — | Shown under the chart, such as a note on the result. |
| draw* จำเป็น | (width: number) => ReactNode | — | Returns the SVG for a width in px. |
ChartLegend
A legend for series that use the series colours in order.
| ชื่อ | ชนิดข้อมูล | ค่าเริ่มต้น | คำอธิบาย |
|---|---|---|---|
| items* จำเป็น | readonly { label: string; paint: number }[] | — | One entry per series: its text, already in the screen language, and its series colour from 0 to 3. |
ChartKey
A legend entry in series colour order.
| ชื่อ | ชนิดข้อมูล | ค่าเริ่มต้น | คำอธิบาย |
|---|---|---|---|
| className* จำเป็น | string | — | The swatch's colour class, such as bg-c1 for the first series colour. |
| children* จำเป็น | ReactNode | — | The entry's text. |
ChartPaints
The gradient paints every chart refers to by id. Render it once per page. It must stay rendered: gradients inside display:none are not reliable.
ไม่มี props
ชนิดข้อมูล
ChartTableData
Rows for a chart's table view. The first column is the label, the rest are figures.
| ชื่อ | ชนิดข้อมูล | ค่าเริ่มต้น | คำอธิบาย |
|---|---|---|---|
| cols* จำเป็น | readonly string[] | — | Column headers, already in the screen language. |
| rows* จำเป็น | readonly (readonly (string | number)[])[] | — | One array per row, in column order. |
ข้อควรปฏิบัติ
- วาง <ChartPaints /> หนึ่งครั้งต่อหน้า เช่น ใน layout ของแอป ถ้าไม่มี กราฟจะไม่มีสี และห้ามซ่อนด้วย display:none
- กราฟแบบใหม่ต้องมี role="img" และ aria-label บน SVG และส่ง table เพื่อให้อ่านค่าได้โดยไม่ต้องพึ่งสี
- title และ label ของ ChartLegend รับข้อความที่แปลแล้ว ไม่ใช่คู่ภาษา ใช้ t() หรือ tx() จาก useLang
- ป้ายใน ChartLegend ต้องไม่ซ้ำกัน
ซอร์สโค้ด: ui/src/charts/frame.tsx, ui/src/charts/paints.tsx