File size: 24,143 Bytes
d5bfab8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
// Construct url for a program id (eg A112088), like the following
// https://raw.githubusercontent.com/loda-lang/loda-programs/main/oeis/112/A112088.asm
function urlFromProgramId(programId) {
    const zeroPad = (num, places) => String(num).padStart(places, '0');
    const dir_index_string = zeroPad(Math.floor(programId/1000), 3);
    const filename_string = "A" + zeroPad(programId, 6) + ".asm";
    let baseurl = "https://raw.githubusercontent.com/loda-lang/loda-programs/main/oeis";
    let url = `${baseurl}/${dir_index_string}/${filename_string}`;
    return url;
}

// Construct an OEIS id from a program id (eg 40), like the following
// "A000040".
function oeisIdFromProgramId(programId) {
    const zeroPad = (num, places) => String(num).padStart(places, '0');
    return "A" + zeroPad(programId, 6);
}

function sleep(ms) {
    return new Promise(resolve => setTimeout(resolve, ms));
}

const ENUM_SCALEMODE_AUTO = 0;
const ENUM_SCALEMODE_LINEAR = 1;
const ENUM_SCALEMODE_LOGARITHMIC = 2;

class PageController {
    constructor() {
        this.mWorkerIsReady = false;
        this.mDidLoadProgram = false;
        this.mIdenticalToOriginal = true;
        this.mOriginalText = "";
        this.mWasUnableToFetchProgram = false;
        this.mScaleMode = ENUM_SCALEMODE_AUTO;
        this.mTermCount = 10;
        this.setupWorker();
        this.setupEditor();
        this.setupChart();
        this.setupKeyboardShortcuts();
        this.prepareProgram();
        this.setupInteractiveArea();
    }
  
    setupWorker() {
        const worker = new Worker('worker.js');
        this.mPromiseWorker = new PromiseWorker(worker);
        const eventListener = (e) => {
            this.workerOnMessage(e);
        };
        worker.addEventListener('message', eventListener);
        this.mWorker = worker;
        this.mWorkerTemporaryEventListener = eventListener;
    }
  
    workerOnMessage(e) {
        // console.log("onMessage", e.data);
        switch (e.data.fn) {
        case "init":
            this.commandInit(e.data);
            break;
        default:
            console.error(`workerOnMessage.unknown: ${e.data}`);
            this.outputArea_appendErrorMessage("workerOnMessage received unknown message");
            break;
        }
    }
  
    commandInit(parameters) {
        // console.log("worker init", parameters);

        // Remove the temporary event listener.
        this.mWorker.removeEventListener("message", this.mWorkerTemporaryEventListener);
        this.mWorkerTemporaryEventListener = null;
        this.mWorker = null;

        // Show an error if the worker failed to load.
        if(!parameters.value) {
            console.error("failed to initialize worker", parameters);
            this.outputArea_clear();
            this.outputArea_appendErrorMessage(`Failed to initialize worker. reason: ${parameters.reason}`);
            return;
        }

        // The worker has been initialized successfully
        // console.log("worker initialized successful", parameters);
        this.mWorkerIsReady = true;

        // Mechanism that prevents the "Worker loaded OK" message to get shown, 
        // if there have been an error during initialization.
        if (this.mWasUnableToFetchProgram) {
            console.log("An error is already shown, that has higher priority.");
            return;
        }

        // Show that the worker has been loaded successfully.
        this.outputArea_clear();
        this.outputArea_appendTerm("Worker loaded OK.");

        // Run the program 
        this.proceedIfAllThingsAreReady();
    }

    proceedIfAllThingsAreReady() {
        if (!this.mDidLoadProgram) {
            return;
        }
        if (!this.mWorkerIsReady) {
            return;
        }
        (async () => {
            this.hideOverlay();
            await this.workerCompileAndExecute();
        })();
    }

    async tellWorkerToStopExecuting() {
        console.log("stop executing BEFORE");
        await this.mPromiseWorker.postMessage({
            fn: "stop"
        });
        console.log("stop executing AFTER");
    }
  
    async compileEditorCode() {
        console.log("compile editor code BEFORE");
        let sourceCode = this.mEditor.getValue();

        try {
            await this.mPromiseWorker.postMessage({
                fn: "compile", 
                sourceCode: sourceCode
            });
        } catch(error) {
            console.error("Unable to compile", error);
            this.outputArea_showError(error);
            return false;
        }
        console.log("compile editor code AFTER");
        return true;
    }
    
