การสร้าง Dashboard โดยใช้ Chart.js ใน Next.js: จากโปรแกรมสำเร็จรูปสู่การพัฒนาเว็บไซต์ 

การสร้าง Dashboard โดยใช้ Chart.js ใน Next.js: จากโปรแกรมสำเร็จรูปสู่การพัฒนาเว็บไซต์ 

24 December 2024

การสร้างแดชบอร์ด (dashboard) เพื่อแสดงข้อมูลในรูปแบบที่เข้าใจง่ายและมีประสิทธิภาพถือเป็นสิ่งสำคัญในยุคดิจิทัลปัจจุบัน ไม่ว่าจะเป็นการใช้งานในธุรกิจ การศึกษา หรือการวิเคราะห์ข้อมูล ในปัจจุบันมีเครื่องมือมากมายที่สามารถสร้างแดชบอร์ดได้อย่างรวดเร็ว เช่น Tableau และ Power BI ซึ่งเป็นโปรแกรมสำเร็จรูปที่มีเครื่องมือและฟังก์ชันการทำงานที่หลากหลาย ช่วยให้ผู้ใช้งานสามารถสร้าง แดชบอร์ด ได้โดยไม่ต้องมีทักษะการเขียนโปรแกรม 

อย่างไรก็ตามการสร้างแดชบอร์ดบนเว็บไซต์โดยใช้ JavaScript libraries เช่น Chart.js ใน Next.js ถือเป็นทางเลือกที่มีความยืดหยุ่นและสามารถปรับแต่งได้อย่างเต็มที่ ซึ่งเป็นการเปิดโอกาสให้ผู้พัฒนาสามารถสร้างแดชบอร์ด ที่ตอบโจทย์เฉพาะของตนเองได้มากยิ่งขึ้น 

Chart.js คืออะไร 

Chart.js เป็น JavaScript library ที่ช่วยให้นักพัฒนาสามารถสร้างกราฟและแผนภูมิในเว็บไซต์ได้อย่างง่ายดาย โดยไม่จำเป็นต้องมีความรู้ด้านการเขียนโค้ดที่ซับซ้อน นอกจากนี้ยังมีคุณสมบัติที่ยืดหยุ่นและปรับแต่งได้ตามความต้องการของผู้ใช้งาน Chart.js รอบรับการสร้างกราฟหลากหลายประเภท เช่น กราฟแท่ง กราฟเส้น กราฟวงกลม กราฟเรดาร์เป็นต้น อีกทั้ง Chart.js ยังมีขนาดเล็กและง่ายต่อการใช้งาน ทำให้เป็นตัวเลือกที่ดีสำหรับการสร้างแดชบอร์ดที่มีประสิทธิภาพ 

การใช้งาน Chart.js 

Chart.js สามารถติดตั้งได้ผ่าน Node Package Manager (npm) หรือ Content Delivery Network (CDN) ก็ได้ หลังจากติดตั้งเรียบร้อยแล้ว การสร้างกราฟจะสร้างผ่านตัวแปรและนำตัวแปรนั้นไปแสดงผลบนเว็บไซต์ในตำแหน่งที่ต้องการ 

ด้านบนเป็นการแสดงตัวอย่างการสร้างกราฟเส้น โดยหลัก ๆ แล้วการสร้างกราฟจะถูกแบ่งเป็น 2 ส่วนคือ ส่วนของข้อมูลและส่วนขององค์ประกอบอื่น ๆ ของกราฟ (option) ในส่วนของข้อมูลนั้นจะเป็นชื่อแกน ตัวเลขที่แสดงในกราฟ สีของกราฟเป็นต้น จากที่แสดงในตัวอย่างค่าของแกนนอน (x) จะเป็นชื่อเดือนตั้งแต่เดือน มกราคม ถึง กรกฎาคม จากตัวแปรที่ชื่อ “labels” ชื่อชุดข้อมูล จำนวนและองค์ประกอบต่าง ๆ ของเส้นกราฟสำหรับข้อมูลนั้นจะอยู่ในตัวแปรที่ชื่อว่า “datasets” ทั้งนี้ถ้าหากมีชุดข้อมูลมากกว่า 1 ชุดก็สามารถเพิ่มในส่วนนี้ได้เลย 

ในด้านองค์ประกอบอื่น ๆ ก็จะเกี่ยวกับการปรับแต่งค่าเริ่มต้นและสิ้นสุดของแกน การแสดงคำแนะนําเครื่องมือ (tooltip) การแสดงตัวเลขข้อมูล (data label) รวมถึงฟังก์ชัน (function) ในการกระทำ (Action) ต่าง ๆ ดังนั้นเมื่อนำกราฟหลาย ๆ อันมารวมกันก็จะสามารถประกอบกันเป็นแดชบอร์ดได้ 

