-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgraph.html
More file actions
68 lines (59 loc) · 2.06 KB
/
graph.html
File metadata and controls
68 lines (59 loc) · 2.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Scatter Plot</title>
<style>
canvas {
display: block;
margin: 0 auto;
}
</style>
</head>
<body>
<canvas id="scatterPlot" width="400" height="200"></canvas>
<script>
// Get the canvas element and its context
const canvas = document.getElementById("scatterPlot");
const ctx = canvas.getContext("2d");
// Define data points (x, y) for the graph
const dataPoints = [
{ x: 50, y: 80 },
{ x: 100, y: 40 },
{ x: 150, y: 120 },
{ x: 200, y: 60 },
{ x: 250, y: 90 },
{ x: 300, y: 30 }
];
// Set the graph properties
const xOffset = 30;
const yOffset = 20;
const graphWidth = canvas.width - xOffset * 2;
const graphHeight = canvas.height - yOffset * 2;
// Function to draw the graph
function drawGraph() {
// Clear the canvas
ctx.clearRect(0, 0, canvas.width, canvas.height);
// Draw the axes
ctx.beginPath();
ctx.moveTo(xOffset, yOffset);
ctx.lineTo(xOffset, yOffset + graphHeight);
ctx.lineTo(xOffset + graphWidth, yOffset + graphHeight);
ctx.stroke();
// Draw data points as circles
ctx.fillStyle = "blue"; // Change circle color
for (let i = 0; i < dataPoints.length; i++) {
const x = xOffset + (dataPoints[i].x / 300) * graphWidth;
const y = yOffset + (1 - dataPoints[i].y / 120) * graphHeight;
ctx.beginPath();
ctx.arc(x, y, 5, 0, Math.PI * 2);
ctx.fill();
ctx.closePath();
}
}
// Call the drawGraph function to initially draw the graph
drawGraph();
</script>
</body>
</html>