1+ import { ChartId } from "components/ChartMenu/ChartMenu"
2+ import { AiOutlineCloseCircle } from "react-icons/ai"
3+ import styles from "components/ChartMenu/ChartElement/ChartElement.module.scss"
4+ import { ChartCanvas } from "./ChartCanvas" ;
5+ import { ChartPoint } from "pages/LoggerPage/LogsColumn/LogLoader/LogsProcessor" ;
6+ import { useEffect , useState } from "react" ;
7+ import { MeasurementId , } from "common" ;
8+ import { ChartLegend } from "./ChartLegend" ;
9+
10+ interface Props {
11+ chartId : ChartId ;
12+ initialMeasurementId : MeasurementId
13+ removeChart : ( chartId : ChartId ) => void ;
14+ getDataFromLogSession : ( measurement : ChartId ) => ChartPoint [ ] ;
15+ }
16+
17+ export interface MeasurementLogger {
18+ id : string ;
19+ color : string ;
20+ }
21+
22+ export const ChartElement = ( { chartId, initialMeasurementId, removeChart, getDataFromLogSession } : Props ) => {
23+
24+ const [ measurementsInChart , setMeasurementsInChart ] = useState < MeasurementLogger [ ] > ( [ { id : initialMeasurementId , color : 'red' } ] ) ;
25+
26+ useEffect ( ( ) => {
27+ console . log ( measurementsInChart )
28+ } , [ measurementsInChart ] )
29+
30+ const addMeasurementToChart = ( measurement : MeasurementLogger ) => {
31+ if ( ! measurementsInChart . some ( measurementInChart => measurementInChart . id === measurement . id ) ) {
32+ setMeasurementsInChart ( [ ...measurementsInChart , measurement ] ) ;
33+ }
34+ }
35+
36+ const handleDrop = ( ev : React . DragEvent < HTMLDivElement > ) => {
37+ ev . stopPropagation ( ) ;
38+ const id = ev . dataTransfer . getData ( "id" ) ;
39+ addMeasurementToChart ( { id, color : getRandomColor ( ) } ) ;
40+ } ;
41+
42+ return (
43+ < div
44+ className = { styles . chartWrapper }
45+ onDrop = { handleDrop }
46+ onDragEnter = { ( ev ) => ev . preventDefault ( ) }
47+ onDragOver = { ( ev ) => ev . preventDefault ( ) }
48+ >
49+ < div className = { styles . chart } >
50+ < AiOutlineCloseCircle
51+ size = { 30 }
52+ cursor = "pointer"
53+ onClick = { ( ) => removeChart ( chartId ) }
54+ />
55+ < ChartCanvas
56+ measurementsInChart = { measurementsInChart }
57+ getDataFromLogSession = { getDataFromLogSession }
58+ />
59+ < ChartLegend
60+ chartId = { chartId }
61+ measurementsInChart = { measurementsInChart }
62+ removeChart = { removeChart }
63+ removeMeasurementFromChart = { ( measurementId : MeasurementId ) => setMeasurementsInChart ( measurementsInChart . filter ( measurement => measurement . id !== measurementId ) ) }
64+ />
65+ </ div >
66+ </ div >
67+ )
68+ }
69+
70+ function getRandomColor ( ) {
71+ var r = Math . floor ( Math . random ( ) * 256 ) ;
72+ var g = Math . floor ( Math . random ( ) * 256 ) ;
73+ var b = Math . floor ( Math . random ( ) * 256 ) ;
74+ var hexR = r . toString ( 16 ) . padStart ( 2 , '0' ) ;
75+ var hexG = g . toString ( 16 ) . padStart ( 2 , '0' ) ;
76+ var hexB = b . toString ( 16 ) . padStart ( 2 , '0' ) ;
77+ return '#' + hexR + hexG + hexB ;
78+ }
0 commit comments