ฟังก์ชันที่สำคัญอีกอย่างของแดชบอร์ดนั้นคือความสามารถในการตอบโต้ (interaction) เมื่อผู้ใช้งานคลิก (click) ที่กราฟ สิ่งที่จะเกิดขึ้นมักจะเป็นการกรองข้อมูล (filter) ให้เหลือข้อมูลในส่วนที่สนใจเท่านั้น ใน chart.js ก็สามารถทำได้เหมือนกันเพียงแค่เพิ่มฟังก์ชันในการกรองข้อมูลหลังจากที่กราฟถูกคลิกตามตัวอย่างดังนี้ 

จากตัวอย่างจะเห็นได้ว่าฟังก์ชัน onClick จะถูกเพิ่มลงในส่วนประกอบของการสร้างกราฟ จากคำสั่งด้านบนจะเป็นการสกัด label ของกราฟในส่วนที่ถูกคลิกเพื่อส่งต่อไปยังฟังก์ชันที่ชื่อ handleLineChartClick เพื่อทำการกรองข้อมูล เปลี่ยนสีกราฟ เป็นต้น 

จากตัวอย่างจะเป็นการกรองข้อมูลให้เหลือเฉพาะข้อมูลที่ถูกเลือกเท่านั้น หากมีหลายกราฟ ข้อมูลในกราฟอื่น ๆ ก็จะถูกกรองไปด้วยเช่นกัน เท่านี้ก็จะทำให้แดชบอร์ดบนเว็บของเรานั้นมีการตอบโต้แล้ว 

โดยสรุปแล้วการสร้าง Dashboard นั้นไม่ได้จำกัดอยู่แค่เครื่องมืออย่าง Power BI และ Tableau เท่านั้น แต่ยังสามารถทำได้ผ่านวิธีอื่น ๆ เช่นการใช้ Chart.js ซึ่งเป็น JavaScript library ที่ช่วยในการสร้างกราฟบนเว็บไซต์ ข้อดีของการใช้ Chart.js คือความยืดหยุ่นและการปรับแต่งที่ง่ายดาย ผู้ใช้สามารถออกแบบกราฟให้เข้ากับความต้องการเฉพาะได้ ไม่ว่าจะเป็นการแสดงผลในรูปแบบใด ๆ และยังสามารถทำงานร่วมกับเว็บไซต์หรือแอปพลิเคชันได้อย่างราบรื่น 

บทความโดย ทักษ์ดนัย แซ่ลี่ 

ทบทวนและปรับปรุงโดย อิสระพงศ์ เอกสินชล 

เอกสารอ้างอิง 

แบ่งปันบทความ

กลุ่มเนื้อหา

แท็กยอดนิยม

แจ้งเรื่องที่อยากอ่าน

คุณสามารถแจ้งเรื่องที่อยากอ่านให้เราทราบได้ !
และเราจะนำไปพัฒนาบทความให้มีเนื้อหาที่น่าสนใจมากขึ้น

PDPA Icon

We use cookies to optimize your browsing experience and improve our website’s performance. Learn more at our Privacy Policy and adjust your cookie settings at Settings

Privacy Preferences

You can choose your cookie settings by turning on/off each type of cookie as needed, except for necessary cookies.

Accept all
Manage Consent Preferences
  • Strictly Necessary Cookies
    Always Active

    This type of cookie is essential for providing services on the website of the Personal Data Protection Committee Office, allowing you to access various parts of the site. It also helps remember information you have previously provided through the website. Disabling this type of cookie will result in your inability to use key services of the Personal Data Protection Committee Office that require cookies to function.
    Cookies Details

  • Performance Cookies

    This type of cookie helps the Big Data Institute (Public Organization) understand user interactions with its website services, including which pages or areas of the site are most popular, as well as analyze other related data. The Big Data Institute (Public Organization) also uses this information to improve website performance and gain a better understanding of user behavior. Although the data collected by these cookies is non-identifiable and used solely for statistical analysis, disabling them will prevent the Big Data Institute (Public Organization) from knowing the number of website visitors and from evaluating the quality of its services.

  • Functional Cookies

    This type of cookie enables the Big Data Institute (Public Organization)’s website to remember the choices you have made and deliver enhanced features and content tailored to your usage. For example, it can remember your username or changes you have made to font sizes or other customizable settings on the page. Disabling these cookies may result in the website not functioning properly.

  • Targeting Cookies

    "This type of cookie helps the Big Data Institute (Public Organization) understand user interactions with its website services, including which pages or areas of the site are most popular, as well as analyze other related data. The Big Data Institute (Public Organization) also uses this information to improve website performance and gain a better understanding of user behavior. Although the data collected by these cookies is non-identifiable and used solely for statistical analysis, disabling them will prevent the Big Data Institute (Public Organization) from knowing the number of website visitors and from evaluating the quality of its services.

Save settings
This site is registered on wpml.org as a development site. Switch to a production site key to remove this banner.