import numberFormatter, {durationFormatter} from '../../util/number-formatter' import dateFormatter from './date-formatter.js' export const METRIC_MAPPING = { 'Unique visitors (last 30 min)': 'visitors', 'Pageviews (last 30 min)': 'pageviews', 'Unique visitors': 'visitors', 'Visit duration': 'visit_duration', 'Total pageviews': 'pageviews', 'Bounce rate': 'bounce_rate', 'Unique conversions': 'conversions', } export const METRIC_LABELS = { 'visitors': 'Visitors', 'pageviews': 'Pageviews', 'bounce_rate': 'Bounce Rate', 'visit_duration': 'Visit Duration', 'conversions': 'Converted Visitors', } export const METRIC_FORMATTER = { 'visitors': numberFormatter, 'pageviews': numberFormatter, 'bounce_rate': (number) => (`${number}%`), 'visit_duration': durationFormatter, 'conversions': numberFormatter, } 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; } function getBody(bodyItem) { return bodyItem.lines; } // Returns a string describing the bucket. Used when hovering the graph to // show time buckets. function renderBucketLabel(label) { const isPeriodFull = graphData.full_intervals?.[label] const formattedLabel = dateFormatter(graphData.interval, true, query.period, isPeriodFull)(label) if (query.period === 'realtime') { return dateFormatter(graphData.interval, true, query.period)(label) } if (graphData.interval === 'hour' || graphData.interval == 'minute') { const date = dateFormatter("date", true, query.period)(label) return `${date}, ${formattedLabel}` } return formattedLabel } // Set Tooltip Body if (tooltipModel.body) { var bodyLines = tooltipModel.body.map(getBody); // Remove duplicated line on overlap between dashed and normal if (bodyLines.length == 3) { bodyLines[1] = false } const data = tooltipModel.dataPoints[0] const label = graphData.labels[data.dataIndex] const point = data.raw || 0 let innerHtml = `