    outputArea_clear() {
        const div = document.getElementById("output-inner");
        div.innerHTML = '';
    }
  
    outputArea_appendTerm(termValueString) {
        const parentDiv = document.getElementById("output-inner");    
        if (parentDiv.hasChildNodes()) {
            const a0 = document.createElement("span");
            a0.className = "separator";
            const a1 = document.createTextNode(",");
            a0.appendChild(a1);        
            parentDiv.appendChild(a0);
        }
        const b0 = document.createElement("span");
        b0.className = "term";
        const b1 = document.createTextNode(termValueString);
        b0.appendChild(b1);
        parentDiv.appendChild(b0);
    }
  
    outputArea_appendErrorMessage(errorMessage) {
        const parentDiv = document.getElementById("output-inner");    
        if (parentDiv.hasChildNodes()) {
            const a0 = document.createElement("span");
            a0.className = "separator";
            const a1 = document.createTextNode(",");
            a0.appendChild(a1);        
            parentDiv.appendChild(a0);
        }
        const b0 = document.createElement("span");
        b0.className = "error";
        const b1 = document.createTextNode(errorMessage);
        b0.appendChild(b1);        
        parentDiv.appendChild(b0);
    }

    outputArea_showError(error) {
        this.outputArea_clear();
        this.outputArea_appendErrorMessage(error.message);
    }
  
    async setRange() {
        let rangeLength = this.mTermCount;
        await this.mPromiseWorker.postMessage({
            fn: "setrange", 
            rangeStart: 0,
            rangeLength: rangeLength
        });
    }
  
    async executeRange() {
        // Usecase:
        // There is nothing executing in the web worker.
        // The user hits Ctrl+Enter a single time.
        // Causing the code to run.
        //
        // Usecase:
        // The user may be hitting Ctrl+Enter several times in rapid succession.
        // This happens surprisingly often.
        //
        // Usecase:
        // There is currently executing stuff in the web worker.
        // The user hits Ctrl+Enter a single time.
        // Causing the current executing to stop.
        // And causing the new code to run.

        await this.mPromiseWorker.postMessage({
            fn: "executerange", 
        });
        this.outputArea_clear();
        await this.pullWorkerResults();
    }

    async pullWorkerResults() {
        const responseDictionary = await this.mPromiseWorker.postMessage({
            fn: "takeresult", 
        });
        // console.log("responseDictionary", responseDictionary);
        const termsArray = responseDictionary.terms;
        const isExecuting = responseDictionary.isExecuting;

        var arrayLength = termsArray.length;
        for (var i = 0; i < arrayLength; i++) {
            const item = termsArray[i];
            if (item.value != null) {
                this.outputArea_appendTerm(item.value);
                continue;
            }
            if (item.error != null) {
                console.error("Unable to compute term", item.error);
                this.outputArea_appendErrorMessage(item.error);
                break;
            }
            console.error("Encountered an integrity error. Expected either 'value' or 'error', but got something else.");
            this.outputArea_appendErrorMessage("Integrity error");
            break;
        }

        this.rebuildChart();

        // Check if the worker is still computing. Stop if all terms have been computed.
        if (!isExecuting) {
            // Stop fetching, when all the data have been fetched.
            return;
        }

        // console.log("pull - before sleep");
        await sleep(30);
        // console.log("pull - after sleep");

        // Fetch more results
        await this.pullWorkerResults();
    }
  
    setupEditor() {
        const editor = CodeMirror.fromTextArea(document.getElementById("editor-inner"), {
            lineNumbers: true,
            lineWrapping: false,
            styleActiveLine: true,
            theme: "idea",
            mode: "loda",
            showTrailingSpace: true,
            tabSize: 2,
            indentWithTabs: false,
        });
        this.mEditor = editor;
    }
  
