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

กรอบกราฟ

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>
  )}
/>

คำอธิบายสีตามลำดับชุดข้อมูล

paint คือสีของชุดข้อมูล 0 ถึง 3 ใช้เลขเดียวกับที่กราฟใช้วาด ข้อความต้องแปลเป็นภาษาของหน้าจอก่อนส่ง

งบแผ่นดินเงินรายได้เงินกองทุนเงินอุดหนุน
<ChartLegend
  items={[
    { label: "งบแผ่นดิน", paint: 0 },
    { label: "เงินรายได้", paint: 1 },
    { label: "เงินกองทุน", paint: 2 },
    { label: "เงินอุดหนุน", paint: 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.

Chart props
ชื่อชนิดข้อมูลค่าเริ่มต้นคำอธิบาย
title* จำเป็นReactNode—The chart's caption, already in the screen language.
controlsReactNode—Shown beside the title, such as a chart style switch.
legendReactNode—Shown between the title and the chart, such as a ChartLegend.
tableChartTableData—Rows for the "View as a table" view. Leave it out only when the chart has no figures to read.
draw* จำเป็น(width: number) => ReactNode—Returns the SVG for a width in px.

ChartLegend

A legend for series that use the series colours in order.

ChartLegend props
ชื่อชนิดข้อมูลค่าเริ่มต้นคำอธิบาย
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.

ChartKey props
ชื่อชนิดข้อมูลค่าเริ่มต้นคำอธิบาย
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.

ChartTableData fields
ชื่อชนิดข้อมูลค่าเริ่มต้นคำอธิบาย
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