📈 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!
TypeScript chartsclickhousedockergdprgoogle-analyticsmetricsmonitoringmysqlnestjsprivacyredisself-hostedselfhostedstatisticstailwindcssweb-analyticswebanalyticswebsite
feature: bar chart and responsive fix
kruzhambus4cff14636b35
diff --git a/src/pages/Project/View/ViewProject.helpers.jsx b/src/pages/Project/View/ViewProject.helpers.jsxindex c51fc0cfd..289c76708 100644--- a/src/pages/Project/View/ViewProject.helpers.jsx+++ b/src/pages/Project/View/ViewProject.helpers.jsx@@ -5,7 +5,9 @@ import {} from '@heroicons/react/24/outline'import * as d3 from 'd3'import dayjs from 'dayjs'-import { area, areaSpline, spline } from 'billboard.js'+import {+ area, areaSpline, spline, bar,+} from 'billboard.js'import _forEach from 'lodash/forEach'import _map from 'lodash/map'import _split from 'lodash/split'@@ -19,7 +21,9 @@ import _reduce from 'lodash/reduce'import _last from 'lodash/last'import JSZip from 'jszip'-import { TimeFormat, tbsFormatMapper, tbsFormatMapper24h } from 'redux/constants'+import {+ TimeFormat, chartTypes, tbsFormatMapper, tbsFormatMapper24h,+} from 'redux/constants'import { getTimeFromSeconds, getStringFromTime, sumArrays } from 'utils/generic'import countries from 'utils/isoCountries'@@ -263,7 +267,7 @@ const getColumnsPerf = (chart, activeChartMetrics) => {const noRegionPeriods = ['custom', 'yesterday']// function to get the settings and data for the chart(main diagram)-const getSettings = (chart, timeBucket, activeChartMetrics, applyRegions, timeFormat, forecasedChartData, rotateXAxias) => {+const getSettings = (chart, timeBucket, activeChartMetrics, applyRegions, timeFormat, forecasedChartData, rotateXAxias, chartType) => {const xAxisSize = _size(chart.x)const lines = []const modifiedChart = { ...chart }@@ -330,13 +334,13 @@ const getSettings = (chart, timeBucket, activeChartMetrics, applyRegions, timeFox: 'x',columns: getColumns(modifiedChart, activeChartMetrics),types: {- unique: area(),- total: area(),- bounce: spline(),- viewsPerUnique: spline(),+ unique: chartType === chartTypes.line ? area() : bar(),+ total: chartType === chartTypes.line ? area() : bar(),+ bounce: chartType === chartTypes.line ? spline() : bar(),+ viewsPerUnique: chartType === chartTypes.line ? spline() : bar(),trendlineUnique: spline(),trendlineTotal: spline(),- sessionDuration: spline(),+ sessionDuration: chartType === chartTypes.line ? spline() : bar(),},colors: {unique: '#2563EB',@@ -421,14 +425,14 @@ const getSettings = (chart, timeBucket, activeChartMetrics, applyRegions, timeFolinearGradient: true,},padding: {- right: rotateXAxias ? 35 : 25,+ right: (rotateXAxias && !(activeChartMetrics.bounce || activeChartMetrics.sessionDuration)) && 35,left: 40,},bindto: '#dataChart',}}-const getSettingsPerf = (chart, timeBucket, activeChartMetrics, rotateXAxias) => {+const getSettingsPerf = (chart, timeBucket, activeChartMetrics, rotateXAxias, chartType) => {const xAxisSize = _size(chart.x)return {@@ -437,16 +441,16 @@ const getSettingsPerf = (chart, timeBucket, activeChartMetrics, rotateXAxias) =>xFormat: tbsFormatMapper[timeBucket],columns: getColumnsPerf(chart, activeChartMetrics),types: {- dns: areaSpline(),- tls: areaSpline(),- conn: areaSpline(),- response: areaSpline(),- render: areaSpline(),- dom_load: areaSpline(),- ttfb: areaSpline(),- frontend: areaSpline(),- network: areaSpline(),- backend: areaSpline(),+ dns: chartType === chartTypes.line ? areaSpline() : bar(),+ tls: chartType === chartTypes.line ? areaSpline() : bar(),+ conn: chartType === chartTypes.line ? areaSpline() : bar(),+ response: chartType === chartTypes.line ? areaSpline() : bar(),+ render: chartType === chartTypes.line ? areaSpline() : bar(),+ dom_load: chartType === chartTypes.line ? areaSpline() : bar(),+ ttfb: chartType === chartTypes.line ? areaSpline() : bar(),+ frontend: chartType === chartTypes.line ? areaSpline() : bar(),+ network: chartType === chartTypes.line ? areaSpline() : bar(),+ backend: chartType === chartTypes.line ? areaSpline() : bar(),},colors: {dns: '#EC4319',@@ -519,6 +523,9 @@ const getSettingsPerf = (chart, timeBucket, activeChartMetrics, rotateXAxias) =>area: {linearGradient: true,},+ padding: {+ right: rotateXAxias && 35,+ },bindto: '#dataChart',}}diff --git a/src/pages/Project/View/ViewProject.jsx b/src/pages/Project/View/ViewProject.jsxindex 86a206eb2..d1a302fec 100644--- a/src/pages/Project/View/ViewProject.jsx+++ b/src/pages/Project/View/ViewProject.jsx@@ -8,7 +8,7 @@ import domToImage from 'dom-to-image'import { saveAs } from 'file-saver'import bb from 'billboard.js'import {- ArrowDownTrayIcon, Cog8ToothIcon, ArrowPathIcon, CurrencyDollarIcon, ChartBarIcon, BoltIcon, BellIcon,+ ArrowDownTrayIcon, Cog8ToothIcon, ArrowPathIcon, CurrencyDollarIcon, ChartBarIcon, BoltIcon, BellIcon, PresentationChartBarIcon, PresentationChartLineIcon,} from '@heroicons/react/24/outline'import cx from 'clsx'import dayjs from 'dayjs'@@ -30,11 +30,12 @@ import * as SwetrixSDK from '@swetrix/sdk'import { SWETRIX_PID } from 'utils/analytics'import { getTimeFromSeconds, getStringFromTime } from 'utils/generic'+import { getItem, setItem } from 'utils/localstorage'import Title from 'components/Title'import EventsRunningOutBanner from 'components/EventsRunningOutBanner'import {tbPeriodPairs, getProjectCacheKey, LIVE_VISITORS_UPDATE_INTERVAL, DEFAULT_TIMEZONE, CDN_URL, isDevelopment,- timeBucketToDays, getProjectCacheCustomKey, roleViewer, MAX_MONTHS_IN_PAST, MAX_MONTHS_IN_PAST_FREE, PROJECT_TABS, TimeFormat, getProjectForcastCacheKey,+ timeBucketToDays, getProjectCacheCustomKey, roleViewer, MAX_MONTHS_IN_PAST, MAX_MONTHS_IN_PAST_FREE, PROJECT_TABS, TimeFormat, getProjectForcastCacheKey, chartTypes,} from 'redux/constants'import Button from 'ui/Button'import Loader from 'ui/Loader'@@ -143,6 +144,7 @@ const ViewProject = ({const timeFormat = useMemo(() => user.timeFormat || TimeFormat['12-hour'], [user])const [ref, size] = useSize()const rotateXAxias = useMemo(() => (size.width > 0 && size.width < 500), [size])+ const [chartType, setChartType] = useState(getItem('chartType') || chartTypes.line)const tabs = useMemo(() => {return [@@ -346,7 +348,7 @@ const ViewProject = ({setIsPanelsDataEmpty(true)} else {const applyRegions = !_includes(noRegionPeriods, activePeriod.period)- const bbSettings = getSettings(chart, timeBucket, activeChartMetrics, applyRegions, timeFormat, forecasedChartData, rotateXAxias)+ const bbSettings = getSettings(chart, timeBucket, activeChartMetrics, applyRegions, timeFormat, forecasedChartData, rotateXAxias, chartType)setChartData(chart)setPanelsData({@@ -424,7 +426,7 @@ const ViewProject = ({setIsPanelsDataEmptyPerf(true)} else {const { chart: chartPerf } = dataPerf- const bbSettings = getSettingsPerf(chartPerf, timeBucket, activeChartMetricsPerf, rotateXAxias)+ const bbSettings = getSettingsPerf(chartPerf, timeBucket, activeChartMetricsPerf, rotateXAxias, chartType)setChartDataPerf(chartPerf)setPanelsDataPerf({@@ -679,7 +681,7 @@ const ViewProject = ({if (activeChartMetrics.bounce || activeChartMetrics.sessionDuration || activeChartMetrics.views || activeChartMetrics.unique) {const applyRegions = !_includes(noRegionPeriods, activePeriod.period)- const bbSettings = getSettings(chartData, timeBucket, activeChartMetrics, applyRegions, timeFormat, forecasedChartData, rotateXAxias)+ const bbSettings = getSettings(chartData, timeBucket, activeChartMetrics, applyRegions, timeFormat, forecasedChartData, rotateXAxias, chartType)if (!_isEmpty(mainChart)) {mainChart.destroy()@@ -694,7 +696,7 @@ const ViewProject = ({if (!activeChartMetrics.bounce || !activeChartMetrics.sessionDuration || activeChartMetrics.views || activeChartMetrics.unique) {const applyRegions = !_includes(noRegionPeriods, activePeriod.period)- const bbSettings = getSettings(chartData, timeBucket, activeChartMetrics, applyRegions, timeFormat, forecasedChartData, rotateXAxias)+ const bbSettings = getSettings(chartData, timeBucket, activeChartMetrics, applyRegions, timeFormat, forecasedChartData, rotateXAxias, chartType)if (!_isEmpty(mainChart)) {mainChart.destroy()@@ -726,7 +728,7 @@ const ViewProject = ({}}} else if (!isLoading && !_isEmpty(chartDataPerf) && !_isEmpty(mainChart)) {- const bbSettings = getSettingsPerf(chartDataPerf, timeBucket, activeChartMetricsPerf, rotateXAxias)+ const bbSettings = getSettingsPerf(chartDataPerf, timeBucket, activeChartMetricsPerf, rotateXAxias, chartType)if (!_isEmpty(mainChart)) {mainChart.destroy()@@ -1204,6 +1206,22 @@ const ViewProject = ({},]+ // function set chart type and save to local storage+ const setChartTypeOnClick = (type) => {+ setItem('chartType', type)+ setChartType(type)+ }++ // useEffect to change chart if we change chart type+ useEffect(() => {+ if (activeTab === PROJECT_TABS.performance) {+ loadAnalyticsPerf()+ } else {+ loadAnalytics()+ }+ // eslint-disable-next-line react-hooks/exhaustive-deps+ }, [chartType])+if (!isLoading) {return (<Title title={name}>@@ -1412,6 +1430,7 @@ const ViewProject = ({!isPanelsDataEmptyPerf && (<Dropdownitems={chartMetricsPerf}+ className='min-w-[170px] xs:min-w-0'title={(<p>{_find(chartMetricsPerf, ({ id: chartId }) => chartId === activeChartMetricsPerf)?.label}@@ -1456,6 +1475,37 @@ const ViewProject = ({)}</div></div>+ <div className='mt-14 xs:mt-0' />+ <div className='relative'>+ <div className={cx('absolute right-0 z-10 -top-2', {+ 'right-[90px]': activeChartMetrics[CHART_METRICS_MAPPING.sessionDuration],+ 'right-[60px]': activeChartMetrics[CHART_METRICS_MAPPING.bounce],+ })}+ >+ <Button+ onClick={() => setChartTypeOnClick(chartTypes.bar)}+ className={cx('text-gray-700 bg-white hover:bg-gray-50 border-transparent dark:text-gray-50 dark:bg-gray-800 dark:hover:bg-gray-700 focus:outline-none focus:!ring-0 focus:!ring-offset-0', {+ 'text-indigo-600 dark:text-indigo-500': chartType === chartTypes.bar,+ 'text-gray-400 dark:text-gray-500': chartType !== chartTypes.bar,+ })}+ small+ noBorder+ >+ <PresentationChartBarIcon className='w-6 h-6' />+ </Button>+ <Button+ onClick={() => setChartTypeOnClick(chartTypes.line)}+ className={cx('text-gray-700 bg-white hover:bg-gray-50 border-transparent dark:text-gray-50 dark:bg-gray-800 dark:hover:bg-gray-700 focus:outline-none focus:!ring-0 focus:!ring-offset-0', {+ 'text-indigo-600 dark:text-indigo-500': chartType === chartTypes.line,+ 'text-gray-400 dark:text-gray-500': chartType !== chartTypes.line,+ })}+ small+ noBorder+ >+ <PresentationChartLineIcon className='w-6 h-6' />+ </Button>+ </div>+ </div></>)}{(activeTab === PROJECT_TABS.alerts && (isSharedProject || project?.isPublicVisitors || !authenticated)) && (diff --git a/src/redux/constants/index.js b/src/redux/constants/index.jsindex 539445b0d..fd14a2a18 100644--- a/src/redux/constants/index.js+++ b/src/redux/constants/index.js@@ -316,3 +316,8 @@ export const PLAN_LIMITS = {}export const TRIAL_DAYS = 14++export const chartTypes = {+ line: 'line',+ bar: 'bar',+}