    setupChart() {
        const plugin_tooltip = {
            mode: 'point',
            callbacks: {
                label: function(context) {
                    const pointItem = context.raw;
                    var s = pointItem.label || '';
                    var is_string = (typeof s == 'string') || (s instanceof String);
                    if (is_string) {
                        return s;
                    } else {
                        return "x: " + pointItem.x + " y: " + pointItem.y;
                    }
                }
            },
        };
        const plugin_legend = {
            display: false,
        };
        const options = {
            animation: false,
            maintainAspectRatio: false,
            plugins: {
                tooltip: plugin_tooltip,
                legend: plugin_legend,
            }
        };
        // Dataset with invisible points
        const dataAll = this.chartEmptyData();
        const datasetAll = {
            pointRadius: 0,
            pointHitRadius: 0,
            borderWidth: 0,
            data: dataAll,
        };
        const data = {
            datasets: [
                datasetAll
            ]
        };
        const config = {
            type: 'scatter',
            data: data,
            options: options
        };
        var ctx = document.getElementById('output-chart').getContext('2d');
        this.mOutputChart = new Chart(ctx, config);
    }

    setupInteractiveArea() {
        this.mGraphScalingAuto = document.getElementById("graph-scaling-auto");
        this.mGraphScalingLinear = document.getElementById("graph-scaling-linear");
        this.mGraphScalingLogarithmic = document.getElementById("graph-scaling-logarithmic");

        this.mTermCount10 = document.getElementById("output-count-10");
        this.mTermCount100 = document.getElementById("output-count-100");
        this.mTermCount1000 = document.getElementById("output-count-1000");
    }
  
    hideOverlay() {
        document.getElementById("overlay").style.display = "none";
    }
  
    showOverlay() {
        document.getElementById("overlay").style.display = "block";
    }

    prepareProgram() {
        let params = new URLSearchParams(window.location.search);
        if (params.has('source')) {
            let sourceCode = params.get('source');
            this.prepareProgramSourceCode(sourceCode);
            return;
        }
        if (params.has('oeis')) {
            var programId = params.get('oeis');
            programId = programId.replace(/^A0*/i, '');
            this.prepareProgramId(programId);
            return;
        }
        console.log("Missing or unrecognized url parameters. Showing an empty program.");
        this.prepareProgramEmpty();
    }
  
    prepareProgramEmpty() {
        const sourceCode = "";
        this.mIdenticalToOriginal = true;
        this.mOriginalText = sourceCode;
        this.mEditor.setValue(sourceCode);
        this.mEditor.focus();
        this.didLoadProgram();
    }
  
    prepareProgramSourceCode(sourceCode) {
        console.log("prepareProgramSourceCode", sourceCode);
        this.mIdenticalToOriginal = true;
        this.mOriginalText = sourceCode;
        this.mEditor.setValue(sourceCode);
        this.mEditor.focus();
        this.didLoadProgram();
    }
  
    prepareProgramId(programId) {
        console.log("prepareProgramId", programId);
        let url = urlFromProgramId(programId);
        fetch(url)
            .then(response => {
                if (response.status == 404) {
                    const oeisId = oeisIdFromProgramId(programId);
                    throw new Error(`There exist no program for ${oeisId}`);
                }
                if (!response.ok) {
                    throw new Error(`Expected status 2xx, but got ${response.status}`);
                }
                return response.text();
            })
            .then(textdata => {
                console.log('Did fetch program');
                this.mIdenticalToOriginal = true;
                this.mOriginalText = textdata;
                this.mEditor.setValue(textdata);
                this.mEditor.focus();
                this.didLoadProgram();
            })
            .catch((error) => {
                console.error('Error:', error);
                const textdata = '';
                this.mIdenticalToOriginal = true;
                this.mOriginalText = textdata;
                this.mEditor.setValue(textdata);
                this.mEditor.focus();
                this.mWasUnableToFetchProgram = true;
                this.outputArea_showError(error);
                this.hideOverlay();
            });
    }
  
    async workerCompileAndExecute() {
        console.log("compile and execute");
        await this.setRange();
        const compileOk = await this.compileEditorCode();
        if (!compileOk) {
            return;
        }
        this.outputArea_clear();
        await this.executeRange();
    }

    didLoadProgram() {
        this.mDidLoadProgram = true;
        this.proceedIfAllThingsAreReady();
    }
  
    setupKeyboardShortcuts() {
        let self = this;
        let keydownHandler = function(event) {
            if(event.defaultPrevented) {
                return; // Should do nothing if the default action has been cancelled
            }
            const isMetaKey = event.metaKey || event.ctrlKey;
            const isEnterKeyCode = (event.keyCode == 10) || (event.keyCode == 13);
            const isEscapeKeyCode = (event.keyCode == 27);
            // intercept CTRL+ENTER, and run the program.
            if(isEnterKeyCode && isMetaKey) {
                console.log("ctrl+enter: submit form");
                event.preventDefault(); // Suppress "double action"
                self.runAction();
                return;
            }
            // intercept ESCape key, and stop a running program.
            if(isEscapeKeyCode) {
                console.log("escape: stop running");
                event.preventDefault(); // Suppress "double action"
                self.stopAction();
                return;
            }
        };
        window.addEventListener('keydown', keydownHandler, true);
    }
  
