@@ -3,24 +3,33 @@ import React, { useState } from 'react';
33import Plot from 'react-plotly.js' ;
44import { all , solo as soloStyle } from './sizeSwitch' ;
55
6- interface ChartData {
7- value : string [ ] ;
8- time : string [ ] ;
9- }
10-
116interface EventChartProps {
127 key : string ;
138 metricName : string ;
14- chartData : ChartData
9+ chartData : {
10+ value : string [ ] ,
11+ time : string [ ]
12+ }
13+ sizing : string ;
14+ colourGenerator : Function ;
1515}
1616
1717interface SoloStyles {
1818 height : number ;
1919 width : number ;
2020}
2121
22+ type PlotlyData = {
23+ name : string ;
24+ x : string [ ] ;
25+ y : string [ ] ;
26+ type : string ;
27+ mode : string ;
28+ marker : { colors : string [ ] } ;
29+ } ;
30+
2231const EventChart : React . FC < EventChartProps > = React . memo ( props => {
23- const { metricName, chartData } = props ;
32+ const { metricName, chartData, sizing , colourGenerator } = props ;
2433 const [ solo , setSolo ] = useState < SoloStyles | null > ( null ) ;
2534
2635 setInterval ( ( ) => {
@@ -29,36 +38,39 @@ const EventChart: React.FC<EventChartProps> = React.memo(props => {
2938 }
3039 } , 20 ) ;
3140
41+ // makes time data human-readable, and reverses it so it shows up correctly in the graph
42+ const prettyTimeInReverse = ( timeArray : string [ ] ) : string [ ] => {
43+ return timeArray . map ( ( el : any ) => moment ( el ) . format ( 'kk:mm:ss' ) ) . reverse ( ) ;
44+ } ;
45+
46+ // removes underscores from metric names to improve their look in the graph
47+ const prettyMetricName = ( metricName : string ) : string => {
48+ const newName = metricName . replace ( / k u b e r n e t e s - c a d v i s o r \/ d o c k e r - d e s k t o p \/ / g, '' ) ;
49+ return newName . replace ( / _ / g, ' ' ) ;
50+ } ;
51+
3252 const createChart = ( ) => {
33- const timeArr = timeList . map ( ( el : any ) => moment ( el ) . format ( 'kk:mm:ss' ) ) ;
34- const reverseTimeArr = timeArr . reverse ( )
35- const hashedColour = colourGenerator ( metric ) ;
36- const newMetricName = metric . replace ( "kubernetes-cadvisor/docker-desktop/" , "" ) ; // this will get rid of the long path
37- const re = / _ / g;
38- let plotlyData : {
39- name : any ;
40- x : any ;
41- y : any ;
42- type : any ;
43- mode : any ;
44- marker : { color : string } ;
45- } ;
46- plotlyData = {
47- name : metric ,
48- x : reverseTimeArr ,
49- y : valueList ,
53+ const prettyName = prettyMetricName ( metricName ) ;
54+ const prettyTime = prettyTimeInReverse ( chartData . time ) ;
55+
56+ const plotlyDataObject : PlotlyData = {
57+ name : prettyName ,
58+ x : prettyTime ,
59+ y : chartData . value ,
5060 type : 'scattergl' ,
5161 mode : 'lines' ,
52- marker : { color : hashedColour } ,
62+ marker : {
63+ colors : [ '#fc4039' , '#4b54ea' , '#32b44f' , '#3788fc' , '#9c27b0' , '#febc2c' ] ,
64+ } ,
5365 } ;
5466 const sizeSwitch = sizing === 'all' ? all : solo ;
5567
5668 return (
5769 < Plot
58- data = { [ plotlyData ] }
59- config = { { displayModeBar : false } }
70+ data = { [ plotlyDataObject ] }
71+ config = { { displayModeBar : true } }
6072 layout = { {
61- title : newMetricName . replace ( re , " " ) , // this will reaplce all the underscores in the graph titlke ,
73+ title : prettyName ,
6274 ...sizeSwitch ,
6375 font : {
6476 color : '#444d56' ,
@@ -69,10 +81,7 @@ const EventChart: React.FC<EventChartProps> = React.memo(props => {
6981 plot_bgcolor : 'white' ,
7082 showlegend : true ,
7183 legend : {
72- orientation : 'h' ,
73- xanchor : 'center' ,
74- x : 0.5 ,
75- y : 5 ,
84+ orientation : 'v' ,
7685 } ,
7786 xaxis : {
7887 title : 'Time' ,
@@ -86,7 +95,7 @@ const EventChart: React.FC<EventChartProps> = React.memo(props => {
8695 } ,
8796 yaxis : {
8897 rangemode : 'nonnegative' ,
89- title : metric ,
98+ title : prettyName ,
9099 } ,
91100 } }
92101 />
0 commit comments