-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmodelleertaal-app.js
1235 lines (999 loc) · 33.6 KB
/
modelleertaal-app.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
var evaluator_js = require('./evaluator.js');
var Blob = require('blob');
var FileSaver = require('file-saver');
// this also depends on:
// jQuery
// jQuery.Flot
// These libs are not included, because the Flot libray does not play well
// with browserify.
// Include this in the HTML with:
//<script src="scripts/jquery-3.2.1.min.js"></script>
//<script src="scripts/jquery.flot.js"></script>
//jshint devel:true
//jshint es5:true
//jshint loopfunc: true
/* version history: CHANGELOG.md */
var version = "v5.4.2 20mei2022";
function ModelleertaalApp(params) {
this.debug = params.debug || false;
this.version = version;
console.log('Modelleertaal App. ' + version + '. Debug = ' + this.debug);
this.model_index = params.model_index || false;
console.log('Model_index: ', this.model_index);
this.base_url = params.base_url || '';
this.rel_url = params.rel_url || 'index.html';
if (this.debug) {
console.log('base_url: ', base_url);
console.log('rel_url:' , rel_url);
}
this.CodeMirror = params.CodeMirror || true;
this.CodeMirrorActive = false;
this.yaxis_autoscale = false; // try to include origin (y=0) by default
this.dom_modelregels = "#modelregels";
this.dom_startwaarden = "#startwaarden";
this.dom_status = "#status_bar";
this.dom_datatable = "#datatable";
this.dom_graph = "#graph";
this.dom_nbox = "#NBox";
this.dom_nbox_continue = "#NBoxContinue";
this.dom_run = "#run";
this.dom_continue = "#continue";
this.dom_trace = "#trace";
this.dom_plot = "#plot";
this.dom_fileinput = "#fileinput";
this.dom_download_xml = "#download_xml";
this.dom_download_xml_fn = "#xml_filename";
this.dom_download_pgf = "#download_pgf";
this.dom_download_pgf_fn = "#pgf_filename";
this.dom_download_tsv = "#download_tsv";
this.dom_download_tsv_fn = "#tsv_filename";
this.dom_download_svg = "#download_svg";
this.dom_download_svg_fn = "#svg_filename";
this.dom_clickdata = "#clickdata";
this.dom_x_var = "#x_var";
this.dom_y_var = "#y_var";
this.dom_select_graph = "#select_graph";
this.dom_model_keuze = "#model_keuze";
this.dom_permalink = "#permaklink";
this.dom_legend = "#legend";
this.read_model();
if ((this.CodeMirror) && (typeof(CodeMirror) == 'function')) {
if (this.debug)
console.log("CodeMirror enabled.");
var codemirror_options = {
lineNumbers: true,
mode: "modelleertaal" };
this.modelregels_editor = CodeMirror.fromTextArea($(this.dom_modelregels)[0], codemirror_options);
this.startwaarden_editor = CodeMirror.fromTextArea($(this.dom_startwaarden)[0], codemirror_options);
this.CodeMirrorActive = true;
} else {
this.CodeMirror = false;
this.CodeMirrorActive = false;
if (this.debug)
console.log("CodeMirror disabled.");
}
// (re)set the app
this.init_app();
this.load_model();
this.max_rows_in_plot = 200;
this.precision = 4;
var self = this;
$(this.dom_run).click(function() {
// read N from input field
self.N = Number($(self.dom_nbox).val());
// count model run as pageview to #run in goatcounter
if (window.goatcounter !== undefined) {
window.goatcounter.count({
path: location.pathname + '#run',
});
}
self.run();
});
$(this.dom_continue).click(function() {
self.N = Number($(self.dom_nbox_continue).val());
self.continue_run();
});
$(this.dom_trace).click(function() {
self.trace();
});
$(this.dom_plot).click(function() {
if (self.results.length === 0) {
console.log('Plot clicked. No results --> Run first');
self.N = Number($(self.dom_nbox).val());
self.run();
}
self.do_plot();
//self.print_status("Plot OK.");
});
$(this.dom_model_keuze).change(function () {
self.dropdown_load_model();
});
$(this.dom_download_xml).click(function() {
self.download_model();
});
$(this.dom_download_pgf).click(function() {
self.download_pgfplot();
});
$(this.dom_download_tsv).click(function() {
self.download_tsv();
});
$(this.dom_download_svg).click(function() {
self.download_svg();
});
$(this.dom_fileinput).change(function(event) {
self.read_file(event);
});
this.multiplot = false;
$("#multiplot").click(function() {
self.multiplot = !self.multiplot;
self.set_graph_menu();
});
}
ModelleertaalApp.prototype.load_model = function() {
// called from $(document).ready();
// listen to URL index.html#model=model_naam&N=100 type URL
// https://stackoverflow.com/a/44169739/4965175
var hash = window.location.hash.substr(1);
var url_params = hash.split('&').reduce(function(result, item) {
var parts = item.split('=');
result[parts[0]] = parts[1];
return result;
}, {});
console.log("params passed in URL", url_params);
var N_preset = url_params.N || N_default || false;
if (N_preset) {
$("#NBox").val(N_preset);
}
this.dropdown_update();
var model_preselected = url_params.model || false;
if (model_preselected) {
// try to load model passed with model=... parameter
var model_url = 'modellen/' + model_preselected + '.xml';
this.load_model_xml_from_url(model_url);
var reverse_dropdown_val = model_index.findIndex(function(element) {
return element.url == model_url;
});
// set dropdown to selected model
$(this.dom_model_keuze).val(reverse_dropdown_val);
} else {
// lees keuze uit drop-down en kies juiste url
this.dropdown_load_model();
}
};
ModelleertaalApp.prototype.print_status = function(status, error) {
$(this.dom_status).html(status);
if (typeof error != "undefined") $(this.dom_graph).html(error).css("font-family", "monospace");
};
ModelleertaalApp.prototype.load_model_xml_from_url = function(url) {
var self = this;
$.ajax({
url: url,
dataType: "text",
success: function(data) {
self.read_model_from_xml(data);
self.init_app();
}, // succes();
error: function(xhr, ajaxOptions, thrownError) {
if (xhr.status === 0) {
alert("Kan model " + url + " niet laden.\nBestaat het model?\nOffline? Zet CORS protection uit");
} else {
alert(thrownError);
}
$(self.dom_graph).html("Model niet geladen! FOUT.");
self.print_status("Status: ERROR.");
} // error();
}); //.ajax
};
ModelleertaalApp.prototype.dropdown_update = function() {
// maak het drop-down modelkeuze menu uit model.js
$(this.dom_model_keuze).empty();
for (var i = 0; i < this.model_index.length; i++) {
$('<option/>').val(i).text(this.model_index[i].title).appendTo(this.dom_model_keuze);
}
};
ModelleertaalApp.prototype.dropdown_load_model = function() {
//var model_keuze = $("#model_keuze").val();
var model_keuze = $(this.dom_model_keuze).val();
url = this.model_index[model_keuze].url;
var myRe = /\/([^.]+)/g;
var rel_link = this.rel_url + 'index.html#model=' + myRe.exec(url)[1];
var permalink = this.base_url + rel_link;
// verander de URL onder de knop "link"
$(this.dom_permalink).attr('href', permalink);
// verander de URL in de browser (history)
if (history.replaceState) {
window.history.replaceState("", "Modelleertaal webapp", rel_link);
} else {
document.location.href = rel_link;
}
// count model reload as pageview in goatcounter
if (window.goatcouter !== undefined) {
window.goatcounter.count({
path: location.pathname,
});
}
this.load_model_xml_from_url(url);
};
ModelleertaalApp.prototype.read_model = function() {
// read model from textarea/CodeMirror
this.model = new evaluator_js.Model();
if (this.CodeMirrorActive) {
this.model.modelregels = this.modelregels_editor.getValue();
this.model.startwaarden = this.startwaarden_editor.getValue();
} else {
this.model.modelregels = $(this.dom_modelregels).val();
this.model.startwaarden = $(this.dom_startwaarden).val();
}
};
ModelleertaalApp.prototype.read_file = function(evt) {
var self = this;
var f = evt.target.files[0];
console.log('read_file: ' + f);
if (f) {
var r = new FileReader();
r.onload = function(e) {
console.log(e.target.result);
self.read_model_from_xml(e.target.result);
self.init_app();
};
r.readAsText(f);
}
};
ModelleertaalApp.prototype.download_model = function() {
// download model in "BogusXML" format
// just a text file with XML like tags...
var filename = $(this.dom_download_xml_fn).val();
this.read_model();
// copy N from app, if exist, write into Model
if (this.N !== undefined) {
this.model.N = this.N;
}
this.save_string(this.model.createBogusXMLString(), filename);
};
ModelleertaalApp.prototype.download_pgfplot = function() {
// save the plot in TikZ/PGFPlot format
if (this.do_plot() === false) return;
var filename = $(this.dom_download_pgf_fn).val();
this.save_string(this.create_pgfplot(), filename);
};
ModelleertaalApp.prototype.download_tsv = function() {
// download the results in TSV format.
var filename = $(this.dom_download_tsv_fn).val();
this.save_string(this.create_tsv(), filename);
};
ModelleertaalApp.prototype.download_svg = function() {
// download SVG Plot.
var filename = $(this.dom_download_svg_fn).val();
this.save_string(this.create_svgplot(), filename);
};
ModelleertaalApp.prototype.save_string = function(data, filename) {
// requires FileSaver.js and Blob.js
// (Blob() not supported on most mobile browsers)
// mime text/plain expects CRLF \r\n instead of \n
// this should work on both Windows and Mac/Linux
var blob = new Blob([data.replace(/([^\r])\n/g, "$1\r\n")], {
type: "text/plain;charset=utf-8"
});
FileSaver.saveAs(blob, filename);
};
ModelleertaalApp.prototype.run = function() {
if (this.setup_run()) {
this.new_run = true;
if (!this.do_run()) this.has_run = false;
this.after_run();
this.has_run = true;
return true;
} else {
return false;
}
};
ModelleertaalApp.prototype.continue_run = function() {
if (this.has_run) {
this.new_run = false;
this.tracing = false;
} else {
this.new_run = true;
this.setup_run();
}
if (!this.do_run()) this.has_run = false;
this.after_run();
this.has_run = true;
return true;
};
ModelleertaalApp.prototype.trace = function() {
if (this.has_run) {
this.new_run = false;
} else {
this.new_run = true;
this.setup_run();
}
this.tracing = true;
this.do_run();
this.after_run();
this.has_run = true;
return true;
};
ModelleertaalApp.prototype.setup_run = function() {
// reset the breakpoint pointer:
this.tracing = false;
this.remove_highlight_trace();
this.read_model();
if (this.debug)
console.log('model = ', this.model);
if (this.N > 1e6) {
alert('N te groot!');
throw new Error('N te groot!');
}
this.print_status("Run started...");
console.log("Run started...");
try {
this.evaluator = new evaluator_js.ModelregelsEvaluator(this.model, this.debug);
} catch (err) {
this.print_status("Model niet in orde.", err.message.replace(/\n/g, "<br>"));
alert("Model niet in orde: \n" + err.message);
this.highlight_error(err.parser_line, err.parser_name);
return false;
}
return true;
};
ModelleertaalApp.prototype.do_run = function() {
var run_result;
try {
this.evaluator.set_state(this.N, this.new_run, this.tracing);
this.evaluator.run();
run_result = this.evaluator.get_state();
this.results = this.evaluator.result;
} catch (err) {
if (err instanceof EvalError) {
alert("Model niet in orde:\nVariable niet gedefineerd in startwaarden?\n" + err.message);
} else {
alert("Model niet in orde:\n" + err.message);
}
this.print_status("Fout in model.", err.message.replace(/\n/g, "<br>"));
this.highlight_error(err.parser_line, err.parser_name);
return false;
}
var N_iterations = this.results.length-1;
if (!run_result.tracing) {
this.print_status("Klaar na "+N_iterations+" iteraties.");
this.tracing = false;
this.remove_highlight_trace();
} else {
this.print_status("Debugger in iteratie "+ N_iterations);
this.highlight_trace(run_result.lineno+1);
}
// make table, plot
this.allVars = this.evaluator.namespace.varNames;
if (this.debug)
console.log(this.allVars);
};
ModelleertaalApp.prototype.after_run = function() {
if (this.allVars !== undefined) {
// create the axis drop-down menu, try to keep value
this.save_axis();
this.reset_axis_dropdown();
this.set_axis();
this.print_table();
this.do_plot();
}
};
ModelleertaalApp.prototype.save_axis = function() {
// save chosen variable, try to plot same graph
this.xvar_last = $(this.dom_x_var).find(":selected").text();
this.yvar_last = $(this.dom_y_var).find(":selected").text();
};
ModelleertaalApp.prototype.reset_axis_dropdown = function() {
if (!this.results_available()) {
return;
}
// (re)set varNames in drop-down select fields
$(this.dom_x_var).empty();
$(this.dom_y_var).empty();
$('<option/>').val('').text('auto').appendTo(this.dom_x_var);
$('<option/>').val('').text('auto').appendTo(this.dom_y_var);
for (var i = 0; i < this.allVars.length; i++) {
$('<option/>').val(i).text(this.allVars[i]).appendTo(this.dom_x_var);
$('<option/>').val(i).text(this.allVars[i]).appendTo(this.dom_y_var);
}
var self = this;
$(this.dom_x_var).change(function(){
// the plotted variables change. Erase previous results.
self.previous_plot = [];
});
$(this.dom_y_var).change(function(){
// the plotted variables change. Erase previous results.
self.previous_plot = [];
});
};
ModelleertaalApp.prototype.set_axis = function() {
// try to plot same graph: Reset axis to previous settings.
var self = this;
idx = this.allVars.findIndex(function(s) {
return s == self.xvar_last;
});
if (idx != -1) $(this.dom_x_var).val(idx);
idx = this.allVars.findIndex(function(s) {
return s == self.yvar_last;
});
if (idx != -1) $(this.dom_y_var).val(idx);
};
//
// Table
//
ModelleertaalApp.prototype.table_header = function() {
var firstrow = $('<tr>');
firstrow.append($('<th>').text('#'));
for (var k = 0; k < this.allVars.length; k++) {
firstrow.append($('<th>').text(this.allVars[k]));
}
return firstrow;
};
ModelleertaalApp.prototype.table_row = function(rowIndex) {
function fix(x) {
if (isNaN(x)) return "X";
if (Math.abs(x) < 0.0001) return 0;
return x;
}
var row = $('<tr>');
row.append($('<td>').text(rowIndex));
var res;
for (var j = 0; j < this.results[rowIndex].length; j++) {
res = this.results[rowIndex][j];
if (typeof(res) === 'number') {
res = fix(res.toPrecision(this.precision));
} else { // -- boolean
if (res) {
res = 'Waar';
} else {
res = 'Onwaar';
}
}
row.append($('<td>').text(res));
}
return row;
};
ModelleertaalApp.prototype.print_table = function(limit) {
// truncated row from: jquery.jsparc.js
// http://github.com/HiSPARC/jSPARC
var self = this;
limit = (limit) ? limit : 10;
limit = Math.min(this.results.length, limit);
var table = $('<table>').addClass('table');
table.append(this.table_header());
for (var i = 0; i < this.results.length; i++) {
table.append(this.table_row(i));
if (limit != this.results.length && i == Math.floor(limit / 2) - 1) {
var truncrow = $('<tr>');
truncrow.append($('<td>')
.text('... Tabel ingekort. Klik voor meer rijen ...')
.attr('colspan', this.results.length + 1)
.css({
'text-align': 'left',
'cursor': 'pointer'
})
.click(function() {
self.print_table(limit * 5);
}));
table.append(truncrow);
i = this.results.length - 1 - Math.ceil(limit / 2);
}
}
$(self.dom_datatable).html(table);
};
//
// Plotten
//
ModelleertaalApp.prototype.do_plot = function() {
if (!this.results_available()) {
//alert('Geen resultaten. Druk eerst op Run!');
console.error('No results! cannot plot');
return false;
}
if (this.multiplot) {
this.do_multi_plot();
return;
}
// if set to "auto" set axis to default settings (x,t)
this.set_axis_to_defaults();
// show "meerdere grafieken button"
this.toggle_plot_mode();
var results = this.reduce_rows(this.results, this.max_rows_in_plot);
var current_plot = {
data: [],
color: 'blue',
label: this.allVars[yvar_colidx]
};
var previous_plot = {
data: this.previous_plot,
color: '#d3d3d3', // light-gray
label: ''
};
for (var i = 0; i < results.length; i++) {
current_plot.data.push([results[i][xvar_colidx], results[i][yvar_colidx]]);
}
// FIXME: left over from (very!) old code...
this.scatter_plot = current_plot.data;
var dataset = [];
dataset.push(previous_plot);
dataset.push(current_plot);
$(this.dom_graph).empty(); // verwijder text enzo
$(this.dom_clickdata).empty();
this.plot_graph(dataset);
this.previous_plot = this.scatter_plot;
}; // do_plot
ModelleertaalApp.prototype.results_available = function() {
if (this.results.length === 0) {
return false;
}
return true;
}; // results_available
ModelleertaalApp.prototype.toggle_plot_mode = function() {
if (!this.results_available()) {
$("#multiplot").empty();
$("#multiplot").removeClass("multiplot");
$("#legend").css('display','none'); //hide legend
return;
}
var msg = "";
if (this.multiplot) {
msg = 'Terug naar enkele grafiek';
} else {
msg = 'Plot meerdere grafieken';
}
$("#multiplot").html(msg).addClass("multiplot");
$("#legend").css('display','inline');
}; // set_plot.mode
ModelleertaalApp.prototype.set_graph_menu = function() {
var self = this;
if (this.multiplot) {
// build multi variable checkboxes for y-var
$(this.dom_select_graph).empty();
for (var i = 0; i < this.allVars.length; i++) {
var checked_y = (i == yvar_colidx) ? true : false;
var checked_x = (i == xvar_colidx) ? true : false;
$(this.dom_select_graph).append($("<input>").attr("type", "checkbox")
.attr("checked", checked_y).attr("idx_yvar", i)
.attr("id", "id_" + this.allVars[i]));
$(this.dom_select_graph).append($("<label>").text(this.allVars[i]));
$(this.dom_select_graph).append($('<br>'));
}
$(this.dom_select_graph).find("input:checkbox").click(function () {
// toglle autoscale on/off and replot!
self.do_plot();
});
} else {
// reset single dropdown menu for y-var.
$(this.dom_select_graph).empty();
$(this.dom_select_graph).append($("<select>").attr("id", "y_var"));
$(this.dom_legend).empty();
this.reset_axis_dropdown();
}
this.toggle_plot_mode();
}; // create_graph_checkboxes
ModelleertaalApp.prototype.do_multi_plot = function() {
var self = this;
// qualitative colour scheme that is colour-blind safe.
// https://personal.sron.nl/~pault/#sec:qualitative
// blue, cyan, green, yellow, red, purple, grey
var graph_colors = ['blue', '#6ce', '#283', '#cb4', '#e67','#a37', '#bbb'];
// FIXME cache this!!!
var results = this.reduce_rows(this.results, this.max_rows_in_plot);
var dataset = [];
// x-var
xvar_colidx = parseInt($(this.dom_x_var).val());
xvar_colidx = (!isNaN(xvar_colidx)) ? xvar_colidx : 0;
$(this.dom_x_var).val(xvar_colidx);
var n = 0;
// y-vars
$("#select_graph").find("input:checked").each(function () {
var ycol_idx = $(this).attr("idx_yvar");
ycol_idx = parseInt(ycol_idx);
if (isNaN(ycol_idx)) return ;
var plot = {
data: []
};
for (var i = 0; i < results.length; i++) {
// FIXME xvar_colidx scope!!!!
plot.data.push([results[i][xvar_colidx], results[i][ycol_idx]]);
}
plot.color = graph_colors[n];
plot.label = self.allVars[ycol_idx];
n += 1;
dataset.push(plot);
});
$(this.dom_graph).empty(); // verwijder text enzo
$(this.dom_clickdata).empty();
this.plot_graph(dataset);
}; // do_multi_plot
ModelleertaalApp.prototype.set_axis_to_defaults = function() {
// get column indices (in results array) of variables to plot
xvar_colidx = parseInt($(this.dom_x_var).val());
yvar_colidx = parseInt($(this.dom_y_var).val());
// if undefined -> x first column, y second column of results
xvar_colidx = (!isNaN(xvar_colidx)) ? xvar_colidx : 0;
yvar_colidx = (!isNaN(yvar_colidx)) ? yvar_colidx : 1;
// set column varnames in input fields
$(this.dom_x_var).val(xvar_colidx);
$(this.dom_y_var).val(yvar_colidx);
};
ModelleertaalApp.prototype.plot_graph = function(dataset) {
var self = this;
var plot_yaxis_min;
var x_var_name = this.allVars[$(this.dom_x_var).val()];
var y_var_name = this.allVars[$(this.dom_y_var).val()];
// FIXME: Dit kan VEEL makkelijker en LEESBAARDER!
function find_datasets_min_below_zero(ds) {
var min = 0;
var len_ds = ds.length;
var val, i;
for (i = 0; i < len_ds; i++ ) {
val = find_dataset_min(ds[i]);
if ( val < min ) {
min = val;
}
}
return min;
}
function find_dataset_min(d) {
var min = Infinity;
var len = d.data.length;
var val;
for ( var i = 0; i < len; i++ ) {
val = d.data[i][1];
if ( val < min ) {
min = val;
}
}
return min;
}
if (self.yaxis_autoscale) {
plot_yaxis_min = undefined; // use autoscale for y-axis
} else {
// plot the y-axis from min(0, minimum of dataset)
// FIXME: Does not work for multiple datasets!!!!
plot_yaxis_min = find_datasets_min_below_zero(dataset);
}
$(this.dom_graph).css("font-family", "sans-serif");
function sciFormatter(val, axis) {
// format large numbers in scientific notation: 1e6
// probably *much* (much!) slower than the default tickformatter
if (Math.abs(val) > 9e4)
return val.toExponential(1);
else
//return val.toFixed(axis.tickDecimals);
return $.plot.defaultTickFormatter(val, axis);
}
var legendContainer = document.getElementById("legend");
var axis_font = {
size: 12,
lineHeight: 13,
family: "sans-serif",
variant: "small-caps",
color: "#545454"
};
var plot_object = $.plot($(this.dom_graph), dataset, {
series: {
lines: {
show: true
},
points: {
radius: 1,
show: true,
fill: true
}
},
grid: {
hoverable: true,
clickable: true
},
axisLabels: {
show: true
},
xaxis: {
font: axis_font,
showTicks: false,
tickFormatter: sciFormatter,
axisLabel: x_var_name
},
yaxis: {
font: axis_font,
showTicks: false,
position: 'left',
tickFormatter: sciFormatter,
min: plot_yaxis_min,
axisLabel: y_var_name
},
legend: {
show: (this.multiplot) ? true : null,
container: legendContainer,
},
tooltip: {
show: true,
content: "%lx: %x.2, %s: %y.2"
}
}); // $.plot()
$(this.dom_graph).bind("plotclick", function(event, pos, item) {
if ((self.multiplot) || (item.seriesIndex == 1)) {
// multiplot: click on all lines, single plot: do not allow click on previous plot
var table = $('<table>').addClass('table');
table.append(self.table_header());
table.append(self.table_row(self.get_result_rowIndex(item.dataIndex)));
$(self.dom_clickdata).html(table);
}
}); // $bind.("plotclick")
// create clickable y-axis that toggles autoscale
var axes = plot_object.getAxes();
var axis = axes.yaxis;
var box = axis.box;
$("<div id='plot_yaxis' class='axisTarget' style='position:absolute; left:" + box.left + "px; top:" + box.top + "px; width:" + box.width + "px; height:" + box.height + "px'></div>")
.data("axis.direction", axis.direction)
.data("axis.n", axis.n)
.css({ backgroundColor: "#f00", opacity: 0, cursor: "pointer" })
.appendTo(plot_object.getPlaceholder())
.hover(
function () { $(this).css({ opacity: 0.10 }); },
function () { $(this).css({ opacity: 0 }); }
)
.click(function () {
// toglle autoscale on/off and replot!
self.yaxis_autoscale = !self.yaxis_autoscale;
self.do_plot();
});
$("#plot_yaxis").hover(function() {
$(this).css('cursor','pointer').attr('title', 'Klik op de as om de schaal te wijzigen (autoscale aan/uit).');
});
}; // plot_graph()
ModelleertaalApp.prototype.set_max_rows_in_plot = function(max_rows) {
this.max_rows_in_plot = max_rows;
};
ModelleertaalApp.prototype.set_precision = function(precision) {
this.precision = precision;
};
ModelleertaalApp.prototype.read_model_from_xml = function(XMLString) {
this.model = new evaluator_js.Model();
this.model.parseBogusXMLString(XMLString);
};
//
// Reset
//
ModelleertaalApp.prototype.init_app = function() {
if (this.CodeMirrorActive) {
this.modelregels_editor.setValue(this.model.modelregels);
this.startwaarden_editor.setValue(this.model.startwaarden);
} else {
$(this.dom_modelregels).val(this.model.modelregels);
$(this.dom_startwaarden).val(this.model.startwaarden);
}
if (this.model.N) $(this.dom_nbox).val(this.model.N);
this.results = [];
this.scatter_plot = [];
this.previous_plot = [];
this.has_run = false;
this.tracing = false;
// (re)set graph menu
this.multiplot = false;
this.set_graph_menu();
$(this.dom_y_var).empty();
$(this.dom_x_var).empty();
$('<option/>').val('').text('auto').appendTo(this.dom_x_var);
$('<option/>').val('').text('auto').appendTo(this.dom_y_var);
this.print_status("Status: Model geladen.", "Model geladen. Geen data. Druk op Run!");
$(this.dom_datatable).empty();
};
//
// TSV -- use TSV instead of CSV to prevent , . decimal problems in Excel.
//
ModelleertaalApp.prototype.create_tsv = function() {
var self = this;
var tsv = '';
tsv += this.allVars.join('\t'); //header row
tsv += "\n";
tsv += this.results.map(function(row ){
return row.map(function(item) {
return typeof(item) === 'number' ? item.toPrecision(self.precision) : item;
}).join('\t');
}).join('\n');
// replace . with , for NL Excel (should be an option)
return tsv.replace(/\./g,",");
};
//
// PGFPlot
//
ModelleertaalApp.prototype.create_pgfplot_header = function() {
// try to create a PGFPlot that fits the 10x10cm grid.
// set x,y axis scales and min max values accordingly
// This only works for graphs starting at (0,0)
// https://stackoverflow.com/a/31643591/4965175
function arrayMax(array) {
return array.reduce(function(a, b) {
return Math.max(a, b);
});
}
function arrayMin(array) {
return array.reduce(function(a, b) {
return Math.min(a, b);
});
}
function round_to_scale(max_val) {
// round to next 10, 20, 50, 100, 200, 500, ...