    runAction() {
        (async () => {
            await this.workerCompileAndExecute();
        })();
    }
  
    stopAction() {
        (async () => {
            await this.tellWorkerToStopExecuting();
        })();
    }
  
    showInfo() {
        console.log("Show info");
        window.open(
            "https://loda-lang.org/",
            '_blank' // Open in a new window.
        );
    }
  
    programURLString() {
        let sourceCode = this.mEditor.getValue();
        var url = new URL(window.location.href);
        url.search = `source=${encodeURIComponent(sourceCode)}`;
        return url.href;
    }
  
    copyProgramURLToClipboard() {
        let urlString = this.programURLString();
        navigator.clipboard.writeText(urlString);
        let byteCount = urlString.length;
        let tooltip = document.getElementById("copy-program-link-to-clipboard-tooltip-text");
        tooltip.innerHTML = `Copied ${byteCount} bytes to clipboard`;
    }
  
    hideCopyToClipboardTooltip() {
        let tooltip = document.getElementById("copy-program-link-to-clipboard-tooltip-text");
        tooltip.innerHTML = "Copy to clipboard";
    }

    chartEmptyData() {
        var count = 10;
        var dataAll = [];
        for ( var i = 0; i < count; i+=1 ) {
            const value = i;
            const y = Math.floor(value);
            const dict = {
                x: i,
                y: y,
                label: `a(${i}) = ${y}`
            };
            dataAll.push(dict);
        }
        return dataAll;
    }

    extractChartDataFromOutput() {
        var dataAll = [];
        const parentDiv = document.getElementById("output-inner");
        var children = parentDiv.children;
        var index = 0;
        for (var i = 0; i < children.length; i += 1) {
            const child = children[i];
            if (child.className != 'term') {
                // Ignore elements such as: separator, error
                continue;
            }
            const textItem = child.innerText;
            var value = parseInt(textItem);
            if (isNaN(value)) { 
                value = 0;
            }
            const y = Math.floor(value);
            const dict = {
                x: index,
                y: y,
                label: `a(${index}) = ${y}`
            };
            dataAll.push(dict);
            index += 1;
        }
        return dataAll;
    }
  
    rebuildChart() {
        var chart = this.mOutputChart;
        
        // const dataAll = this.chartEmptyData();
        var dataAll = this.extractChartDataFromOutput();
        if (dataAll.length < 1) {
            // console.log("length is empty");
            return;
        }

        var pointRadius = 1;
        if (dataAll.length <= 200) {
            pointRadius = 2;
        }
        if (dataAll.length <= 10) {
            pointRadius = 3;
        }

        const useLogarithmic = this.determineIfLogarithmShouldBeUsed(dataAll);

        var backgroundColor = 'rgba(25,25,25,1.0)';
        if (useLogarithmic) {
            var backgroundColorArray = [];
            var newDataAll = [];
            this.clampDataForLogarithmicChart(backgroundColorArray, newDataAll, dataAll);
            backgroundColor = backgroundColorArray;
            dataAll = newDataAll;
        }

        const datasetAll = {
            backgroundColor: backgroundColor,
            pointRadius: pointRadius,
            pointHitRadius: 5,
            borderWidth: 0,
            data: dataAll
        };
        
        while (chart.data.datasets.length > 0) {
            chart.data.datasets.pop();
        }
        chart.data.datasets.push(datasetAll);
        
        if (useLogarithmic) {
            chart.options.scales.y.type = 'logarithmic';
        } else {
            chart.options.scales.y.type = 'linear';
        }

        chart.update();
    }

