Open source, privacy-first web analytics. Lightweight, cookie-free Google Analytics alternative. Self-hosted or cloud.
TypeScript analyticsanalytics-dashboardclickhouseelixirgoogle-analyticsmarketingopen-source-analyticsphoenixplausible-analyticspostgresqlprivacyprivacy-friendlyself-hostedsimple-analyticsstatisticstailwindcssweb-analyticswebsite-analyticswebsite-statswebsite-tracking
Refactor top stats and graph positioning (#2662)
RobertJoonas6f4d4798527b
diff --git a/assets/css/app.css b/assets/css/app.cssindex 939d8e5310..298eaf64af 100644--- a/assets/css/app.css+++ b/assets/css/app.css@@ -67,64 +67,6 @@ blockquote {@tailwind utilities;-.main-graph {- position: relative;- height: 0;-- /* Formula is: (top row height + (graph height / graph width * 100%) - adjustment for stat selectors) */- padding-top: calc(168px + (451 / 1088 * 100%) - 20px);-}--@screen sm {- .main-graph {- padding-top: calc(148px + (451 / 1088 * 100%));- }-}--@screen md {- .main-graph {- padding-top: calc(128px + (451 / 1088 * 100%));- }-}--@screen lg {- .main-graph {- padding-top: calc(28px + (451 / 1088 * 100%) - 10px);- }-}--.top-stats-only {- position: relative;- height: 0;- padding-top: calc(158px + 15px);-}--@screen sm {- .top-stats-only {- padding-top: calc(155px + 15px);- }-}--@screen md {- .top-stats-only {- padding-top: calc(168px + 15px);- }-}--@screen lg {- .top-stats-only {- padding-top: calc(75px + 15px);- }-}--.graph-inner {- position: absolute;- top: 0;- left: 0;- width: 100%;- height: auto;-}-.light-text {color: #F0F4F8;}diff --git a/assets/js/dashboard/stats/graph/visitor-graph.js b/assets/js/dashboard/stats/graph/visitor-graph.jsindex c445b88527..4c84c96756 100644--- a/assets/js/dashboard/stats/graph/visitor-graph.js+++ b/assets/js/dashboard/stats/graph/visitor-graph.js@@ -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} /></div><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}/></div><FadeIn show={graphData}>- <canvas id="main-graph-canvas" className={'mt-4 select-none ' + extraClass} width="1054" height="342"></canvas>+ <div className="relative h-96 w-full">+ <canvas id="main-graph-canvas" className={canvasClass}></canvas>+ </div></FadeIn></div></div>@@ -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 showGraph =- LoadingState.isLoadedOrRefreshing(topStatsLoadingState) &&+ const shouldShow =+ topStatsLoadingState === LoadingState.loaded &&LoadingState.isLoadedOrRefreshing(mainGraphLoadingState) &&(topStatData && mainGraphRefreshing || topStatAndGraphLoaded)return (- <FadeIn show={showGraph}>- <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={shouldShow}>+ <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} /></FadeIn>)}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.refreshingreturn (<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></div></div></div>}+ <div className={"relative w-full mt-2 bg-white rounded shadow-xl dark:bg-gray-825"}>+ {showLoader && renderLoader()}{this.renderInner()}</div></LazyLoader>)}}++function renderLoader() {+ return (+ <div className="absolute h-full w-full flex items-center justify-center">+ <div className="loading">+ <div></div>+ </div>+ </div>+ )+}