forked from nervosnetwork/ckb-explorer-frontend
-
Notifications
You must be signed in to change notification settings - Fork 6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: add node geo distribution chart #283
Merged
Merged
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
350702b
feat: add node geo distribution chart
Keith-CY 2586189
feat: use rgbpp sdk to parse btc time lock
Keith-CY 94dfaec
Merge branch 'develop' into add-node-geo-distribution
Keith-CY 4280e0c
feat: add city label
Keith-CY 3889408
refactor: update code style
Keith-CY File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
172 changes: 172 additions & 0 deletions
172
src/pages/StatisticsChart/mining/NodeGeoDistribution.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,172 @@ | ||
import { useEffect, useRef } from 'react' | ||
import { useQuery } from '@tanstack/react-query' | ||
import 'echarts/lib/component/tooltip' | ||
import 'echarts-gl' | ||
import echarts from 'echarts/lib/echarts' | ||
import Loading from '../../../components/Loading/SmallLoading' | ||
import { getPeers, RawPeer } from '../../../services/NodeProbService' | ||
import { getPrimaryColor, IS_MAINNET } from '../../../constants/common' | ||
|
||
type Point = [long: number, lat: number, city: string] | ||
type Line = [Point, Point] | ||
|
||
const fetchData = async (): Promise<{ lines: Line[]; points: Point[] }> => { | ||
const list: RawPeer[] = await getPeers() | ||
const points: Point[] = list.map(peer => [peer.longitude, peer.latitude, peer.city]) | ||
const lines: Line[] = [] | ||
for (let i = 0; i < points.length - 1; i++) { | ||
for (let j = i + 1; j < points.length; j++) { | ||
const p1: Point = points[i] | ||
const p2: Point = points[j] | ||
lines.push([p1, p2]) | ||
} | ||
} | ||
|
||
return { lines, points } | ||
} | ||
|
||
const option = { | ||
backgroundColor: '#000', | ||
tooltip: { | ||
show: true, | ||
formatter: (params: { data: Point }) => { | ||
return params.data[2] | ||
}, | ||
}, | ||
globe: { | ||
environment: '/images/chart/dark.webp', | ||
baseTexture: '/images/chart/earth.jpg', | ||
heightTexture: '/images/chart/earth.jpg', | ||
displacementScale: 0.04, | ||
displacementQuality: 'high', | ||
shading: 'realistic', | ||
realisticMaterial: { | ||
roughness: 0.9, | ||
metalness: 0, | ||
}, | ||
temporalSuperSampling: { | ||
enable: true, | ||
}, | ||
postEffect: { | ||
enable: true, | ||
depthOfField: { | ||
enable: false, | ||
focalDistance: 150, | ||
}, | ||
}, | ||
light: { | ||
main: { | ||
intensity: 10, | ||
shadow: true, | ||
time: new Date(0x16e70e6985c), // launch time of CKB mainnet | ||
}, | ||
}, | ||
viewControl: { | ||
autoRotate: true, | ||
autoRotateSpeed: 1, | ||
distance: 800, | ||
}, | ||
silent: true, | ||
}, | ||
} | ||
|
||
const color = getPrimaryColor() | ||
|
||
export const NodeGeoDistribution = ({ isThumbnail = false }: { isThumbnail?: boolean }) => { | ||
const containerRef = useRef<HTMLDivElement | null>(null) | ||
const { data, isLoading } = useQuery(['node distribution'], fetchData, { enabled: !isThumbnail }) | ||
|
||
useEffect(() => { | ||
if (!containerRef.current) return | ||
if (!data) return | ||
let ins = echarts.getInstanceByDom(containerRef.current) | ||
if (!ins) { | ||
ins = echarts.init(containerRef.current) | ||
} | ||
|
||
const series = [ | ||
{ | ||
type: 'lines3D', | ||
name: 'blocks', | ||
coordinateSystem: 'globe', | ||
blendMode: 'lighter', | ||
symbolSize: 2, | ||
itemStyle: { | ||
color, | ||
opacity: 0.1, | ||
}, | ||
effect: { | ||
show: true, | ||
trailWidth: 1, | ||
trailLength: 0.15, | ||
trailOpacity: 0.1, | ||
constantSpeed: 10, | ||
}, | ||
lineStyle: { | ||
width: 1, | ||
color, | ||
opacity: 0.02, | ||
}, | ||
data: data.lines, | ||
}, | ||
{ | ||
type: 'scatter3D', | ||
coordinateSystem: 'globe', | ||
blendMode: 'lighter', | ||
symbolSize: 10, | ||
itemStyle: { | ||
color, | ||
opacity: 0.2, | ||
}, | ||
label: { | ||
show: true, | ||
formatter: '{b}', | ||
}, | ||
data: data.points, | ||
}, | ||
] | ||
|
||
ins.setOption({ | ||
...option, | ||
series, | ||
} as any) | ||
}, [data]) | ||
|
||
useEffect(() => { | ||
if (!containerRef.current) return | ||
const ins = echarts.getInstanceByDom(containerRef.current) | ||
const handleResize = () => { | ||
if (ins) { | ||
ins.resize() | ||
} | ||
} | ||
window.addEventListener('resize', handleResize) | ||
return () => { | ||
window.removeEventListener('resize', handleResize) | ||
} | ||
}) | ||
|
||
if (isThumbnail) { | ||
return ( | ||
<div | ||
style={{ | ||
width: '280px', | ||
height: 200, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use both number or both string maybe better here I think. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Updated by 3889408 |
||
background: `center / cover url(/images/chart/geo_cover_${IS_MAINNET ? 'mainnet' : 'testnet'}.png)`, | ||
}} | ||
/> | ||
) | ||
} | ||
|
||
if (isLoading) { | ||
return <Loading /> | ||
} | ||
|
||
if (!data) { | ||
return <div>Fail to load data</div> | ||
} | ||
|
||
return <div style={{ width: '100vw', height: '100vh' }} ref={containerRef} /> | ||
} | ||
|
||
export default NodeGeoDistribution |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use a constant value named the comment could be better.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updated by 3889408