Skip to content

Commit

Permalink
Update Chart to be lazy loaded
Browse files Browse the repository at this point in the history
  • Loading branch information
Ru Chern Chong committed Nov 6, 2023
1 parent 0f0317c commit 65e672e
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
10 changes: 7 additions & 3 deletions components/Infographic.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"use client";

import { Fragment, useState } from "react";
import { LineChart } from "@/components/LineChart";
import { useState } from "react";
import dynamic from "next/dynamic";
import { ChartData, ChartOptions } from "chart.js";
import { stringToUniqueColour } from "@/lib/stringToUniqueColour";
import { transformDataToDatasets } from "@/lib/transformDataToDatasets";
Expand All @@ -11,6 +11,10 @@ type InfographicProps = {
electricCars: Car[];
};

const DynamicLineChart = dynamic(() => import("../components/LineChart"), {
loading: () => <p>Loading...</p>,
});

export const Infographic = ({ electricCars }: InfographicProps) => {
const initialDatasets: ChartDataset[] = transformDataToDatasets(
electricCars,
Expand Down Expand Up @@ -82,7 +86,7 @@ export const Infographic = ({ electricCars }: InfographicProps) => {
return (
<>
<div className="min-h-screen w-full md:aspect-video md:min-h-full">
<LineChart data={data} options={options} />
<DynamicLineChart data={data} options={options} />
</div>
<div className="prose dark:prose-invert">
<h3>Make</h3>
Expand Down
2 changes: 2 additions & 0 deletions components/LineChart.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,5 @@ type LineChartProps = {
export const LineChart = ({ data, options }: LineChartProps) => {
return <Line data={data} options={options} className="bg-neutral-50 p-4" />;
};

export default LineChart;

0 comments on commit 65e672e

Please sign in to comment.