Skip to content

📈 Open source, privacy-first web analytics — a cookie-less Google Analytics alternative with built-in error tracking and performance monitoring. Self-host it or use our cloud. Get started for free!

4.9 KB
Raw Blame
import type { ChartOptions } from 'billboard.js'
import { area, bar, line } from 'billboard.js'
import * as d3 from 'd3'
import dayjs from 'dayjs'
import { useMemo } from 'react'

import BillboardChart from '~/ui/BillboardChart'
import { cn, nFormatter } from '~/utils/generic'

import type { SeriesPoint } from './types'

type AdminChartType = 'area' | 'bar' | 'line'

const CHART_TYPES = { area, bar, line }

export interface AdminChartSeries {
  id: string
  name: string
  color: string
  data: SeriesPoint[]
  type?: AdminChartType
}

interface AdminChartFormat {
  // Tick label under each point ('%b %d' for daily, '%b %Y' for monthly)
  xTick?: string
  // Heading of the tooltip
  xTooltip?: string
  // Exact value shown in the tooltip
  value?: (value: number) => string
  // Abbreviated value shown on the y axis
  axis?: (value: number) => string
}

// Same palette as the main analytics dashboard chart
export const ADMIN_CHART_COLORS = {
  blue: '#2563EB',
  amber: '#D97706',
  purple: '#c945ed',
  teal: '#2AC4B3',
  green: '#10B981',
} as const

const DEFAULT_TOOLTIP_FORMAT = '%a, %b %d, %Y'

// Chart options mirroring the main dashboard's look (gradient areas, circle
// legend, custom tooltip) instead of billboard.js defaults
const buildOptions = (
  series: AdminChartSeries[],
  format: AdminChartFormat,
): ChartOptions => {
  const dates = Array.from(
    new Set(series.flatMap(({ data }) => data.map(({ date }) => date))),
  ).sort()

  const columns: [string, ...(Date | number)[]][] = [
    ['x', ...dates.map((date) => dayjs(date).toDate())],
  ]

  for (const { id, data } of series) {
    const countByDate = new Map(data.map(({ date, count }) => [date, count]))
    columns.push([id, ...dates.map((date) => countByDate.get(date) || 0)])
  }

  const tooltipDateFormat = d3.timeFormat(
    format.xTooltip || DEFAULT_TOOLTIP_FORMAT,
  )
  const formatValue =
    format.value || ((value: number) => value.toLocaleString('en-US'))

  return {
    data: {
      x: 'x',
      columns,
      types: Object.fromEntries(
        series.map(({ id, type }) => [id, CHART_TYPES[type || 'area']()]),
      ),
      colors: Object.fromEntries(series.map(({ id, color }) => [id, color])),
      names: Object.fromEntries(series.map(({ id, name }) => [id, name])),
    },
    area: {
      linearGradient: true,
    },
    bar: {
      width: {
        ratio: 0.6,
      },
      radius: {
        ratio: 0.15,
      },
    },
    grid: {
      y: {
        show: true,
      },
    },
    transition: {
      duration: 200,
    },
    resize: {
      auto: true,
      timer: true,
    },
    axis: {
      x: {
        type: 'timeseries',
        clipPath: false,
        tick: {
          fit: false,
          format: format.xTick || '%b %d',
        },
      },
      y: {
        tick: {
          format:
            format.axis ||
            ((d: number) => (Number.isInteger(d) ? nFormatter(d, 1) : '')),
        },
        show: true,
        inner: true,
      },
    },
    point: {
      focus: {
        only: dates.length > 1,
      },
      pattern: ['circle'],
      r: 4,
    },
    legend: {
      show: series.length > 1,
      item: {
        tile: {
          type: 'circle',
          width: 10,
          r: 3,
        },
      },
    },
    tooltip: {
      contents: (items, _, __, color) => {
        const rows = items
          .map(
            (item) => `
              <li class='flex justify-between items-center py-px leading-snug'>
                <div class='flex items-center min-w-0 mr-4'>
                  <div class='w-2.5 h-2.5 rounded-xs mr-1.5 shrink-0' style='background-color:${color(item.id)}'></div>
                  <span class='truncate'>${item.name || item.id}</span>
                </div>
                <span class='tabular-nums whitespace-nowrap'>${formatValue(Number(item.value))}</span>
              </li>`,
          )
          .join('')

        return `<ul class='bg-gray-50 dark:text-gray-50 dark:bg-slate-900 rounded-md ring-1 ring-black/10 px-2 py-1 text-xs md:text-sm shadow-md z-50'>
          <li class='font-semibold pb-1 mb-1 border-b border-gray-200 dark:border-slate-800'>${tooltipDateFormat(items[0].x as unknown as Date)}</li>
          ${rows}
        </ul>`
      },
    },
  }
}

// Module-level so the default does not change identity between renders and
// re-generate the chart
const NO_FORMAT: AdminChartFormat = {}

export const AdminChart = ({
  series,
  className,
  format = NO_FORMAT,
}: {
  series: AdminChartSeries[]
  className?: string
  format?: AdminChartFormat
}) => {
  const options = useMemo(() => buildOptions(series, format), [series, format])

  return (
    <BillboardChart
      options={options}
      // admin-chart scopes the ProjectViewStyle.css overrides (solid bars, no
      // stroke) - admin series ids never match the project view's per-target
      // stroke rules, so without it bars render with a black outline
      className={cn('admin-chart', className)}
      deps={[series, format]}
    />
  )
}