From 79bfafa5fb4f69dc110c15179eb125e1e8a41fc4 Mon Sep 17 00:00:00 2001 From: Antonio Villagra De La Cruz Date: Sun, 6 Aug 2023 15:24:25 +0800 Subject: [PATCH] Reimplement bar charts --- app/components/BarChart.client.tsx | 165 +++++++++++++++-------------- app/routes/app/dashboard.$id.tsx | 20 +++- 2 files changed, 102 insertions(+), 83 deletions(-) diff --git a/app/components/BarChart.client.tsx b/app/components/BarChart.client.tsx index 36440b4..ac3a85e 100644 --- a/app/components/BarChart.client.tsx +++ b/app/components/BarChart.client.tsx @@ -1,114 +1,121 @@ -/* eslint-disable @typescript-eslint/ban-ts-comment */ - -import { useState } from "react"; +import { useEffect, useState } from "react"; import ReactApexChart from "react-apexcharts"; +import { DashboardElement } from "~/types/dashboard"; + +import type { ApexOptions } from "apexcharts"; type Props = { - categories: string[]; - data: number[]; - elements: Array<{ id: string }>; + data: DashboardElement[]; limit?: number; onClick?: (id: string) => void; }; -const VIEW_LIMIT = 5; +const VIEW_LIMIT = 4; export default function BarChart({ - categories, data, - elements, limit = VIEW_LIMIT, onClick = () => null, }: Props) { const [viewAll, setViewAll] = useState(false); + const [series, setSeries] = useState([]); + const [options, setOptions] = useState({}); - const options = { - chart: { - type: "bar", - height: 55 * (viewAll || data.length < limit ? data.length : limit) + 55, - offsetX: -25, - offsetY: -25, - toolbar: { - show: true, - offsetX: -30, - offsetY: -23, + useEffect(() => { + const displayData = viewAll ? data : data.slice(0, limit); + + setSeries([ + { + name: "Page Views", + data: displayData.map(({ count, label }) => ({ x: label, y: count })), }, - events: { - click(_: never, __: never, config: { dataPointIndex: number }) { - onClick(elements[config.dataPointIndex].id); + ]); + + setOptions({ + chart: { + type: "bar", + height: + 55 * + (viewAll || displayData.length < limit + ? displayData.length + : limit) + + 55, + offsetX: -25, + offsetY: -25, + toolbar: { + show: true, + offsetX: -30, + offsetY: -23, }, - }, - }, - plotOptions: { - bar: { - borderRadius: 4, - horizontal: true, - barHeight: "80%", - dataLabels: { - position: "bottom", + events: { + click(_: never, __: never, config: { dataPointIndex: number }) { + onClick(data[config.dataPointIndex].id); + }, }, }, - }, - dataLabels: { - enabled: true, - textAnchor: "start", - style: { - colors: ["#1e293b"], - fontWeight: 400, - }, - formatter: function (_: any, opt: any) { - return opt.w.globals.labels[opt.dataPointIndex]; - }, - offsetX: 10, - dropShadow: { - enabled: false, - }, - }, - colors: ["#e2e8f0"], - tooltip: { - y: { - formatter(val: number) { - return Math.round(val); + plotOptions: { + bar: { + borderRadius: 4, + horizontal: true, + barHeight: "80%", + dataLabels: { + position: "bottom", + }, }, }, - }, - xaxis: { - categories, - axisBorder: { - show: true, - color: "#1e293b", + dataLabels: { + enabled: true, + textAnchor: "start", + style: { + colors: ["#1e293b"], + fontWeight: 400, + }, + formatter: function (_: any, opt: any) { + return opt.w.globals.labels[opt.dataPointIndex]; + }, + offsetX: 10, + dropShadow: { + enabled: false, + }, }, - axisTicks: { - show: false, + colors: ["#e2e8f0"], + tooltip: { + y: { + formatter(val: number) { + return String(Math.round(val)); + }, + }, }, - labels: { - formatter(val: number) { - return Math.round(val); + xaxis: { + categories: displayData.map(({ label }) => label), + axisBorder: { + show: true, + color: "#1e293b", + }, + axisTicks: { + show: false, + }, + labels: { + formatter(val: string) { + return String(Math.round(Number(val))); + }, }, }, - }, - yaxis: { - labels: { - show: false, + yaxis: { + labels: { + show: false, + }, }, - }, - }; - - const series = [ - { - name: "Page Views", - data: viewAll ? data : data.slice(0, limit), - }, - ]; + }); + }, [viewAll, data, limit]); return (
- {/* @ts-ignore */} {data.length > limit ? (
diff --git a/app/routes/app/dashboard.$id.tsx b/app/routes/app/dashboard.$id.tsx index a03e936..006511a 100644 --- a/app/routes/app/dashboard.$id.tsx +++ b/app/routes/app/dashboard.$id.tsx @@ -182,7 +182,10 @@ export default function DashboardRoute() {

Pages

{isMounted ? ( - + ) : (
@@ -194,7 +197,10 @@ export default function DashboardRoute() {

Referrers

{isMounted ? ( - + ) : (
@@ -206,7 +212,10 @@ export default function DashboardRoute() {

Browsers

{isMounted ? ( - + ) : (
@@ -218,7 +227,10 @@ export default function DashboardRoute() {

Platforms

{isMounted ? ( - + ) : (