@@ -3,22 +3,19 @@ import CanvasJSReact from '@canvasjs/react-charts';
33import styles from "./ChartElement.module.scss" ;
44import { AiOutlineCloseCircle } from 'react-icons/ai'
55import { memo , useRef , useEffect , useCallback } from "react" ;
6- import { ChartId , MeasurementId , MeasurementInfo , UpdateFunction , } from '../ChartList ' ;
7- import { createChart , ColorType , IChartApi , ISeriesApi , UTCTimestamp } from 'lightweight-charts ' ;
8- import { useGlobalTicker } from 'common ' ;
6+ import { IChartApi , ISeriesApi , UTCTimestamp } from 'lightweight-charts ' ;
7+ import { MeasurementId , NumericMeasurementInfo , UpdateFunction , useGlobalTicker } from 'common ' ;
8+ import { ChartId , useChartStore } from '../ChartStore ' ;
99
1010type MeasurementToSerieAndUpdater = Map < MeasurementId , { DataSerie : ISeriesApi < "Line" > , Updater : UpdateFunction } > ;
1111
1212type Props = {
1313 chartId : ChartId ;
14- measurementId : MeasurementId ;
1514 chartHeight : number ;
16- removeChart : ( id : ChartId ) => void ;
17- getMeasurementInfo : ( id : MeasurementId ) => MeasurementInfo ;
1815} ;
1916
2017// React component that keeps the chart render and measurements represented on it.
21- export const ChartElement = memo ( ( { chartId, measurementId , chartHeight, removeChart , getMeasurementInfo } : Props ) => {
18+ export const ChartElement = memo ( ( { chartId, chartHeight } : Props ) => {
2219
2320 // Ref to the CanvasJS chart render
2421 const chartContainerRef = useRef < HTMLDivElement > ( null ) ;
@@ -27,60 +24,61 @@ export const ChartElement = memo(({ chartId, measurementId, chartHeight, removeC
2724 let chartLegend = useRef < HTMLDivElement > ( null ) ;
2825 // Ref to chart's data series
2926 const chartDataSeries = useRef < MeasurementToSerieAndUpdater > ( new Map ( ) ) ;
27+ const removeChart = useChartStore ( ( state ) => state . removeChart ) ;
3028
3129 // INITIALIZE ALL THE CHART INITIAL SETTINGS.
3230 useEffect ( ( ) => {
33- const measurement = getMeasurementInfo ( measurementId ) ;
34- const chartLegendItem = createChartLegendItem ( measurement ) ;
35- chartLegendItem . onclick = ( ev ) => removeSeriesFromChart ( ev , measurementId ) ;
36- if ( chartLegend ) chartLegend . current ?. appendChild ( chartLegendItem ) ;
37-
38- const handleResize = ( ) => {
39- if ( chartContainerRef . current )
40- chart . applyOptions ( { width : chartContainerRef . current . clientWidth } ) ;
41- } ;
42-
43- const resizeObserver = new ResizeObserver ( handleResize ) ;
44- if ( chartContainerRef . current )
45- resizeObserver . observe ( chartContainerRef . current ) ;
46-
47- if ( ! chartContainerRef . current ) return ;
48- chart = createChart ( chartContainerRef . current , {
49- layout : {
50- background : { type : ColorType . Solid , color : "white" } ,
51- textColor : "black" ,
52- } ,
53- width : chartContainerRef . current . clientWidth ,
54- height : chartHeight ,
55- timeScale : {
56- timeVisible : true ,
57- secondsVisible : true ,
58- fixLeftEdge : true ,
59- fixRightEdge : true ,
60- lockVisibleTimeRangeOnResize : true ,
61- }
62- } ) ;
63-
64- chartDataSeries . current . set ( measurementId , { DataSerie : chart . addLineSeries ( { color : measurement . color } ) , Updater : measurement . getUpdate } ) ;
65-
66- return ( ) => {
67- resizeObserver . disconnect ( ) ;
68- chart . remove ( ) ;
69- } ;
31+ // const measurement = getNumericMeasurementInfo (measurementId);
32+ // const chartLegendItem = createChartLegendItem(measurement);
33+ // chartLegendItem.onclick = (ev) => removeSeriesFromChart(ev, measurementId);
34+ // if (chartLegend) chartLegend.current?.appendChild(chartLegendItem);
35+
36+ // const handleResize = () => {
37+ // if (chartContainerRef.current)
38+ // chart.applyOptions({ width: chartContainerRef.current.clientWidth });
39+ // };
40+
41+ // const resizeObserver = new ResizeObserver(handleResize);
42+ // if (chartContainerRef.current)
43+ // resizeObserver.observe(chartContainerRef.current);
44+
45+ // if (!chartContainerRef.current) return;
46+ // chart = createChart(chartContainerRef.current, {
47+ // layout: {
48+ // background: { type: ColorType.Solid, color: "white" },
49+ // textColor: "black",
50+ // },
51+ // width: chartContainerRef.current.clientWidth,
52+ // height: chartHeight,
53+ // timeScale: {
54+ // timeVisible: true,
55+ // secondsVisible: true,
56+ // fixLeftEdge: true,
57+ // fixRightEdge: true,
58+ // lockVisibleTimeRangeOnResize: true,
59+ // }
60+ // });
61+
62+ // chartDataSeries.current.set(measurementId, {DataSerie: chart.addLineSeries({ color: measurement.color }), Updater: measurement.getUpdate});
63+
64+ // return () => {
65+ // resizeObserver.disconnect();
66+ // chart.remove();
67+ // };
7068 } ) ;
7169
7270 useEffect ( ( ) => {
73- if ( chartContainerRef . current )
74- chartContainerRef . current . ondrop = ( ev ) => {
75- ev . stopPropagation ( ) ;
76- const measurementId = ev . dataTransfer ?. getData ( "id" ) ;
77- if ( measurementId === undefined || chartDataSeries . current . get ( measurementId ) ) return ;
78- const measurement = getMeasurementInfo ( measurementId ) ;
79- chartDataSeries . current . set ( measurementId , { DataSerie : chart . addLineSeries ( { color : measurement . color } ) , Updater : measurement . getUpdate } ) ;
80- const chartLegendItem = createChartLegendItem ( measurement ) ;
81- chartLegendItem . onclick = ( ev ) => removeSeriesFromChart ( ev , measurementId ) ;
82- if ( chartLegend ) chartLegend . current ?. appendChild ( chartLegendItem ) ;
83- } ;
71+ // if(chartContainerRef.current)
72+ // chartContainerRef.current.ondrop = (ev) => {
73+ // ev.stopPropagation();
74+ // const measurementId = ev.dataTransfer?.getData("id");
75+ // if (measurementId === undefined || chartDataSeries.current.get(measurementId)) return;
76+ // const measurement = getMeasurementInfo(measurementId);
77+ // chartDataSeries.current.set(measurementId, {DataSerie: chart.addLineSeries({ color: measurement.color }), Updater: measurement.getUpdate});
78+ // const chartLegendItem = createChartLegendItem(measurement);
79+ // chartLegendItem.onclick = (ev) => removeSeriesFromChart(ev, measurementId);
80+ // if (chartLegend) chartLegend.current?.appendChild(chartLegendItem);
81+ // };
8482 } ) ;
8583
8684 // Update the chart with the new measurements values.
@@ -122,7 +120,7 @@ export const ChartElement = memo(({ chartId, measurementId, chartHeight, removeC
122120 ) ;
123121} ) ;
124122
125- function createChartLegendItem ( measurement : MeasurementInfo ) {
123+ function createChartLegendItem ( measurement : NumericMeasurementInfo ) {
126124 const legendItem = document . createElement ( "div" ) ;
127125 legendItem . setAttribute ( "data-id" , measurement . id ) ;
128126 legendItem . className = styles . chartLegendItem ;
0 commit comments