updated donut chart

This commit is contained in:
Nalvazhuthi 2025-04-14 18:14:40 +05:30
parent 5066638782
commit 7b2d77e273
3 changed files with 32 additions and 35 deletions

View File

@ -259,8 +259,8 @@ const MultiLevelDropdown = ({
{open && ( {open && (
<div className="dropdown-menu"> <div className="dropdown-menu">
<div className="dropdown-content "> <div className="dropdown-content ">
{/* loading list */}
{/* loading list */}
{/* <div className="loading" /> */} {/* <div className="loading" /> */}
{/* Unselect Option */} {/* Unselect Option */}

View File

@ -93,6 +93,7 @@ const RealTimeVisulization: React.FC = () => {
"sidebar-right-wrapper", "sidebar-right-wrapper",
"card", "card",
"dropdown-menu", "dropdown-menu",
"dropdown-options",
], ],
setMenuVisible: () => setSelectedChartId(null), setMenuVisible: () => setSelectedChartId(null),
}); });
@ -158,52 +159,52 @@ const RealTimeVisulization: React.FC = () => {
try { try {
const email = localStorage.getItem("email") || ""; const email = localStorage.getItem("email") || "";
const organization = email?.split("@")[1]?.split(".")[0]; const organization = email?.split("@")[1]?.split(".")[0];
const data = event.dataTransfer.getData("text/plain"); const data = event.dataTransfer.getData("text/plain");
if (widgetSubOption === "3D") return; if (widgetSubOption === "3D") return;
if (!data || selectedZone.zoneName === "") return; if (!data || selectedZone.zoneName === "") return;
const droppedData = JSON.parse(data); const droppedData = JSON.parse(data);
const canvasElement = document.getElementById("real-time-vis-canvas"); const canvasElement = document.getElementById("real-time-vis-canvas");
if (!canvasElement) throw new Error("Canvas element not found"); if (!canvasElement) throw new Error("Canvas element not found");
const rect = canvasElement.getBoundingClientRect(); const rect = canvasElement.getBoundingClientRect();
const relativeX = event.clientX - rect.left; const relativeX = event.clientX - rect.left;
const relativeY = event.clientY - rect.top; const relativeY = event.clientY - rect.top;
// Widget dimensions // Widget dimensions
const widgetWidth = droppedData.width || 125; const widgetWidth = droppedData.width || 125;
const widgetHeight = droppedData.height || 100; const widgetHeight = droppedData.height || 100;
// Center the widget at cursor // Center the widget at cursor
const centerOffsetX = widgetWidth / 2; const centerOffsetX = widgetWidth / 2;
const centerOffsetY = widgetHeight / 2; const centerOffsetY = widgetHeight / 2;
const adjustedX = relativeX - centerOffsetX; const adjustedX = relativeX - centerOffsetX;
const adjustedY = relativeY - centerOffsetY; const adjustedY = relativeY - centerOffsetY;
const finalPosition = determinePosition(rect, adjustedX, adjustedY); const finalPosition = determinePosition(rect, adjustedX, adjustedY);
const [activeProp1, activeProp2] = getActiveProperties(finalPosition); const [activeProp1, activeProp2] = getActiveProperties(finalPosition);
let finalY = 0; let finalY = 0;
let finalX = 0; let finalX = 0;
if (activeProp1 === "top") { if (activeProp1 === "top") {
finalY = adjustedY; finalY = adjustedY;
} else { } else {
finalY = rect.height - (adjustedY + widgetHeight); finalY = rect.height - (adjustedY + widgetHeight);
} }
if (activeProp2 === "left") { if (activeProp2 === "left") {
finalX = adjustedX; finalX = adjustedX;
} else { } else {
finalX = rect.width - (adjustedX + widgetWidth); finalX = rect.width - (adjustedX + widgetWidth);
} }
// Clamp to boundaries // Clamp to boundaries
finalX = Math.max(0, Math.min(rect.width - widgetWidth, finalX)); finalX = Math.max(0, Math.min(rect.width - widgetWidth, finalX));
finalY = Math.max(0, Math.min(rect.height - widgetHeight, finalY)); finalY = Math.max(0, Math.min(rect.height - widgetHeight, finalY));
const boundedPosition = { const boundedPosition = {
...finalPosition, ...finalPosition,
[activeProp1]: finalY, [activeProp1]: finalY,
@ -211,13 +212,13 @@ const RealTimeVisulization: React.FC = () => {
[activeProp1 === "top" ? "bottom" : "top"]: "auto", [activeProp1 === "top" ? "bottom" : "top"]: "auto",
[activeProp2 === "left" ? "right" : "left"]: "auto", [activeProp2 === "left" ? "right" : "left"]: "auto",
}; };
const newObject = { const newObject = {
...droppedData, ...droppedData,
id: generateUniqueId(), id: generateUniqueId(),
position: boundedPosition, position: boundedPosition,
}; };
const existingZone = const existingZone =
useDroppedObjectsStore.getState().zones[selectedZone.zoneName]; useDroppedObjectsStore.getState().zones[selectedZone.zoneName];
if (!existingZone) { if (!existingZone) {
@ -225,24 +226,24 @@ const RealTimeVisulization: React.FC = () => {
.getState() .getState()
.setZone(selectedZone.zoneName, selectedZone.zoneId); .setZone(selectedZone.zoneName, selectedZone.zoneId);
} }
const addFloatingWidget = { const addFloatingWidget = {
organization, organization,
widget: newObject, widget: newObject,
zoneId: selectedZone.zoneId, zoneId: selectedZone.zoneId,
}; };
if (visualizationSocket) { if (visualizationSocket) {
visualizationSocket.emit("v2:viz-float:add", addFloatingWidget); visualizationSocket.emit("v2:viz-float:add", addFloatingWidget);
} }
useDroppedObjectsStore useDroppedObjectsStore
.getState() .getState()
.addObject(selectedZone.zoneName, newObject); .addObject(selectedZone.zoneName, newObject);
const droppedObjectsStore = useDroppedObjectsStore.getState(); const droppedObjectsStore = useDroppedObjectsStore.getState();
const currentZone = droppedObjectsStore.zones[selectedZone.zoneName]; const currentZone = droppedObjectsStore.zones[selectedZone.zoneName];
if (currentZone && currentZone.zoneId === selectedZone.zoneId) { if (currentZone && currentZone.zoneId === selectedZone.zoneId) {
console.log( console.log(
`Objects for Zone ${selectedZone.zoneId}:`, `Objects for Zone ${selectedZone.zoneId}:`,
@ -256,7 +257,6 @@ const RealTimeVisulization: React.FC = () => {
console.error("Error in handleDrop:", error); console.error("Error in handleDrop:", error);
} }
}; };
useEffect(() => { useEffect(() => {
const handleClickOutside = (event: MouseEvent) => { const handleClickOutside = (event: MouseEvent) => {
@ -301,10 +301,9 @@ const RealTimeVisulization: React.FC = () => {
return () => resizeObserver.unobserve(canvas); return () => resizeObserver.unobserve(canvas);
}, []); }, []);
return ( return (
<> <>
<style> <style>
{` {`
:root { :root {
--realTimeViz-container-width: ${canvasDimensions.width}px; --realTimeViz-container-width: ${canvasDimensions.width}px;

View File

@ -1,5 +1,5 @@
import React, { useEffect, useMemo, useState } from "react"; import React, { useEffect, useMemo, useState } from "react";
import { Line } from "react-chartjs-2"; import { Doughnut } from "react-chartjs-2";
import io from "socket.io-client"; import io from "socket.io-client";
import axios from "axios"; import axios from "axios";
@ -16,7 +16,7 @@ interface ChartComponentProps {
fontWeight?: "Light" | "Regular" | "Bold"; fontWeight?: "Light" | "Regular" | "Bold";
} }
const LineGraphComponent = ({ const DoughnutGraphComponent = ({
id, id,
type, type,
title, title,
@ -52,7 +52,7 @@ const LineGraphComponent = ({
}; };
useEffect(() => { useEffect(() => {
},[]) },[])
// Memoize Theme Colors // Memoize Theme Colors
@ -97,11 +97,11 @@ const LineGraphComponent = ({
}, },
}, },
scales: { scales: {
x: { // x: {
ticks: { // ticks: {
display: true, // This hides the x-axis labels // display: true, // This hides the x-axis labels
}, // },
}, // },
}, },
}), }),
[title, chartFontStyle, name] [title, chartFontStyle, name]
@ -161,8 +161,6 @@ const LineGraphComponent = ({
try { try {
const response = await axios.get(`http://${process.env.REACT_APP_SERVER_REST_API_BASE_URL}/api/v2/WidgetData/${id}/${organization}`); const response = await axios.get(`http://${process.env.REACT_APP_SERVER_REST_API_BASE_URL}/api/v2/WidgetData/${id}/${organization}`);
if (response.status === 200) { if (response.status === 200) {
console.log('line chart res',response);
setmeasurements(response.data.Data.measurements) setmeasurements(response.data.Data.measurements)
setDuration(response.data.Data.duration) setDuration(response.data.Data.duration)
setName(response.data.widgetName) setName(response.data.widgetName)
@ -186,7 +184,7 @@ const LineGraphComponent = ({
} }
,[chartMeasurements, chartDuration, widgetName]) ,[chartMeasurements, chartDuration, widgetName])
return <Line data={Object.keys(measurements).length > 0 ? chartData : defaultData} options={options} />; return <Doughnut data={Object.keys(measurements).length > 0 ? chartData : defaultData} options={options} />;
}; };
export default LineGraphComponent; export default DoughnutGraphComponent;