diff --git a/assets/css/app.css b/assets/css/app.css index 1a302b24b..1dbb7101e 100644 --- a/assets/css/app.css +++ b/assets/css/app.css @@ -287,13 +287,3 @@ blockquote { iframe[hidden] { display: none; } - -#chartjs-tooltip { - background-color: rgba(25, 30, 56); - position: absolute; - font-size: 14px; - font-style: normal; - padding: 10px 12px; - pointer-events: none; - border-radius: 5px; -} diff --git a/assets/js/dashboard/stats/visitor-graph.js b/assets/js/dashboard/stats/visitor-graph.js index 3556a6243..b61d78d33 100644 --- a/assets/js/dashboard/stats/visitor-graph.js +++ b/assets/js/dashboard/stats/visitor-graph.js @@ -2,72 +2,47 @@ import React from 'react'; import { withRouter } from 'react-router-dom' import Chart from 'chart.js' import { eventName, navigateToQuery } from '../query' -import numberFormatter, { durationFormatter } from '../number-formatter' +import numberFormatter, {durationFormatter} from '../number-formatter' import * as api from '../api' -import { ThemeContext } from '../theme-context' +import {ThemeContext} from '../theme-context' -function buildDataSet(plot, present_index, ctx, label, isPrevious) { +function buildDataSet(plot, present_index, ctx, label) { var gradient = ctx.createLinearGradient(0, 0, 0, 300); - var prev_gradient = ctx.createLinearGradient(0, 0, 0, 300); gradient.addColorStop(0, 'rgba(101,116,205, 0.2)'); gradient.addColorStop(1, 'rgba(101,116,205, 0)'); - prev_gradient.addColorStop(0, 'rgba(101,116,205, 0.075)'); - prev_gradient.addColorStop(1, 'rgba(101,116,205, 0)'); - - if (!isPrevious) { - if (present_index) { - var dashedPart = plot.slice(present_index - 1); - var dashedPlot = (new Array(plot.length - dashedPart.length)).concat(dashedPart) - const _plot = [...plot] - for (var i = present_index; i < _plot.length; i++) { - _plot[i] = undefined - } - return [{ + 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 [{ label: label, - data: _plot, + data: plot, borderWidth: 3, borderColor: 'rgba(101,116,205)', - pointHoverBackgroundColor: 'rgba(71, 87, 193)', - pointBorderColor: 'transparent', - pointHoverRadius: 4, + pointBackgroundColor: 'rgba(101,116,205)', backgroundColor: gradient, }, { label: label, data: dashedPlot, borderWidth: 3, - borderDash: [3, 3], + borderDash: [5, 10], borderColor: 'rgba(101,116,205)', - pointHoverBackgroundColor: 'rgba(71, 87, 193)', - pointBorderColor: 'transparent', - pointHoverRadius: 4, + pointBackgroundColor: 'rgba(101,116,205)', backgroundColor: gradient, - }] - } else { - return [{ - label: label, - data: plot, - borderWidth: 3, - borderColor: 'rgba(101,116,205)', - pointHoverBackgroundColor: 'rgba(71, 87, 193)', - pointBorderColor: 'transparent', - pointHoverRadius: 4, - backgroundColor: gradient, - }] - } + }] } else { return [{ label: label, data: plot, - borderWidth: 2, - // borderDash: [10, 1], - borderColor: 'rgba(166,187,210,0.5)', - pointHoverBackgroundColor: 'rgba(166,187,210,0.8)', - pointBorderColor: 'transparent', - pointHoverBorderColor: 'transparent', - pointHoverRadius: 4, - backgroundColor: prev_gradient, + borderWidth: 3, + borderColor: 'rgba(101,116,205)', + pointBackgroundColor: 'rgba(101,116,205)', + backgroundColor: gradient, }] } } @@ -88,7 +63,7 @@ const DAYS_ABBREV = [ ] function dateFormatter(interval, longForm) { - return function (isoDate) { + return function(isoDate) { let date = new Date(isoDate) if (interval === 'month') { @@ -100,7 +75,7 @@ function dateFormatter(interval, longForm) { return day + ', ' + date_ + ' ' + month; } else if (interval === 'hour') { const parts = isoDate.split(/[^0-9]/); - date = new Date(parts[0], parts[1] - 1, parts[2], parts[3], parts[4], parts[5]) + date = new Date(parts[0],parts[1]-1,parts[2],parts[3],parts[4],parts[5]) var hours = date.getHours(); // Not sure why getUTCHours doesn't work here var ampm = hours >= 12 ? 'pm' : 'am'; hours = hours % 12; @@ -124,147 +99,61 @@ class LineGraph extends React.Component { } regenerateChart() { - const { graphData } = this.props + const {graphData} = this.props this.ctx = document.getElementById("main-graph-canvas").getContext('2d'); 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) - const prev_dataSet = buildDataSet(graphData.prev_plot, false, this.ctx, label, true) return new Chart(this.ctx, { type: 'line', data: { labels: graphData.labels, - datasets: [...dataSet, ...prev_dataSet] + datasets: dataSet }, options: { animation: false, - legend: { display: false }, + legend: {display: false}, responsive: true, - elements: { line: { tension: 0.1 }, point: { radius: 0 } }, + elements: {line: {tension: 0}, point: {radius: 0}}, onClick: this.onClick.bind(this), - hover: { - mode: 'index', - intersect: false - }, tooltips: { - enabled: false, mode: 'index', - position: 'average', intersect: false, - custom: function (tooltipModel) { - // Canvas Offset from 0,0 - const offset = this._chart.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'; - document.body.appendChild(tooltipEl); - } - - if (tooltipEl && offset && window.innerWidth < 768) { - tooltipEl.style.top = offset.y + offset.height + window.pageYOffset + 'px' - tooltipEl.style.left = offset.x + window.pageXOffset + 'px' - } - - // Stop if no tooltip showing - if (tooltipModel.opacity === 0) { - tooltipEl.style.opacity = 0; - return; - } - - function getBody(bodyItem) { - return bodyItem.lines; - } - - // 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 - } - // Don't render if we only have one dataset available (this means the point is in the future) - if (bodyLines.length == 1) { - tooltipEl.style.opacity = 0; - return; - } - - const data = tooltipModel.dataPoints[0] - const prev_data = tooltipModel.dataPoints.slice(-1)[0] - const label = graphData.labels[data.index] - const prev_label = graphData.prev_labels[prev_data.index] - const point = data.yLabel || 0 - const prev_point = prev_data.yLabel || 0 - const pct_change = point === prev_point ? 0 : prev_point === 0 ? 100 : parseFloat(((point - prev_point) / prev_point * 100).toFixed(1)) - - function renderLabel(isPrevious) { - const formattedLabel = dateFormatter(graphData.interval, true)(label) - const prev_formattedLabel = dateFormatter(graphData.interval, true)(prev_label) - - if (graphData.interval === 'month') { - return !isPrevious ? `${formattedLabel} ${(new Date(label)).getUTCFullYear()}` : `${prev_formattedLabel} ${(new Date(prev_label)).getUTCFullYear()}` - } - - if (graphData.interval === 'date') { - return !isPrevious ? formattedLabel : prev_formattedLabel - } - - if (graphData.interval === 'hour') { - return !isPrevious ? `${dateFormatter("date", true)(label)}, ${formattedLabel}` : `${dateFormatter("date", true)(prev_label)}, ${dateFormatter(graphData.interval, true)(prev_label)}` - } - - return !isPrevious ? formattedLabel : prev_formattedLabel + xPadding: 10, + yPadding: 10, + titleFontSize: 18, + footerFontSize: 14, + bodyFontSize: 14, + backgroundColor: 'rgba(25, 30, 56)', + titleMarginBottom: 8, + bodySpacing: 6, + footerMarginTop: 8, + xPadding: 16, + yPadding: 12, + multiKeyBackground: 'none', + callbacks: { + title: function(dataPoints) { + const data = dataPoints[0] + return dateFormatter(graphData.interval, true)(data.xLabel) + }, + beforeBody: function() { + this.drawnLabels = {} + }, + label: function(item) { + const dataset = this._data.datasets[item.datasetIndex] + if (!this.drawnLabels[dataset.label]) { + this.drawnLabels[dataset.label] = true + const pluralizedLabel = item.yLabel === 1 ? dataset.label.slice(0, -1) : dataset.label + return ` ${item.yLabel} ${pluralizedLabel}` } - - function renderComparison(change) { - const formattedComparison = numberFormatter(Math.abs(change)) - - if (change > 0) { - return `${formattedComparison}%` - } - if (change < 0) { - return `${formattedComparison}%` - } - if (change === 0) { - return `0%` - } + }, + footer: function(dataPoints) { + if (graphData.interval === 'month') { + return 'Click to view month' + } else if (graphData.interval === 'date') { + return 'Click to view day' } - - let innerHtml = ` -