Skip to content

Commit

Permalink
Optimize Chart.js bundle size in DataVisualization component
Browse files Browse the repository at this point in the history
- Implement dynamic import for Chart.js and react-chartjs-2
- Use tree shaking by importing only necessary Chart.js components
- Register only required chart elements to reduce bundle size
- Remove unused chart styles and colors to further minimize payload
  • Loading branch information
Violet-Bora-Lee committed Aug 16, 2024
1 parent ef2950d commit 1df2740
Showing 1 changed file with 4 additions and 18 deletions.
22 changes: 4 additions & 18 deletions src/components/advanced/DataVisualization.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ function DataVisualization() {

useEffect(() => {
const loadChart = async () => {
const { Chart, registerables } = await import('chart.js');
Chart.register(...registerables);
const { Chart, BarController, CategoryScale, LinearScale, BarElement } = await import('chart.js');
Chart.register(BarController, CategoryScale, LinearScale, BarElement);
const { Bar } = await import('react-chartjs-2');
setChartComponent(() => Bar);
};
Expand All @@ -19,22 +19,8 @@ function DataVisualization() {
datasets: [{
label: '# of Votes',
data: [12, 19, 3, 5, 2, 3],
backgroundColor: [
'rgba(255, 99, 132, 0.2)',
'rgba(54, 162, 235, 0.2)',
'rgba(255, 206, 86, 0.2)',
'rgba(75, 192, 192, 0.2)',
'rgba(153, 102, 255, 0.2)',
'rgba(255, 159, 64, 0.2)'
],
borderColor: [
'rgba(255, 99, 132, 1)',
'rgba(54, 162, 235, 1)',
'rgba(255, 206, 86, 1)',
'rgba(75, 192, 192, 1)',
'rgba(153, 102, 255, 1)',
'rgba(255, 159, 64, 1)'
],
backgroundColor: 'rgba(75, 192, 192, 0.2)',
borderColor: 'rgba(75, 192, 192, 1)',
borderWidth: 1
}]
};
Expand Down

0 comments on commit 1df2740

Please sign in to comment.