-
Notifications
You must be signed in to change notification settings - Fork 1
/
elexforecast.js
745 lines (590 loc) · 22 KB
/
elexforecast.js
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
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
load('sbbsdefs.js');
load('json-client.js');
load('frame.js');
load('js-date-format.js');
// some of the custom functions I'm using
load(js.exec_dir + 'helper-functions.js');
// Screen size
var termRows = console.screen_rows;
var termCols = console.screen_columns;
var f = new File(js.exec_dir + 'server.ini');
f.open('r');
var serverIni = f.iniGetObject();
f.close();
// https://stackoverflow.com/a/31687097
function scaleBetween(unscaledNum, minAllowed, maxAllowed, min, max) {
return (maxAllowed - minAllowed) * (unscaledNum - min) / (max - min) + minAllowed;
}
// Adapted Bresenham JS routine
function line(x0, y0, x1, y1){
var points = [];
var dx = Math.abs(x1-x0);
var dy = Math.abs(y1-y0);
var sx = (x0 < x1) ? 1 : -1;
var sy = (y0 < y1) ? 1 : -1;
var err = dx-dy;
while(true){
points.push( [x0,y0] );
if ((x0==x1) && (y0==y1)) break;
var e2 = 2*err;
if (e2 >-dy){ err -= dy; x0 += sx; }
if (e2 < dx){ err += dx; y0 += sy; }
}
return points;
}
// CHARACTER SET NOTE:
// I edit this document in BBedit on the Mac. I've found I only get the right characters
// if I save it in Western (Mac OS Roman) encoding.
// COLORS
var lowWhite = 'NW0';
var highWhite = 'HW0';
var lowGreen = 'NG0';
var highGreen = 'HG0';
var lowBrown = 'NY0';
var highBrown = 'HY0';
var lowCyan = 'NC0';
var highCyan = 'HC0';
var lowBlack = 'NK0';
var highBlack = 'HK0';
var highYellowDarkBlue = 'HY4';
var highWhiteDarkCyan = 'HW6';
var lowWhiteHighRed = 'HR7';
var lowWhiteHighBlue = 'HB7';
var highRed = 'HR0';
var highBlue = 'HB0';
var lowRed = 'NR0';
var lowBlue = 'NB0';
var lowRedLowBlue = 'NR4';
var lowRedHighWhite = 'NW1';
var lowBlueHighWhite = 'NW4';
var lowRedLowWhite = 'HW1';
var lowBlueLowWhite = 'HW4';
// CHARACTERS
var charHorizSingle = ascii(196);
var charHorizSingleDownSingle = ascii(197);
var charHorizSingleDownDouble = ascii(210);
var charVertDouble = ascii(186);
var charVertSingle = ascii(179);
var frac12 = ascii(171);
var shade1 = ascii(176);
var shade2 = ascii(177);
var shade3 = ascii(178);
var solid = ascii(219);
var blockUpper = ascii(223);
var blockLower = ascii(220);
var blockMid = ascii(254);
var upTriangle = ascii(30);
var downTriangle = ascii(31);
// Frame for the whole app
var frame = new Frame(1, 1, termCols, termRows, 0);
frame.putmsg( lowWhite + 'hi' );
function sortByFirstElem(a, b) {
if (a[0] === b[0]) {
return 0;
}
else {
return (a[0] < b[0]) ? -1 : 1;
}
}
function getStateInfo( abbr ) {
var states = [
{ "nbrL":"me", "nbrR":"me", "nbrU":"hi", "nbrD":"hi", "abbr":"ak","long":"Alaska", "x":6, "y":1 },
{ "nbrL":"ms", "nbrR":"ga", "nbrU":"nc", "nbrD":"wi", "abbr":"al","long":"Alabama", "x":48, "y":19 },
{ "nbrL":"ks", "nbrR":"tn", "nbrU":"mo", "nbrD":"la", "abbr":"ar","long":"Arkansas", "x":36, "y":16 },
{ "nbrL":"dc", "nbrR":"nm", "nbrU":"ut", "nbrD":"id", "abbr":"az","long":"Arizona", "x":18, "y":16 },
{ "nbrL":"de", "nbrR":"ut", "nbrU":"or", "nbrD":"wa", "abbr":"ca","long":"California", "x":12, "y":13 },
{ "nbrL":"ut", "nbrR":"ne", "nbrU":"wy", "nbrD":"nm", "abbr":"co","long":"Colorado", "x":24, "y":13 },
{ "nbrL":"nj", "nbrR":"or", "nbrU":"ri", "nbrD":"de", "abbr":"ct","long":"Connecticut", "x":66, "y":10 },
{ "nbrL":"sc", "nbrR":"az", "nbrU":"md", "nbrD":"fl", "abbr":"dc","long":"Washington DC", "x":60, "y":16 },
{ "nbrL":"md", "nbrR":"ca", "nbrU":"ct", "nbrD":"vt", "abbr":"de","long":"Delaware", "x":66, "y":13 },
{ "nbrL":"tx", "nbrR":"hi", "nbrU":"dc", "nbrD":"ny", "abbr":"fl","long":"Florida", "x":60, "y":22 },
{ "nbrL":"al", "nbrR":"ok", "nbrU":"sc", "nbrD":"mi", "abbr":"ga","long":"Georgia", "x":54, "y":19 },
{ "nbrL":"fl", "nbrR":"tx", "nbrU":"ak", "nbrD":"ak", "abbr":"hi","long":"Hawaii", "x":6, "y":22 },
{ "nbrL":"sd", "nbrR":"in", "nbrU":"mn", "nbrD":"mo", "abbr":"ia","long":"Iowa", "x":36, "y":10 },
{ "nbrL":"wa", "nbrR":"mt", "nbrU":"az", "nbrD":"nv", "abbr":"id","long":"Idaho", "x":18, "y":7 },
{ "nbrL":"mn", "nbrR":"wi", "nbrU":"ms", "nbrD":"in", "abbr":"il","long":"Illinois", "x":42, "y":7 },
{ "nbrL":"ia", "nbrR":"oh", "nbrU":"il", "nbrD":"ky", "abbr":"in","long":"Indiana", "x":42, "y":10 },
{ "nbrL":"nm", "nbrR":"ar", "nbrU":"ne", "nbrD":"ok", "abbr":"ks","long":"Kansas", "x":30, "y":16 },
{ "nbrL":"mo", "nbrR":"wv", "nbrU":"in", "nbrD":"tn", "abbr":"ky","long":"Kentucky", "x":42, "y":13 },
{ "nbrL":"ok", "nbrR":"ms", "nbrU":"ar", "nbrD":"mn", "abbr":"la","long":"Louisiana", "x":36, "y":19 },
{ "nbrL":"ri", "nbrR":"wa", "nbrU":"nh", "nbrD":"me", "abbr":"ma","long":"Massachusetts", "x":72, "y":7 },
{ "nbrL":"va", "nbrR":"de", "nbrU":"nj", "nbrD":"dc", "abbr":"md","long":"Maryland", "x":60, "y":13 },
{ "nbrL":"ak", "nbrR":"ak", "nbrU":"ma", "nbrD":"nh", "abbr":"me","long":"Maine", "x":72, "y":1 },
{ "nbrL":"wi", "nbrR":"ny", "nbrU":"ga", "nbrD":"pa", "abbr":"mi","long":"Michigan", "x":54, "y":7 },
{ "nbrL":"nd", "nbrR":"il", "nbrU":"la", "nbrD":"ia", "abbr":"mn","long":"Minnesota", "x":36, "y":7 },
{ "nbrL":"ne", "nbrR":"ky", "nbrU":"ia", "nbrD":"ar", "abbr":"mo","long":"Missouri", "x":36, "y":13 },
{ "nbrL":"la", "nbrR":"al", "nbrU":"tn", "nbrD":"il", "abbr":"ms","long":"Mississippi", "x":42, "y":19 },
{ "nbrL":"id", "nbrR":"nd", "nbrU":"nm", "nbrD":"wy", "abbr":"mt","long":"Montana", "x":24, "y":7 },
{ "nbrL":"tn", "nbrR":"sc", "nbrU":"wv", "nbrD":"al", "abbr":"nc","long":"North Carolina", "x":48, "y":16 },
{ "nbrL":"mt", "nbrR":"mn", "nbrU":"tx", "nbrD":"sd", "abbr":"nd","long":"North Dakota", "x":30, "y":7 },
{ "nbrL":"co", "nbrR":"mo", "nbrU":"sd", "nbrD":"ks", "abbr":"ne","long":"Nebraska", "x":30, "y":13 },
{ "nbrL":"vt", "nbrR":"vt", "nbrU":"me", "nbrD":"ma", "abbr":"nh","long":"New Hampshire", "x":72, "y":4 },
{ "nbrL":"pa", "nbrR":"ct", "nbrU":"ny", "nbrD":"md", "abbr":"nj","long":"New Jersey", "x":60, "y":10 },
{ "nbrL":"az", "nbrR":"ks", "nbrU":"co", "nbrD":"mt", "abbr":"nm","long":"New Mexico", "x":24, "y":16 },
{ "nbrL":"or", "nbrR":"wy", "nbrU":"id", "nbrD":"ut", "abbr":"nv","long":"Nevada", "x":18, "y":10 },
{ "nbrL":"mi", "nbrR":"ri", "nbrU":"fl", "nbrD":"nj", "abbr":"ny","long":"New York", "x":60, "y":7 },
{ "nbrL":"in", "nbrR":"pa", "nbrU":"wi", "nbrD":"wv", "abbr":"oh","long":"Ohio", "x":48, "y":10 },
{ "nbrL":"ga", "nbrR":"la", "nbrU":"ks", "nbrD":"tx", "abbr":"ok","long":"Oklahoma", "x":30, "y":19 },
{ "nbrL":"ct", "nbrR":"nv", "nbrU":"wa", "nbrD":"ca", "abbr":"or","long":"Oregon", "x":12, "y":10 },
{ "nbrL":"oh", "nbrR":"nj", "nbrU":"mi", "nbrD":"va", "abbr":"pa","long":"Pennsylvania", "x":54, "y":10 },
{ "nbrL":"ny", "nbrR":"ma", "nbrU":"vt", "nbrD":"ct", "abbr":"ri","long":"Rhode Island", "x":66, "y":7 },
{ "nbrL":"nc", "nbrR":"dc", "nbrU":"va", "nbrD":"ga", "abbr":"sc","long":"South Carolina", "x":54, "y":16 },
{ "nbrL":"wy", "nbrR":"ia", "nbrU":"nd", "nbrD":"ne", "abbr":"sd","long":"South Dakota", "x":30, "y":10 },
{ "nbrL":"ar", "nbrR":"nc", "nbrU":"ky", "nbrD":"ms", "abbr":"tn","long":"Tennessee", "x":42, "y":16 },
{ "nbrL":"hi", "nbrR":"fl", "nbrU":"ok", "nbrD":"nd", "abbr":"tx","long":"Texas", "x":30, "y":22 },
{ "nbrL":"ca", "nbrR":"co", "nbrU":"nv", "nbrD":"az", "abbr":"ut","long":"Utah", "x":18, "y":13 },
{ "nbrL":"wv", "nbrR":"md", "nbrU":"pa", "nbrD":"sc", "abbr":"va","long":"Virginia", "x":54, "y":13 },
{ "nbrL":"nh", "nbrR":"nh", "nbrU":"de", "nbrD":"ri", "abbr":"vt","long":"Vermont", "x":66, "y":4 },
{ "nbrL":"ma", "nbrR":"id", "nbrU":"ca", "nbrD":"or", "abbr":"wa","long":"Washington", "x":12, "y":7 },
{ "nbrL":"il", "nbrR":"mi", "nbrU":"al", "nbrD":"oh", "abbr":"wi","long":"Wisconsin", "x":48, "y":7 },
{ "nbrL":"ky", "nbrR":"va", "nbrU":"oh", "nbrD":"nc", "abbr":"wv","long":"West Virginia", "x":48, "y":13 },
{ "nbrL":"nv", "nbrR":"sd", "nbrU":"mt", "nbrD":"co", "abbr":"wy","long":"Wyoming", "x":24, "y":10 }
];
for (var i=0; i<states.length; i++) {
if ( states[i]['abbr'].toLowerCase() == abbr.toLowerCase() ) {
return states[i];
}
}
return false;
}
// Get statistics from JSON database
function getData() {
try {
var jsonClient = new JSONClient(serverIni.host, serverIni.port);
var data = jsonClient.read("ELEXFORECAST", "ELEXFORECAST", 1);
if (data === undefined) {
debug("JSON client error: jsonClient returned undefined");
jsonClient.disconnect();
return false;
}
else {
jsonClient.disconnect();
return data;
}
}
catch(err) {
debug("JSON client error: " + err);
return false;
}
}
function displayTitle() {
console.clear();
frame.open();
frame.cycle();
var titleFrame = new Frame(1, 1, 80, 24, 0);
titleFrame.load( js.exec_dir + 'graphics/elexforecast-title.bin');
titleFrame.draw();
var userInput = '';
while( ascii(userInput) != 27 && ascii(userInput) != 81 && ascii(userInput) != 13 ) {
userInput = console.getkey( K_UPPER );
}
titleFrame.close();
titleFrame.delete();
}
function displayInfo ( data, abbr ) {
var state;
var stateName = 'NATIONAL'.center(14,' ');
if ( abbr == 'us' ) {
state = data[abbr];
}
else {
for ( var s=0; s < data['states'].length; s++) {
if ( data['states'][s]['abbr'].toLowerCase() == abbr.toLowerCase() ) {
state = data['states'][s];
stateName = getStateInfo( abbr )['long'].toUpperCase().center(14,' ');
}
}
}
var dem, rep;
if ( state['candidates'][0]['party'] == 'D' ) {
dem = state['candidates'][0];
rep = state['candidates'][1];
}
else {
dem = state['candidates'][1];
rep = state['candidates'][0];
}
dWinProbStr = ( dem['winprob'].toString() + '%' ).ljust(5,' ');
rWinProbStr = ( rep['winprob'].toString() + '%' ).rjust(5,' ');
infoFrame.gotoxy(17,2);
infoFrame.putmsg( highBlack + stateName );
infoFrame.gotoxy(3,4);
infoFrame.putmsg( highBlue + dWinProbStr );
infoFrame.gotoxy(38,4);
infoFrame.putmsg( highRed + rWinProbStr );
dRepeat = Math.round( ( dem['winprob'] * 40) / 100 );
rRepeat = 40 - dRepeat;
var pctBar = highBlue + solid.repeat( dRepeat ) + highRed + solid.repeat( rRepeat );
infoFrame.gotoxy(3,3);
infoFrame.putmsg( pctBar );
infoFrame.draw();
}
function displayMap( data ) {
console.clear();
frame.open();
frame.cycle();
// CONSTRUCT OBJECT TO HOLD ALL STATE INFO AND FRAMES
var states = [];
for (var i=0; i < data['states'].length; i++) {
var stateData = data['states'][i];
var stateInfo = getStateInfo( stateData['abbr'] );
states.push(
{
'abbr': stateInfo['abbr'],
'long': stateInfo['long'],
'frame': new Frame(stateInfo['x'], stateInfo['y'], 4, 3, BG_BLACK, frame),
'votes': stateData['votes'],
'candidates': stateData['candidates']
}
);
}
// DRAW FRAMES WITH CORRECT SHADING
for (var i=0; i < states.length; i++) {
var cands = states[i]['candidates'];
var dem, rep;
var output = '';
if ( cands[0]['party'] == 'D' ) {
dem = cands[0];
rep = cands[1];
}
else {
dem = cands[1];
rep = cands[0];
}
if ( dem['winprob'] > rep['winprob'] ) {
if ( dem['winprob'] > 88 ) {
output = lowWhiteHighBlue + solid + solid + solid + solid;
}
else if ( dem['winprob'] > 75 ) {
output = lowWhiteHighBlue + shade3 + shade3 + shade3 + shade3;
}
else if ( dem['winprob'] > 63 ) {
output = lowWhiteHighBlue + shade2 + shade2 + shade2 + shade2
}
else if ( dem['winprob'] > 50 ) {
output = lowWhiteHighBlue + shade1 + shade1 + shade1 + shade1;
}
}
else {
if ( rep['winprob'] > 88 ) {
output = lowWhiteHighRed + solid + solid + solid + solid;
}
else if ( rep['winprob'] > 75 ) {
output = lowWhiteHighRed + shade3 + shade3 + shade3 + shade3;
}
else if ( rep['winprob'] > 63 ) {
output = lowWhiteHighRed + shade2 + shade2 + shade2 + shade2;
}
else if ( rep['winprob'] > 50 ) {
output = lowWhiteHighRed + shade1 + shade1 + shade1 + shade1;
}
}
states[i]['frame'].open();
states[i]['frame'].gotoxy(1,1);
states[i]['frame'].putmsg( output );
states[i]['frame'].gotoxy(1,2);
states[i]['frame'].putmsg( output );
states[i]['frame'].gotoxy(1,3);
states[i]['frame'].putmsg( lowWhite + ' ' + states[i]['abbr'] + ' ');
states[i]['frame'].draw();
}
displayInfo( data, 'us' );
var creditFrame = new Frame( 12, 23, 18, 2, BG_BLACK, frame);
creditFrame.gotoxy(1,1);
creditFrame.putmsg( highBlack + 'Data source:' );
creditFrame.gotoxy(1,2);
creditFrame.putmsg( highBlack + 'FiveThirtyEight' );
creditFrame.draw();
var instructFrame = new Frame( 38, 23, 18, 2, BG_BLACK, frame);
instructFrame.gotoxy(1,1);
instructFrame.putmsg( highBlack + 'Arrows: browse map' );
instructFrame.gotoxy(1,2);
instructFrame.putmsg( highBlack + 'Enter: next screen' );
instructFrame.draw();
// Keep running as long as the user hasn't hit [esc] or [q]
var currentState = 'ak'
var userInput = '';
while( ascii(userInput) != 27 && ascii(userInput) != 81 && ascii(userInput) != 13 ) {
userInput = console.getkey( K_UPPER );
if ( userInput == KEY_LEFT || userInput == KEY_RIGHT || userInput == KEY_UP || userInput == KEY_DOWN ) {
switch( userInput ) {
case KEY_LEFT:
nextState = getStateInfo( currentState )['nbrL'].slice(0);
break;
case KEY_RIGHT:
nextState = getStateInfo( currentState )['nbrR'].slice(0);
break;
case KEY_UP:
nextState = getStateInfo( currentState )['nbrU'].slice(0);
break;
case KEY_DOWN:
nextState = getStateInfo( currentState )['nbrD'].slice(0);
break;
}
move( nextState );
currentState = nextState.slice(0);
nextState = '';
displayInfo( data, currentState );
}
}
for (var i=0; i < states.length; i++) {
states[i]['frame'].delete();
}
creditFrame.delete();
infoFrame.delete();
instructFrame.delete();
}
var move = function( nbr ) {
nbrObj = getStateInfo( nbr );
var x = nbrObj['x'] - 1;
var y = nbrObj['y'];
highlightFrame.moveTo(x,y);
highlightFrame.top();
maskFrame( highlightFrame, solid, MAGENTA );
highlightFrame.draw();
frame.cycle();
}
// CHANGES TO MAKE:
// Change green/brown color scheme to red/blue
// Need to add party info
// Need to re-scale x-axis
function displayLineChart( data ) {
console.clear();
frame.open();
frame.cycle();
// Supporting frames
var lineFrame = new Frame(1, 1, termCols, termRows, undefined, frame);
var chartFrame = new Frame(1, 1, termCols, termRows, undefined, frame);
var labelFrame = new Frame(1, 1, termCols, termRows, undefined, frame);
lineFrame.transparent = true;
chartFrame.transparent = true;
labelFrame.transparent = true;
emptyFrame( lineFrame );
emptyFrame( chartFrame );
emptyFrame( labelFrame );
var candidates = [];
for (var key in data['history']) {
candidates.push(key);
}
// Trim the data to most recent 80 days
var overset = data['history'][candidates[0]].length - termCols;
if (overset > 0) {
data['history'][candidates[0]] = data['history'][candidates[0]].slice(-80);
data['history'][candidates[1]] = data['history'][candidates[1]].slice(-80);
}
// Get dates for labels
var startDate = new Date( data['history'][candidates[0]][0][0] );
var endDate = new Date( data['history'][candidates[0]][termCols-1][0] );
// Extract the data into separate arrays
var cand_1_data = data['history'][candidates[0]].map(function(a) { return a[1] });
var cand_2_data = data['history'][candidates[1]].map(function(a) { return a[1] });
// Combine two arrays into one so we can get min, max
var all_data = cand_1_data.concat( cand_2_data );
// Get min, max
// var dataMax = all_data.reduce(function(a, b) { return Math.max(a, b); });
// var dataMin = all_data.reduce(function(a, b) { return Math.min(a, b); });
// Let's use 0 and 100 to keep chart clear
var dataMax = 100;
var dataMin = 0;
// Iterate over each series, convert each value into a Y-coordinate
var cand_1_scaled = cand_1_data.map( function(a) { return parseInt( scaleBetween( a, (termRows-2)*2, 1, dataMin, dataMax ) ) });
var cand_2_scaled = cand_2_data.map( function(a) { return parseInt( scaleBetween( a, (termRows-2)*2, 1, dataMin, dataMax ) ) });
// Construct a Chart object
var chart = [
{ 'name': candidates[0], 'series': cand_1_scaled },
{ 'name': candidates[1], 'series': cand_2_scaled }
];
// Build the chart background
for (var y=0; y<termRows-1; y++) {
var text;
if (y == 0) {
text = highBlack + charHorizSingleDownSingle + charHorizSingle + ' ' + (parseInt(dataMax).toString() + '%').ljust(3,' ') + charHorizSingle.repeat( termCols-7 ) + charHorizSingleDownSingle;
}
else if (y == parseInt(termRows-2)/2) {
text = highBlack + charHorizSingleDownSingle + charHorizSingle + ' ' + (parseInt(50).toString() + '%').ljust(3,' ') + charHorizSingle.repeat( termCols-7 ) + charHorizSingleDownSingle;
}
else if (y == termRows-2) {
text = highBlack + charHorizSingleDownSingle + charHorizSingle + ' ' + (parseInt(dataMin).toString() + '%').ljust(3,' ') + charHorizSingle.repeat( termCols-7 ) + charHorizSingleDownSingle;
}
else {
text = highBlack + charVertSingle + ' '.repeat( termCols-2 ) + charVertSingle;
}
lineFrame.gotoxy(1,y+1);
lineFrame.putmsg( text );
}
// Label begin date
lineFrame.gotoxy(1,termRows);
text = highBlack + upTriangle + ' ' + startDate.format('MMM D').ljust(12, ' ');
lineFrame.putmsg( text );
// Label end date
lineFrame.gotoxy(termCols-14,termRows);
text = highBlack + endDate.format('MMM D').rjust(12, ' ') + ' /';
lineFrame.putmsg( text );
// Legend
lineFrame.gotoxy(13,termRows);
text = lowWhite + 'How the forecast has changed'.ljust(34,' ') + highBlue + blockMid + lowWhite + ' ' + candidates[0].ljust(11,' ') + highRed + blockMid + lowWhite + ' ' + candidates[1];
lineFrame.putmsg( text );
lineFrame.draw();
// Create array to hold pixels
var pixel_array = [];
for (var i = 0; i < termRows*2; i++) {
var row = [];
for (var j = 0; j < termCols; j++) {
row[j] = 0;
}
pixel_array[i] = row;
}
// Iterate over data points and draw lines from point to point
for (var p=0; p < chart.length; p++ ) {
var values = chart[p].series;
for (var v=0; v<values.length; v++ ) {
var this_y = values[v];
var this_x = v;
var points = [];
// if this is not the last coordinate, then compute a line from present coordinate to next coordinate
if ( v<values.length-1 ) {
var next_y = values[v+1];
var next_x = v+1;
points = line( this_x, this_y, next_x, next_y );
}
// if this is the last coordinate, just use this single coordinate. no lines.
else {
points = [ [this_x, this_y] ];
}
// Now that we've computed all points to be plotted, actually plot them within our pixel array.
for (var pt=0; pt<points.length; pt++) {
// debug ('p: ' + p + ' | v: ' + v + ' | pt: ' + pt);
// debug ('pt: ' + pt);
// debug ( points[pt] );
try {
pixel_array[ points[pt][1] ][ points[pt][0] ] = p+1;
}
catch(e) {
debug('=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=');
debug(e);
debug( points[pt][1] );
debug( pixel_array );
}
}
}
}
// for (var i = 0; i < pixel_array.length; i++) {
// debug(pixel_array[i]);
// }
var attrs = [undefined, highBlue, highRed];
// debug( 'pixel_array.length (Y): ' + pixel_array.length );
// debug( 'pixel_array[0].length (x): ' + pixel_array[0].length );
// Render pixel array to ANSI screen
for (var i = 0; i < pixel_array.length; i+=2) {
var y = (i+2)/2 - 1;
for (var x = 0; x< pixel_array[i].length; x++) {
var top = pixel_array[i][x];
var bot = pixel_array[i+1][x];
var theCh = undefined, theAttr = undefined;
// Determine how to color this block
// Solid block, same color
if ( top == bot && top > 0 ) {
theCh = solid;
theAttr = attrs[top];
}
// Empty cell, set to transparent
else if ( top == bot ) {
theCh = undefined;
theAttr = undefined;
}
// Half block, only top has color
else if ( bot == 0 ) {
theCh = blockUpper;
theAttr = attrs[top];
}
// Half block, only bottom has color
else if ( top == 0 ) {
theCh = blockLower;
theAttr = attrs[bot];
}
// I'M IGNORING THIS PART. I want to use high colors, and obviously can't use high Red and high Blue together.
// But the lines on this chart will never intersect in 2020, so it doesn't matter.
// // Half blocks, two colors. Green top, brown bottom
// else if ( top == 1 && bot == 2 ) {
// theCh = blockUpper;
// theAttr = 'NG3';
// }
// // Half blocks, two colors. Brown top, green bottom
// else if ( top == 2 && bot == 1 ) {
// theCh = blockLower;
// theAttr = 'NG3';
// }
// Not implemented yet. Probably shouldn't get here.
else {
}
if (theCh || theAttr) {
try {
chartFrame.setData(x,y, theCh, theAttr);
}
catch(e) {
debug('=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=');
debug(e);
debug('top: ' + top + ' | bot: ' + bot);
debug('theCh: ' + theCh + ' | theAttr: ' + theAttr);
debug('i: ' + i + ' | x: ' + x + ' | y: ' + y);
debugFrame(chartFrame);
}
}
}
}
// Attach labels
for (var c=0; c<2; c++) {
var ly = chart[c]['series'][termCols-1] / 2;
if ( parseInt(ly) != ly ) {
ly = parseInt(ly) - 1;
}
else {
ly = ly + 2;
}
labelFrame.gotoxy( 72, ly);
labelFrame.putmsg( attrs[c+1] + candidates[c] );
labelFrame.gotoxy( 72, ly+1);
if (c==0) {
labelFrame.putmsg( attrs[c+1] + (cand_1_data[termCols-1]) + '%' );
}
else {
labelFrame.putmsg( attrs[c+1] + (cand_2_data[termCols-1]) + '%' );
}
}
lineFrame.open();
lineFrame.top();
chartFrame.open();
chartFrame.top();
labelFrame.open();
labelFrame.top();
frame.cycle();
// frame.screenShot( js.exec_dir + '/graph.bin' );
var userInput = '';
while( ascii(userInput) != 27 && ascii(userInput) != 81 && ascii(userInput) != 13 ) {
userInput = console.getkey( K_UPPER );
}
}
var cleanUp = function() {
frame.close();
console.clear();
}
var data = getData();
displayTitle();
var highlightFrame = new Frame( 5, 1, 6, 3, undefined, frame);
highlightFrame.load( js.exec_dir + 'graphics/highlight.bin', 6, 3 );
highlightFrame.transparent = true;
maskFrame( highlightFrame, solid, LIGHTMAGENTA );
highlightFrame.close();
frame.draw();
var infoFrame = new Frame( 16, 1, 44, 5, BG_BLACK, frame);
infoFrame.load(
js.exec_dir + 'graphics/cand-info.bin',
44,
5
);
displayMap( data );
displayLineChart( data );
// When done, remove all the frames
cleanUp();
// Quit
exit();