Skip to content

Commit 0defb1d

Browse files
added graphs
1 parent 8911e01 commit 0defb1d

6 files changed

Lines changed: 246 additions & 19 deletions

File tree

src/assets/data/data.jsx

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/* eslint-disable max-len */
2+
export const dropdownArrays = [
3+
{value: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], label: 'Zero'},
4+
{value: [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], label: 'Ones'},
5+
{value: [1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0], label: 'Kamm'},
6+
{
7+
value: [
8+
0, 0.3826834323650898, 0.7071067811865475, 0.9238795325112867, 1, 0.9238795325112867, 0.7071067811865476, 0.3826834323650899, 0, -0.3826834323650898,
9+
-0.7071067811865475, -0.9238795325112867, -1, -0.9238795325112867, -0.7071067811865476, -0.3826834323650899,
10+
],
11+
label: 'Sinus',
12+
},
13+
];
14+
15+
export const initialArrays = {
16+
baseArray: [...dropdownArrays.find(({label}) => label === 'Sinus').value],
17+
zeros: [...dropdownArrays.find(({label}) => label === 'Zero').value],
18+
};

src/components/canvas.jsx

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import {useEffect} from 'react';
2+
import {drawFunction} from '../utils/graph';
3+
import PropTypes from 'prop-types';
4+
5+
6+
const Canvas = ( {id, canvasRef, array} ) => {
7+
useEffect(() => {
8+
drawFunction(canvasRef, array);
9+
}, [canvasRef, array]);
10+
11+
return (
12+
<>
13+
{/* <Dropdown id={ref} array={array} /> */}
14+
<canvas
15+
onContextMenu={(e) => (e.button === 2 ? e.preventDefault() : null)}
16+
id={id}
17+
ref={canvasRef}
18+
width={800}
19+
height={400}
20+
/>
21+
</>
22+
23+
);
24+
};
25+
26+
export default Canvas;
27+
28+
Canvas.propTypes = {
29+
id: PropTypes.number,
30+
canvasRef: PropTypes.string,
31+
array: PropTypes.array,
32+
};

src/components/dropdown.jsx

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
import {useEffect, useState, useRef} from 'react';
2+
import PropTypes from 'prop-types';
3+
4+
const Icon = () => {
5+
return (
6+
<svg height="20" width="20" viewBox="0 0 20 20">
7+
{/* eslint-disable-next-line max-len */}
8+
<path d="M4.516 7.548c0.436-0.446 1.043-0.481 1.576 0l3.908 3.747 3.908-3.747c0.533-0.481 1.141-0.446 1.574 0 0.436 0.445 0.408 1.197 0 1.615-0.406 0.418-4.695 4.502-4.695 4.502-0.217 0.223-0.502 0.335-0.787 0.335s-0.57-0.112-0.789-0.335c0 0-4.287-4.084-4.695-4.502s-0.436-1.17 0-1.615z"></path>
9+
</svg>
10+
);
11+
};
12+
13+
const Dropdown = ( {placeHolder, options, onChange} ) => {
14+
const [showMenu, setShowMenu] = useState(false);
15+
const [selectedValue, setSelectedValue] = useState(null);
16+
17+
const inputRef = useRef();
18+
19+
useEffect( () => {
20+
const handler = (e) => {
21+
if (inputRef.current && !inputRef.current.contains(e.target)) {
22+
setShowMenu(false);
23+
}
24+
};
25+
26+
window.addEventListener('click', handler);
27+
return () => {
28+
window.removeEventListener('click', handler);
29+
};
30+
});
31+
32+
const handleInputClick = (e) => {
33+
setShowMenu(!showMenu);
34+
};
35+
36+
const getDisplay = () => {
37+
if (selectedValue) {
38+
return selectedValue.label;
39+
}
40+
return placeHolder;
41+
};
42+
43+
const onItemClick = (option) => {
44+
setSelectedValue(option);
45+
const array = option.value;
46+
onChange(array);
47+
};
48+
49+
const isSelected = (option) => {
50+
if (!selectedValue) {
51+
return false;
52+
}
53+
54+
return selectedValue.value === option.value;
55+
};
56+
57+
return (
58+
<>
59+
<div className="dropdown--container">
60+
<div ref={inputRef} onClick={handleInputClick} className="dropdown--input">
61+
<div className="dropdown--selected-value">{getDisplay()}</div>
62+
<div className="dropdown--tools">
63+
<div className="dropdown--tool">
64+
<Icon />
65+
</div>
66+
</div>
67+
</div>
68+
{ showMenu && (
69+
<div className="dropdown--menu">
70+
{
71+
options.map((option) => (
72+
<div
73+
onClick={() => onItemClick(option) }
74+
key={option.value}
75+
className={`dropdown--item ${isSelected(option) && 'selected'}`}
76+
>
77+
{option.label}
78+
</div>
79+
))}
80+
</div>
81+
)}
82+
</div>
83+
</>
84+
);
85+
};
86+
87+
export default Dropdown;
88+
89+
Dropdown.propTypes = {
90+
onChange: PropTypes.func,
91+
options: PropTypes.object,
92+
placeHolder: PropTypes.string,
93+
};

