@ -11,7 +11,7 @@ import TopStats from './top-stats';
import { IntervalPicker , getStoredInterval , storeInterval } from './interval-picker' ;
import FadeIn from '../../fade-in' ;
import * as url from '../../util/url'
import classNames from "classnames" ;
import classNames from 'classnames' ;
const calculateMaximumY = function ( dataset ) {
const yAxisValues = dataset
@ -61,6 +61,7 @@ class LineGraph extends React.Component {
} ,
} ,
responsive : true ,
maintainAspectRatio : false ,
onResize : this . updateWindowDimensions ,
elements : { line : { tension : 0 } , point : { radius : 0 } } ,
onClick : this . onClick . bind ( this ) ,
@ -316,24 +317,39 @@ class LineGraph extends React.Component {
}
}
// This function is used for maintaining the main-graph/top-stats container height in the
// loading process. The container height depends on how many top stat metrics are returned
// from the API, but in the loading state, we don't know that yet. We can use localStorage
// to keep track of the Top Stats container height.
getTopStatsHeight ( ) {
if ( this . props . topStatData ) {
return 'auto'
} else {
return ` ${ storage . getItem ( ` topStatsHeight__ ${ this . props . site . domain } ` ) || 89 } px `
}
}
render ( ) {
const { updateMetric , metric , topStatData , query , site , graphData } = this . props
const extraClass = this . props . graphData && this . props . graphData . interval === 'hour' ? '' : 'cursor-pointer'
const { mainGraphRefreshing, updateMetric, updateInterval, metric, topStatData , query , site , graphData , lastLoadTimestamp } = this . props
const canvasClass = classNames ( 'mt-4 select-none' , { 'cursor-pointer' : ! [ 'minute' , 'hour' ] . includes ( graphData ? . interval ) } )
return (
< div className = "graph-inner" >
< div className = "flex flex-wrap" ref = { this . boundary } >
< TopStats query = { query } metric = { metric } updateMetric = { updateMetric } topStatData = { topStatData } tooltipBoundary = { this . boundary . current } lastLoadTimestamp = { this . props . lastLoadTimestamp } / >
< div >
< div id= "top-stats-container" className= "flex flex-wrap" ref = { this . boundary } style = { { height : this . getTopStatsHeight ( ) } } >
< TopStats query = { query } metric = { metric } updateMetric = { updateMetric } topStatData = { topStatData } tooltipBoundary = { this . boundary . current } lastLoadTimestamp = { lastLoadTimestamp } / >
< / d i v >
< div className = "relative px-2" >
{ mainGraphRefreshing && renderLoader ( ) }
< div className = "absolute right-4 -top-10 py-2 md:py-0 flex items-center" >
{ this . downloadLink ( ) }
{ this . samplingNotice ( ) }
{ this . importedNotice ( ) }
< IntervalPicker site = { site } query = { query } graphData = { graphData } metric = { metric } updateInterval = { this . props . updateInterval } / >
< IntervalPicker site = { site } query = { query } graphData = { graphData } metric = { metric } updateInterval = { updateInterval } / >
< / d i v >
< FadeIn show = { graphData } >
< canvas id = "main-graph-canvas" className = { 'mt-4 select-none ' + extraClass } width = "1054" height = "342" > < / c a n v a s >
< div className = "relative h-96 w-full" >
< canvas id = "main-graph-canvas" className = { canvasClass } > < / c a n v a s >
< / d i v >
< / F a d e I n >
< / d i v >
< / d i v >
@ -379,7 +395,7 @@ export default class VisitorGraph extends React.Component {
updateInterval ( interval ) {
if ( this . isIntervalValid ( interval ) ) {
storeInterval ( this . props . query . period , this . props . site . domain , interval )
this . setState ( { mainGraphLoadingState : LoadingState . refreshing } , this . fetchGraphData )
this . setState ( { mainGraphLoadingState : LoadingState . refreshing , graphData : null } , this . fetchGraphData )
}
}
@ -428,6 +444,10 @@ export default class VisitorGraph extends React.Component {
document . removeEventListener ( 'tick' , this . fetchTopStatData )
}
storeTopStatsContainerHeight ( ) {
storage . setItem ( ` topStatsHeight__ ${ this . props . site . domain } ` , document . getElementById ( 'top-stats-container' ) . clientHeight )
}
updateMetric ( clickedMetric ) {
if ( this . state . metric == clickedMetric ) return
@ -455,7 +475,10 @@ export default class VisitorGraph extends React.Component {
fetchTopStatData ( ) {
api . get ( ` /api/stats/ ${ encodeURIComponent ( this . props . site . domain ) } /top-stats ` , this . props . query )
. then ( ( res ) => {
this . setState ( { topStatsLoadingState : LoadingState . loaded , topStatData : res } , this . resetMetric )
this . setState ( { topStatsLoadingState : LoadingState . loaded , topStatData : res } , ( ) => {
this . storeTopStatsContainerHeight ( )
this . resetMetric ( )
} )
return res
} )
}
@ -469,36 +492,42 @@ export default class VisitorGraph extends React.Component {
const mainGraphRefreshing = ( mainGraphLoadingState === LoadingState . refreshing )
const topStatAndGraphLoaded = ! ! ( topStatData && graphData )
const sho wGraph =
LoadingState. isLoadedOrRefreshing ( topStatsLoadingState ) &&
const sho uldSho w =
topStatsLoadingState === LoadingState . loaded &&
LoadingState . isLoadedOrRefreshing ( mainGraphLoadingState ) &&
( topStatData && mainGraphRefreshing || topStatAndGraphLoaded )
return (
< FadeIn show = { sho wGraph } >
< LineGraphWithRouter graphData= { graphData } topStatData = { topStatData } site = { site } query = { query } darkTheme = { theme } metric = { metric } updateMetric = { this . updateMetric } updateInterval = { this . updateInterval } lastLoadTimestamp = { this . props . lastLoadTimestamp } / >
< FadeIn show = { sho uldSho w} >
< LineGraphWithRouter mainGraphRefreshing= { mainGraphRefreshing } graphData= { graphData } topStatData = { topStatData } site = { site } query = { query } darkTheme = { theme } metric = { metric } updateMetric = { this . updateMetric } updateInterval = { this . updateInterval } lastLoadTimestamp = { this . props . lastLoadTimestamp } / >
< / F a d e I n >
)
}
render ( ) {
const { mainGraphLoadingState , topStatsLoadingState } = this . state
const loaderClassName = classNames ( 'mx-auto loading' , {
'pt-52 sm:pt-56 md:pt-60' : mainGraphLoadingState == LoadingState . refreshing ,
'pt-32 sm:pt-36 md:pt-48' : mainGraphLoadingState !== LoadingState . refreshing ,
} )
const showLoader =
LoadingState . isLoadingOrRefreshing ( mainGraphLoadingState ) ||
LoadingState. isLoadingOrRefreshing ( topStatsLoadingState )
[ mainGraphLoadingState , topStatsLoadingState ] . includes ( LoadingState . loading ) &&
mainGraphLoadingState !== LoadingState . refreshing
return (
< LazyLoader onVisible = { this . onVisible } >
< div className = { "relative w-full mt-2 bg-white rounded shadow-xl dark:bg-gray-825 main-graph "} >
{ showLoader && < div className = "graph-inner" > < div className = { loaderClassName } > < div > < / d i v > < / d i v > < / d i v > }
< div className = { "relative w-full mt-2 bg-white rounded shadow-xl dark:bg-gray-825 "} >
{ showLoader && renderLoader ( ) }
{ this . renderInner ( ) }
< / d i v >
< / L a z y L o a d e r >
)
}
}
function renderLoader ( ) {
return (
< div className = "absolute h-full w-full flex items-center justify-center" >
< div className = "loading" >
< div > < / d i v >
< / d i v >
< / d i v >
)
}