HomieCare/frontend/src/components/Charts/LineChart.tsx
2024-05-11 19:04:02 +07:00

191 lines
4.4 KiB
TypeScript

import { ApexOptions } from 'apexcharts';
import React, { useEffect, useState } from 'react';
import ReactApexChart from 'react-apexcharts';
import { fetchIndoorPredictData } from '../../api/WeatherData';
const options: ApexOptions = {
legend: {
show: false,
position: 'top',
horizontalAlign: 'left',
},
colors: ['#3C50E0', '#80CAEE'],
chart: {
fontFamily: 'Satoshi, sans-serif',
height: 335,
type: 'area',
dropShadow: {
enabled: true,
color: '#623CEA14',
top: 10,
blur: 4,
left: 0,
opacity: 0.1,
},
toolbar: {
show: false,
},
},
responsive: [
{
breakpoint: 1024,
options: {
chart: {
height: 300,
},
},
},
{
breakpoint: 1366,
options: {
chart: {
height: 350,
},
},
},
],
stroke: {
width: [2, 2],
curve: 'straight',
},
// labels: {
// show: false,
// position: "top",
// },
grid: {
xaxis: {
lines: {
show: true,
},
},
yaxis: {
lines: {
show: true,
},
},
},
dataLabels: {
enabled: false,
},
markers: {
size: 4,
colors: '#fff',
strokeColors: ['#3056D3', '#80CAEE'],
strokeWidth: 3,
strokeOpacity: 0.9,
strokeDashArray: 0,
fillOpacity: 1,
discrete: [],
hover: {
size: undefined,
sizeOffset: 5,
},
},
xaxis: {
type: 'category',
categories: [
'Current',
'3 Hour',
'6 Hour',
'9 Hour',
'12 Hour',
'15 Hour',
'18 Hour',
'21 Hour',
'24 Hour',
'27 Hour',
'30 Hour',
'33 Hour',
],
axisBorder: {
show: false,
},
axisTicks: {
show: false,
},
},
yaxis: {
title: {
style: {
fontSize: '0px',
},
},
min: 10,
max: 60,
},
};
interface ChartOneState {
series: {
name: string;
data: number[];
}[];
}
const LineChart: React.FC = () => {
const [seriesData, setSeriesData] = useState<ChartOneState['series']>([]);
const [categories, setCategories] = useState<string[]>([]);
useEffect(() => {
const fetchData = async () => {
const data = await fetchIndoorPredictData();
if (data) {
const categories = data.map((item) => item.timestamp);
const indoorTempData = data.map((item) => item.indoor_temp);
const outdoorTempData = data.map((item) => item.outdoor_temp);
setCategories(categories);
setSeriesData([
{ name: 'Indoor Temperature', data: indoorTempData },
{ name: 'Outdoor Temperature', data: outdoorTempData },
]);
}
};
fetchData();
}, []);
return (
<div className="col-span-12 rounded-sm border border-stroke bg-white px-5 pt-7.5 pb-5 shadow-default dark:border-strokedark dark:bg-boxdark sm:px-7.5 xl:col-span-8">
<div className="flex flex-wrap items-start justify-between gap-3 sm:flex-nowrap">
<div className="flex w-full flex-wrap gap-3 sm:gap-5">
<div className="flex min-w-47.5">
<span className="mt-1 mr-2 flex h-4 w-full max-w-4 items-center justify-center rounded-full border border-primary">
<span className="block h-2.5 w-full max-w-2.5 rounded-full bg-primary"></span>
</span>
<div className="w-full">
<p className="font-semibold text-primary">Indoor Temperature</p>
<p className="text-sm font-medium">3 Hour Interval</p>
</div>
</div>
<div className="flex min-w-47.5">
<span className="mt-1 mr-2 flex h-4 w-full max-w-4 items-center justify-center rounded-full border border-secondary">
<span className="block h-2.5 w-full max-w-2.5 rounded-full bg-secondary"></span>
</span>
<div className="w-full">
<p className="font-semibold text-secondary">
Outdoor Temperature
</p>
<p className="text-sm font-medium">3 Hour Interval</p>
</div>
</div>
</div>
</div>
<div>
<div id="chartOne" className="-ml-5">
<ReactApexChart
options={options}
series={seriesData}
type="area"
height={350}
categories={categories}
/>
</div>
</div>
</div>
);
};
export default LineChart;