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!

added chart for custom events

kruzhambus042bfa52926f
diff --git a/src/pages/Project/View/Panels.jsx b/src/pages/Project/View/Panels.jsx
index fb4e0c0e9..4c3e00491 100644
--- a/src/pages/Project/View/Panels.jsx
+++ b/src/pages/Project/View/Panels.jsx
@@ -3,7 +3,7 @@ import React, {
} from 'react'
import { ArrowSmUpIcon, ArrowSmDownIcon } from '@heroicons/react/solid'
import {
- FilterIcon, MapIcon, ViewListIcon, ArrowsExpandIcon,
+ FilterIcon, MapIcon, ViewListIcon, ArrowsExpandIcon, ChartPieIcon,
} from '@heroicons/react/outline'
import cx from 'clsx'
import PropTypes from 'prop-types'
@@ -20,6 +20,8 @@ import _sum from 'lodash/sum'
import Progress from 'ui/Progress'
import PulsatingCircle from 'ui/icons/PulsatingCircle'
import Modal from 'ui/Modal'
+import { pie } from 'billboard.js'
+import Chart from 'ui/Chart'
import InteractiveMap from './InteractiveMap'
import { iconClassName } from './ViewProject'
@@ -69,6 +71,24 @@ const PanelContainer = ({
/>
</div>
)}
+ { type === 'ce' && (
+ <div className='flex'>
+ <ViewListIcon
+ className={cx(iconClassName, 'cursor-pointer', {
+ 'text-blue-500': activeFragment === 0,
+ 'text-gray-900 dark:text-gray-50': activeFragment === 1,
+ })}
+ onClick={() => setActiveFragment(0)}
+ />
+ <ChartPieIcon
+ className={cx(iconClassName, 'ml-2 cursor-pointer', {
+ 'text-blue-500': activeFragment === 1,
+ 'text-gray-900 dark:text-gray-50': activeFragment === 0,
+ })}
+ onClick={() => setActiveFragment(1)}
+ />
+ </div>
+ )}
</div>
<div className='flex flex-col h-full scroll-auto'>
{children}
@@ -242,9 +262,47 @@ const CustomEvents = ({
}) => {
const keys = _keys(customs)
const uniques = _sum(chartData.uniques)
+ const [chartOptions, setChartOptions] = useState({})
+ const [activeFragment, setActiveFragment] = useState(0)
+
+ useEffect(() => {
+ if (!_isEmpty(chartData)) {
+ setChartOptions({
+ data: {
+ columns: _map(keys, (ev) => [ev, customs[ev]]),
+ type: pie(),
+ },
+ legend: {
+ show: false,
+ },
+ })
+ }
+ }, [chartData, customs])
+
+ if (activeFragment === 1 && !_isEmpty(chartData)) {
+ return (
+ <PanelContainer
+ name={t('project.customEv')}
+ type='ce'
+ setActiveFragment={setActiveFragment}
+ activeFragment={activeFragment}
+ >
+ {_isEmpty(chartData) ? (
+ <p className='mt-1 text-base text-gray-700 dark:text-gray-300'>
+ {t('project.noParamData')}
+ </p>
+ ) : (
+ <Chart
+ options={chartOptions}
+ current='Panels-ce'
+ />
+ )}
+ </PanelContainer>
+ )
+ }
return (
- <PanelContainer name={t('project.customEv')}>
+ <PanelContainer name={t('project.customEv')} type='ce' setActiveFragment={setActiveFragment} activeFragment={activeFragment}>
<table className='table-fixed'>
<thead>
<tr>
diff --git a/src/ui/Chart.jsx b/src/ui/Chart.jsx
new file mode 100644
index 000000000..c1313d141
--- /dev/null
+++ b/src/ui/Chart.jsx
@@ -0,0 +1,44 @@
+import React, { useEffect } from 'react'
+import bb from 'billboard.js'
+import 'billboard.js/dist/theme/datalab.css'
+import 'billboard.js/dist/billboard.css'
+
+const Chart = ({ current, children, options }) => {
+ let chartInstance = {}
+
+ const destroy = () => {
+ if (chartInstance !== null) {
+ try {
+ chartInstance.destroy()
+ } catch (error) {
+ console.error('Internal billboard.js error', error)
+ } finally {
+ chartInstance = null
+ }
+ }
+ }
+
+ const renderChart = () => {
+ if (current !== null) {
+ chartInstance = bb.generate({
+ ...options,
+ bindto: `#${current}`,
+ })
+ }
+ }
+
+ useEffect(() => {
+ renderChart()
+
+ return () => { destroy() }
+ })
+
+ return (
+ <div>
+ <div id={current} />
+ {children}
+ </div>
+ )
+}
+
+export default Chart