@@ -33,7 +33,7 @@ import { Bar, Pie } from 'react-chartjs-2';
3333import { getPeers , getPeersResponse , getSyncPeers } from '../../../lib/api' ;
3434import GetAppIcon from '@mui/icons-material/GetApp' ;
3535import HelpOutlineOutlinedIcon from '@mui/icons-material/HelpOutlineOutlined' ;
36- import { useContext , useEffect , useState } from 'react' ;
36+ import { useContext , useEffect , useState , useMemo } from 'react' ;
3737import { MAX_PAGE_SIZE } from '../../../lib/constants' ;
3838import styles from './inde.module.css' ;
3939import { exportCSVFile } from '../../../lib/utils' ;
@@ -53,6 +53,7 @@ import { ReactComponent as Export } from '../../../assets/images/cluster/peer/ex
5353import { ReactComponent as ExportFile } from '../../../assets/images/cluster/peer/export-file.svg' ;
5454import { ReactComponent as Count } from '../../../assets/images/cluster/scheduler/number.svg' ;
5555import ErrorHandler from '../../error-handler' ;
56+ import type { ChartOptions } from 'chart.js' ;
5657
5758ChartJS . register ( ArcElement , Tooltip , Legend , CategoryScale , LinearScale , BarElement , Title ) ;
5859Chart . defaults . font . family = 'mabry-light' ;
@@ -207,87 +208,116 @@ export default function Peer() {
207208 setExportGitCommit ( filteredByCommit ) ;
208209 } , [ exportSelectedVersion , exportSelectedCommit , peer ] ) ;
209210
210- const barOptions = {
211- plugins : {
212- legend : {
213- display : false ,
214- } ,
215- } ,
216- scales : {
217- x : {
218- grid : { color : 'rgba(0, 0, 0, 0)' } ,
219- } ,
220- } ,
211+ const greenPalette = useMemo (
212+ ( ) =>
213+ theme . palette . mode === 'dark'
214+ ? [ '#01A76F' , '#5BE49B' , '#C8FAD6' , '#004B50' , '#007868' ]
215+ // istanbul ignore next
216+ : [
217+ 'rgba(67,160,71,0.95)' ,
218+ 'rgba(76,175,80,0.9)' ,
219+ 'rgba(102,187,106,0.85)' ,
220+ 'rgba(129,199,132,0.8)' ,
221+ 'rgba(165,214,167,0.75)' ,
222+ ] ,
223+ [ theme . palette . mode ] ,
224+ ) ;
225+ // istanbul ignore next
226+ const getGradient = ( ctx : CanvasRenderingContext2D , isHover = false ) => {
227+ const gradient = ctx . createLinearGradient ( 0 , 0 , 0 , 400 ) ;
228+ const colors =
229+ theme . palette . mode === 'dark'
230+ // istanbul ignore next
231+ ? isHover
232+ ? [ '#00CB69' , '#008C74' ]
233+ : [ '#00E676' , '#009688' ]
234+ : isHover
235+ ? [ '#5AA360' , '#1E9088' ]
236+ : [ '#66BB6A' , '#26A69A' ] ;
237+ gradient . addColorStop ( 0 , colors [ 0 ] ) ;
238+ gradient . addColorStop ( 1 , colors [ 1 ] ) ;
239+ return gradient ;
221240 } ;
222241
223- const doughnutOptions = {
224- plugins : {
225- legend : {
226- position : 'bottom' as 'bottom' ,
242+ const barOptions : ChartOptions < 'bar' > = useMemo (
243+ ( ) => ( {
244+ plugins : {
245+ legend : { display : false } ,
246+ tooltip : {
247+ backgroundColor : theme . palette . mode === 'dark' ? '#043B34' : '#E8F5E9' ,
248+ titleColor : theme . palette . mode === 'dark' ? '#A5D6A7' : '#1B5E20' ,
249+ bodyColor : theme . palette . mode === 'dark' ? '#B9F6CA' : '#2E7D32' ,
250+ borderWidth : 1 ,
251+ borderColor : theme . palette . mode === 'dark' ? '#1B5E20' : '#C8E6C9' ,
252+ cornerRadius : 6 ,
253+ } ,
227254 } ,
228- } ,
229- } ;
230-
231- const gitVersionBar = {
232- labels : gitVersion . map ( ( item ) => item . name ) ,
233- datasets : [
234- {
235- data : gitVersion . map ( ( item ) => item . count ) ,
236- backgroundColor : theme . palette . mode === 'dark' ? '#01A76F' : 'rgb(31, 125, 83)' ,
237- borderRadius : 5 ,
238- barPercentage : 0.6 ,
255+ scales : {
256+ x : { grid : { color : 'rgba(0,0,0,0)' } , ticks : { color : theme . palette . text . secondary } } ,
257+ y : {
258+ grid : { color : theme . palette . mode === 'dark' ? '#004D40' : '#E0F2F1' } ,
259+ ticks : { color : theme . palette . text . secondary } ,
260+ } ,
239261 } ,
240- ] ,
241- } ;
242-
243- const doughnutBackgroundColor = [
244- 'rgb(31, 125, 83)' ,
245- 'rgba(31, 125, 83,0.8)' ,
246- 'rgba(31, 125, 83,0.6)' ,
247- 'rgba(31, 125, 83,0.4)' ,
248- 'rgba(31, 125, 83,0.2)' ,
249- ] ;
250-
251- const darkDoughnutBackgroundColor = [ '#01A76F' , '#5BE49B' , '#C8FAD6' , '#004B50' , '#007868' ] ;
262+ elements : { bar : { borderRadius : 6 } } ,
263+ hover : { mode : 'nearest' , intersect : true } ,
264+ animation : { duration : 800 , easing : 'easeOutQuart' } ,
265+ } ) ,
266+ [ theme . palette . mode , theme . palette . text . secondary ] ,
267+ ) ;
252268
253- const gitVersionDoughnut = {
254- labels : gitVersion . map ( ( item ) => item . name ) ,
255- datasets : [
256- {
257- label : 'Git Version' ,
258- data : gitVersion . map ( ( item ) => item . count ) ,
259- backgroundColor : theme . palette . mode === 'dark' ? darkDoughnutBackgroundColor : doughnutBackgroundColor ,
260- borderWidth : 0 ,
261- borderColor : 'rgba(255, 255, 255, 0.6)' ,
269+ const doughnutOptions : ChartOptions < 'pie' > = useMemo (
270+ ( ) => ( {
271+ plugins : {
272+ legend : {
273+ position : 'bottom' ,
274+ labels : { color : theme . palette . text . primary , padding : 12 , font : { size : 13 } } ,
275+ } ,
276+ tooltip : {
277+ backgroundColor : theme . palette . mode === 'dark' ? '#043B34' : '#E8F5E9' ,
278+ titleColor : theme . palette . mode === 'dark' ? '#A5D6A7' : '#1B5E20' ,
279+ bodyColor : theme . palette . mode === 'dark' ? '#B9F6CA' : '#2E7D32' ,
280+ } ,
262281 } ,
263- ] ,
264- } ;
282+ cutout : gitVersion . length === 1 ? '40%' : '68%' ,
283+ animation : { animateRotate : true , duration : 1000 , easing : 'easeOutCubic' } ,
284+ } ) ,
285+ [ theme . palette . mode , theme . palette . text . primary , gitVersion . length ] ,
286+ ) ;
265287
266- const gitCommitBar = {
267- labels : gitCommit . map ( ( item ) => item . name ) ,
288+ const createBarData = ( data : { name : string ; count : number } [ ] , label : string ) => ( {
289+ labels : data . map ( ( i ) => i . name ) ,
268290 datasets : [
269291 {
270- data : gitCommit . map ( ( item ) => item . count ) ,
271- backgroundColor : theme . palette . mode === 'dark' ? '#01A76F' : 'rgb(31, 125, 83)' ,
272- borderWidth : 0 ,
273- borderRadius : 5 ,
292+ label,
293+ data : data . map ( ( i ) => i . count ) ,
294+ backgroundColor : ( ctx : any ) => getGradient ( ctx . chart . ctx ) ,
295+ // istanbul ignore next
296+ hoverBackgroundColor : ( ctx : any ) => getGradient ( ctx . chart . ctx , true ) ,
297+ borderRadius : 6 ,
274298 barPercentage : 0.6 ,
275299 } ,
276300 ] ,
277- } ;
301+ } ) ;
278302
279- const gitCommitDoughnut = {
280- labels : gitCommit . map ( ( item ) => item . name ) ,
303+ const createDoughnutData = ( data : { name : string ; count : number } [ ] , label : string ) => ( {
304+ labels : data . map ( ( i ) => i . name ) ,
281305 datasets : [
282306 {
283- label : 'Git Commit' ,
284- data : gitCommit . map ( ( item ) => item . count ) ,
285- backgroundColor : theme . palette . mode === 'dark' ? darkDoughnutBackgroundColor : doughnutBackgroundColor ,
286- borderWidth : 0 ,
287- borderColor : 'rgba(255, 255, 255, 0.6)' ,
307+ label,
308+ data : data . map ( ( i ) => i . count ) ,
309+ backgroundColor : greenPalette ,
310+ borderWidth : 2 ,
311+ borderColor : theme . palette . background . paper ,
312+ hoverOffset : 8 ,
288313 } ,
289314 ] ,
290- } ;
315+ } ) ;
316+
317+ const gitVersionBar = createBarData ( gitVersion , 'Git Version' ) ;
318+ const gitVersionDoughnut = createDoughnutData ( gitVersion , 'Git Version' ) ;
319+ const gitCommitBar = createBarData ( gitCommit , 'Git Commit' ) ;
320+ const gitCommitDoughnut = createDoughnutData ( gitCommit , 'Git Commit' ) ;
291321
292322 const ExportCSV = async ( ) => {
293323 setLoadingButton ( true ) ;
0 commit comments