11import { Chart } from "chart.js" ;
2- import { useEffect , useRef } from "react" ;
3- import AreaChart from "../chart/AreaChart" ;
4- import BarChartComparison from "../chart/BarChartComparison" ;
5- import BarChartNegative from "../chart/BarChartNegative" ;
6- import MixedChart from "../chart/MixedChart" ;
7- import WaterfallChart from "../chart/WaterfallChart" ;
2+ import React , { Suspense , useEffect , useRef } from "react" ;
83import { SelectedTitle } from "../data/SelectedConstant" ;
94
105const ChartLayout = ( {
@@ -24,20 +19,20 @@ const ChartLayout = ({
2419 } ;
2520 } , [ ] ) ;
2621
22+ const chartMap = {
23+ BAR_CHART_1 : React . lazy ( ( ) => import ( "../chart/BarChartComparison" ) ) ,
24+ BAR_CHART_2 : React . lazy ( ( ) => import ( "../chart/BarChartNegative" ) ) ,
25+ WATERFALL_CHART : React . lazy ( ( ) => import ( "../chart/WaterfallChart" ) ) ,
26+ MIXED_CHART : React . lazy ( ( ) => import ( "../chart/MixedChart" ) ) ,
27+ AREA_CHART : React . lazy ( ( ) => import ( "../chart/AreaChart" ) ) ,
28+ } ;
29+
2730 const renderChart = ( ) => {
28- switch ( selectedChart ) {
29- case SelectedTitle . BAR_CHART_1 :
30- return < BarChartComparison chartRef = { chartRef } /> ;
31- case SelectedTitle . BAR_CHART_2 :
32- return < BarChartNegative chartRef = { chartRef } /> ;
33- case SelectedTitle . WATERFALL_CHART :
34- return < WaterfallChart chartRef = { chartRef } /> ;
35- case SelectedTitle . MIXED_CHART :
36- return < MixedChart chartRef = { chartRef } /> ;
37- case SelectedTitle . AREA_CHART :
38- return < AreaChart chartRef = { chartRef } /> ;
39- default :
40- return < div > Please select a chart.</ div > ;
31+ if ( selectedChart ) {
32+ const Chart = chartMap [ selectedChart ] ;
33+ return < Chart chartRef = { chartRef } /> ;
34+ } else {
35+ return < div > Please select a chart.</ div > ;
4136 }
4237 } ;
4338
@@ -51,7 +46,9 @@ const ChartLayout = ({
5146 boxShadow : "0px 4px 12px rgba(0, 0, 0, 0.15)" ,
5247 } }
5348 >
54- < > { renderChart ( ) } </ >
49+ < Suspense fallback = { < div > Loading...</ div > } >
50+ < > { renderChart ( ) } </ >
51+ </ Suspense >
5552 </ div >
5653 ) ;
5754} ;
0 commit comments