-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
204 lines (176 loc) · 7.01 KB
/
index.html
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>Scatter Chart in HTML5 Canvas</title>
<script>
function ScatterChart(con) {
// user defined properties
this.canvas = document.getElementById(con.canvasId);
this.minX = con.minX;
this.minY = con.minY;
this.maxX = con.maxX;
this.maxY = con.maxY;
this.unitsPerTickX = con.unitsPerTickX;
this.unitsPerTickY = con.unitsPerTickY;
// constants
this.padding = 10;
this.tickSize = 543;
this.axisColor = "white";
this.pointRadius = 15;
this.font = "12pt Calibri";
this.fontHeight = 12;
// relationships
this.context = this.canvas.getContext("2d");
this.rangeX = this.maxX - this.minY;
this.rangeY = this.maxY - this.minY;
this.numXTicks = Math.round(this.rangeX / this.unitsPerTickX);
this.numYTicks = Math.round(this.rangeY / this.unitsPerTickY);
this.x = this.getLongestValueWidth() + this.padding * 2;
this.y = this.padding * 2;
this.width = this.canvas.width - this.x - this.padding * 2;
this.height = this.canvas.height - this.y - this.padding - this.fontHeight;
this.scaleX = this.width / this.rangeX;
this.scaleY = this.height / this.rangeY;
// draw x y axis and tick marks
this.drawXAxis();
this.drawYAxis();
}
ScatterChart.prototype.getLongestValueWidth = function () {
this.context.font = this.font;
var longestValueWidth = 0;
for (var n = 0; n <= this.numYTicks; n++) {
var value = this.maxY - (n * this.unitsPerTickY);
longestValueWidth = Math.max(longestValueWidth, this.context.measureText(value).width);
}
return longestValueWidth;
};
ScatterChart.prototype.drawXAxis = function () {
var context = this.context;
context.save();
context.beginPath();
context.moveTo(this.x, this.y + this.height);
context.lineTo(this.x + this.width, this.y + this.height);
context.strokeStyle = this.axisColor;
context.lineWidth = 1;
context.stroke();
// draw tick marks
for (var n = 0; n < this.numXTicks; n++) {
context.beginPath();
context.moveTo((n + 1) * this.width / this.numXTicks + this.x, this.y + this.height);
context.lineTo((n + 1) * this.width / this.numXTicks + this.x, this.y + this.height - this.tickSize);
context.stroke();
}
// draw labels
context.font = this.font;
context.fillStyle = "black";
context.textAlign = "center";
context.textBaseline = "middle";
for (var n = 0; n < this.numXTicks; n++) {
var label = Math.round((n + 1) * this.maxX / this.numXTicks);
context.save();
context.translate((n + 1) * this.width / this.numXTicks + this.x, this.y + this.height + this.padding);
context.fillText(label, 0, 0);
context.restore();
}
context.restore();
};
ScatterChart.prototype.drawYAxis = function () {
var context = this.context;
context.save();
context.save();
context.beginPath();
context.moveTo(this.x, this.y);
context.lineTo(this.x, this.y + this.height);
context.strokeStyle = this.axisColor;
context.lineWidth = 1;
context.stroke();
// draw tick marks
for (var n = 0; n < this.numYTicks; n++) {
context.beginPath();
context.moveTo(this.x, n * this.height / this.numYTicks + this.y);
context.lineTo(this.x + this.tickSize, n * this.height / this.numYTicks + this.y);
context.stroke();
}
// draw values
context.font = this.font;
context.fillStyle = "black";
context.textAlign = "right";
context.textBaseline = "middle";
for (var n = 0; n < this.numYTicks; n++) {
var value = Math.round(this.maxY - n * this.maxY / this.numYTicks);
context.save();
context.translate(this.x - this.padding, n * this.height / this.numYTicks + this.y);
context.fillText(value, 0, 0);
context.restore();
}
context.restore();
};
ScatterChart.prototype.drawLine = function (data) {
var context = this.context;
context.save();
this.transformContext();
context.beginPath();
context.moveTo(data[0].x * this.scaleX, data[0].y * this.scaleY);
for (var n = 0; n < data.length; n++) {
var point = data[n];
// draw segment
context.beginPath();
context.arc(point.x * this.scaleX, point.y * this.scaleY, this.pointRadius, 0, 2 * Math.PI, false);
context.fillStyle = '#59788E';
context.fill();
context.closePath();
// position for next segment
context.beginPath();
context.moveTo(point.x * this.scaleX, point.y * this.scaleY);
}
context.restore();
};
ScatterChart.prototype.transformContext = function () {
var context = this.context;
// move context to center of canvas
this.context.translate(this.x, this.y + this.height);
// invert the y scale so that that increments
// as you move upwards
context.scale(1, -1);
};
window.onload = function () {
var myScatterChart = new ScatterChart({
canvasId: "myCanvas",
minX: 0,
minY: 0,
maxX: 10,
maxY: 10,
unitsPerTickX: 2,
unitsPerTickY: 2
});
var data = [
{
id: 1,
title: 1,
x: 3.9,
y: 7
}, {
id: 2,
title: 2,
x: 1.7,
y: 7.6
}, {
id: 3,
title: 3,
x: 5.4,
y: 2.1
}, {
id: 4,
title: 4,
x: 8.9,
y: 8.1
}];
myScatterChart.drawLine(data);
};
</script>
</head>
<body>
<canvas id="myCanvas" width="600" height="300" style="border: 1px solid black; background-color: #f2f2f2"></canvas>
</body>
</html>