From 45df1e7023addfb002828a89cc0d34c89360c2c5 Mon Sep 17 00:00:00 2001 From: RoB Murdock Date: Fri, 13 Dec 2024 13:12:07 -0500 Subject: [PATCH] updating rechart line chart --- .../client/CouplingResponsiveLine.jsx | 97 +++++++++++-------- 1 file changed, 57 insertions(+), 40 deletions(-) diff --git a/client/src/jsMain/javascript/com/zegreatrob/coupling/client/CouplingResponsiveLine.jsx b/client/src/jsMain/javascript/com/zegreatrob/coupling/client/CouplingResponsiveLine.jsx index f7ef02804..f38471dd2 100644 --- a/client/src/jsMain/javascript/com/zegreatrob/coupling/client/CouplingResponsiveLine.jsx +++ b/client/src/jsMain/javascript/com/zegreatrob/coupling/client/CouplingResponsiveLine.jsx @@ -1,4 +1,3 @@ -import {ResponsiveLine} from '@nivo/line' import { LineChart, CartesianGrid, XAxis, @@ -6,10 +5,11 @@ import { Tooltip, Legend, Line, + ResponsiveContainer, } from 'recharts' import {timeFormat as d3TimeFormat} from 'd3-time-format' -import { scaleOrdinal } from 'd3-scale' -import { schemeCategory10 } from 'd3-scale-chromatic' +import {scaleOrdinal, scaleTime} from 'd3-scale' +import {schemeCategory10} from 'd3-scale-chromatic' import * as React from "react" export const CouplingResponsiveLine = (props) => { @@ -17,10 +17,11 @@ export const CouplingResponsiveLine = (props) => { ...value, x: value.x.getTime(), [`${line.id}`]: value.y - })))]) - console.log('allPoints', allPoints) + })))]).sort((a, b) => a.x - b.x) + + const flattenedPoints = Object.entries(Object.groupBy(allPoints, item => item.x)) + .map(group => group[1].reduce((result, current) => Object.assign(result, current), {})); const lineIds = props.data.map(value => value.id); - console.log('line ids', lineIds) const xValues = allPoints.map(point => point.x); const xMin = props.xMin ?? new Date(Math.min(...xValues)) const xMax = props.xMax ?? new Date(Math.max(...xValues)) @@ -50,40 +51,56 @@ export const CouplingResponsiveLine = (props) => { const {format, precision} = calculatePrecision() const myColor = scaleOrdinal().domain(lineIds).range(schemeCategory10); - return ( - - - - d3TimeFormat(format)(value)}/> - - {lineIds.map((lineId, index) => - d3TimeFormat(format)(value)} - />)} - ) + const timeScale = scaleTime().domain([xMinMillis, xMaxMillis]).nice(); + const [activeSeries, setActiveSeries] = React.useState([]); + const handleLegendClick = (dataKey) => { + if (activeSeries.includes(dataKey)) { + setActiveSeries(activeSeries.filter(el => el !== dataKey)); + } else { + setActiveSeries(prev => [...prev, dataKey]); + } + }; + return ( + + + + date.valueOf())} + scale={timeScale} + type={'number'} + ticks={timeScale.ticks(5).map(date => date.valueOf())} + tickFormatter={d3TimeFormat(format)} + fontSize={12} + /> + + d3TimeFormat(format)(value)}/> + handleLegendClick(props.dataKey)} + /> + {lineIds.map((lineId, index) => + d3TimeFormat(format)(value)} + />)} + + + ) return (