Chart.js

  • หน้าก่อนหน้า Plotly.js
  • หน้าต่อไป Google

Chart.js คือไลบรารีจาวาสคริปต์ที่ฟรี ใช้สำหรับการทำกราฟขึ้นกับ HTML

มันเป็นหนึ่งในไลบรารีจาวาสคริปต์ที่ง่ายที่ใช้สำหรับการวิสัยทัศน์ และมีชนิดกราฟที่ให้ไว้ในลิบรารีต่อไปนี้:

  • กราฟจุด (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 ที่เดี่ยวเดียว

นี่คือนั้น!

ตัวอย่างภาษาสแปตต์แกรฟ

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

ตัวอย่างภาษาลินแกรฟ

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

ตัวอย่างภาษาบาร์โกร์ฟ

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", {
  
  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", {
  type: "line",
  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)",

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

multi-line

源代碼

const xValues = [100,200,300,400,500,600,700,800,900,1000];
new Chart("myChart", {
  type: "line",
  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", {
  type: "line",
  data: {
    labels: xValues,
    datasets: [{
      fill: false,
      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 = ["意大利", "法國", "西班牙", "美國", "阿根廣"];
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";

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

  • หน้าก่อนหน้า Plotly.js
  • หน้าต่อไป Google