    determineIfLogarithmShouldBeUsed(dataAll) {
        if (this.mScaleMode == ENUM_SCALEMODE_LINEAR) {
            return false;
        }
        if (this.mScaleMode == ENUM_SCALEMODE_LOGARITHMIC) {
            return true;
        }
        // The scale mode is `ENUM_SCALEMODE_AUTO`.

        if (dataAll.length < 1) {
            return false;
        }

        const dataItem = dataAll[0];
        var minY = dataItem.y;
        var maxY = dataItem.y;
        for (var i = 0; i < dataAll.length; i += 1) {
            const dataItem = dataAll[i];
            const y = dataItem.y;
            if (y < minY) {
                minY = y;
            }
            if (y > maxY) {
                maxY = y;
            }
        }
        const yRangeLength = maxY - minY + 1;
        if (minY <= 0) {
            // chartjs cannot do logarithmic plots with negative numbers nor zeros.
            return false;
        }
        if (yRangeLength < 50) {
            return false;
        }

        const binCount = 10;
        var bins = [];
        for (var i = 0; i < binCount; i += 1) {
            bins.push(0);
        }
        for (var i = 0; i < dataAll.length; i += 1) {
            const dataItem = dataAll[i];
            const y = dataItem.y - minY;
            const binIndex = Math.floor(y * (binCount-1) / yRangeLength);
            bins[binIndex] += 1;
        }
        const target0 = Math.floor(dataAll.length * 0.8);
        if (bins[0] > target0) {
            // More than 80% of the data appears to end up in one bin.
            // Logarithmic plot seems best for this.
            return true;
        }
        // Data appears to be spread out more evenly across the bins.
        // Linear plot seems best for this.
        return false;
    }

    // Problem: Zero and negative numbers cannot be used for log.
    // The values that works are 1 and higher.
    // Solution: This function clamps the troublesome values to 1, 
    // so the points show up on the log plot.
    clampDataForLogarithmicChart(backgroundColorArray, clampedDataArray, originalDataArray) {
        for (var i = 0; i < originalDataArray.length; i += 1) {
            const dataItem = originalDataArray[i];
            const y = dataItem.y;
            if (y < 1) {
                backgroundColorArray.push('rgba(255,25,25,1.0)');
                var newDataItem = {};
                Object.assign(newDataItem, dataItem);
                newDataItem.y = 1;
                clampedDataArray.push(newDataItem);
                continue;
            }
            backgroundColorArray.push('rgba(25,25,25,1.0)');
            clampedDataArray.push(dataItem);
        }
    }

    useAutoScalingAction() {
        this.mGraphScalingAuto.className = 'selected';
        this.mGraphScalingLinear.className = 'not-selected';
        this.mGraphScalingLogarithmic.className = 'not-selected';
        this.mScaleMode = ENUM_SCALEMODE_AUTO;
        this.rebuildChart();
    }

    useLinearScalingAction() {
        this.mGraphScalingAuto.className = 'not-selected';
        this.mGraphScalingLinear.className = 'selected';
        this.mGraphScalingLogarithmic.className = 'not-selected';
        this.mScaleMode = ENUM_SCALEMODE_LINEAR;
        this.rebuildChart();
    }

    useLogarithmicScalingAction() {
        this.mGraphScalingAuto.className = 'not-selected';
        this.mGraphScalingLinear.className = 'not-selected';
        this.mGraphScalingLogarithmic.className = 'selected';
        this.mScaleMode = ENUM_SCALEMODE_LOGARITHMIC;
        this.rebuildChart();
    }

    show10TermsAction() {
        if (this.mTermCount == 10) {
            return;
        }
        this.mTermCount = 10;
        this.mTermCount10.className = 'selected';
        this.mTermCount100.className = 'not-selected';
        this.mTermCount1000.className = 'not-selected';
        this.didUpdateTermCount();
    }
  
    show100TermsAction() {
        if (this.mTermCount == 100) {
            return;
        }
        this.mTermCount = 100;
        this.mTermCount10.className = 'not-selected';
        this.mTermCount100.className = 'selected';
        this.mTermCount1000.className = 'not-selected';
        this.didUpdateTermCount();
    }
  
    show1000TermsAction() {
        if (this.mTermCount == 1000) {
            return;
        }
        this.mTermCount = 1000;
        this.mTermCount10.className = 'not-selected';
        this.mTermCount100.className = 'not-selected';
        this.mTermCount1000.className = 'selected';
        this.didUpdateTermCount();
    }
  
    didUpdateTermCount() {
        (async () => {
            await this.workerCompileAndExecute();
        })();
    }
}
  
var gPageController = null;
  
function body_onload() {
    gPageController = new PageController();
}
  
function body_onbeforeunload() {
    if (gPageController.mIdenticalToOriginal) {
        return undefined;
    } else {
        return "The data on this page will be lost if you leave";
    }
}