Umami is a privacy-first web and product analytics platform. Traffic, campaigns, behavior, conversions, and revenue in one place. No cookies, no surveillance, self-hosted or in the cloud.
TypeScript analyticsaudience-segmentationchartscohort-analysisgoogle-analyticsproduct-analyticsstatisticsuser-journeyweb-analytics
Replace chart legend with custom component.
Mike Cao8e987b6b9dd3
diff --git a/components/metrics/BarChart.js b/components/metrics/BarChart.jsindex a31a6f400..2f99fa288 100644--- a/components/metrics/BarChart.js+++ b/components/metrics/BarChart.js@@ -2,6 +2,7 @@ import React, { useState, useRef, useEffect } from 'react';import ReactTooltip from 'react-tooltip';import classNames from 'classnames';import ChartJS from 'chart.js';+import Dot from 'components/common/Dot';import { formatLongNumber } from 'lib/format';import { dateFormat } from 'lib/lang';import useLocale from 'hooks/useLocale';@@ -24,6 +25,7 @@ export default function BarChart({}) {const canvas = useRef();const chart = useRef();+ const [legend, setLegend] = useState();const [tooltip, setTooltip] = useState(null);const [locale] = useLocale();const [theme] = useTheme();@@ -111,9 +113,7 @@ export default function BarChart({responsiveAnimationDuration: 0,maintainAspectRatio: false,legend: {- labels: {- fontColor: colors.text,- },+ display: false,},scales: {xAxes: [@@ -143,6 +143,7 @@ export default function BarChart({callback: renderYLabel,beginAtZero: true,fontColor: colors.text,+ precision: 0,},gridLines: {color: colors.line,@@ -163,6 +164,8 @@ export default function BarChart({},options,});++ chart.current.generateLegend();}function updateChart() {@@ -179,6 +182,10 @@ export default function BarChart({options.tooltips.custom = renderTooltip;onUpdate(chart.current);++ chart.current.update();++ chart.current.generateLegend();}useEffect(() => {@@ -189,6 +196,7 @@ export default function BarChart({setTooltip(null);updateChart();}+ setLegend({ ...chart.current.legend });}}, [datasets, unit, animationDuration, locale, theme]);@@ -202,6 +210,7 @@ export default function BarChart({><canvas ref={canvas} /></div>+ <Legend items={legend?.legendItems} locale={locale} /><ReactTooltip id={`${chartId}-tooltip`}>{tooltip ? <Tooltip {...tooltip} /> : null}</ReactTooltip>@@ -209,6 +218,17 @@ export default function BarChart({);}+const Legend = ({ items = [], locale }) => (+ <div className={styles.legend}>+ {items.map(({ text, fillStyle }) => (+ <div key={text} className={styles.label}>+ <Dot color={fillStyle} />+ <span className={locale}>{text}</span>+ </div>+ ))}+ </div>+);+const Tooltip = ({ title, value, label, labelColor }) => (<div className={styles.tooltip}><div className={styles.content}>diff --git a/components/metrics/BarChart.module.css b/components/metrics/BarChart.module.cssindex cd26d3afe..2c394e830 100644--- a/components/metrics/BarChart.module.css+++ b/components/metrics/BarChart.module.css@@ -41,3 +41,20 @@width: 10px;height: 10px;}++.legend {+ display: flex;+ justify-content: center;+ flex-wrap: wrap;+ margin-top: 10px;+}++.label {+ display: flex;+ align-items: center;+ font-size: var(--font-size-xsmall);+}++.label + .label {+ margin-left: 20px;+}diff --git a/components/metrics/EventsChart.js b/components/metrics/EventsChart.jsindex 9a5827b20..3d55a40d2 100644--- a/components/metrics/EventsChart.js+++ b/components/metrics/EventsChart.js@@ -61,14 +61,6 @@ export default function EventsChart({ websiteId, className, token }) {});}, [data]);- function handleCreate(options) {- const legend = {- position: 'bottom',- };-- options.legend = legend;- }-function handleUpdate(chart) {chart.data.datasets = datasets;@@ -86,7 +78,6 @@ export default function EventsChart({ websiteId, className, token }) {datasets={datasets}unit={unit}records={getDateLength(startDate, endDate, unit)}- onCreate={handleCreate}onUpdate={handleUpdate}stacked/>diff --git a/components/metrics/PageviewsChart.js b/components/metrics/PageviewsChart.jsindex 79fe49174..e9d30449e 100644--- a/components/metrics/PageviewsChart.js+++ b/components/metrics/PageviewsChart.js@@ -45,8 +45,6 @@ export default function PageviewsChart({id: 'metrics.page-views',defaultMessage: 'Page views',});-- chart.update();};if (!data) {diff --git a/components/pages/WebsiteDetails.module.css b/components/pages/WebsiteDetails.module.cssindex cd771c966..e8dd53994 100644--- a/components/pages/WebsiteDetails.module.css+++ b/components/pages/WebsiteDetails.module.css@@ -31,5 +31,5 @@}.eventschart {- padding: 30px 0;+ padding-top: 30px;}