HTML Canvas
- 前のページ JS グラフィック
- 次のページ JS Plotly.js
HTML Canvasは非常に適しています散布図(散布図)。
HTML Canvasは非常に適しています折れ線グラフ(ライングラフ)。
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(); } // 折線描画 ctx.moveTo(0, intercept); ctx.lineTo(xMax, f(xMax)); ctx.strokeStyle = "black"; ctx.stroke(); // 折線関数 function f(x) { return x * slope + intercept; }
- 前のページ JS グラフィック
- 次のページ JS Plotly.js