-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcanvasScript.js
More file actions
377 lines (318 loc) · 11.7 KB
/
canvasScript.js
File metadata and controls
377 lines (318 loc) · 11.7 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
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
// desc: This function bolds the text from 0 to rightBound and makes the text from
// rightBound to text.length normal
//
// parameters:
// -----------
// ctx : canvas context
// ctx allows function to draw text to the canvas
// text : string
// The text we want to output to the screen
// x : int
// X position of text
// y : int
// Y position of text
// font : string
// Font of text the function will output
// color : string
// Color of text the function will output
// rightBound : int
// Right most character we want to make bold
function LeftBold(ctx, text, x, y, font, fontSize, color, rightBound)
{
ctx.fillStyle = color;
ctx.textAlign = "left";
ctx.textBaseline = "middle";
ctx.font = "bold " + fontSize + "px " + font;
ctx.fillText(text.substring(0, rightBound), x - (ctx.measureText(text).width) / 2, y);
ctx.font = fontSize + "px " + font;
console.log("text length: " + text.length);
ctx.fillText(text.substring(rightBound, text.length), (x - ctx.measureText(text).width / 2
+ ctx.measureText(text.substring(0, rightBound)).width) - 3, y);
}
function SetTextElement(name, text, xPos, yPos, fontVal, fontSize, fillVal, clearVal)
{
return {
elementName: name,
currentValue: text,
font: fontVal,
fontSize: fontSize,
fillStyle: fillVal,
clearStyle: clearVal,
textX: xPos,
textY: yPos,
textWidth: ctx.measureText(text).width,
x: (xPos - (ctx.measureText(text).width / 2)) * 2,
y: (yPos - (fontSize / 2)) * 2,
width: ((xPos - (ctx.measureText(text).width / 2)) + ctx.measureText(text).width) * 2,
height: ((yPos - (fontSize / 2)) + fontSize) * 2
};
}
function GetMousePosition(e)
{
var canvasBounds = canvas.getBoundingClientRect();
return {
x: e.clientX - canvasBounds.left,
y: e.clientY - canvasBounds.top
};
}
function ClearText(textElement)
{
ctx.font = textElement.font;
ctx.fillStyle = textElement.clearStyle;
ctx.textBaseline = "middle";
ctx.textAlign = "center";
for (var i = 0; i < 8; i++) {
ctx.fillText(textElement.currentValue, textElement.textX, textElement.textY);
}
}
function UpdateText(textElement)
{
var currentText = document.getElementById("inputField").value;
ctx.font = textElement.font;
ctx.fillStyle = textElement.fillStyle;
ctx.textBaseline = "middle";
ctx.textAlign = "center";
ctx.fillText(currentText, textElement.textX, textElement.textY);
return SetTextElement(textElement.elementName, currentText,
textElement.textX, textElement.textY, textElement.fontVal, textElement.fontSize,
textElement.fillStyle, textElement.clearStyle);
}
function CreateHighlightRect(textElement)
{
ctx.strokeStyle = "red";
ctx.strokeRect((textElement.x / 2) - 5, (textElement.y / 2) - 5, textElement.textWidth + 10, textElement.fontSize + 10);
}
function RemoveHighlightRect(textElement)
{
ctx.strokeStyle = textElement.clearStyle;
for (var i = 0; i < 8; i++) {
ctx.strokeRect((textElement.x / 2) - 5, (textElement.y / 2) - 5, textElement.textWidth + 10, textElement.fontSize + 10);
}
}
document.getElementById("inputButton").addEventListener("click", () => {
if (selectedText != -1) {
ClearText(textElementArr[selectedText]);
RemoveHighlightRect(textElementArr[selectedText]);
textElementArr[selectedText] = UpdateText(textElementArr[selectedText]);
}
});
document.getElementById("canvas").addEventListener("click", e => {
var pos = GetMousePosition(e);
for (var i = 0; i < textElementArr.length; i++) {
if ((pos.x > textElementArr[i].x && pos.x < textElementArr[i].width) && (pos.y > textElementArr[i].y && pos.y < textElementArr[i].height)) {
if (selectedText != -1) RemoveHighlightRect(textElementArr[selectedText]);
CreateHighlightRect(textElementArr[i]);
document.getElementById("selectorDisplay").value = textElementArr[i].elementName;
selectedText = i;
return;
}
}
RemoveHighlightRect(textElementArr[selectedText]);
document.getElementById("selectorDisplay").value = "None";
selectedText = -1;
});
// dimensions
var infogHeight = 480, infogWidth = 600;
var graphGroupX = (infogWidth / 2) - 40, graphGroupY = 150;
var footerX = 0, footerY = infogHeight - 50;
var textElementArr = new Array(4);
var selectedText = -1;
// other
var textColor = "navy", textFont = "'Oswald', sans-serif";
// setting up canvas
var canvas = document.getElementById("canvas");
var ctx = canvas.getContext("2d");
canvas.setAttribute("width", infogWidth * 2);
canvas.setAttribute("height", infogHeight * 2);
ctx.scale(2,2);
/* ACTUAL INFOGRAPHIC CODE */
ctx.fillStyle = "#33cccc";
ctx.fillRect(0, 0, infogWidth, 50);
var currentTitle = "Rx DRUG MUSUSE has mixed results";
/*LeftBold(ctx, currentTitle, (infogWidth / 2) + 9.35, 25, textFont, 16, textColor, 14);
ctx.fillStyle = "lavender";
ctx.fillRect(0, 50, infogWidth, infogHeight);*/
ctx.font = "16px " + textFont;
ctx.fillStyle = textColor;
ctx.textBaseline = "middle";
ctx.textAlign = "center";
ctx.fillText(currentTitle, (infogWidth / 2), 25);
textElementArr[0] = SetTextElement("Title", currentTitle, (infogWidth / 2), 25, "16px " + textFont, 16, textColor, "#33cccc");
ctx.fillStyle = "lavender";
ctx.fillRect(0, 50, infogWidth, infogHeight);
ctx.fillStyle = "white";
ctx.fillRect(20, 50, infogWidth - 40, infogHeight);
ctx.fillStyle = textColor;
ctx.textAlign = "center";
ctx.textBaseline = "middle";
ctx.font = "bold 13px " + textFont;
ctx.fillText("2018 Monitoring the Future College Students and Young Adults " +
"Survey Results", (infogWidth / 2), 75);
textElementArr[1] = SetTextElement("Description", "2018 Monitoring the Future College Students and Young Adults " +
"Survey Results", (infogWidth / 2), 75, "bold 13px " + textFont, 13, textColor, "white");
var text = "Rx OPIOID MUSUSE: SIGNIFICANT FIVE-YEAR DROP IN BOTH GROUPS*"
// LeftBold(ctx, text, (infogWidth / 2) + 12.75, 125, textFont, 16, textColor, 17)
ctx.font = "16px " + textFont;
ctx.fillText(text, (infogWidth / 2), 125);
textElementArr[2] = SetTextElement("Subtitle", text, (infogWidth / 2), 125, "16px " + textFont, 16, textColor, "white");
ctx.font = "100 16px " + textFont;
ctx.fillText("PAST YEAR MISUSE", graphGroupX + 120, graphGroupY + 20);
textElementArr[3] = SetTextElement("Graph Title", "PAST YEAR MISUSE", graphGroupX + 120, graphGroupY + 20, "100 16px " + textFont, 16, textColor, "white");
var bottleImg = new Image();
bottleImg.onload = function() {
ctx.drawImage(bottleImg, graphGroupX - 225, graphGroupY + 80, 130, 130);
}
bottleImg.src = "src/bottle.png";
ctx.fillStyle = "navy";
ctx.fillRect(footerX, footerY, infogWidth, 50);
var logoImg = new Image();
logoImg.onload = function() {
ctx.drawImage(logoImg, footerX + 10, footerY + 5, 180, 40);
}
logoImg.src = "src/nida-logo.svg";
ctx.fillStyle = "white";
ctx.textAlign = "center";
ctx.textBaseline = "middle";
ctx.font = "16px " + textFont;
ctx.fillText("DRUGABUSE.GOV", infogWidth - 100, footerY + 25);
/* D3 + CANVAS CODE FOR BAR GRAPHS */
var chartWidth = 150, chartHeight = 125;
var xScale = d3.scaleBand()
.range([0, chartWidth])
.padding(0.4),
yScale = d3.scaleLinear()
.range([chartHeight, 0]);
// Female Graph Code
// Label
ctx.fillStyle = "Slateblue";
ctx.textAlign = "center";
ctx.textBaseline = "middle";
ctx.font = "700 12px " + textFont;
ctx.fillText("FEMALES in thousands", graphGroupX + 15, graphGroupY + 70);
// Drawing the graph
d3.csv("female.csv", function(error, data) {
if (error) throw error;
console.log(data);
var offsetX = graphGroupX - 60, offsetY = graphGroupY + 220,
yTop = offsetY - chartHeight;
// (offsetX, offsetY) == (0, 0) on chart
var yTicks;
// Standard D3 set up
xScale.domain(data.map(function(d) { return d.county; }));
yScale.domain([0, d3.max(data, function(d) {
data.forEach(function (d) {
d.value = parseInt(d.value);
});
return d.value;
})/ 1000]);
yTicks = yScale.ticks(12);
// Drawing x-axis
ctx.fillStyle = "black";
ctx.moveTo(offsetX, offsetY);
ctx.lineTo(offsetX + chartWidth, offsetY);
ctx.stroke();
// Drawing x-axis ticks
ctx.fillStyle = "black";
xScale.domain().forEach(d => {
ctx.moveTo((xScale(d) + xScale.bandwidth() / 2) + offsetX, offsetY);
ctx.lineTo((xScale(d) + xScale.bandwidth() / 2) + offsetX, offsetY + 6);
});
ctx.stroke();
// Drawing x-axis labels
ctx.textAlign = "center";
ctx.textBaseline = "top";
ctx.font = "10px Times New Roman, Times, serif";
xScale.domain().forEach( (d, i) => {
ctx.fillText(d, (xScale(d) + xScale.bandwidth() / 2) + offsetX, offsetY + 6);
});
// Drawing y-axis
ctx.moveTo(offsetX, yTop);
ctx.lineTo(offsetX, offsetY);
ctx.stroke();
// drawing y-axis ticks
ctx.fillStyle = "black";
yTicks.forEach(d => {
console.log("d: " + d);
ctx.moveTo(offsetX, yTop + (yScale(d) - 0.5));
ctx.lineTo(offsetX - 6, yTop + (yScale(d) - 0.5));
});
ctx.stroke();
// drawing y-axis tick labels
ctx.textAlign = "right";
ctx.textBaseline = "middle";
ctx.font = "8px Times New Roman, Times, serif";
yTicks.forEach(d => {
ctx.fillText(d, offsetX - 9, (yTop) + yScale(d));
});
// drawing the bars
ctx.fillStyle = "skyblue";
data.forEach(d => {
ctx.fillRect(offsetX + xScale(d.county), offsetY, xScale.bandwidth(), -(chartHeight - yScale(d.value / 1000)));
});
});
// Male chart
ctx.fillStyle = "Slateblue";
ctx.textAlign = "center";
ctx.textBaseline = "middle";
ctx.font = "700 12px " + textFont;
ctx.fillText("MALES in thousands", graphGroupX + 220, graphGroupY + 70);
// D3 code
d3.csv("male.csv", function(error, data) {
if (error) throw error;
console.log(data);
// (offsetX, offsetY) == (0, 0) on chart
var offsetX = graphGroupX + 140, offsetY = graphGroupY + 220,
yTop = offsetY - chartHeight;
var yTicks;
// Standard D3 set up
xScale.domain(data.map(function(d) { return d.county; }));
yScale.domain([0, d3.max(data, function(d) {
data.forEach(function (d) {
d.value = parseInt(d.value);
});
return d.value;
})/ 1000]);
yTicks = yScale.ticks(12);
// Drawing x-axis
ctx.fillStyle = "black";
ctx.moveTo(offsetX, offsetY);
ctx.lineTo(offsetX + chartWidth, offsetY);
ctx.stroke();
// Drawing x-axis ticks
ctx.fillStyle = "black";
xScale.domain().forEach(d => {
ctx.moveTo((xScale(d) + xScale.bandwidth() / 2) + offsetX, offsetY);
ctx.lineTo((xScale(d) + xScale.bandwidth() / 2) + offsetX, offsetY + 6);
});
ctx.stroke();
// Drawing x-axis labels
ctx.textAlign = "center";
ctx.textBaseline = "top";
ctx.font = "10px Times New Roman, Times, serif";
xScale.domain().forEach( (d, i) => {
ctx.fillText(d, (xScale(d) + xScale.bandwidth() / 2) + offsetX, offsetY + 6);
});
// Drawing y-axis
ctx.moveTo(offsetX, yTop);
ctx.lineTo(offsetX, offsetY);
ctx.stroke();
// drawing y-axis ticks
ctx.fillStyle = "black";
yTicks.forEach(d => {
ctx.moveTo(offsetX, yTop + (yScale(d) - 0.5));
ctx.lineTo(offsetX - 6, yTop + (yScale(d) - 0.5));
});
ctx.stroke();
// drawing y-axis tick labels
ctx.textAlign = "right";
ctx.textBaseline = "middle";
ctx.font = "8px Times New Roman, Times, serif";
yTicks.forEach(d => {
ctx.fillText(d, offsetX - 9, (yTop) + yScale(d));
});
// drawing the bars
ctx.fillStyle = "skyblue";
data.forEach(d => {
ctx.fillRect(offsetX + xScale(d.county), offsetY, xScale.bandwidth(), -(chartHeight - yScale(d.value / 1000)));
});
});