Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit cfe97b5

Browse files
create bar chart
Signed-off-by: Amit Amrutiya <amitamrutiya2210@gmail.com>
1 parent 0fe1809 commit cfe97b5

File tree

6 files changed

+245
-0
lines changed

6 files changed

+245
-0
lines changed

‎React/bar-chart/package.json‎

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
{
2+
"name": "faq-comp",
3+
"version": "0.1.0",
4+
"private": true,
5+
"dependencies": {
6+
"@testing-library/jest-dom": "^5.17.0",
7+
"@testing-library/react": "^13.4.0",
8+
"@testing-library/user-event": "^13.5.0",
9+
"react": "^18.2.0",
10+
"react-dom": "^18.2.0",
11+
"react-scripts": "5.0.1",
12+
"web-vitals": "^2.1.4"
13+
},
14+
"scripts": {
15+
"start": "react-scripts start",
16+
"build": "react-scripts build",
17+
"test": "react-scripts test",
18+
"eject": "react-scripts eject"
19+
},
20+
"eslintConfig": {
21+
"extends": [
22+
"react-app",
23+
"react-app/jest"
24+
]
25+
},
26+
"browserslist": {
27+
"production": [
28+
">0.2%",
29+
"not dead",
30+
"not op_mini all"
31+
],
32+
"development": [
33+
"last 1 chrome version",
34+
"last 1 firefox version",
35+
"last 1 safari version"
36+
]
37+
}
38+
}

‎React/bar-chart/public/favicon.ico‎

3.78 KB
Binary file not shown.

‎React/bar-chart/public/index.html‎

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="utf-8" />
5+
<link rel="icon" href="%PUBLIC_URL%/favicon.ico" />
6+
<meta name="viewport" content="width=device-width, initial-scale=1" />
7+
<meta name="theme-color" content="#000000" />
8+
<meta
9+
name="description"
10+
content="Web site created using create-react-app"
11+
/>
12+
<link rel="apple-touch-icon" href="%PUBLIC_URL%/logo192.png" />
13+
<!--
14+
manifest.json provides metadata used when your web app is installed on a
15+
user's mobile device or desktop. See https://developers.google.com/web/fundamentals/web-app-manifest/
16+
-->
17+
<link rel="manifest" href="%PUBLIC_URL%/manifest.json" />
18+
<!--
19+
Notice the use of %PUBLIC_URL% in the tags above.
20+
It will be replaced with the URL of the `public` folder during the build.
21+
Only files inside the `public` folder can be referenced from the HTML.
22+
23+
Unlike "/favicon.ico" or "favicon.ico", "%PUBLIC_URL%/favicon.ico" will
24+
work correctly both with client-side routing and a non-root public URL.
25+
Learn how to configure a non-root public URL by running `npm run build`.
26+
-->
27+
<title>React App</title>
28+
</head>
29+
<body>
30+
<noscript>You need to enable JavaScript to run this app.</noscript>
31+
<div id="root"></div>
32+
<!--
33+
This HTML file is a template.
34+
If you open it directly in the browser, you will see an empty page.
35+
36+
You can add webfonts, meta tags, or analytics to this file.
37+
The build step will place the bundled scripts into the <body> tag.
38+
39+
To begin the development, run `npm start` or `yarn start`.
40+
To create a production bundle, use `npm run build` or `yarn build`.
41+
-->
42+
</body>
43+
</html>

