Updated color selection. Added loading to fetch hook.

This commit is contained in:
Mike Cao 2020-09-20 11:28:38 -07:00
parent 5524d504f4
commit 569fcc7f0b
7 changed files with 47 additions and 27 deletions

View File

@ -19,12 +19,8 @@
font-weight: 600; font-weight: 600;
} }
.nav > * { .nav a + a {
margin-left: 30px; margin-left: 40px;
}
.nav > *:first-child {
margin: 0;
} }
.buttons { .buttons {

View File

@ -18,6 +18,7 @@ export default function BarChart({
animationDuration = 300, animationDuration = 300,
className, className,
stacked = false, stacked = false,
loading = false,
onCreate = () => {}, onCreate = () => {},
onUpdate = () => {}, onUpdate = () => {},
}) { }) {
@ -33,6 +34,7 @@ export default function BarChart({
}; };
function renderXLabel(label, index, values) { function renderXLabel(label, index, values) {
if (loading) return '';
const d = new Date(values[index].value); const d = new Date(values[index].value);
const w = canvas.current.width; const w = canvas.current.width;

View File

@ -5,17 +5,7 @@ import { getDateArray, getDateLength } from 'lib/date';
import useFetch from 'hooks/useFetch'; import useFetch from 'hooks/useFetch';
import useDateRange from 'hooks/useDateRange'; import useDateRange from 'hooks/useDateRange';
import useTimezone from 'hooks/useTimezone'; import useTimezone from 'hooks/useTimezone';
import { EVENT_COLORS } from 'lib/constants';
const COLORS = [
'#2680eb',
'#9256d9',
'#44b556',
'#e68619',
'#e34850',
'#1b959a',
'#d83790',
'#85d044',
];
export default function EventsChart({ websiteId, token }) { export default function EventsChart({ websiteId, token }) {
const [dateRange] = useDateRange(websiteId); const [dateRange] = useDateRange(websiteId);
@ -51,13 +41,13 @@ export default function EventsChart({ websiteId, token }) {
}); });
return Object.keys(map).map((key, index) => { return Object.keys(map).map((key, index) => {
const color = tinycolor(COLORS[index]); const color = tinycolor(EVENT_COLORS[index]);
return { return {
label: key, label: key,
data: map[key], data: map[key],
lineTension: 0, lineTension: 0,
backgroundColor: color.setAlpha(0.4).toRgbString(), backgroundColor: color.setAlpha(0.6).toRgbString(),
borderColor: color.setAlpha(0.5).toRgbString(), borderColor: color.setAlpha(0.7).toRgbString(),
borderWidth: 1, borderWidth: 1,
}; };
}); });

View File

@ -1,10 +1,25 @@
import React from 'react'; import React from 'react';
import { useIntl } from 'react-intl'; import { useIntl } from 'react-intl';
import tinycolor from 'tinycolor2';
import CheckVisible from 'components/helpers/CheckVisible'; import CheckVisible from 'components/helpers/CheckVisible';
import BarChart from './BarChart'; import BarChart from './BarChart';
import useTheme from 'hooks/useTheme';
import { THEME_COLORS } from 'lib/constants';
export default function PageviewsChart({ websiteId, data, unit, records, className }) { export default function PageviewsChart({ websiteId, data, unit, records, className, loading }) {
const intl = useIntl(); const intl = useIntl();
const [theme] = useTheme();
const primaryColor = tinycolor(THEME_COLORS[theme].primary);
const colors = {
views: {
background: primaryColor.setAlpha(0.4).toRgbString(),
border: primaryColor.setAlpha(0.5).toRgbString(),
},
visitors: {
background: primaryColor.setAlpha(0.6).toRgbString(),
border: primaryColor.setAlpha(0.7).toRgbString(),
},
};
const handleUpdate = chart => { const handleUpdate = chart => {
const { const {
@ -43,8 +58,8 @@ export default function PageviewsChart({ websiteId, data, unit, records, classNa
}), }),
data: data.uniques, data: data.uniques,
lineTension: 0, lineTension: 0,
backgroundColor: 'rgb(38, 128, 235, 0.4)', backgroundColor: colors.visitors.background,
borderColor: 'rgb(13, 102, 208, 0.4)', borderColor: colors.visitors.border,
borderWidth: 1, borderWidth: 1,
}, },
{ {
@ -54,8 +69,8 @@ export default function PageviewsChart({ websiteId, data, unit, records, classNa
}), }),
data: data.pageviews, data: data.pageviews,
lineTension: 0, lineTension: 0,
backgroundColor: 'rgb(38, 128, 235, 0.2)', backgroundColor: colors.views.background,
borderColor: 'rgb(13, 102, 208, 0.2)', borderColor: colors.views.border,
borderWidth: 1, borderWidth: 1,
}, },
]} ]}
@ -63,6 +78,7 @@ export default function PageviewsChart({ websiteId, data, unit, records, classNa
records={records} records={records}
animationDuration={visible ? 300 : 0} animationDuration={visible ? 300 : 0}
onUpdate={handleUpdate} onUpdate={handleUpdate}
loading={loading}
/> />
)} )}
</CheckVisible> </CheckVisible>

