-
Notifications
You must be signed in to change notification settings - Fork 2
/
describler.js
executable file
·4676 lines (3924 loc) · 176 KB
/
describler.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
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"use strict";
window.onfocus = function (event) {
event.target.blur();
};
SVGElement.prototype.getTransformToElement = SVGElement.prototype.getTransformToElement
|| function(toElement) {
return toElement.getScreenCTM().inverse().multiply(this.getScreenCTM());
};
function showEvent(event) {
console.log(event.type);
}
function match_element( obj ) {
return obj.element === this;
}
function match_id( obj ) {
return obj.id === this;
}
function generate_unique_id( base_id ) {
var i = 0;
var uid = base_id;
while ( null != document.getElementById( uid ) ) {
uid = base_id + "-" + i++;
}
return uid;
}
function describlerObj(root) {
this.root = root;
this.app = null;
// chart properties
// document.activeElement
this.charts = [];
// voice and sonification properties
this.speeches = [];
this.options = [];
this.menu = new menuObj();
// this.voice = new SpeechSynthesisUtterance();
this.sonifier = null;
//
this.interaction_mode = "voice"; // "voice" or "sonifier"
// constants
this.svgns = "http://www.w3.org/2000/svg";
ntc.init();
this.taskAssessments = [];
// create a list of all focusable elements, including the root
var focusList = this.root.parentNode.querySelectorAll("[tabindex='0']");
this.focusList = Array.from( focusList );
// unshift
console.log("this.focusList");
console.log(this.focusList);
// focus properties
this.focusIndex = -1;
this.activeElement = null;
this.previous_datapoint = null;
this.previous_node = null; // flowcharts
this.activeObject = null;
this.padding = 0;
this.strokewidth = 0;
// find appropriate sizes
var basesize = 10;
var vb = root.getAttribute("viewBox");
if (vb) {
var vb_array = vb.split(" ");
basesize = (Math.max(vb_array[2], vb_array[3]) / 100);
}
this.padding = basesize;
this.strokewidth = basesize / 2;
// create focus box
this.focusBox = document.createElementNS(this.svgns, "rect");
this.focusBox.setAttribute("rx", this.padding/2 );
this.focusBox.setAttribute("ry", this.padding/2 );
var style = "fill:none; stroke:cornflowerblue; stroke-linejoin:round; stroke-opacity:0.6; stroke-width:"
+ this.strokewidth + "px; ";
this.focusBox.setAttribute("style", style);
this.root.appendChild( this.focusBox );
this.root.addEventListener("click", bind(this, this.click), false );
this.root.addEventListener("keydown", bind(this, this.trackKeys), false );
this.createModel();
this.speech_volume = 0.3;
this.sonifier_volume = 1;
this.sonifier = new Sonifier();
this.metaGroup = document.createElementNS(this.svgns, "g");
this.metaGroup.setAttribute("id", "describler-metadata" );
this.root.appendChild( this.metaGroup );
console.log(this);
// this.setActiveElement(this.root);
}
describlerObj.prototype.createModel = function () {
var charts = this.root.querySelectorAll("[role='chart']");
if (!charts.length) {
var role = this.root.getAttribute("role");
if ( "chart" == role){
charts = [this.root];
}
}
// console.log( charts );
// parse all charts
for (var c = 0, c_len = charts.length; c_len > c; ++c) {
var chartEl = charts[c];
var chart = new chartObj( chartEl );
this.charts.push( chart );
var taskAssessment = new taskAssessmentObj( this, this.root, chart );
if (taskAssessment.element) {
this.taskAssessments.push( taskAssessment );
}
}
console.log( this.charts );
this.exportCSV();
var flowcharts = this.root.querySelectorAll("[role='flowchart']");
if (!flowcharts.length) {
var role = this.root.getAttribute("role");
if ( "flowchart" == role){
flowcharts = [this.root];
}
}
console.log( charts );
// parse all flowcharts
for (var f = 0, f_len = flowcharts.length; f_len > f; ++f) {
var flowchart_el = flowcharts[f];
var flowchart = new flowchartObj(flowchart_el);
this.charts.push( flowchart );
}
/*
*/
// generic unstructured graphics/text content
if ( 0 == this.charts.length) {
this.findTextContent();
// console.log( charts );
}
// this.mapToRange();
}
describlerObj.prototype.exportCSV = function () {
for (var c = 0, c_len = this.charts.length; c_len > c; ++c) {
var chart = this.charts[c];
var csv = "";
for (var d = 0, d_len = chart.datasets.length; d_len > d; ++d) {
var dataset = chart.datasets[d];
var allies = [];
for (var a in chart.axes) {
// console.log(key, chart[axis]);
var eachAxis = chart.axes[a];
if (eachAxis["labels"]) {
if ( dataset.length == eachAxis["labels"].length){
allies.push({
"label": eachAxis.label,
"fields": eachAxis["labels"]
});
}
}
}
if ( "bar" == chart.type){
if ( 1 == allies.length){
var axis = allies[0];
csv = axis["label"] + ",values\n";
for (var i = 0, i_len = axis["fields"].length; i_len > i; ++i) {
csv += axis["fields"][i] + "," + dataset[i].value + "\n";
}
}
} else if ( "pie" == chart.type){
for (var i = 0, i_len = dataset.length; i_len > i; ++i) {
csv += dataset[i].label + "\n";
}
}
}
console.log( "CSV file: \n" + csv );
}
}
describlerObj.prototype.findTextContent = function () {
console.log("findTextContent");
var textContents = document.querySelectorAll("text,title");
for (var t = 0, t_len = textContents.length; t_len > t; ++t) {
var eachTextContent= textContents[t];
var el = eachTextContent;
if ( "title" == el.localName){
el = el.parentNode;
}
el.setAttribute("tabindex", 0 );
}
this.focusList = this.root.parentNode.querySelectorAll("[tabindex='0']");
// console.log(this.focusList);
}
describlerObj.prototype.navNext = function () {
// console.log( "tabNext: " + focus.index );
this.focusIndex++;
if (this.focusList.length - 1 < this.focusIndex) {
this.focusIndex = 0;
}
this.setActiveElement( this.focusList[ this.focusIndex ] );
}
describlerObj.prototype.navPrev = function () {
// console.log( "tabPrev: " + this.focusIndex );
this.focusIndex--;
if (-1 >= this.focusIndex) {
this.focusIndex = this.focusList.length - 1;
}
this.setActiveElement( this.focusList[ this.focusIndex ] );
}
describlerObj.prototype.setActiveElement = function ( el ) {
if (el) {
if ( this.activeElement ) {
var last_role = this.activeElement.getAttribute("role");
if ( "datapoint" == last_role ) {
this.previous_datapoint = this.activeElement;
// console.log( this.previous_datapoint );
} else if ( "node" == last_role ) {
this.previous_node = this.activeElement;
// console.log( this.previous_node );
}
// } else {
// var focus_index = this.focusList.findIndex( function ( element ) {
// return element == this;
// }, el );
// if ( null != focus_index) {
// this.focusIndex = focus_index;
// }
}
var focus_index = this.focusList.findIndex( function ( element ) {
return element == this;
}, el );
if ( null != focus_index ) {
this.focusIndex = focus_index;
}
this.activeElement = el;
this.showFocus();
}
}
describlerObj.prototype.showFocus = function () {
if (!this.activeElement) {
console.log("oops");
}
this.activeElement.focus();
// simulate focus
var bbox = this.activeElement.getBBox();
var transform = this.activeElement.getScreenCTM().inverse().multiply(this.root.getScreenCTM()).inverse();
// console.log(transform);
var x = bbox.x - this.padding;
var y = bbox.y - this.padding;
var w = bbox.width + (this.padding * 2);
var h = bbox.height + (this.padding * 2);
this.focusBox.setAttribute("x", x );
this.focusBox.setAttribute("y", y );
this.focusBox.setAttribute("width", w );
this.focusBox.setAttribute("height", h );
// TODO: figure out how to apply matrix transform to this element
this.focusBox.setAttribute("transform", "translate(" + transform.e + "," + transform.f + ")" );
this.menu.reset();
this.getInfo();
}
describlerObj.prototype.trackKeys = function (event) {
// console.log("describlerObj.trackKeys");
var key = event.key.toLowerCase();
// console.log( key );
// console.log( "key: " + event.key);
if ( "tab" == key
|| "arrowdown" == key
|| "arrowright" == key
|| "arrowup" == key
|| "arrowleft" == key ) {
// tab
event.preventDefault();
event.stopPropagation();
// document.activeElement.blur();
if ( (event && event.shiftKey)
|| "arrowup" == key
|| "arrowleft" == key ) {
this.navPrev();
} else {
this.navNext();
}
} else if ("d" == key ) {
// d
// TODO: set "more details"
this.getInfo();
} else if ("s" == key) {
// s
this.sonify();
} else if ( "0" == key
|| "1" == key
|| "2" == key
|| "3" == key
|| "4" == key
|| "5" == key
|| "6" == key
|| "7" == key
|| "8" == key
|| "9" == key ) {
// 1 to 9
// var number = parseInt(key.substr(2)) - 30;
var number = parseInt(key);
// console.log( "key: " + number);
var is_selection = this.menu.select( number );
if (is_selection) {
var selected = this.menu.selected;
if ( "assessment" != selected.type ) {
this.getInfo();
if ( this.speeches.length){
this.speak();
}
} else {
if ( "answer" == selected.context ){
this.taskAssessments[0].evaluateAnswer( selected.id, selected.label, selected.context, selected.type );
} else {
this.taskAssessments[0].runTest();
}
}
} else {
this.speeches.length = 0;
this.speeches.push( "Invalid option." );
this.speak();
}
} else if ("escape" == key ) {
// escape
if ( speechSynthesis && speechSynthesis.speaking ){
speechSynthesis.cancel();
}
if (this.sonifier) {
// this.sonifier.stopPlay();
// this.sonifier.toggleVolume( true );
this.sonifier.setVolume( 0 );
}
}
}
describlerObj.prototype.click = function (event){
event.preventDefault();
event.stopPropagation();
// document.activeElement.blur();
var focusEl = event.target;
while ( !focusEl.hasAttribute("tabindex")){
// console.log( focusEl );
if ( document == focusEl.parentNode){
return false;
}
focusEl = focusEl.parentNode;
}
// console.log( focusEl );
focusEl.blur();
var new_focus_el = this.focusList.find( function ( element ) {
return element == this;
}, focusEl );
if ( new_focus_el ) {
this.focusIndex = this.focusList.findIndex( function ( element ) {
return element == this;
}, focusEl );
this.setActiveElement( new_focus_el );
}
}
describlerObj.prototype.mapToRange = function (val, range1, range2){
// affine transformation transforms number x in range [a,b] to number y in range [c,d]
// y = (x-a)(d-c/b-a) + c
var newVal = ( (val - range1[0]) * ((range2[1] - range2[0]) / (range1[1] - range1[0])) ) + range2[0];
return newVal;
}
describlerObj.prototype.getFraction = function (decimal){
console.log("decimal: " + decimal);
var msg = "The same as ";
if ( isNaN(decimal) ){
return "not a number!";
}
if ( 1 != decimal){
var fraction = 1;
var numerator = 1;
var denominator = 1;
while (fraction.toFixed(3) != decimal.toFixed(3)) {
// console.log( fraction + "," + decimal );
if (fraction.toFixed(3) < decimal.toFixed(3)){
numerator += 1;
}
else {
denominator += 1;
numerator = parseInt(decimal * denominator);
}
fraction = numerator / denominator;
}
msg = numerator + "/" + denominator;
if ( numerator > denominator){
var number = parseInt( numerator / denominator );
var mod = numerator % denominator;
/*
for (var d = 0; 10 > d; ++d) {
var mod2 = denominator % mod;
if ( 0 != mod2){
}
}
*/
msg = number.toString() + " " + mod + "/" + denominator;
if ( 0 == mod){
msg = number.toString() + " times ";
}
}
}
return msg;
}
describlerObj.prototype.getOrdinalNumber = function (number){
var ordinal = "";
if ( 3 < ordinal && 21 > number ){
ordinal += number + "th";
} else {
switch ( number.toString().split("").pop() ){
case "1":
ordinal += number + "st";
break;
case "2":
ordinal += number + "nd";
break;
case "3":
ordinal += number + "rd";
break;
default:
ordinal += number + "th";
}
}
return ordinal;
}
describlerObj.prototype.convertNumberToWords = function (number){
var names = [{"0":"zero", "1":"one", "2":"two", "3":"three", "4":"four", "5":"five", "6":"six",
"7":"seven", "8":"eight", "9":"nine" },{"0":"ten", "1":"eleven", "2":"twelve",
"3":"thirteen", "4":"fourteen", "5":"fifteen", "6":"sixteen", "7":"seventeen",
"8":"eighteen", "9":"nineteen"},{"2":"twenty", "3":"thirty", "4":"forty", "5":"fifty",
"6":"sixty", "7":"seventy", "8":"eighty", "9":"ninety"},["", "thousand", "million",
"billion", "trillion", "quadrillion", "quintillion", "sextillion", "septillion",
"octillion", "nonillion", "decillion", "undecillion", "duodecillion", "tredecillion",
"quattuordecillion", "quindecillion", "sexdecillion", "septdecillion", "octdecillion",
"novemdecillion", "vigintillion"]];
var to_words = function (s, n){
var ns = s.slice(0,3);
return (ns.length < 1)?"":to_words(s.slice(3,s.length),n+1)
+((ns.length>1)?((ns.length==3&&ns[2]!="0")?names[0][ns[2]]+" hundred "
+((ns[1]=="1")?names[1][ns[0]]+" ":(ns[1]!="0")?names[2][ns[1]]+" "
+((ns[0]!="0")?names[0][ns[0]]+" ":""):(ns[0]!="0"
?names[0][ns[0]]+" ":"")):((ns[1]=="1")?names[1][ns[0]]+" ":(ns[1]!="0")
?names[2][ns[1]]+" "+((ns[0]!="0")?names[0][ns[0]]+" ":""):(ns[0]!="0"
?names[0][ns[0]]+" ":""))) + (((ns.length==3&&(ns[0]!="0"||ns[1]!="0"
||ns[2]!="0"))||(ns.length==2&&(ns[0]!="0"||ns[1]!="0"))||(ns.length==1&&ns[0]!="0"))
?"<span class='magnitude'>"+names[3][n]+"</span> ":""):((ns.length==1&&ns[0]!="0")
?names[0][ns[0]]+" ":"") + (((ns.length==3&&(ns[0]!="0"||ns[1]!="0"||ns[2]!="0"))
||(ns.length==2&&(ns[0]!="0"||ns[1]!="0"))||(ns.length==1&&ns[0]!="0"))
?"<span class='magnitude'>"+names[3][n]+"</span> ":""));
}, input;
document.getElementById("input").addEventListener("keyup", function (){
document.getElementById("output").innerHTML = to_words(this.value.replace(/[^0-9]/g, "")
.split("").reverse(), 0);
}, false);
}
describlerObj.prototype.getInfo = function (){
this.speeches.length = 0;
this.options.length = 0;
if (!this.activeElement) {
return false;
}
var role = this.activeElement.getAttribute("role");
// if ( "chart" != role){
// }
// TODO: cycle through charts, find matching element
switch ( role ){
case "chart":
this.handle_chart();
break;
case "datapoint":
this.handle_datapoint();
break;
case "datagroup":
this.handle_datagroup();
break;
case "axis":
case "xaxis":
case "yaxis":
this.handle_axis();
break;
case "axisitem":
this.handle_axisitem();
break;
case "legend":
this.handle_legend();
break;
case "legenditem":
this.handle_legenditem();
break;
case "flowchart":
this.handle_flowchart();
break;
case "node":
this.handle_node();
break;
case "connector":
this.handle_connector();
break;
default:
this.handle_default();
break;
}
if ( this.speeches.length){
this.speak();
}
}
describlerObj.prototype.handle_chart = function (){
// console.log("handle_chart");
// this.speeches.push( "chart" );
var chart = this.charts.find( match_element, this.activeElement );
if (chart) {
var chart_index = this.charts.findIndex( match_element, this.activeElement );
this.activeObject = chart;
// console.log("chart:");
// console.log(chart);
// console.log("index: " + chart_index);
for (var d = 0, d_len = chart.datasets.length; d_len > d; ++d) {
var dataset = chart.datasets[d];
if ( chart.label){
this.speeches.push( "Chart: " + chart.label );
}
if ( this.menu.selected ){
// console.log( "option: " + this.menu.selected.id );
if ( "stats" == this.menu.selected.id ){
// this.speeches.push( "This is a " + chart.type + " chart, with "
// + dataset.length + " data points" );
var stats_msg = chart.type + " chart, with "
+ dataset.statistics.count + " data points. "
+ "The highest value is " + this.getStat(dataset, "high")
+ ", and the lowest value is " + this.getStat(dataset, "low")
+ ", with a range of " + this.getStat(dataset, "range") +". "
+ "The average is " + this.getStat(dataset, "mean")
+ ", the median is " + this.getStat(dataset, "median")
+ ", and the total is " + this.getStat(dataset, "sum") +". ";
this.speeches.push( stats_msg );
} else if ( "low-high" == this.menu.selected.id
|| "high-low" == this.menu.selected.id ){
// list datapoints lowest to highest, or highest to lowest
// create a copy of the dataset, so we don't mess with the original ordering
var datasort = dataset.datapoints.slice();
if ( "low-high" == this.menu.selected.id ){
datasort.sort( function (a, b) {
return a.value - b.value;
});
this.speeches.push( "Lowest to highest: " );
} else if ( "high-low" == this.menu.selected.id ){
datasort.sort( function (a, b) {
return b.value - a.value;
});
this.speeches.push( "Highest to lowest: " );
}
for (var dp = 0, dp_len = datasort.length; dp_len > dp; ++dp) {
var datapoint = datasort[dp];
this.speeches.push( datapoint.label );
}
} else if ( "sonification" == this.menu.selected.id ){
// cancel speech
this.speeches.length = 0;
this.interaction_mode = "sonifier";
this.sonify();
this.sonifier.togglePlay();
}
}
}
this.menu.reset();
this.menu.add( "stats", "chart statistics", null, null, false );
this.menu.add( "low-high", "datapoints from lowest to highest", null, null, false );
this.menu.add( "high-low", "datapoints from highest to lowest", null, null, false );
this.menu.add( "sonification", "trend sonification", null, null, false );
if ( this.taskAssessments.length ) {
this.menu.add( "task-assessment", "start the test", null, "assessment", true );
}
// console.log(this.menu);
}
}
describlerObj.prototype.handle_datapoint = function (){
// console.log("handle_datapoint");
// this.speeches.push( "datapoint" );
for (var c = 0, c_len = this.charts.length; c_len > c; ++c) {
var chart = this.charts[c];
// if ( this.activeElement == chart.element){
for (var d = 0, d_len = chart.datasets.length; d_len > d; ++d) {
var dataset = chart.datasets[d];
console.log(dataset);
var datapoint = dataset.datapoints.find( match_element, this.activeElement );
if (datapoint) {
var dp_index = dataset.datapoints.findIndex( match_element, this.activeElement );
this.activeObject = datapoint;
// console.log("datapoint:");
// console.log(datapoint);
// console.log("index: " + dp_index);
// console.log( datapoint );
if ( !this.menu.selected ){
this.speeches.push( "Data point " + (dp_index + 1) + " of " + dataset.statistics.count );
}
this.speeches.push( datapoint.label );
var default_menu = true;
if ( this.menu.selected ){
var value = datapoint.value;
// if ( 1 == option ){
if ( "details" == this.menu.selected.id ){
// describe change of value from previous datapoint
if ( this.previous_datapoint ) {
var prev_datapoint = dataset.datapoints.find( match_element, this.previous_datapoint );
var lastValue = prev_datapoint.value;
var lastField = prev_datapoint.label_text;
var delta = "";
if ( value > lastValue){
delta += "This is an increase of " + (value - lastValue);
} else if ( value < lastValue) {
delta += "This is a decrease of " + (lastValue - value);
} else {
delta += "There is no change";
}
delta += " from the last value of " + lastValue + " for " + lastField;
this.speeches.push( delta );
}
// describe statistical status of value
if ( dataset.statistics["low"] == value){
this.speeches.push( "This is the lowest value." );
}
if ( dataset.statistics["high"] == value){
this.speeches.push( "This is the highest value." );
}
if ( dataset.statistics["median"] == value){
this.speeches.push( "This is the median value." );
}
if ( dataset.statistics["mean"] == value){
this.speeches.push( "This is the average value." );
}
// indicate datapoint index
var index_message = "This is the ";
index_message += this.getOrdinalNumber( dp_index + 1 );
if ( dataset.statistics.count == dp_index + 1 ) {
index_message += " and final ";
}
index_message += " data point.";
this.speeches.push( index_message );
// this.speeches.push( "This is the " + this.getOrdinalNumber( dp_index + 1 ) + " data point." );
// describe colors
var fills = [];
var strokes = [];
for (var c = 0, c_len = datapoint.colors.length; c_len > c; ++c) {
var eachColor = datapoint.colors[ c ];
if ( eachColor.fill_name && !fills.includes(eachColor.fill_name) ) {
fills.push( eachColor.fill_name );
}
if ( eachColor.stroke_name && !strokes.includes(eachColor.stroke_name) ) {
strokes.push( eachColor.stroke_name );
}
}
var color_message = "";
var fill_message = this.arrayToSentence(fills, "color", "colors" );
var stroke_message = this.arrayToSentence(strokes, "outline", "outlines" );
if ( fill_message && stroke_message ) {
color_message += fill_message + ", and " + stroke_message.replace("The", "the") + ".";
} else {
color_message += fill_message + stroke_message + ".";
}
this.speeches.push( color_message );
} else if ( "compare" == this.menu.selected.id ) {
var firstItem = true;
for (var odp = 0, odp_len = dataset.datapoints.length; odp_len > odp; ++odp) {
var otherDatapoint = dataset.datapoints[odp];
if ( datapoint.element != otherDatapoint.element){
var otherValue = otherDatapoint.value;
var delta = "";
if (firstItem) {
firstItem = false;
delta = "This is ";
} else if ( odp_len - 1 == odp
|| ( odp_len - 1 == dp_index && odp_len - 2 == odp )){
delta = " and ";
}
// delta += (value/otherValue).toFixed(2);
// delta += this.getFraction(value/otherValue) + " the value of ";
delta += ((value/otherValue) * 100).toFixed() + "%" + " the value of ";
if ( otherDatapoint.label_text){
delta += otherDatapoint.label_text;
} else {
delta += otherDatapoint.label;
}
this.speeches.push( delta );
}
}
} else if ( "compare-single" == this.menu.selected.id
|| "compare-single" == this.menu.selected.context ) {
if ( "compare-single" == this.menu.selected.id ) {
this.speeches.push( "Select datapoint for comparison:" );
default_menu = false;
this.menu.reset();
for (var odp = 0, odp_len = dataset.datapoints.length; odp_len > odp; ++odp) {
var otherDatapoint = dataset.datapoints[odp];
if ( datapoint.element != otherDatapoint.element){
var other_label = otherDatapoint.label_text;
this.menu.add( other_label, other_label, "compare-single", null, false );
}
}
} else if ( "compare-single" == this.menu.selected.context) {
var otherDatapoint = dataset.datapoints.find( function (datapoint) {
return datapoint.label_text == this;
}, this.menu.selected.id );
if ( otherDatapoint ){
// var value = datapoint.value;
var otherValue = otherDatapoint.value;
var delta_msg = "Comparison to " + otherDatapoint.label_text + ": ";
var mean_delta = value - otherValue;
var mean_delta_comp = " above";
if ( 0 > mean_delta ) {
mean_delta_comp = " below";
}
delta_msg += "This is " + Number(Math.abs(mean_delta).toFixed(2)) + mean_delta_comp
+ " the value of " + otherValue + " for "
+ otherDatapoint.label_text;
delta_msg += ", which is ";
// delta_msg += this.getFraction(value/otherValue);
delta_msg += ((value/otherValue) * 100).toFixed() + "%";
delta_msg += " the amount.";
this.speeches.push( delta_msg );
}
}
} else if ( "stats" == this.menu.selected.id ) {
var stats_msg = "";
if ( dataset.statistics["mean"] == value){
stats_msg += "This is the mean value, ";
} else {
// describe statistical status of value
var mean_delta = value - dataset.statistics["mean"];
var mean_delta_comp = " above";
if ( 0 > mean_delta ) {
mean_delta_comp = " below";
}
stats_msg += "This is " + (Math.abs(mean_delta).toFixed(2)) + mean_delta_comp
+ " the mean value of " + this.getStat(dataset, "mean") + ", ";
}
if ( dataset.statistics["median"] == value){
stats_msg += "and this is the median value. ";
} else {
// describe statistical status of value
var median_delta = (value - dataset.statistics["median"]);
var median_delta_comp = " above";
if ( 0 > median_delta ) {
median_delta_comp = " below";
}
stats_msg += "and " + (Math.abs(median_delta).toFixed(2)) + median_delta_comp
+ " the median value of " + this.getStat(dataset, "median") + ". ";
}
if ( dataset.statistics["low"] == value){
stats_msg += "This is the lowest value, ";
} else {
var low_delta = (value - dataset.statistics["low"]).toFixed(2);
stats_msg += "This is " + low_delta
+ " above the low value of " + this.getStat(dataset, "low") + ", ";
}
if ( dataset.statistics["high"] == value){
stats_msg += " and is the highest value. ";
} else {
var high_delta = (dataset.statistics["high"] - value).toFixed(2);
stats_msg += " and " + high_delta
+ " below the high value of " + this.getStat(dataset, "high") + ". ";
}
stats_msg += datapoint.label_text + " is "
// + this.getFraction(value/dataset.statistics["sum"])
+ ((value/dataset.statistics["sum"]) * 100).toFixed(2) + "%"
+ " of the total value of " + this.getStat(dataset, "sum") + ". ";
this.speeches.push( stats_msg );
}
}
if ( default_menu ) {
this.menu.reset();
this.menu.add( "details", "more details", null, null, false );
this.menu.add( "compare", "comparison to all other data points", null, null, false );
this.menu.add( "compare-single", "comparison to a specific data point", null, null, false );
this.menu.add( "stats", "datapoint statistics", null, null, false );
}
}
}
}
}
describlerObj.prototype.handle_datagroup = function (){
console.log("handle_datagroup");
this.speeches.push( "datagroup" );
}
describlerObj.prototype.handle_axis = function (){
// console.log("handle_axis");
// this.speeches.push( "axis" );
for (var c = 0, c_len = this.charts.length; c_len > c; ++c) {
var chart = this.charts[c];
for (var a in chart.axes) {
// console.log(key, chart[axis]);
var axis = chart.axes[a];
if ( this.activeElement == axis.element){
this.activeObject = axis;
if ( axis.label){
this.speeches.push( axis.type + " axis: " + axis.label );
}
if ( !isNaN(axis.min) && !isNaN(axis.max)){
this.speeches.push( ", " + axis.items.length + " items, ranging from "
+ axis.min + " to " + axis.max );
} else {
this.speeches.push( ", " + axis.items.length + " items, ranging from "
+ axis.items[0].label + " to "
+ axis.items[axis.items.length - 1].label );
// if ( 1 == option ){
// this.speeches.push( ", with the following labels: " );
// for (var l = 0, l_len = axis.labels.length; l_len > l; ++l) {
// this.speeches.push( axis.labels[l] );
// }
// } else {
// this.speeches.push( ", " + axis.labels.length + " items, ranging from "
// + axis.labels[0] + " to "
// + axis.labels[axis.labels.length - 1] );
// }
}
var default_menu = true;
if ( this.menu.selected ){
if ( "labels" == this.menu.selected.id ){
this.speeches.push( ", with the following labels: " );
for (var l = 0, l_len = axis.items.length; l_len > l; ++l) {
this.speeches.push( axis.items[l].label );
}
} else if ( "select" == this.menu.selected.id
|| "select" == this.menu.selected.context ) {
// TODO: let user select axis label by menu or typing name
// TODO: account for nested/group axis labels
if ( "select" == this.menu.selected.id ){
this.speeches.push( "Select axis label:" );
default_menu = false;
this.menu.reset();
for (var l = 0, l_len = axis.items.length; l_len > l; ++l) {
this.menu.add( axis.items[l].label, axis.items[l].label, "select", null, false );
}
} else if ( "select" == this.menu.selected.context ) {
var axisitem = axis.items.find( function (axis) {
return axis.label == this;
}, this.menu.selected.id );