-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.mjs
74 lines (65 loc) · 1.93 KB
/
main.mjs
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
import { writeDyadic } from "./fraction-converter.mjs";
import { getVertices } from "./group.mjs";
const inputWord = document.getElementById("word");
const validateBtn = document.getElementById("validate");
const validityMsg = document.getElementById("validity");
const wordInTeX = document.getElementById("wordInTex");
let word;
const re = /^\s*([ab](\^\{-?[1-9]\d*\})?\s*)*$/;
validateBtn.addEventListener("click", () => {
if (re.test(inputWord.value)) {
word = inputWord.value.trim();
validityMsg.textContent = "Your input: ";
wordInTeX.textContent =
word != "" ? `g = ${word}` : `g = \\mathrm{identity}`;
plotBtn.removeAttribute("disabled");
} else {
validityMsg.textContent = "Invalid input!";
wordInTeX.textContent = "";
plotBtn.setAttribute("disabled", "true");
}
});
const plotBtn = document.getElementById("plot");
const chart = document.getElementById("chart");
const commonFrBtn = document.getElementById("commonFr");
const decimalBtn = document.getElementById("decimalFr");
let option = "common";
for (const button of [commonFrBtn, decimalBtn]) {
button.addEventListener("change", (event) => {
option = event.target.value;
});
}
plotBtn.addEventListener("click", () => {
plotElement(word, option);
});
function plotElement(string, opt) {
let vertices = getVertices(string);
let data = {
x: vertices.map((v) => v[0]),
y: vertices.map((v) => v[1]),
type: "scatter",
mode: "lines+markers",
};
if (opt == "common") {
data.text = vertices.map(
(v) => `(${writeDyadic(v[0])}, ${writeDyadic(v[1])})`
);
data.hovertemplate = "%{text}<extra></extra>";
}
if ((opt = "decimal")) {
data.hoverinfo = "x+y";
}
const layout = {
title: "Graph of g",
xaxis: {
title: "argument",
hoverformat: ".12~e",
},
yaxis: {
title: "value",
hoverformat: ".12~e",
},
hovermode: "closest",
};
Plotly.newPlot(chart, [data], layout);
}