-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathcalculator.js
147 lines (129 loc) · 4.72 KB
/
calculator.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
$(document).ready(function() {
let chart;
$('#calculatorForm').submit(function(e) {
e.preventDefault();
let equations = $('#equations').val().split(','); // Split equations by comma
let minX = parseFloat($('#minX').val());
let maxX = parseFloat($('#maxX').val());
let selectedGraphType = $('#graphType').val(); // Read the selected graph type from the dropdown
if (isNaN(minX) || isNaN(maxX) || minX >= maxX) {
alert('Invalid range. Please check your input.');
return;
}
let canvas = document.getElementById('graph').getContext('2d');
let xValues = [];
let datasets = [];
for (let equation of equations) {
equation = equation.trim(); // Remove leading/trailing spaces
let yValues = [];
try {
for (let x = minX; x <= maxX; x += 0.1) {
xValues.push(x);
// Replace "^" with "**" and append "Math." to mathematical expressions in the equation
let equationForEval = equation;
equationForEval = equationForEval.replace(/(sin|cos|tan|sqrt|log|exp|pi|e)(?=\()|pi|e/g, 'Math.$&');
equationForEval = equationForEval.replace(/\^/g, '**');
let result = eval(equationForEval);
// Check if the result is a valid number, not NaN
if (!isNaN(result)) {
yValues.push(result);
} else {
// Handle the case where the equation is invalid
throw new Error('Invalid Equation');
}
}
datasets.push({
label: equation,
data: yValues,
borderColor: getRandomColor(),
borderWidth: 2,
});
} catch (error) {
// Handle the error and provide feedback to the user
console.error(error);
alert(`Invalid equation: "${equation}". Please check your input.`);
return;
}
}
if (chart) chart.destroy();
chart = new Chart(canvas, {
type: selectedGraphType,
data: {
labels: xValues,
datasets: datasets,
},
options: {
responsive: true,
maintainAspectRatio: false,
scales: {
x: {
display: true,
title: {
display: true,
text: 'X'
}
},
y: {
display: true,
title: {
display: true,
text: 'Y'
}
}
}
}
});
$('.graphic-container').addClass('active');
});
$('#resetBtn').on('click', () => {
if (chart) chart.destroy();
$('.graphic-container').removeClass('active');
});
});
function getRandomColor() {
// Generates a random hexadecimal color
return '#' + Math.floor(Math.random()*16777215).toString(16);
}
document.body.style="background-color: var(--bs-dark);transition: 0.5s;"
const sun = "https://www.uplooder.net/img/image/55/7aa9993fc291bc170abea048589896cf/sun.svg";
const moon = "https://www.uplooder.net/img/image/2/addf703a24a12d030968858e0879b11e/moon.svg"
var theme = "dark";
const root = document.querySelector(":root");
const container = document.getElementsByClassName("theme-container")[0];
const themeIcon = document.getElementById("theme-icon");
container.addEventListener("click", setTheme);
function setTheme() {
switch (theme) {
case "dark":
setLight();
theme = "light";
break;
case "light":
setDark();
theme = "dark";
break;
}
}
function setLight() {
root.style.setProperty(
"--bs-dark",
"linear-gradient(318.32deg, #c3d1e4 0%, #dde7f3 55%, #d4e0ed 100%)"
);
container.classList.remove("shadow-dark");
setTimeout(() => {
container.classList.add("shadow-light");
themeIcon.classList.remove("change");
}, 300);
themeIcon.classList.add("change");
themeIcon.src = sun;
}
function setDark() {
root.style.setProperty("--bs-dark", "#000");
container.classList.remove("shadow-light");
setTimeout(() => {
container.classList.add("shadow-dark");
themeIcon.classList.remove("change");
}, 300);
themeIcon.classList.add("change");
themeIcon.src = moon;
}