src/pages/Home.jsx

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// import images
2-
import Logo from '../assets/images/logo.jpg';
2+
33

44
// import styles
55
import '../assets/scss/home.scss';
@@ -8,15 +8,12 @@ import '../assets/scss/home.scss';
88
* Home Component
99
* @return {Home} The sum of the two numbers.
1010
*/
11-
12-
1311
const Home = () => {
1412
return (
1513
<>
1614
<div className="container">
1715
<h1>Welcome To My App</h1>
1816
<p>This is going to be the coolest app in the world!</p>
19-
<img src={Logo} alt="Sammy Image" width={200} height={200} />
2017
</div>
2118
</>
2219
);

src/pages/Sampling.jsx

Lines changed: 92 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,100 @@
1+
// import react
2+
import {useState, useRef, useId} from 'react';
3+
4+
// import Components
5+
import Dropdown from '../components/dropdown';
6+
import Canvas from '../components/canvas';
7+
8+
// import data
9+
import {initialArrays, dropdownArrays} from '../assets/data/data';
10+
11+
// import functions
12+
import * as Calculate from '../utils/calculations';
13+
14+
// import styling
115
import '../assets/scss/sampling.scss';
216

17+
318
const Sampling = () => {
19+
// set IDs
20+
const canvas1ID = useId();
21+
const canvas2ID = useId();
22+
const canvas3ID = useId();
23+
const canvas4ID = useId();
24+
25+
// set Refs
26+
const canvas1Ref = useRef();
27+
const canvas2Ref = useRef();
28+
const canvas3Ref = useRef();
29+
const canvas4Ref = useRef();
30+
31+
// set array states
32+
const [array1, setArray1] = useState([...initialArrays.baseArray]);
33+
const [array2, setArray2] = useState([...initialArrays.zeros]);
34+
const [array3, setArray3] = useState([...initialArrays.zeros]);
35+
const [array4, setArray4] = useState([...initialArrays.zeros]);
36+
37+
38+
const handleButtonClick = () => {
39+
setArray2([...Calculate.calculateSampling(array2)]);
40+
};
41+
42+
43+
const dropDownHandler1 = (selectedArray) => {
44+
// deep clone
45+
const array = [...selectedArray];
46+
47+
// set vaalue of selected dropdown item to this canvas
48+
setArray1([...array]);
49+
50+
// calculate FourierTransformation
51+
const fourierTransform = Calculate.calculateFourierTransformation(array);
52+
setArray2([...fourierTransform]);
53+
};
54+
55+
const dropDownHandler2 = (selectedArray) => {
56+
// set vaalue of selected dropdown item to this canvas
57+
const array = [...selectedArray]; // deep clone
58+
setArray3([...array]);
59+
60+
// calculate FourierTransformation
61+
const fourierTransform = Calculate.calculateSampling(array);
62+
setArray4([...fourierTransform]);
63+
};
64+
465
return (
566
<>
6-
<head>
7-
<title>TEST</title>
8-
<meta name="description" content="BLABLA" />
9-
</head>
10-
<h1>My App</h1>
67+
<div>
68+
Array1
69+
<Dropdown
70+
placeHolder="Select..."
71+
options={dropdownArrays}
72+
onChange={(selectedArray) => dropDownHandler1(selectedArray)}
73+
/>
74+
<Canvas id={canvas1ID} canvasRef={canvas1Ref} array={array1} />
75+
</div>
76+
77+
<div>
78+
Array2
79+
<Canvas id={canvas2ID} canvasRef={canvas2Ref} array={array2} />
80+
</div>
81+
82+
<div>
83+
Array3
84+
<Dropdown
85+
placeHolder="Select..."
86+
options={dropdownArrays}
87+
onChange={(selectedArray) => dropDownHandler2(selectedArray)}
88+
/>
89+
<Canvas id={canvas3ID} canvasRef={canvas3Ref} array={array3} />
90+
</div>
91+
92+
<div>
93+
Array4
94+
<Canvas id={canvas4ID} canvasRef={canvas4Ref} array={array4} />
95+
</div>
96+
97+
<button onClick={handleButtonClick}>CLICK</button>
1198
</>
1299
);
13100
};

src/utils/graph.js

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
// just drawing no return
22
// neu schreiben
33
export const drawFunction = (canvasID, array) => {
4-
let centeredZero = true;
5-
let line = true;
6-
let canvasArray = array;
4+
const centeredZero = true;
5+
const line = true;
6+
const canvasArray = array;
77
let canvas;
88
let ctx;
99
if (canvasID.current) {
@@ -63,7 +63,7 @@ export const drawFunction = (canvasID, array) => {
6363

6464
ctx.beginPath();
6565
ctx.lineWidth = 2;
66-
let grad = ctx.createLinearGradient(i, 0, i, yposition);
66+
const grad = ctx.createLinearGradient(i, 0, i, yposition);
6767
grad.addColorStop(0, 'hsl(176, 72%, 71%)');
6868
grad.addColorStop(1, 'hsl(251, 53%, 45%)');
6969
ctx.strokeStyle = grad;
@@ -91,7 +91,7 @@ export const drawFunction = (canvasID, array) => {
9191
// draw circles
9292
ctx.beginPath();
9393
ctx.arc(i, yposition, 3, 0, 2 * Math.PI);
94-
let centered = centeredZero ? canvasArray.length / 2 : 1;
94+
const centered = centeredZero ? canvasArray.length / 2 : 1;
9595
// Buffer circles transparent
9696
if (count == 0 || count == canvasArray.length || count == canvasArray.length - 1) {
9797
ctx.beginPath();
@@ -138,11 +138,11 @@ export const drawTest = (canvasID, funcArray) => {
138138
canvas.height = canvasHeight;
139139

140140
// Skalierungsfaktoren berechnen
141-
const xMin = -canvasWidth / 2;
142-
const xMax = canvasWidth / 2;
143-
const xRange = xMax - xMin;
141+
// const xMin = -canvasWidth / 2;
142+
// const xMax = canvasWidth / 2;
143+
// const xRange = xMax - xMin;
144144
const yRange = Math.max(...funcArray) - Math.min(...funcArray);
145-
const xScale = canvasWidth / xRange;
145+
// const xScale = canvasWidth / xRange;
146146
const yScale = canvasHeight / yRange;
147147

148148
// Zeichnen der Achsen
@@ -158,7 +158,7 @@ export const drawTest = (canvasID, funcArray) => {
158158
ctx.beginPath();
159159
ctx.moveTo(0, canvasHeight / 2 - (funcArray[0] - Math.min(...funcArray)) * yScale);
160160
for (let i = 1; i < canvasWidth; i++) {
161-
const x = xMin + i / xScale;
161+
// const x = xMin + i / xScale;
162162
const y = funcArray[i] - Math.min(...funcArray);
163163
ctx.lineTo(i, canvasHeight / 2 - y * yScale);
164164
}

0 commit comments

Comments
 (0)