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

กราฟเส้น

LineChart

Values over time as a line, area or step chart, with a style switch. Series are drawn in array order, so the last one sits on top and gets the area fill. The legend and tooltip list series from the top down.

ใช้แสดงค่าที่เปลี่ยนตามเวลา เช่น การเบิกจ่ายรายเดือน ส่งป้ายแกน x ชุดข้อมูล ค่าสูงสุด และเส้นแบ่งแกน y กราฟมีปุ่มสลับรูปแบบ และตารางข้อมูลสำหรับโปรแกรมอ่านหน้าจอ

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

ตัวอย่าง

แผนเทียบผลจริง

ชุดข้อมูลวาดตามลำดับในอาร์เรย์ ชุดสุดท้ายอยู่บนสุด

การเบิกจ่ายสะสมเทียบแผน (ล้านบาท)
ผลจริงแผน
ดูข้อมูลเป็นตาราง
เดือนแผนจริง
ต.ค.53.2
พ.ย.117
ธ.ค.1711.1
ม.ค.2416.3
ก.พ.3120.9
มี.ค.3926.8
เม.ย.4733.2
พ.ค.5438.3
มิ.ย.6144.1
ก.ค.6750.8
ส.ค.7258
ก.ย.7664.8
<LineChart
  title="การเบิกจ่ายสะสมเทียบแผน (ล้านบาท)"
  ariaLabel="แผนและผลการเบิกจ่ายสะสม"
  labels={[
    "ต.ค.",
    "พ.ย.",
    "ธ.ค.",
    "ม.ค.",
    "ก.พ.",
    "มี.ค.",
    "เม.ย.",
    "พ.ค.",
    "มิ.ย.",
    "ก.ค.",
    "ส.ค.",
    "ก.ย.",
  ]}
  labelHead="เดือน"
  max={80}
  ticks={[0, 20, 40, 60, 80]}
  series={[
    {
      name: "แผน",
      label: "แผน",
      tip: "แผน",
      paint: 1,
      dashed: true,
      values: [5, 11, 17, 24, 31, 39, 47, 54, 61, 67, 72, 76],
    },
    {
      name: "ผลจริง",
      label: "จริง",
      tip: "จริง",
      paint: 0,
      values: [3.2, 7, 11.1, 16.3, 20.9, 26.8, 33.2, 38.3, 44.1, 50.8, 58, 64.8],
    },
  ]}
/>

เริ่มเป็นกราฟพื้นที่

initialStyle เลือกรูปแบบแรก ผู้ใช้ยังสลับเองได้

การเบิกจ่ายสะสมเทียบแผน (ล้านบาท)
ผลจริงแผน
ดูข้อมูลเป็นตาราง
เดือนแผนจริง
ต.ค.53.2
พ.ย.117
ธ.ค.1711.1
ม.ค.2416.3
ก.พ.3120.9
มี.ค.3926.8
เม.ย.4733.2
พ.ค.5438.3
มิ.ย.6144.1
ก.ค.6750.8
ส.ค.7258
ก.ย.7664.8
<LineChart
  title="การเบิกจ่ายสะสมเทียบแผน (ล้านบาท)"
  ariaLabel="แผนและผลการเบิกจ่ายสะสม"
  labels={[
    "ต.ค.",
    "พ.ย.",
    "ธ.ค.",
    "ม.ค.",
    "ก.พ.",
    "มี.ค.",
    "เม.ย.",
    "พ.ค.",
    "มิ.ย.",
    "ก.ค.",
    "ส.ค.",
    "ก.ย.",
  ]}
  labelHead="เดือน"
  max={80}
  ticks={[0, 20, 40, 60, 80]}
  series={[
    {
      name: "แผน",
      label: "แผน",
      tip: "แผน",
      paint: 1,
      dashed: true,
      values: [5, 11, 17, 24, 31, 39, 47, 54, 61, 67, 72, 76],
    },
    {
      name: "ผลจริง",
      label: "จริง",
      tip: "จริง",
      paint: 0,
      values: [3.2, 7, 11.1, 16.3, 20.9, 26.8, 33.2, 38.3, 44.1, 50.8, 58, 64.8],
    },
  ]}
  initialStyle="area"
/>

props ทั้งหมด

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

LineChart props
ชื่อชนิดข้อมูลค่าเริ่มต้นคำอธิบาย
title* จำเป็นstring—The chart's caption.
ariaLabel* จำเป็นstring—Names the drawing for screen readers.
labels* จำเป็นreadonly string[]—One label per point, such as month names.
labelHead* จำเป็นstring—Table header for the label column.
series* จำเป็นreadonly LineSeries[]—The lines, drawn in array order. At most four.
max* จำเป็นnumber—Top of the value axis.
ticks* จำเป็นreadonly number[]—Values for the axis grid lines, from 0 up to max.
initialStyleLineStyle
ค่าที่ใช้ได้:"line""area""step"
"line"The style shown first. People can still switch.

ชนิดข้อมูล

LineSeries

LineSeries fields
ชื่อชนิดข้อมูลค่าเริ่มต้นคำอธิบาย
name* จำเป็นstring—Legend name, such as "ผลจริง".
label* จำเป็นstring—Short name for the end label and the table column, such as "จริง".
tip* จำเป็นstring—Word before the value in the tooltip, such as "จริง" / "actual".
values* จำเป็นreadonly number[]—One value per label, in the same order.
paint* จำเป็นnumber—Series colour, 0 to 3.
dashedboolean—Draws the line dashed, such as for a plan.
format(v: number) => string—Formats the values in the tooltip and table.

LineStyle

How the series are drawn: lines, lines with the top series filled, or steps.

type LineStyle = "line" | "area" | "step";

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

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

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