‎React/bar-chart/src/App.js‎

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
import { useEffect } from "react";
2+
import { useState } from "react";
3+
4+
function App() {
5+
const [freq, setFreq] = useState(undefined);
6+
const [yAxis, setYAxis] = useState([]);
7+
const fetchNumbers = async () => {
8+
const url =
9+
"https://www.random.org/integers/?num=200&min=1&max=10&col=1&base=10&format=plain&rnd=new";
10+
const res = await fetch(url);
11+
let data = await res.text();
12+
data = data.split("\n").filter(Boolean);
13+
console.log(data);
14+
const map = {};
15+
data?.forEach((item) => {
16+
if (map[item]) {
17+
map[item] = map[item] + 1;
18+
} else {
19+
map[item] = 1;
20+
}
21+
});
22+
setFreq(map);
23+
};
24+
25+
console.log(freq);
26+
27+
//preparing y-axis data
28+
// [30,20,10,0]
29+
useEffect(() => {
30+
if (freq) {
31+
const max = Math.max(...Object.values(freq));
32+
const maxVal = Math.ceil(max / 10) * 10;
33+
let arr = [];
34+
for (let i = maxVal / 10; i >= 0; i--) {
35+
//3,2,1,0
36+
arr.push(i * 10); //30,20,10,0
37+
}
38+
setYAxis(arr);
39+
}
40+
}, [freq]);
41+
42+
console.log("yAxis ", yAxis);
43+
44+
useEffect(() => {
45+
fetchNumbers();
46+
}, []);
47+
48+
return (
49+
<div className="App">
50+
<div className="container">
51+
<div className="box">
52+
<div
53+
className="box-y-axis"
54+
style={{
55+
height: `${yAxis[0]}%`,
56+
}}
57+
>
58+
{yAxis?.map((val, idx) => (
59+
<div key={idx}>
60+
<span>{val}</span>
61+
</div>
62+
))}
63+
</div>
64+
65+
{freq &&
66+
Object.entries(freq)?.map(([key, val]) => (
67+
<div className="box-x-axis">
68+
<div
69+
style={{
70+
height: `${val}%`,
71+
}}
72+
className="graph"
73+
></div>
74+
<div className="index">{key}</div>
75+
</div>
76+
))}
77+
</div>
78+
</div>
79+
</div>
80+
);
81+
}
82+
83+
export default App;

‎React/bar-chart/src/index.css‎

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
body {
2+
margin: 0;
3+
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', 'Oxygen',
4+
'Ubuntu', 'Cantarell', 'Fira Sans', 'Droid Sans', 'Helvetica Neue',
5+
sans-serif;
6+
-webkit-font-smoothing: antialiased;
7+
-moz-osx-font-smoothing: grayscale;
8+
}
9+
10+
code {
11+
font-family: source-code-pro, Menlo, Monaco, Consolas, 'Courier New',
12+
monospace;
13+
}
14+
15+
16+
.App {
17+
height: 100vh;
18+
width: 100%;
19+
}
20+
21+
.container{
22+
width: 100%;
23+
height: 100%;
24+
display: flex;
25+
flex-direction: column;
26+
align-items: center;
27+
justify-content: center;
28+
}
29+
30+
.box{
31+
display: flex;
32+
border-left: 1px solid black;
33+
border-bottom: 1px solid black;
34+
width: 50%;
35+
height: 70%;
36+
position: relative;
37+
}
38+
39+
.box-y-axis{
40+
display: flex;
41+
flex-direction: column;
42+
position: absolute;
43+
bottom: 0;
44+
left: -32px;
45+
justify-content: space-between;
46+
align-items: center;
47+
}
48+
49+
.box-x-axis{
50+
flex: 1;
51+
text-align: center;
52+
position: relative;
53+
display: flex;
54+
}
55+
56+
.graph{
57+
width: 20px;
58+
background-color: lightseagreen;
59+
transform: translateX((-50%));
60+
position: absolute;
61+
bottom: 0;
62+
left: 50%;
63+
}
64+
65+
.index{
66+
position: absolute;
67+
bottom: -7%;
68+
align-self: center;
69+
left: 45%;
70+
}

‎React/bar-chart/src/index.js‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import React from 'react';
2+
import ReactDOM from 'react-dom/client';
3+
import './index.css';
4+
import App from './App';
5+
6+
const root = ReactDOM.createRoot(document.getElementById('root'));
7+
root.render(
8+
<React.StrictMode>
9+
<App />
10+
</React.StrictMode>
11+
);

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /