2019-11-19 07:30:42 +03:00
|
|
|
import React from 'react';
|
|
|
|
import { withRouter } from 'react-router-dom'
|
2021-06-07 11:29:42 +03:00
|
|
|
import Chart from 'chart.js/auto';
|
2020-11-11 13:31:42 +03:00
|
|
|
import { eventName, navigateToQuery } from '../query'
|
2021-01-21 15:42:55 +03:00
|
|
|
import numberFormatter, {durationFormatter} from '../number-formatter'
|
2019-11-19 07:30:42 +03:00
|
|
|
import * as api from '../api'
|
2021-01-21 15:42:55 +03:00
|
|
|
import {ThemeContext} from '../theme-context'
|
2021-03-25 12:55:15 +03:00
|
|
|
import LazyLoader from '../lazy-loader'
|
2019-11-19 07:30:42 +03:00
|
|
|
|
2021-01-21 15:42:55 +03:00
|
|
|
function buildDataSet(plot, present_index, ctx, label) {
|
2019-11-19 07:30:42 +03:00
|
|
|
var gradient = ctx.createLinearGradient(0, 0, 0, 300);
|
|
|
|
gradient.addColorStop(0, 'rgba(101,116,205, 0.2)');
|
|
|
|
gradient.addColorStop(1, 'rgba(101,116,205, 0)');
|
|
|
|
|
2021-01-21 15:42:55 +03:00
|
|
|
if (present_index) {
|
|
|
|
var dashedPart = plot.slice(present_index - 1);
|
|
|
|
var dashedPlot = (new Array(plot.length - dashedPart.length)).concat(dashedPart)
|
|
|
|
for(var i = present_index; i < plot.length; i++) {
|
|
|
|
plot[i] = undefined
|
|
|
|
}
|
|
|
|
|
|
|
|
return [{
|
2020-07-14 16:52:26 +03:00
|
|
|
label: label,
|
2021-01-21 15:42:55 +03:00
|
|
|
data: plot,
|
2019-11-19 07:30:42 +03:00
|
|
|
borderWidth: 3,
|
|
|
|
borderColor: 'rgba(101,116,205)',
|
2021-01-21 15:42:55 +03:00
|
|
|
pointBackgroundColor: 'rgba(101,116,205)',
|
2019-11-19 07:30:42 +03:00
|
|
|
backgroundColor: gradient,
|
2021-06-07 11:29:42 +03:00
|
|
|
fill: true
|
2019-11-19 07:30:42 +03:00
|
|
|
},
|
|
|
|
{
|
2020-07-14 16:52:26 +03:00
|
|
|
label: label,
|
2019-11-19 07:30:42 +03:00
|
|
|
data: dashedPlot,
|
|
|
|
borderWidth: 3,
|
2021-01-21 15:42:55 +03:00
|
|
|
borderDash: [5, 10],
|
2019-11-19 07:30:42 +03:00
|
|
|
borderColor: 'rgba(101,116,205)',
|
2021-01-21 15:42:55 +03:00
|
|
|
pointBackgroundColor: 'rgba(101,116,205)',
|
2019-11-19 07:30:42 +03:00
|
|
|
backgroundColor: gradient,
|
2021-06-07 11:29:42 +03:00
|
|
|
fill: true
|
2021-01-21 15:42:55 +03:00
|
|
|
}]
|
2019-11-19 07:30:42 +03:00
|
|
|
} else {
|
|
|
|
return [{
|
2020-07-14 16:52:26 +03:00
|
|
|
label: label,
|
2019-11-25 06:37:50 +03:00
|
|
|
data: plot,
|
2021-01-21 15:42:55 +03:00
|
|
|
borderWidth: 3,
|
|
|
|
borderColor: 'rgba(101,116,205)',
|
|
|
|
pointBackgroundColor: 'rgba(101,116,205)',
|
|
|
|
backgroundColor: gradient,
|
2021-06-07 11:29:42 +03:00
|
|
|
fill: true
|
2019-11-19 07:30:42 +03:00
|
|
|
}]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const MONTHS = [
|
|
|
|
"January", "February", "March",
|
|
|
|
"April", "May", "June", "July",
|
|
|
|
"August", "September", "October",
|
|
|
|
"November", "December"
|
|
|
|
]
|
|
|
|
|
2020-10-29 16:43:58 +03:00
|
|
|
const MONTHS_ABBREV = [
|
|
|
|
"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
|
|
|
|
]
|
|
|
|
|
|
|
|
const DAYS_ABBREV = [
|
|
|
|
"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
|
|
|
|
]
|
|
|
|
|
2020-07-14 16:52:26 +03:00
|
|
|
function dateFormatter(interval, longForm) {
|
2021-06-07 11:29:42 +03:00
|
|
|
return function(isoDate, index, ticks) {
|
2020-07-14 12:15:23 +03:00
|
|
|
let date = new Date(isoDate)
|
2019-11-19 07:30:42 +03:00
|
|
|
|
2020-07-14 16:52:26 +03:00
|
|
|
if (interval === 'month') {
|
2019-11-19 07:30:42 +03:00
|
|
|
return MONTHS[date.getUTCMonth()];
|
2020-07-14 16:52:26 +03:00
|
|
|
} else if (interval === 'date') {
|
2020-10-29 16:43:58 +03:00
|
|
|
var day = DAYS_ABBREV[date.getUTCDay()];
|
|
|
|
var date_ = date.getUTCDate();
|
|
|
|
var month = MONTHS_ABBREV[date.getUTCMonth()];
|
|
|
|
return day + ', ' + date_ + ' ' + month;
|
2020-07-14 17:04:34 +03:00
|
|
|
} else if (interval === 'hour') {
|
2020-07-14 12:15:23 +03:00
|
|
|
const parts = isoDate.split(/[^0-9]/);
|
2021-01-21 15:42:55 +03:00
|
|
|
date = new Date(parts[0],parts[1]-1,parts[2],parts[3],parts[4],parts[5])
|
2019-11-19 07:30:42 +03:00
|
|
|
var hours = date.getHours(); // Not sure why getUTCHours doesn't work here
|
|
|
|
var ampm = hours >= 12 ? 'pm' : 'am';
|
|
|
|
hours = hours % 12;
|
|
|
|
hours = hours ? hours : 12; // the hour '0' should be '12'
|
|
|
|
return hours + ampm;
|
2020-07-14 16:52:26 +03:00
|
|
|
} else if (interval === 'minute') {
|
|
|
|
if (longForm) {
|
|
|
|
const minutesAgo = Math.abs(isoDate)
|
|
|
|
return minutesAgo === 1 ? '1 minute ago' : minutesAgo + ' minutes ago'
|
|
|
|
} else {
|
|
|
|
return isoDate + 'm'
|
|
|
|
}
|
2019-11-19 07:30:42 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class LineGraph extends React.Component {
|
2020-12-16 12:57:28 +03:00
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
|
|
|
this.regenerateChart = this.regenerateChart.bind(this);
|
2021-06-28 16:05:22 +03:00
|
|
|
this.updateWindowDimensions = this.updateWindowDimensions.bind(this);
|
2020-12-16 12:57:28 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
regenerateChart() {
|
2021-01-21 15:42:55 +03:00
|
|
|
const {graphData} = this.props
|
2020-07-14 16:52:26 +03:00
|
|
|
this.ctx = document.getElementById("main-graph-canvas").getContext('2d');
|
2020-07-21 10:30:03 +03:00
|
|
|
const label = this.props.query.filters.goal ? 'Converted visitors' : graphData.interval === 'minute' ? 'Pageviews' : 'Visitors'
|
|
|
|
const dataSet = buildDataSet(graphData.plot, graphData.present_index, this.ctx, label)
|
2019-11-19 07:30:42 +03:00
|
|
|
|
2020-12-16 12:57:28 +03:00
|
|
|
return new Chart(this.ctx, {
|
2019-11-19 07:30:42 +03:00
|
|
|
type: 'line',
|
|
|
|
data: {
|
|
|
|
labels: graphData.labels,
|
2021-01-21 15:42:55 +03:00
|
|
|
datasets: dataSet
|
2019-11-19 07:30:42 +03:00
|
|
|
},
|
|
|
|
options: {
|
|
|
|
animation: false,
|
2021-06-07 11:29:42 +03:00
|
|
|
plugins: {
|
|
|
|
legend: {display: false},
|
|
|
|
tooltip: {
|
|
|
|
mode: 'index',
|
|
|
|
intersect: false,
|
|
|
|
titleFont: {size: 18},
|
|
|
|
footerFont: {size: 14},
|
|
|
|
bodyFont: {size: 14},
|
|
|
|
backgroundColor: 'rgba(25, 30, 56)',
|
|
|
|
titleMarginBottom: 8,
|
|
|
|
bodySpacing: 6,
|
|
|
|
footerMarginTop: 8,
|
|
|
|
padding: {x: 10, y: 10},
|
|
|
|
multiKeyBackground: 'none',
|
|
|
|
callbacks: {
|
|
|
|
title: function(dataPoints) {
|
|
|
|
const data = dataPoints[0]
|
|
|
|
return dateFormatter(graphData.interval, true)(data.label)
|
|
|
|
},
|
|
|
|
beforeBody: function() {
|
|
|
|
this.drawnLabels = {}
|
|
|
|
},
|
|
|
|
label: function(item) {
|
|
|
|
const dataset = item.dataset
|
|
|
|
if (!this.drawnLabels[dataset.label]) {
|
|
|
|
this.drawnLabels[dataset.label] = true
|
|
|
|
const pluralizedLabel = item.formattedValue === "1" ? dataset.label.slice(0, -1) : dataset.label
|
|
|
|
return ` ${item.formattedValue} ${pluralizedLabel}`
|
|
|
|
}
|
|
|
|
},
|
|
|
|
footer: function(dataPoints) {
|
|
|
|
if (graphData.interval === 'month') {
|
|
|
|
return 'Click to view month'
|
|
|
|
} else if (graphData.interval === 'date') {
|
|
|
|
return 'Click to view day'
|
|
|
|
}
|
2021-01-20 16:49:39 +03:00
|
|
|
}
|
2019-11-19 07:30:42 +03:00
|
|
|
}
|
2021-06-07 11:29:42 +03:00
|
|
|
},
|
2019-11-19 07:30:42 +03:00
|
|
|
},
|
2021-06-07 11:29:42 +03:00
|
|
|
responsive: true,
|
2021-06-28 16:05:22 +03:00
|
|
|
onResize: this.updateWindowDimensions,
|
2021-06-07 11:29:42 +03:00
|
|
|
elements: {line: {tension: 0}, point: {radius: 0}},
|
|
|
|
onClick: this.onClick.bind(this),
|
2019-11-19 07:30:42 +03:00
|
|
|
scales: {
|
2021-06-07 11:29:42 +03:00
|
|
|
y: {
|
|
|
|
beginAtZero: true,
|
2019-11-19 07:30:42 +03:00
|
|
|
ticks: {
|
|
|
|
callback: numberFormatter,
|
|
|
|
maxTicksLimit: 8,
|
2021-06-07 11:29:42 +03:00
|
|
|
color: this.props.darkTheme ? 'rgb(243, 244, 246)' : undefined
|
2019-11-19 07:30:42 +03:00
|
|
|
},
|
2021-06-07 11:29:42 +03:00
|
|
|
grid: {
|
2019-11-19 07:30:42 +03:00
|
|
|
zeroLineColor: 'transparent',
|
|
|
|
drawBorder: false,
|
|
|
|
}
|
2021-06-07 11:29:42 +03:00
|
|
|
},
|
|
|
|
x: {
|
|
|
|
grid: {display: false},
|
2019-11-19 07:30:42 +03:00
|
|
|
ticks: {
|
|
|
|
maxTicksLimit: 8,
|
2021-06-07 11:29:42 +03:00
|
|
|
callback: function(val, index, ticks) { return dateFormatter(graphData.interval)(this.getLabelForValue(val)) },
|
|
|
|
color: this.props.darkTheme ? 'rgb(243, 244, 246)' : undefined
|
2019-11-19 07:30:42 +03:00
|
|
|
}
|
2021-06-07 11:29:42 +03:00
|
|
|
}
|
2019-11-19 07:30:42 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-12-16 12:57:28 +03:00
|
|
|
componentDidMount() {
|
|
|
|
this.chart = this.regenerateChart();
|
|
|
|
}
|
|
|
|
|
2020-07-14 16:52:26 +03:00
|
|
|
componentDidUpdate(prevProps) {
|
2021-01-21 15:42:55 +03:00
|
|
|
if (this.props.graphData !== prevProps.graphData) {
|
|
|
|
const label = this.props.query.filters.goal ? 'Converted visitors' : this.props.graphData.interval === 'minute' ? 'Pageviews' : 'Visitors'
|
|
|
|
const newDataset = buildDataSet(this.props.graphData.plot, this.props.graphData.present_index, this.ctx, label)
|
|
|
|
|
|
|
|
for (let i = 0; i < newDataset[0].data.length; i++) {
|
|
|
|
this.chart.data.datasets[0].data[i] = newDataset[0].data[i]
|
|
|
|
}
|
|
|
|
|
|
|
|
this.chart.update()
|
2020-07-14 16:52:26 +03:00
|
|
|
}
|
2020-12-16 12:57:28 +03:00
|
|
|
|
|
|
|
if (prevProps.darkTheme !== this.props.darkTheme) {
|
2021-06-14 16:44:46 +03:00
|
|
|
this.chart.destroy();
|
2020-12-16 12:57:28 +03:00
|
|
|
this.chart = this.regenerateChart();
|
|
|
|
this.chart.update();
|
|
|
|
}
|
2020-07-14 16:52:26 +03:00
|
|
|
}
|
|
|
|
|
2021-06-28 16:05:22 +03:00
|
|
|
/**
|
|
|
|
* The current ticks' limits are set to treat iPad (regular/Mini/Pro) as a regular screen.
|
|
|
|
* @param {*} chart - The chart instance.
|
|
|
|
* @param {*} dimensions - An object containing the new dimensions *of the chart.*
|
|
|
|
*/
|
|
|
|
updateWindowDimensions(chart, dimensions) {
|
|
|
|
chart.options.scales.x.ticks.maxTicksLimit = dimensions.width < 720 ? 5 : 8
|
|
|
|
chart.options.scales.y.ticks.maxTicksLimit = dimensions.height < 233 ? 3 : 8
|
|
|
|
}
|
|
|
|
|
2019-11-19 07:30:42 +03:00
|
|
|
onClick(e) {
|
2021-01-21 15:42:55 +03:00
|
|
|
const element = this.chart.getElementsAtEventForMode(e, 'index', {intersect: false})[0]
|
2021-06-07 11:29:42 +03:00
|
|
|
const date = this.chart.data.labels[element.index]
|
|
|
|
|
2019-11-19 07:30:42 +03:00
|
|
|
if (this.props.graphData.interval === 'month') {
|
2020-11-11 13:31:42 +03:00
|
|
|
navigateToQuery(
|
|
|
|
this.props.history,
|
|
|
|
this.props.query,
|
|
|
|
{
|
|
|
|
period: 'month',
|
|
|
|
date,
|
|
|
|
}
|
|
|
|
)
|
2019-11-19 07:30:42 +03:00
|
|
|
} else if (this.props.graphData.interval === 'date') {
|
2020-11-11 13:31:42 +03:00
|
|
|
navigateToQuery(
|
|
|
|
this.props.history,
|
|
|
|
this.props.query,
|
|
|
|
{
|
|
|
|
period: 'day',
|
|
|
|
date,
|
|
|
|
}
|
|
|
|
)
|
2019-11-19 07:30:42 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-20 06:22:37 +03:00
|
|
|
renderComparison(name, comparison) {
|
2019-11-19 07:30:42 +03:00
|
|
|
const formattedComparison = numberFormatter(Math.abs(comparison))
|
|
|
|
|
|
|
|
if (comparison > 0) {
|
2020-03-06 12:11:38 +03:00
|
|
|
const color = name === 'Bounce rate' ? 'text-red-400' : 'text-green-500'
|
2021-01-21 15:42:55 +03:00
|
|
|
return <span className="text-xs dark:text-gray-100"><span className={color + ' font-bold'}>↑</span> {formattedComparison}%</span>
|
|
|
|
} else if (comparison < 0) {
|
2020-03-06 12:11:38 +03:00
|
|
|
const color = name === 'Bounce rate' ? 'text-green-500' : 'text-red-400'
|
2021-01-21 15:42:55 +03:00
|
|
|
return <span className="text-xs dark:text-gray-100"><span className={color + ' font-bold'}>↓</span> {formattedComparison}%</span>
|
|
|
|
} else if (comparison === 0) {
|
|
|
|
return <span className="text-xs text-gray-700 dark:text-gray-300">〰 N/A</span>
|
2019-11-19 07:30:42 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-24 13:08:05 +03:00
|
|
|
topStatNumberShort(stat) {
|
2021-07-23 13:44:05 +03:00
|
|
|
if (['visit duration', 'time on page'].includes(stat.name.toLowerCase())) {
|
|
|
|
return durationFormatter(stat.value)
|
|
|
|
} else if (['bounce rate', 'conversion rate'].includes(stat.name.toLowerCase())) {
|
|
|
|
return stat.value + '%'
|
2020-07-17 11:09:54 +03:00
|
|
|
} else {
|
2021-07-23 13:44:05 +03:00
|
|
|
return numberFormatter(stat.value)
|
2020-07-17 11:09:54 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-24 13:08:05 +03:00
|
|
|
topStatTooltip(stat) {
|
2021-08-16 11:58:36 +03:00
|
|
|
if (typeof(stat.value) == 'number') {
|
2021-03-24 16:14:17 +03:00
|
|
|
let name = stat.name.toLowerCase()
|
2021-08-16 11:58:36 +03:00
|
|
|
name = stat.value === 1 ? name.slice(0, -1) : name
|
|
|
|
return stat.value.toLocaleString() + ' ' + name
|
2021-03-24 13:08:05 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-25 12:17:18 +03:00
|
|
|
renderTopStats() {
|
2021-01-21 15:42:55 +03:00
|
|
|
const {graphData} = this.props
|
2020-07-14 16:52:26 +03:00
|
|
|
const stats = this.props.graphData.top_stats.map((stat, index) => {
|
2020-03-06 12:11:38 +03:00
|
|
|
let border = index > 0 ? 'lg:border-l border-gray-300' : ''
|
2019-12-20 06:22:37 +03:00
|
|
|
border = index % 2 === 0 ? border + ' border-r lg:border-r-0' : border
|
2019-11-25 12:17:18 +03:00
|
|
|
|
2019-11-22 11:37:44 +03:00
|
|
|
return (
|
2020-02-10 16:17:00 +03:00
|
|
|
<div className={`px-8 w-1/2 my-4 lg:w-auto ${border}`} key={stat.name}>
|
2021-03-31 15:18:49 +03:00
|
|
|
<div className="text-xs font-bold tracking-wide text-gray-500 uppercase dark:text-gray-400 whitespace-nowrap">{stat.name}</div>
|
|
|
|
<div className="flex items-center justify-between my-1 whitespace-nowrap">
|
|
|
|
<b className="mr-4 text-xl md:text-2xl dark:text-gray-100" tooltip={this.topStatTooltip(stat)}>{ this.topStatNumberShort(stat) }</b>
|
2020-02-10 16:17:00 +03:00
|
|
|
{this.renderComparison(stat.name, stat.change)}
|
2019-11-22 11:37:44 +03:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
)
|
2019-11-25 12:17:18 +03:00
|
|
|
})
|
2020-07-14 16:52:26 +03:00
|
|
|
|
|
|
|
if (graphData.interval === 'minute') {
|
2021-01-21 15:42:55 +03:00
|
|
|
stats.push(<div key="dot" className="block pulsating-circle" style={{left: '125px', top: '52px'}}></div>)
|
2020-07-14 16:52:26 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return stats
|
2019-11-22 11:37:44 +03:00
|
|
|
}
|
|
|
|
|
2020-01-13 16:16:35 +03:00
|
|
|
downloadLink() {
|
2021-06-17 10:42:08 +03:00
|
|
|
if (this.props.query.period !== 'realtime') {
|
|
|
|
const endpoint = `/${encodeURIComponent(this.props.site.domain)}/visitors.csv${api.serializeQuery(this.props.query)}`
|
2020-01-13 16:16:35 +03:00
|
|
|
|
2021-06-17 10:42:08 +03:00
|
|
|
return (
|
|
|
|
<a href={endpoint} download>
|
|
|
|
<svg className="absolute w-4 h-5 text-gray-700 feather dark:text-gray-300 -top-8 right-8" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round"><path d="M21 15v4a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2v-4"></path><polyline points="7 10 12 15 17 10"></polyline><line x1="12" y1="15" x2="12" y2="3"></line></svg>
|
|
|
|
</a>
|
|
|
|
)
|
|
|
|
}
|
2020-01-13 16:16:35 +03:00
|
|
|
}
|
|
|
|
|
2021-03-24 12:19:10 +03:00
|
|
|
samplingNotice() {
|
|
|
|
const samplePercent = this.props.graphData.sample_percent
|
|
|
|
|
|
|
|
if (samplePercent < 100) {
|
|
|
|
return (
|
2021-08-18 12:17:45 +03:00
|
|
|
<div tooltip={`Stats based on a ${samplePercent}% sample of all visitors`} className="absolute cursor-pointer -top-8 right-14 lg:-top-20 lg:right-8">
|
2021-03-24 12:19:10 +03:00
|
|
|
<svg className="w-4 h-4 text-gray-300 text-gray-700" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke="currentColor">
|
2021-03-25 12:55:15 +03:00
|
|
|
<path strokeLinecap="round" strokeLinejoin="round" strokeWidth="2" d="M13 16h-1v-4h-1m1-4h.01M21 12a9 9 0 11-18 0 9 9 0 0118 0z" />
|
2021-03-24 12:19:10 +03:00
|
|
|
</svg>
|
|
|
|
</div>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-19 07:30:42 +03:00
|
|
|
render() {
|
2019-11-25 12:17:18 +03:00
|
|
|
const extraClass = this.props.graphData.interval === 'hour' ? '' : 'cursor-pointer'
|
2019-11-19 07:30:42 +03:00
|
|
|
|
|
|
|
return (
|
2021-03-15 16:56:12 +03:00
|
|
|
<div className="graph-inner">
|
2020-01-13 16:16:35 +03:00
|
|
|
<div className="flex flex-wrap">
|
2021-01-21 15:42:55 +03:00
|
|
|
{ this.renderTopStats() }
|
2019-11-19 07:30:42 +03:00
|
|
|
</div>
|
2021-03-15 16:56:12 +03:00
|
|
|
<div className="relative px-2">
|
2021-01-21 15:42:55 +03:00
|
|
|
{ this.downloadLink() }
|
2021-03-24 12:19:10 +03:00
|
|
|
{ this.samplingNotice() }
|
2019-11-19 07:30:42 +03:00
|
|
|
<canvas id="main-graph-canvas" className={'mt-4 ' + extraClass} width="1054" height="342"></canvas>
|
|
|
|
</div>
|
2021-03-15 16:56:12 +03:00
|
|
|
</div>
|
2019-11-19 07:30:42 +03:00
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
LineGraph = withRouter(LineGraph)
|
|
|
|
|
|
|
|
export default class VisitorGraph extends React.Component {
|
|
|
|
constructor(props) {
|
|
|
|
super(props)
|
2021-01-21 15:42:55 +03:00
|
|
|
this.state = {loading: true}
|
2021-03-25 12:55:15 +03:00
|
|
|
this.onVisible = this.onVisible.bind(this)
|
2019-11-19 07:30:42 +03:00
|
|
|
}
|
|
|
|
|
2021-03-25 12:55:15 +03:00
|
|
|
onVisible() {
|
2019-11-19 07:30:42 +03:00
|
|
|
this.fetchGraphData()
|
2020-07-14 16:52:26 +03:00
|
|
|
if (this.props.timer) this.props.timer.onTick(this.fetchGraphData.bind(this))
|
2019-11-19 07:30:42 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
componentDidUpdate(prevProps) {
|
|
|
|
if (this.props.query !== prevProps.query) {
|
2021-01-21 15:42:55 +03:00
|
|
|
this.setState({loading: true, graphData: null})
|
2019-11-19 07:30:42 +03:00
|
|
|
this.fetchGraphData()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fetchGraphData() {
|
2020-02-04 16:44:13 +03:00
|
|
|
api.get(`/api/stats/${encodeURIComponent(this.props.site.domain)}/main-graph`, this.props.query)
|
2019-11-19 07:30:42 +03:00
|
|
|
.then((res) => {
|
2021-01-21 15:42:55 +03:00
|
|
|
this.setState({loading: false, graphData: res})
|
2019-11-19 07:30:42 +03:00
|
|
|
return res
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
renderInner() {
|
2020-03-03 12:13:08 +03:00
|
|
|
if (this.state.graphData) {
|
2019-11-19 07:30:42 +03:00
|
|
|
return (
|
2020-12-16 12:57:28 +03:00
|
|
|
<ThemeContext.Consumer>
|
|
|
|
{theme => (
|
2021-01-21 15:42:55 +03:00
|
|
|
<LineGraph graphData={this.state.graphData} site={this.props.site} query={this.props.query} darkTheme={theme}/>
|
2020-12-16 12:57:28 +03:00
|
|
|
)}
|
|
|
|
</ThemeContext.Consumer>
|
2019-11-19 07:30:42 +03:00
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
|
|
|
return (
|
2021-03-25 12:55:15 +03:00
|
|
|
<LazyLoader onVisible={this.onVisible}>
|
2021-07-21 16:50:26 +03:00
|
|
|
<div className="relative w-full bg-white rounded shadow-xl dark:bg-gray-825 main-graph">
|
2021-03-25 12:55:15 +03:00
|
|
|
{ this.state.loading && <div className="graph-inner"><div className="pt-24 mx-auto loading sm:pt-32 md:pt-48"><div></div></div></div> }
|
|
|
|
{ this.renderInner() }
|
|
|
|
</div>
|
|
|
|
</LazyLoader>
|
2019-11-19 07:30:42 +03:00
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|