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: Graph Tooltip (#2685)
Vini Brasil565036c9b2eb
diff --git a/assets/js/dashboard/stats/graph/graph-tooltip.js b/assets/js/dashboard/stats/graph/graph-tooltip.jsnew file mode 100644index 0000000000..bdefc6826d--- /dev/null+++ b/assets/js/dashboard/stats/graph/graph-tooltip.js@@ -0,0 +1,120 @@+import { METRIC_FORMATTER, METRIC_LABELS } from './graph-util.js'+import dateFormatter from './date-formatter.js'++const renderBucketLabel = function(query, graphData, label, comparison = false) {+ let isPeriodFull = graphData.full_intervals?.[label]+ if (comparison) isPeriodFull = true++ const formattedLabel = dateFormatter({+ interval: graphData.interval, longForm: true, period: query.period, isPeriodFull,+ })(label)++ if (query.period === 'realtime') {+ return dateFormatter({+ interval: graphData.interval, longForm: true, period: query.period,+ })(label)+ }++ if (graphData.interval === 'hour' || graphData.interval == 'minute') {+ const date = dateFormatter({ interval: "date", longForm: true, period: query.period })(label)+ return `${date}, ${formattedLabel}`+ }++ return formattedLabel+}++const calculatePercentageDifference = function(oldValue, newValue) {+ if (oldValue == 0 && newValue > 0) {+ return 100+ } else if (oldValue == 0 && newValue == 0) {+ return 0+ } else {+ return Math.round((newValue - oldValue) / oldValue * 100)+ }+}++const buildTooltipData = function(query, graphData, metric, tooltipModel) {+ const data = tooltipModel.dataPoints.find((dataPoint) => dataPoint.dataset.yAxisID == "y")+ const comparisonData = tooltipModel.dataPoints.find((dataPoint) => dataPoint.dataset.yAxisID == "yComparison")++ const label = renderBucketLabel(query, graphData, graphData.labels[data.dataIndex])+ const comparisonLabel = comparisonData && renderBucketLabel(query, graphData, graphData.comparison_labels[data.dataIndex], true)++ const value = data?.raw || 0+ const comparisonValue = comparisonData?.raw || 0+ const comparisonDifference = comparisonData && calculatePercentageDifference(comparisonValue, value)++ const metricFormatter = METRIC_FORMATTER[metric]+ const formattedValue = metricFormatter(value)+ const formattedComparisonValue = comparisonData && metricFormatter(comparisonValue)++ return { label, formattedValue, comparisonLabel, formattedComparisonValue, comparisonDifference }+}++export default function GraphTooltip(graphData, metric, query) {+ return (context) => {+ const tooltipModel = context.tooltip+ const offset = document.getElementById("main-graph-canvas").getBoundingClientRect()+ let tooltipEl = document.getElementById('chartjs-tooltip')++ if (!tooltipEl) {+ tooltipEl = document.createElement('div')+ tooltipEl.id = 'chartjs-tooltip'+ tooltipEl.style.display = 'none'+ tooltipEl.style.opacity = 0+ document.body.appendChild(tooltipEl)+ }++ if (tooltipEl && offset && window.innerWidth < 768) {+ tooltipEl.style.top = offset.y + offset.height + window.scrollY + 15 + 'px'+ tooltipEl.style.left = offset.x + 'px'+ tooltipEl.style.right = null+ tooltipEl.style.opacity = 1+ }++ if (tooltipModel.opacity === 0) {+ tooltipEl.style.display = 'none'+ return+ }++ if (tooltipModel.body) {+ const tooltipData = buildTooltipData(query, graphData, metric, tooltipModel)++ tooltipEl.innerHTML = `+ <aside class="text-gray-100 flex flex-col">+ <div class="flex justify-between items-center">+ <span class="font-semibold mr-4 text-lg">${METRIC_LABELS[metric]}</span>+ <div class="inline-flex items-center space-x-1">+ ${tooltipData.comparisonDifference > 0 ? `<span class="font-semibold text-sm text-green-500">↑</span><span>${tooltipData.comparisonDifference}%</span>` : ""}+ ${tooltipData.comparisonDifference < 0 ? `<span class="font-semibold text-sm text-red-400">↓</span><span>${tooltipData.comparisonDifference * -1}%</span>` : ""}+ ${tooltipData.comparisonDifference == 0 ? `<span class="font-semibold text-sm">〰 0%</span>` : ""}+ </div>+ </div>++ <div class="flex flex-col">+ <div class="flex flex-row justify-between items-center">+ <span class="flex items-center mr-4">+ <div class="w-3 h-3 mr-1 rounded-full" style="background-color: rgba(101,116,205)"></div>+ <span>${tooltipData.label}</span>+ </span>+ <span class="text-base font-bold">${tooltipData.formattedValue}</span>+ </div>++ ${tooltipData.comparisonLabel ?+ `<div class="flex flex-row justify-between items-center">+ <span class="flex items-center mr-4">+ <div class="w-3 h-3 mr-1 rounded-full bg-gray-500"></div>+ <span>${tooltipData.comparisonLabel}</span>+ </span>+ <span class="text-base font-bold">${tooltipData.formattedComparisonValue}</span>+ </div>` : ""}+ </div>++ ${graphData.interval === "month" ? `<span class="font-semibold italic">Click to view month</span>` : ""}+ ${graphData.interval === "date" ? `<span class="font-semibold italic">Click to view day</span>` : ""}+ </aside>+ `+ }+ tooltipEl.style.display = null+ }+}diff --git a/assets/js/dashboard/stats/graph/graph-util.js b/assets/js/dashboard/stats/graph/graph-util.jsindex e5d59934e0..1b6b58e633 100644--- a/assets/js/dashboard/stats/graph/graph-util.js+++ b/assets/js/dashboard/stats/graph/graph-util.js@@ -1,5 +1,4 @@import numberFormatter, {durationFormatter} from '../../util/number-formatter'-import dateFormatter from './date-formatter.js'export const METRIC_MAPPING = {'Unique visitors (last 30 min)': 'visitors',@@ -35,123 +34,6 @@ export const LoadingState = {isLoadedOrRefreshing: function (state) { return [this.loaded, this.refreshing].includes(state) }}-const renderBucketLabel = function(query, graphData, label, comparison = false) {- let isPeriodFull = graphData.full_intervals?.[label]- if (comparison) isPeriodFull = true-- const formattedLabel = dateFormatter({- interval: graphData.interval, longForm: true, period: query.period, isPeriodFull,- })(label)-- if (query.period === 'realtime') {- return dateFormatter({- interval: graphData.interval, longForm: true, period: query.period,- })(label)- }-- if (graphData.interval === 'hour' || graphData.interval == 'minute') {- const date = dateFormatter({ interval: "date", longForm: true, period: query.period })(label)- return `${date}, ${formattedLabel}`- }-- return formattedLabel-}--const calculatePercentageDifference = function(oldValue, newValue) {- if (oldValue == 0 && newValue > 0) {- return 100- } else if (oldValue == 0 && newValue == 0) {- return 0- } else {- return Math.round((newValue - oldValue) / oldValue * 100)- }-}--const buildTooltipData = function(query, graphData, metric, tooltipModel) {- const data = tooltipModel.dataPoints.find((dataPoint) => dataPoint.dataset.yAxisID == "y")- const comparisonData = tooltipModel.dataPoints.find((dataPoint) => dataPoint.dataset.yAxisID == "yComparison")-- const label = renderBucketLabel(query, graphData, graphData.labels[data.dataIndex])- const comparisonLabel = comparisonData && renderBucketLabel(query, graphData, graphData.comparison_labels[data.dataIndex], true)-- const value = data?.raw || 0- const comparisonValue = comparisonData?.raw || 0- const comparisonDifference = comparisonData && calculatePercentageDifference(comparisonValue, value)-- const metricFormatter = METRIC_FORMATTER[metric]- const formattedValue = metricFormatter(value)- const formattedComparisonValue = comparisonData && metricFormatter(comparisonValue)-- return { label, formattedValue, comparisonLabel, formattedComparisonValue, comparisonDifference }-}--export const GraphTooltip = (graphData, metric, query) => {- return (context) => {- const tooltipModel = context.tooltip;- const offset = document.getElementById("main-graph-canvas").getBoundingClientRect()-- // Tooltip Element- let tooltipEl = document.getElementById('chartjs-tooltip');-- // Create element on first render- if (!tooltipEl) {- tooltipEl = document.createElement('div');- tooltipEl.id = 'chartjs-tooltip';- tooltipEl.style.display = 'none';- tooltipEl.style.opacity = 0;- document.body.appendChild(tooltipEl);- }-- if (tooltipEl && offset && window.innerWidth < 768) {- tooltipEl.style.top = offset.y + offset.height + window.scrollY + 15 + 'px'- tooltipEl.style.left = offset.x + 'px'- tooltipEl.style.right = null;- tooltipEl.style.opacity = 1;- }-- // Stop if no tooltip showing- if (tooltipModel.opacity === 0) {- tooltipEl.style.display = 'none';- return;- }-- // Set Tooltip Body- if (tooltipModel.body) {- const tooltipData = buildTooltipData(query, graphData, metric, tooltipModel)-- let innerHtml = `- <div class='text-gray-100 flex flex-col'>- <div class='flex justify-between items-center'>- <span class='font-semibold mr-4 text-lg'>${METRIC_LABELS[metric]}</span>- ${tooltipData.comparisonDifference ? `<span class='font-semibold text-sm'>${tooltipData.comparisonDifference}%</span>` : ''}- </div>- <div class='flex flex-col'>- <div class='flex flex-row justify-between items-center'>- <span class='flex items-center mr-4'>- <div class='w-3 h-3 mr-1 rounded-full' style='background-color: rgba(101,116,205)'></div>- <span>${tooltipData.label}</span>- </span>- <span class='text-base font-bold'>${tooltipData.formattedValue}</span>- </div>-- ${tooltipData.comparisonLabel ? `<div class='flex flex-row justify-between items-center'>- <span class='flex items-center mr-4'>- <div class='w-3 h-3 mr-1 rounded-full bg-gray-500'></div>- <span>${tooltipData.comparisonLabel}</span>- </span>- <span class='text-base font-bold'>${tooltipData.formattedComparisonValue}</span>- </div>- </div>- <span class='font-semibold italic'>${graphData.interval === 'month' ? 'Click to view month' : graphData.interval === 'date' ? 'Click to view day' : ''}</span>- </div>` : ''}- `;-- tooltipEl.innerHTML = innerHtml;- }- tooltipEl.style.display = null;- }-}-const truncateToPresentIndex = function(array, presentIndex) {return array.slice(0, presentIndex)}diff --git a/assets/js/dashboard/stats/graph/visitor-graph.js b/assets/js/dashboard/stats/graph/visitor-graph.jsindex 4c84c96756..ea2bff4f2d 100644--- a/assets/js/dashboard/stats/graph/visitor-graph.js+++ b/assets/js/dashboard/stats/graph/visitor-graph.js@@ -5,7 +5,8 @@ import { navigateToQuery } from '../../query'import * as api from '../../api'import * as storage from '../../util/storage'import LazyLoader from '../../components/lazy-loader'-import {GraphTooltip, buildDataSet, METRIC_MAPPING, METRIC_LABELS, METRIC_FORMATTER, LoadingState} from './graph-util';+import GraphTooltip from './graph-tooltip'+import { buildDataSet, METRIC_MAPPING, METRIC_LABELS, METRIC_FORMATTER, LoadingState } from './graph-util'import dateFormatter from './date-formatter';import TopStats from './top-stats';import { IntervalPicker, getStoredInterval, storeInterval } from './interval-picker';