Replace chart legend with custom component.

This commit is contained in:
Mike Cao 2020-10-14 16:39:26 -07:00
parent 894736d474
commit 8e987b6b9d
5 changed files with 41 additions and 15 deletions

View File

@ -2,6 +2,7 @@ import React, { useState, useRef, useEffect } from 'react';
import ReactTooltip from 'react-tooltip'; import ReactTooltip from 'react-tooltip';
import classNames from 'classnames'; import classNames from 'classnames';
import ChartJS from 'chart.js'; import ChartJS from 'chart.js';
import Dot from 'components/common/Dot';
import { formatLongNumber } from 'lib/format'; import { formatLongNumber } from 'lib/format';
import { dateFormat } from 'lib/lang'; import { dateFormat } from 'lib/lang';
import useLocale from 'hooks/useLocale'; import useLocale from 'hooks/useLocale';
@ -24,6 +25,7 @@ export default function BarChart({
}) { }) {
const canvas = useRef(); const canvas = useRef();
const chart = useRef(); const chart = useRef();
const [legend, setLegend] = useState();
const [tooltip, setTooltip] = useState(null); const [tooltip, setTooltip] = useState(null);
const [locale] = useLocale(); const [locale] = useLocale();
const [theme] = useTheme(); const [theme] = useTheme();
@ -111,9 +113,7 @@ export default function BarChart({
responsiveAnimationDuration: 0, responsiveAnimationDuration: 0,
maintainAspectRatio: false, maintainAspectRatio: false,
legend: { legend: {
labels: { display: false,
fontColor: colors.text,
},
}, },
scales: { scales: {
xAxes: [ xAxes: [
@ -143,6 +143,7 @@ export default function BarChart({
callback: renderYLabel, callback: renderYLabel,
beginAtZero: true, beginAtZero: true,
fontColor: colors.text, fontColor: colors.text,
precision: 0,
}, },
gridLines: { gridLines: {
color: colors.line, color: colors.line,
@ -163,6 +164,8 @@ export default function BarChart({
}, },
options, options,
}); });
chart.current.generateLegend();
} }
function updateChart() { function updateChart() {
@ -179,6 +182,10 @@ export default function BarChart({
options.tooltips.custom = renderTooltip; options.tooltips.custom = renderTooltip;
onUpdate(chart.current); onUpdate(chart.current);
chart.current.update();
chart.current.generateLegend();
} }
useEffect(() => { useEffect(() => {
@ -189,6 +196,7 @@ export default function BarChart({
setTooltip(null); setTooltip(null);
updateChart(); updateChart();
} }
setLegend({ ...chart.current.legend });
} }
}, [datasets, unit, animationDuration, locale, theme]); }, [datasets, unit, animationDuration, locale, theme]);
@ -202,6 +210,7 @@ export default function BarChart({
> >
<canvas ref={canvas} /> <canvas ref={canvas} />
</div> </div>
<Legend items={legend?.legendItems} locale={locale} />
<ReactTooltip id={`${chartId}-tooltip`}> <ReactTooltip id={`${chartId}-tooltip`}>
{tooltip ? <Tooltip {...tooltip} /> : null} {tooltip ? <Tooltip {...tooltip} /> : null}
</ReactTooltip> </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 }) => ( const Tooltip = ({ title, value, label, labelColor }) => (
<div className={styles.tooltip}> <div className={styles.tooltip}>
<div className={styles.content}> <div className={styles.content}>

View File

@ -41,3 +41,20 @@
width: 10px; width: 10px;
height: 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;
}

View File

@ -61,14 +61,6 @@ export default function EventsChart({ websiteId, className, token }) {
}); });
}, [data]); }, [data]);
function handleCreate(options) {
const legend = {
position: 'bottom',
};
options.legend = legend;
}
function handleUpdate(chart) { function handleUpdate(chart) {
chart.data.datasets = datasets; chart.data.datasets = datasets;
@ -86,7 +78,6 @@ export default function EventsChart({ websiteId, className, token }) {
datasets={datasets} datasets={datasets}
unit={unit} unit={unit}
records={getDateLength(startDate, endDate, unit)} records={getDateLength(startDate, endDate, unit)}
onCreate={handleCreate}
onUpdate={handleUpdate} onUpdate={handleUpdate}
stacked stacked
/> />

View File

@ -45,8 +45,6 @@ export default function PageviewsChart({
id: 'metrics.page-views', id: 'metrics.page-views',
defaultMessage: 'Page views', defaultMessage: 'Page views',
}); });
chart.update();
}; };
if (!data) { if (!data) {

View File

@ -31,5 +31,5 @@
} }
.eventschart { .eventschart {
padding: 30px 0; padding-top: 30px;
} }