Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Seperate days by a bar and added time stamp of data on graph #103

Merged
merged 5 commits into from
Jul 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 34 additions & 12 deletions nextjs-interface/src/app/ui/dashboard/ChartElement.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
"use client";

import React, { useContext } from "react";
import {
LineChart,
Line,
Expand All @@ -10,16 +9,29 @@ import {
ReferenceLine,
ResponsiveContainer,
} from "recharts";

import React, { useContext } from "react";
import { avgData } from "@/lib/context";
import { ThemeContext } from "@/lib/Theme";

export function ChartElement({ data }: { data: avgData[] }) {
const { darkMode } = useContext(ThemeContext);

let textColor;
darkMode ? (textColor = "white") : (textColor = "");
let textColor = darkMode ? "white" : "";

if (!Array.isArray(data) || data === undefined) {
console.error("Data is not an array or is undefined");
return null;
}

const dateChanges = data.reduce((acc: any[], curr, index, src) => {
if (
index > 0 &&
new Date(curr.date).getDate() !== new Date(src[index - 1].date).getDate()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can't new Date(src[index - 1].date).getDate() be stored in a variable that can be updated when the date change so we don't recalculate it every time

) {
acc.push(curr.date);
}
return acc;
}, []);

return (
<ResponsiveContainer width="100%" height={500}>
<LineChart width={800} height={500} data={data}>
Expand All @@ -41,11 +53,6 @@ export function ChartElement({ data }: { data: avgData[] }) {
stroke={textColor}
padding={{ top: 50, bottom: 10 }}
/>
<ReferenceLine
y={50}
label={{ value: "Max humidity", position: "top", stroke: textColor }}
stroke="red"
/>
<Tooltip
cursor={{
stroke: "#334155",
Expand All @@ -60,7 +67,13 @@ export function ChartElement({ data }: { data: avgData[] }) {
<div className="flex flex-col">
<h1 className="text-black dark:text-zinc-50">
Date :{" "}
{new Date(payload[0].payload.date).toLocaleDateString()}
{new Date(
payload[0].payload.date,
).toLocaleDateString() +
" " +
new Date(
payload[0].payload.date,
).toLocaleTimeString()}
</h1>
<p className="text-black dark:text-zinc-50">
Temperature :{" "}
Expand Down Expand Up @@ -90,6 +103,15 @@ export function ChartElement({ data }: { data: avgData[] }) {
strokeWidth="2px"
stroke="#82ca9d"
/>
{dateChanges.map((date, index) => (
<ReferenceLine
key={index}
x={date}
stroke="Gray"
ifOverflow="extendDomain"
label={{ value: "Date change", position: "top", fill: "green" }}
/>
))}
</LineChart>
</ResponsiveContainer>
);
Expand Down
2 changes: 1 addition & 1 deletion nextjs-interface/src/app/ui/dashboard/EspLinksElement.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"use client";

// import nextjs and react
import { useEffect, useState } from "react";
import React, { useEffect, useState } from "react";
import Link from "next/link";
import { usePathname } from "next/navigation";
import clsx from "clsx";
Expand Down