Skip to main content
Version: Next

TimeLineChart Component

The TimeLineChart component is a versatile and interactive chart that displays data over time. It uses the Recharts library to render a bar chart and provides options for date range selection and data grouping.

Importing the Component

To use the TimeLineChart component in your React application, import it as follows:

import TimeLineChart from '@/components/TimeLineChart';

Basic Usage

Here's a basic example of how to use the TimeLineChart component:

const getData = async ({ from, to, groupBy }) => {
// Fetch and return data based on the parameters
};

<TimeLineChart getData={getData} open={true} />;

Props

The TimeLineChart component accepts the following props:

PropTypeDescription
getDatafunctionA function that fetches data based on date range and grouping
openbooleanDetermines if the ContentWrapper is open or closed

The getData function should return a Promise that resolves to an array of objects with time and value properties.

Functionality

The TimeLineChart component provides the following functionality:

  1. Displays a bar chart of data over time using Recharts.
  2. Allows users to select a date range for the data display.
  3. Provides options to group data by day, week, month, or year.
  4. Fetches and updates data based on the selected date range and grouping.
  5. Shows loading state and "No data available" message when appropriate.

State Management

The component uses React's useState hook to manage several pieces of state:

  • chartStartDate and chartEndDate: The selected date range
  • isLoading: Indicates whether data is being fetched
  • chart: The data to be displayed in the chart
  • groupBy: The current grouping option (day, week, month, year)

Data Fetching

Data is fetched using the getData prop function whenever the date range, grouping option, or props change. This is handled in a useEffect hook:

useEffect(() => {
setIsLoading(true);
props
.getData({
from: chartStartDate,
to: chartEndDate,
groupBy: groupBy,
})
.then((res) => {
setChart(res);
})
.finally(() => {
setIsLoading(false);
});
}, [groupBy, chartStartDate, chartEndDate, props]);

UI Components

The component uses several UI components:

  • ContentWrapper: Wraps the entire chart component
  • DatePickerWithRange: Allows users to select a date range
  • Button: Used for grouping options
  • Recharts components: BarChart, Bar, XAxis, YAxis, Tooltip, LabelList, ResponsiveContainer

Styling

The component uses Tailwind CSS for styling:

  • Flexbox is used for layout of the date picker and grouping options
  • Custom colors are applied to the chart elements (e.g., #34afd2 for bars and axes)
  • Responsive design is implemented using Tailwind's responsive classes (e.g., lg:flex-row)

Customization

To customize the TimeLineChart component:

  1. Modify the Tailwind classes to adjust the layout and styling.
  2. Change the color scheme by updating the fill and stroke colors in the Recharts components.
  3. Adjust the chart dimensions by modifying the ResponsiveContainer props.
  4. Customize the date range picker by modifying the DatePickerWithRange component.

Accessibility

To improve accessibility:

  1. Add aria-label attributes to interactive elements for better screen reader support.
  2. Ensure color contrast ratios meet WCAG standards for better readability.
  3. Consider adding keyboard navigation support for the grouping options.

Best Practices

  1. Implement error handling for cases where data fetching fails.
  2. Consider adding PropTypes or TypeScript props interface for better type checking.
  3. Optimize performance by memoizing the getData function if it's recreated on every render of the parent component.

Example Usage

Here's an example of how you might use the TimeLineChart component in a Next.js page:

import TimeLineChart from '@/components/TimeLineChart';

export default function AnalyticsPage() {
const fetchData = async ({ from, to, groupBy }) => {
// Fetch data from an API
const response = await fetch(
`/api/analytics?from=${from}&to=${to}&groupBy=${groupBy}`,
);
return response.json();
};

return (
<div className='container mx-auto py-8'>
<h1 className='text-3xl font-bold mb-6'>Analytics</h1>
<TimeLineChart getData={fetchData} open={true} />
</div>
);
}

This example demonstrates how to use the TimeLineChart component within an analytics page, providing a custom data fetching function.


By using the TimeLineChart component, you can easily integrate an interactive and customizable timeline chart into your React application. Remember to provide an appropriate data fetching function and customize the styling to match your application's design.