HTML Canvas

HTML Canvas는 매우 적합합니다점 그래프(Scatter Plots).

HTML Canvas는 매우 적합합니다선형 그래프(Line Graphs).

HTML Canvas는 매우 적합합니다조합점 그래프와 선형 그래프.

점 그래프

源代码

const xArray = [50,60,70,80,90,100,110,120,130,140,150];
const yArray = [7,8,8,9,9,9,10,11,14,14,15];
// 산점도
ctx.fillStyle = "red";
for (let i = 0; i < xArray.length-1; i++) {
  let x = xArray[i]*400/150;
  let y = yArray[i]*400/15;
  ctx.beginPath();
  ctx.ellipse(x, y, 2, 3, 0, 0, Math.PI * 2);
  ctx.fill();
}

본인이 직접 테스트해보세요

선형 그래프

源代码

let xMax = canvas.height;
let slope = 1.2;
let intercept = 70;
// 산점도
ctx.moveTo(0, intercept);
ctx.lineTo(xMax, f(xMax));
ctx.strokeStyle = "black";
ctx.stroke();
// 선 함수
function f(x) {
  return x * slope + intercept;
}

본인이 직접 테스트해보세요

조합

源代码

let xMax = canvas.height;
let yMax = canvas.width;
let slope = 1.2;
let intercept = 70;
const xArray = [50,60,70,80,90,100,110,120,130,140,150];
const yArray = [7,8,8,9,9,9,10,11,14,14,15];
// 산점도
ctx.fillStyle = "red";
for (let i = 0; i < xArray.length-1; i++) {
  let x = xArray[i]*400/150;
  let y = yArray[i]*400/15;
  ctx.beginPath();
  ctx.ellipse(x, y, 2, 3, 0, 0, Math.PI * 2);
  ctx.fill();
}
// Plot Line
ctx.moveTo(0, intercept);
ctx.lineTo(xMax, f(xMax));
ctx.strokeStyle = "black";
ctx.stroke();
// 선 함수
function f(x) {
  return x * slope + intercept;
}

본인이 직접 테스트해보세요