View File

@ -23,7 +23,7 @@ export default function WebsiteChart({
const { startDate, endDate, unit, value, modified } = dateRange; const { startDate, endDate, unit, value, modified } = dateRange;
const [timezone] = useTimezone(); const [timezone] = useTimezone();
const { data } = useFetch( const { data, loading } = useFetch(
`/api/website/${websiteId}/pageviews`, `/api/website/${websiteId}/pageviews`,
{ {
start_at: +startDate, start_at: +startDate,
@ -74,6 +74,7 @@ export default function WebsiteChart({
data={{ pageviews, uniques }} data={{ pageviews, uniques }}
unit={unit} unit={unit}
records={getDateLength(startDate, endDate, unit)} records={getDateLength(startDate, endDate, unit)}
loading={loading}
/> />
</div> </div>
</div> </div>

View File

@ -7,6 +7,7 @@ export default function useFetch(url, params = {}, options = {}) {
const dispatch = useDispatch(); const dispatch = useDispatch();
const [data, setData] = useState(); const [data, setData] = useState();
const [error, setError] = useState(); const [error, setError] = useState();
const [loading, setLoadiing] = useState(false);
const keys = Object.keys(params) const keys = Object.keys(params)
.sort() .sort()
.map(key => params[key]); .map(key => params[key]);
@ -14,6 +15,7 @@ export default function useFetch(url, params = {}, options = {}) {
async function loadData() { async function loadData() {
try { try {
setLoadiing(true);
setError(null); setError(null);
const time = performance.now(); const time = performance.now();
const data = await get(url, params); const data = await get(url, params);
@ -25,6 +27,8 @@ export default function useFetch(url, params = {}, options = {}) {
} catch (e) { } catch (e) {
console.error(e); console.error(e);
setError(e); setError(e);
} finally {
setLoadiing(false);
} }
} }
@ -42,5 +46,5 @@ export default function useFetch(url, params = {}, options = {}) {
} }
}, [url, ...keys, ...update]); }, [url, ...keys, ...update]);
return { data, error, loadData }; return { data, error, loading, loadData };
} }

View File

@ -35,6 +35,17 @@ export const THEME_COLORS = {
}, },
}; };
export const EVENT_COLORS = [
'#2680eb',
'#9256d9',
'#44b556',
'#e68619',
'#e34850',
'#1b959a',
'#d83790',
'#85d044',
];
export const DEFAULT_DATE_RANGE = '24hour'; export const DEFAULT_DATE_RANGE = '24hour';
export const POSTGRESQL = 'postgresql'; export const POSTGRESQL = 'postgresql';