-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
145 lines (122 loc) · 4.1 KB
/
index.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
let isShowError = false;
const getDerivative = (input) => {
try {
return math.derivative(input, 'x').toString()
} catch (error) {
console.log(error)
return false;
}
}
const getResult = (input, myX) => {
try {
return math.evaluate(input.replace(/x/g, myX))
} catch (error) {
console.log(error)
return false;
}
}
const showLine = () => {
const dataNormal = []
const dataDerivative = []
const table = document.getElementById('table:result').getElementsByTagName('tr')
for (let index = 0; index < table.length; index++) {
const element = table[index].getElementsByTagName('td')
const point = {
x: element[0].getElementsByTagName('input')[0].value,
y: element[2].innerText
}
dataDerivative.push(point)
}
const input = document.getElementById('txt:input').value;
for (let index = 0; index < table.length; index++) {
const element = table[index].getElementsByTagName('td')
const x = element[0].getElementsByTagName('input')[0].value
const point = {
x,
y: getResult(input, x)
}
dataNormal.push(point)
}
const config = {
type: 'line',
data: {
labels: dataDerivative.map(point => point.x),
datasets: [{
label: 'Linha da derivada',
backgroundColor: "#059e65",
borderColor: "#32d296",
fill: false,
data: dataDerivative,
},
{
label: 'Linha da original',
backgroundColor: "#0e6dcd",
borderColor: "#3c99f5",
fill: false,
data: dataNormal,
}
]
},
options: {
responsive: true,
scales: {
xAxes: [{
display: true,
}],
yAxes: [{
display: true,
stacked: true
}]
}
}
};
const view = document.getElementById('div:line')
const canvas = document.createElement('canvas')
view.innerHTML = ''
view.appendChild(canvas)
new Chart(canvas, config)
}
const liveUpdate = (event) => {
const {
value
} = event.srcElement
const textInput = event.path[2].getElementsByTagName('td')[1].innerText
event.path[2].getElementsByTagName('td')[2].innerText = getResult(textInput, value)
showLine()
}
const main = () => {
const input = document.getElementById('txt:input').value
if (input.length <= 0) {
return alert('Campo vazio!')
}
document.getElementById('hidden').className = ''
const derivative = getDerivative(input)
const table = document.getElementById('table:result')
table.innerHTML = ''
for (let index = -3; index <= 3; index++) {
const lineDocument = document.createElement('tr')
const xTD = document.createElement('td')
const textX = document.createElement('input')
const textInput = document.createElement('td')
const textY = document.createElement('td')
textX.value = index
textInput.innerText = derivative
textY.innerText = getResult(derivative, index)
textX.addEventListener('input', liveUpdate)
textX.min = -99999
textX.max = 99999
textX.type = 'number'
xTD.appendChild(textX)
lineDocument.appendChild(xTD)
lineDocument.appendChild(textInput)
lineDocument.appendChild(textY)
table.appendChild(lineDocument)
}
if (derivative === false) {
document.getElementById('hidden').className = 'uk-hidden'
} else {
showLine()
}
document.getElementById('p:result').innerText = `Derivada de "${input}" é "${derivative}".`
}
document.getElementById('bt:load').addEventListener('click', main, false)