Skip to content

Commit 023c0ba

Browse files
author
Mousavi Jalali
committed
Added circles and a line svg elements using D3 to control the DOM
1 parent 5208608 commit 023c0ba

12 files changed

+387
-246
lines changed

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
"@testing-library/jest-dom": "^4.2.4",
77
"@testing-library/react": "^9.3.2",
88
"@testing-library/user-event": "^7.1.2",
9+
"d3": "^5.16.0",
910
"react": "^16.13.1",
1011
"react-dom": "^16.13.1",
1112
"react-scripts": "3.4.1"

src/App.css

+2-37
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,3 @@
1-
.App {
2-
text-align: center;
3-
}
4-
5-
.App-logo {
6-
height: 40vmin;
7-
pointer-events: none;
8-
}
9-
10-
@media (prefers-reduced-motion: no-preference) {
11-
.App-logo {
12-
animation: App-logo-spin infinite 20s linear;
13-
}
14-
}
15-
16-
.App-header {
17-
background-color: #282c34;
18-
min-height: 100vh;
19-
display: flex;
20-
flex-direction: column;
21-
align-items: center;
22-
justify-content: center;
23-
font-size: calc(10px + 2vmin);
24-
color: white;
25-
}
26-
27-
.App-link {
28-
color: #61dafb;
29-
}
30-
31-
@keyframes App-logo-spin {
32-
from {
33-
transform: rotate(0deg);
34-
}
35-
to {
36-
transform: rotate(360deg);
37-
}
1+
svg {
2+
background: #eee;
383
}

src/App.js

+33-21
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,38 @@
1-
import React from 'react';
2-
import logo from './logo.svg';
3-
import './App.css';
1+
import React, { useRef, useEffect, useState, Fragment } from 'react'
2+
import './App.css'
3+
4+
import Circles from './components/Circles'
5+
import Line from './components/Line'
6+
7+
48

59
function App() {
10+
611
return (
7-
<div className="App">
8-
<header className="App-header">
9-
<img src={logo} className="App-logo" alt="logo" />
10-
<p>
11-
Edit <code>src/App.js</code> and save to reload.
12-
</p>
13-
<a
14-
className="App-link"
15-
href="https://reactjs.org"
16-
target="_blank"
17-
rel="noopener noreferrer"
18-
>
19-
Learn React
20-
</a>
21-
</header>
22-
</div>
23-
);
12+
<Fragment>
13+
{/* <Circles/> */}
14+
<Line/>
15+
</Fragment>
16+
)
2417
}
2518

26-
export default App;
19+
export default App
20+
21+
22+
23+
24+
// In pure React, to draw a connected line is svg, can use the line element or the path element
25+
//path element is more powerful and takes the attribute d, which defines how your path is drawn, where it starts and ends etc:
26+
//e.g. if you want it to start at 0 and go to 150, then 100 to 100, then 150,120
27+
28+
// function App() {
29+
// return(
30+
// <React.Fragment>
31+
// <svg>
32+
// <path d='M0,150 100,100 150,120' stoke='blue' fill='none' />
33+
// </svg>
34+
// </React.Fragment>
35+
// )
36+
// }
37+
38+

src/App.test.js

-9
This file was deleted.

src/components/Circles.jsx

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import React, { useRef, useEffect, useState, Fragment } from 'react'
2+
import { select } from 'd3'
3+
4+
5+
6+
function Circles() {
7+
8+
const [data, setData] = useState([25, 30, 45, 60, 20])
9+
10+
const svgRef = useRef()
11+
useEffect(() => {
12+
const svg = select(svgRef.current)
13+
svg
14+
.selectAll('circle')
15+
.data(data)
16+
.join('circle')
17+
.attr('r', value => value)
18+
.attr('cx', value => value * 2 )
19+
.attr('cy', value => value * 2 )
20+
.attr('stroke', 'red')
21+
}, [data])
22+
23+
return (
24+
<Fragment>
25+
<svg ref={svgRef}></svg>
26+
<br />
27+
<button onClick={() => setData(data.map(value => value + 5))}>Update Data</button>
28+
<button onClick={() => setData(data.filter(value => value < 35 ))}>filter Data</button>
29+
</Fragment>
30+
)
31+
32+
}
33+
34+
export default Circles

src/components/Line.jsx

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import React, { useEffect, useRef, Fragment, useState } from 'react'
2+
import { select, line, curveCardinal } from 'd3'
3+
4+
5+
const Line = () => {
6+
7+
const [data, setData] = useState([25, 30, 45, 60, 20, 65, 75])
8+
const svgRef = useRef()
9+
10+
useEffect(() => {
11+
const svg = select(svgRef.current)
12+
const myLine = line()
13+
.x((value, index) => index * 50 )
14+
.y((value => 150 - value))
15+
.curve(curveCardinal)
16+
17+
svg
18+
.selectAll('path')
19+
.data([data])
20+
.join('path')
21+
.attr('d', value => myLine(value))
22+
.attr('fill', 'none')
23+
.attr('stroke', 'blue')
24+
}, [data])
25+
26+
return (
27+
<Fragment>
28+
<svg ref={svgRef}></svg>
29+
<br />
30+
<button onClick={() => setData(data.map(value => value + 5))}>Update Data</button>
31+
<button onClick={() => setData(data.filter(value => value < 35 ))}>filter Data</button>
32+
</Fragment>
33+
)
34+
}
35+
36+
export default Line
37+
38+
39+
40+
// In pure React, to draw a connected line is svg, can use the line element or the path element
41+
// path element is more powerful and takes the attribute d, which defines how your path is drawn, where it starts and ends etc so you give it data points. Each data point has an x and a y value thus the line is drawn accordingly:
42+
// e.g. first data point 0,150
43+
// Second data point is at 100,100
44+
// Third data point is at 150,20:
45+
46+
// function App() {
47+
// return(
48+
// <React.Fragment>
49+
// <svg>
50+
// <path d='M0,150 100,100 150,120' stoke='blue' fill='none' />
51+
// </svg>
52+
// </React.Fragment>
53+
// )
54+
// }
55+

src/index.css

-13
This file was deleted.

src/index.js

+7-11
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,13 @@
1-
import React from 'react';
2-
import ReactDOM from 'react-dom';
3-
import './index.css';
4-
import App from './App';
5-
import * as serviceWorker from './serviceWorker';
1+
import React from 'react'
2+
import { render } from 'react-dom'
3+
import App from './App'
64

7-
ReactDOM.render(
5+
6+
render(
87
<React.StrictMode>
98
<App />
109
</React.StrictMode>,
1110
document.getElementById('root')
12-
);
11+
)
12+
1313

14-
// If you want your app to work offline and load faster, you can change
15-
// unregister() to register() below. Note this comes with some pitfalls.
16-
// Learn more about service workers: https://bit.ly/CRA-PWA
17-
serviceWorker.unregister();

src/logo.svg

-7
This file was deleted.

src/serviceWorker.js

-141
This file was deleted.

0 commit comments

Comments
 (0)