Chart.js

Chart.js เป็นคลาสเบ็คที่มาฟรีของ JavaScript สำหรับทำแบบกราฟที่ฐานความเข้าออกทาง HTML。

มันเป็นหนึ่งในแบบสารแนวที่ง่ายที่สุดของ JavaScript และมีช่องทางแบบกราฟต่าง ๆ ดังนี้:

  • กราฟแบบจุด (Scatter Plot)
  • กราฟแบบเส้น (Line Chart)
  • กราฟแบบสาย (Bar Chart)
  • กราฟบวม (Pie Chart)
  • โค้งดอก (Donut Chart)
  • ทรายน้ำ (Bubble Chart)
  • กราฟพื้นที่ (Area Chart)
  • กราฟเรดาร์ (Radar Chart)
  • กราฟผสม (Mixed Chart)

วิธีการใช้ Chart.js

Chart.js ง่ายที่ใช้

เพื่อตอบโต้ก่อนหน้านี้ ต้องเพิ่มลิงก์ที่มาจาก CDN (Content Delivery Network)

<script
src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.9.4/Chart.js">
</script>

จากนั้น,เพิ่ม <canvas> ไปยังตำแหน่งที่ต้องการวาดกราฟ

<canvas id="myChart" style="width:100%;max-width:700px"></canvas>

ตัวแทน canvas ต้องมี id ที่เดี่ยวเดียว

นี่คือนั้น!

สyntax ตัวอย่างของกราฟจุด

const myChart = new Chart("myChart", {
  type: "scatter",
  data: {}
  options: {}
});

สyntax ตัวอย่างของกราฟลายสาย

const myChart = new Chart("myChart", {
  
  data: {}
  options: {}
});

สyntax ตัวอย่างของกราฟแบบลำดับ

const myChart = new Chart("myChart", {
  type: "bar",
  data: {}
  options: {}
});

กราฟจุด

ราคาบ้าน vs. พื้นที่

โค้ดต้นฐาน

const xyValues = [
  {x:50, y:7},
  {x:60, y:8},
  {x:70, y:8},
  {x:80, y:9},
  {x:90, y:9},
  {x:100, y:9},
  {x:110, y:10},
  {x:120, y:11},
  {x:130, y:14},
  {x:140, y:14},
  {x:150, y:15}
];
new Chart("myChart", {
  type: "scatter",
  data: {
    datasets: [{
      pointRadius: 4,
      pointBackgroundColor: "rgba(0,0,255,1)"
      data: xyValues
    }]
  ,
  options:{...}
});

ทดลองด้วยตัวเอง

กราฟลายสาย

ราคาบ้าน vs. พื้นที่

โค้ดต้นฐาน

const xValues = [50,60,70,80,90,100,110,120,130,140,150];
const yValues = [7,8,8,9,9,9,10,11,14,14,15];
new Chart("myChart", {
  
  data: {
    labels: xValues,
    datasets: [{
      backgroundColor:"rgba(0,0,255,1.0)"
      borderColor: "rgba(0,0,255,0.1)"
      data: yValues
    }]
  ,
  options:{...}
});

ทดลองด้วยตัวเอง

ถ้าจะตั้งค่า borderColor ให้ 0,แล้วสามารถวาดกราฟลายสายด้วยจุดตามตำแหน่ง:

borderColor: "rgba(0,0,0,0)"

ทดลองด้วยตัวเอง

มากยิ่งขึ้น

โค้ดต้นฐาน

const xValues = [100,200,300,400,500,600,700,800,900,1000];
new Chart("myChart", {
  
  data: {
    labels: xValues,
    datasets: [{
      data: [860,1140,1060,1060,1070,1110,1330,2210,7830,2478],
      borderColor: "red",
      fill: false
    },{
      data: [1600,1700,1700,1900,2000,2700,4000,5000,6000,7000],
      borderColor: "green",
      fill: false
    },{
      data: [300,700,2000,5000,6000,4000,2000,1000,200,100],
      borderColor: "blue",
      fill: false
    }]
  ,
  options: {
    legend: {display: false}
  };
});

ทดลองด้วยตัวเอง

กราฟแบบเส้นตรง

โค้ดต้นฐาน

const xValues = [];
const yValues = [];
generateData("x * 2 + 7", 0, 10, 0.5);
new Chart("myChart", {
  
  data: {
    labels: xValues,
    datasets: [{
      
      pointRadius: 1,
      borderColor: "rgba(255,0,0,0.5)",
      data: yValues
    }]
  ,
  options: {...}
});
function generateData(value, i1, i2, step = 1) {
  for (let x = i1; x <= i2; x += step) {
    yValues.push(eval(value));
    xValues.push(x);
  };
};

ทดลองด้วยตัวเอง

กราฟฟังก์ชัน

เหมือนกราฟแบบเส้นตรง. แค่เปลี่ยนตัวอักษร generateData ค่าเพียงพอ:

generateData("Math.sin(x)", 0, 10, 0.5);

ทดลองด้วยตัวเอง

กราฟแบบกราฟสลับ

โค้ดต้นฐาน

var xValues = ["Italy", "France", "Spain", "United States", "Argentina"];
var yValues = [55, 49, 44, 24, 15];
var barColors = ["red", "green","blue","orange","brown"];
new Chart("myChart", {
  type: "bar",
  data: {
    labels: xValues,
    datasets: [{
      backgroundColor: barColors,
      data: yValues
    }]
  ,
  options: {...}
});

ทดลองด้วยตัวเอง

เฉพาะช่องเดียวเท่านั้น:

var barColors = ["blue"];

ทดลองด้วยตัวเอง

ทุกช่องเฉพาะเพียงสีหนึ่ง:

var barColors ="red";

ทดลองด้วยตัวเอง

สีที่มีความลึกลงต่างกัน:}}

var barColors = [
  "rgba(0,0,255,1.0)",
  "rgba(0,0,255,0.8)",
  "rgba(0,0,255,0.6)",
  "rgba(0,0,255,0.4)",
  "rgba(0,0,255,0.2)",
];

ทดลองด้วยตัวเอง

แผนภาพแบบตารางน้ำเงิน

แค่เปลี่ยน type จาก "bar" แปลงเป็น "horizontalBar":

type: "horizontalBar",

ทดลองด้วยตัวเอง

แผนภาพกลม

ตัวอย่าง

new Chart("myChart", {
  type: "pie",
  data: {
    labels: xValues,
    datasets: [{
      backgroundColor: barColors,
      data: yValues
    }]
  ,
  options: {
    title: {
      display: true,
      text: "การผลิตชาติชาวโลก"
    };
  };
});

ทดลองด้วยตัวเอง

แผนภาพกลม

แค่เปลี่ยน type จาก "pie" แปลงเป็น "doughnut":

type: "doughnut";

ทดลองด้วยตัวเอง