diff --git a/assets/js/dashboard/stats/graph/line-graph.js b/assets/js/dashboard/stats/graph/line-graph.js new file mode 100644 index 000000000..ba5b55355 --- /dev/null +++ b/assets/js/dashboard/stats/graph/line-graph.js @@ -0,0 +1,236 @@ +import React from 'react'; +import { withRouter } from 'react-router-dom' +import Chart from 'chart.js/auto'; +import { navigateToQuery } from '../../query' +import GraphTooltip from './graph-tooltip' +import { buildDataSet, METRIC_LABELS, METRIC_FORMATTER } from './graph-util' +import dateFormatter from './date-formatter'; +import FadeIn from '../../fade-in'; +import classNames from 'classnames'; + +const calculateMaximumY = function(dataset) { + const yAxisValues = dataset + .flatMap((item) => item.data) + .map((item) => item || 0) + + if (yAxisValues) { + return Math.max(...yAxisValues) + } else { + return 1 + } +} + +class LineGraph extends React.Component { + constructor(props) { + super(props); + this.regenerateChart = this.regenerateChart.bind(this); + this.updateWindowDimensions = this.updateWindowDimensions.bind(this); + } + + regenerateChart() { + const { graphData, metric, query } = this.props + const graphEl = document.getElementById("main-graph-canvas") + this.ctx = graphEl.getContext('2d'); + const dataSet = buildDataSet(graphData.plot, graphData.comparison_plot, graphData.present_index, this.ctx, METRIC_LABELS[metric]) + + return new Chart(this.ctx, { + type: 'line', + data: { + labels: graphData.labels, + datasets: dataSet + }, + options: { + animation: false, + plugins: { + legend: { display: false }, + tooltip: { + enabled: false, + mode: 'index', + intersect: false, + position: 'average', + external: GraphTooltip(graphData, metric, query) + }, + }, + responsive: true, + maintainAspectRatio: false, + onResize: this.updateWindowDimensions, + elements: { line: { tension: 0 }, point: { radius: 0 } }, + onClick: this.maybeHopToHoveredPeriod.bind(this), + scale: { + ticks: { precision: 0, maxTicksLimit: 8 } + }, + scales: { + y: { + min: 0, + suggestedMax: calculateMaximumY(dataSet), + ticks: { + callback: METRIC_FORMATTER[metric], + color: this.props.darkTheme ? 'rgb(243, 244, 246)' : undefined + }, + grid: { + zeroLineColor: 'transparent', + drawBorder: false, + } + }, + yComparison: { + min: 0, + suggestedMax: calculateMaximumY(dataSet), + display: false, + grid: { display: false }, + }, + x: { + grid: { display: false }, + ticks: { + callback: function(val, _index, _ticks) { + if (this.getLabelForValue(val) == "__blank__") return "" + + const hasMultipleYears = + graphData.labels + .filter((date) => typeof date === 'string') + .map(date => date.split('-')[0]) + .filter((value, index, list) => list.indexOf(value) === index) + .length > 1 + + if (graphData.interval === 'hour' && query.period !== 'day') { + const date = dateFormatter({ + interval: "date", + longForm: false, + period: query.period, + shouldShowYear: hasMultipleYears, + })(this.getLabelForValue(val)) + + const hour = dateFormatter({ + interval: graphData.interval, + longForm: false, + period: query.period, + shouldShowYear: hasMultipleYears, + })(this.getLabelForValue(val)) + + // Returns a combination of date and hour. This is because + // small intervals like hour may return multiple days + // depending on the query period. + return `${date}, ${hour}` + } + + if (graphData.interval === 'minute' && query.period !== 'realtime') { + return dateFormatter({ + interval: "hour", longForm: false, period: query.period, + })(this.getLabelForValue(val)) + } + + return dateFormatter({ + interval: graphData.interval, longForm: false, period: query.period, shouldShowYear: hasMultipleYears, + })(this.getLabelForValue(val)) + }, + color: this.props.darkTheme ? 'rgb(243, 244, 246)' : undefined + } + } + }, + interaction: { + mode: 'index', + intersect: false, + } + } + }); + } + + repositionTooltip(e) { + const tooltipEl = document.getElementById('chartjs-tooltip'); + if (tooltipEl && window.innerWidth >= 768) { + if (e.clientX > 0.66 * window.innerWidth) { + tooltipEl.style.right = (window.innerWidth - e.clientX) + window.pageXOffset + 'px' + tooltipEl.style.left = null; + } else { + tooltipEl.style.right = null; + tooltipEl.style.left = e.clientX + window.pageXOffset + 'px' + } + tooltipEl.style.top = e.clientY + window.pageYOffset + 'px' + tooltipEl.style.opacity = 1; + } + } + + componentDidMount() { + if (this.props.graphData) { + this.chart = this.regenerateChart(); + } + window.addEventListener('mousemove', this.repositionTooltip); + } + + componentDidUpdate(prevProps) { + const { graphData, darkTheme } = this.props; + const tooltip = document.getElementById('chartjs-tooltip'); + + if ( + graphData !== prevProps.graphData || + darkTheme !== prevProps.darkTheme + ) { + + if (graphData) { + if (this.chart) { + this.chart.destroy(); + } + this.chart = this.regenerateChart(); + this.chart.update(); + } + + if (tooltip) { + tooltip.style.display = 'none'; + } + } + + if (!graphData) { + if (this.chart) { + this.chart.destroy(); + } + + if (tooltip) { + tooltip.style.display = 'none'; + } + } + } + + componentWillUnmount() { + // Ensure that the tooltip doesn't hang around when we are loading more data + const tooltip = document.getElementById('chartjs-tooltip'); + if (tooltip) { + tooltip.style.opacity = 0; + tooltip.style.display = 'none'; + } + window.removeEventListener('mousemove', this.repositionTooltip) + } + + /** + * 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 + } + + maybeHopToHoveredPeriod(e) { + const element = this.chart.getElementsAtEventForMode(e, 'index', { intersect: false })[0] + const date = this.props.graphData.labels[element.index] || this.props.graphData.comparison_labels[element.index] + + if (this.props.graphData.interval === 'month') { + navigateToQuery(this.props.history, this.props.query, { period: 'month', date }) + } else if (this.props.graphData.interval === 'date') { + navigateToQuery(this.props.history, this.props.query, { period: 'day', date }) + } + } + + render() { + const { graphData } = this.props + const canvasClass = classNames('mt-4 select-none', { 'cursor-pointer': !['minute', 'hour'].includes(graphData?.interval) }) + + return ( + +
+ +
+
+ ) + } +} + +export default withRouter(LineGraph) \ No newline at end of file diff --git a/assets/js/dashboard/stats/graph/visitor-graph.js b/assets/js/dashboard/stats/graph/visitor-graph.js index 16841d035..caf0eee1c 100644 --- a/assets/js/dashboard/stats/graph/visitor-graph.js +++ b/assets/js/dashboard/stats/graph/visitor-graph.js @@ -1,346 +1,16 @@ import React from 'react'; -import { withRouter, Link } from 'react-router-dom' -import Chart from 'chart.js/auto'; -import { navigateToQuery } from '../../query' +import { Link } from 'react-router-dom' import * as api from '../../api' import * as storage from '../../util/storage' import LazyLoader from '../../components/lazy-loader' -import GraphTooltip from './graph-tooltip' -import { buildDataSet, METRIC_MAPPING, METRIC_LABELS, METRIC_FORMATTER, LoadingState } from './graph-util' -import dateFormatter from './date-formatter'; +import { METRIC_MAPPING, LoadingState } from './graph-util' 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 { monthsBetweenDates, parseNaiveDate, isBefore } from '../../util/date' import { isComparisonEnabled } from '../../comparison-input' -import { BarsArrowUpIcon } from '@heroicons/react/20/solid' - -const calculateMaximumY = function(dataset) { - const yAxisValues = dataset - .flatMap((item) => item.data) - .map((item) => item || 0) - - if (yAxisValues) { - return Math.max(...yAxisValues) - } else { - return 1 - } -} - -class LineGraph extends React.Component { - constructor(props) { - super(props); - this.regenerateChart = this.regenerateChart.bind(this); - this.updateWindowDimensions = this.updateWindowDimensions.bind(this); - } - - regenerateChart() { - const { graphData, metric, query } = this.props - const graphEl = document.getElementById("main-graph-canvas") - this.ctx = graphEl.getContext('2d'); - const dataSet = buildDataSet(graphData.plot, graphData.comparison_plot, graphData.present_index, this.ctx, METRIC_LABELS[metric]) - - return new Chart(this.ctx, { - type: 'line', - data: { - labels: graphData.labels, - datasets: dataSet - }, - options: { - animation: false, - plugins: { - legend: { display: false }, - tooltip: { - enabled: false, - mode: 'index', - intersect: false, - position: 'average', - external: GraphTooltip(graphData, metric, query) - }, - }, - responsive: true, - maintainAspectRatio: false, - onResize: this.updateWindowDimensions, - elements: { line: { tension: 0 }, point: { radius: 0 } }, - onClick: this.maybeHopToHoveredPeriod.bind(this), - scale: { - ticks: { precision: 0, maxTicksLimit: 8 } - }, - scales: { - y: { - min: 0, - suggestedMax: calculateMaximumY(dataSet), - ticks: { - callback: METRIC_FORMATTER[metric], - color: this.props.darkTheme ? 'rgb(243, 244, 246)' : undefined - }, - grid: { - zeroLineColor: 'transparent', - drawBorder: false, - } - }, - yComparison: { - min: 0, - suggestedMax: calculateMaximumY(dataSet), - display: false, - grid: { display: false }, - }, - x: { - grid: { display: false }, - ticks: { - callback: function(val, _index, _ticks) { - if (this.getLabelForValue(val) == "__blank__") return "" - - const hasMultipleYears = - graphData.labels - .filter((date) => typeof date === 'string') - .map(date => date.split('-')[0]) - .filter((value, index, list) => list.indexOf(value) === index) - .length > 1 - - if (graphData.interval === 'hour' && query.period !== 'day') { - const date = dateFormatter({ - interval: "date", - longForm: false, - period: query.period, - shouldShowYear: hasMultipleYears, - })(this.getLabelForValue(val)) - - const hour = dateFormatter({ - interval: graphData.interval, - longForm: false, - period: query.period, - shouldShowYear: hasMultipleYears, - })(this.getLabelForValue(val)) - - // Returns a combination of date and hour. This is because - // small intervals like hour may return multiple days - // depending on the query period. - return `${date}, ${hour}` - } - - if (graphData.interval === 'minute' && query.period !== 'realtime') { - return dateFormatter({ - interval: "hour", longForm: false, period: query.period, - })(this.getLabelForValue(val)) - } - - return dateFormatter({ - interval: graphData.interval, longForm: false, period: query.period, shouldShowYear: hasMultipleYears, - })(this.getLabelForValue(val)) - }, - color: this.props.darkTheme ? 'rgb(243, 244, 246)' : undefined - } - } - }, - interaction: { - mode: 'index', - intersect: false, - } - } - }); - } - - repositionTooltip(e) { - const tooltipEl = document.getElementById('chartjs-tooltip'); - if (tooltipEl && window.innerWidth >= 768) { - if (e.clientX > 0.66 * window.innerWidth) { - tooltipEl.style.right = (window.innerWidth - e.clientX) + window.pageXOffset + 'px' - tooltipEl.style.left = null; - } else { - tooltipEl.style.right = null; - tooltipEl.style.left = e.clientX + window.pageXOffset + 'px' - } - tooltipEl.style.top = e.clientY + window.pageYOffset + 'px' - tooltipEl.style.opacity = 1; - } - } - - componentDidMount() { - if (this.props.graphData) { - this.chart = this.regenerateChart(); - } - window.addEventListener('mousemove', this.repositionTooltip); - } - - componentDidUpdate(prevProps) { - const { graphData, darkTheme } = this.props; - const tooltip = document.getElementById('chartjs-tooltip'); - - if ( - graphData !== prevProps.graphData || - darkTheme !== prevProps.darkTheme - ) { - - if (graphData) { - if (this.chart) { - this.chart.destroy(); - } - this.chart = this.regenerateChart(); - this.chart.update(); - } - - if (tooltip) { - tooltip.style.display = 'none'; - } - } - - if (!graphData) { - if (this.chart) { - this.chart.destroy(); - } - - if (tooltip) { - tooltip.style.display = 'none'; - } - } - } - - componentWillUnmount() { - // Ensure that the tooltip doesn't hang around when we are loading more data - const tooltip = document.getElementById('chartjs-tooltip'); - if (tooltip) { - tooltip.style.opacity = 0; - tooltip.style.display = 'none'; - } - window.removeEventListener('mousemove', this.repositionTooltip) - } - - /** - * 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 - } - - maybeHopToHoveredPeriod(e) { - const element = this.chart.getElementsAtEventForMode(e, 'index', { intersect: false })[0] - const date = this.props.graphData.labels[element.index] || this.props.graphData.comparison_labels[element.index] - - if (this.props.graphData.interval === 'month') { - navigateToQuery(this.props.history, this.props.query, { period: 'month', date }) - } else if (this.props.graphData.interval === 'date') { - navigateToQuery(this.props.history, this.props.query, { period: 'day', date }) - } - } - -<<<<<<< HEAD - pollExportReady() { - if (document.cookie.includes('exporting')) { - setTimeout(this.pollExportReady.bind(this), 1000); - } else { - this.setState({ exported: false }) - } - } - - downloadSpinner() { - this.setState({ exported: true }); - document.cookie = "exporting="; - setTimeout(this.pollExportReady.bind(this), 1000); - } - - downloadLink() { - if (this.props.query.period !== 'realtime') { - - if (this.state.exported) { - return ( -
- - - - -
- ) - } else { - const interval = this.props.graphData?.interval - const queryParams = api.serializeQuery(this.props.query, [{ interval }]) - const endpoint = `/${encodeURIComponent(this.props.site.domain)}/export${queryParams}` - - return ( - - - - ) - } - } - } - - samplingNotice() { - const samplePercent = this.props.topStatData && this.props.topStatData.sample_percent - - if (samplePercent < 100) { - return ( -
- - - -
- ) - } - } - - importedNotice() { - if (!this.props.topStatData?.imports_exist) return - - const isBeforeNativeStats = (date) => { - if (!date) return false - - const nativeStatsBegin = parseNaiveDate(this.props.site.nativeStatsBegin) - const parsedDate = parseNaiveDate(date) - - return isBefore(parsedDate, nativeStatsBegin, "day") - } - - const isQueryingImportedPeriod = isBeforeNativeStats(this.props.topStatData.from) - const isComparingImportedPeriod = isBeforeNativeStats(this.props.topStatData.comparing_from) - - if (isQueryingImportedPeriod || isComparingImportedPeriod) { - const withImported = this.props.topStatData.with_imported; - const toggleColor = withImported ? " dark:text-gray-300 text-gray-700" : " dark:text-gray-500 text-gray-400" - const target = url.setQuery('with_imported', !withImported) - const tip = withImported ? "" : "do not "; - - return ( - -
- -
- - ) - } - } - - // 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 { graphData } = this.props - const canvasClass = classNames('mt-4 select-none', { 'cursor-pointer': !['minute', 'hour'].includes(graphData?.interval) }) - - return ( - -
- -
-
- ) - } -} - -const LineGraphWithRouter = withRouter(LineGraph) +import LineGraphWithRouter from './line-graph' export default class VisitorGraph extends React.Component { constructor(props) {