Skip to content

Commit

Permalink
replacement of am5charts for a more simple library
Browse files Browse the repository at this point in the history
  • Loading branch information
romer8 committed May 12, 2024
1 parent 6414ef3 commit 1612efb
Show file tree
Hide file tree
Showing 11 changed files with 269 additions and 2,365 deletions.
2,162 changes: 54 additions & 2,108 deletions package-lock.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@
"author": "",
"license": "ISC",
"dependencies": {
"@amcharts/amcharts5": "^5.9.4",
"axios": "^0.27.2",
"bootstrap": "^5.1.3",
"chartist": "^1.3.0",
"color": "^4.2.3",
"css-loader": "^6.5.1",
"dotenv": "^16.0.1",
Expand Down
1 change: 1 addition & 0 deletions reactapp/App.scss
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ $primary: #2c3e50;

/* import bootstrap to set changes */
@import "../node_modules/bootstrap/scss/bootstrap";
@import '../node_modules/chartist/dist/index.css';

:root {
--ts-header-height: 56px;
Expand Down
118 changes: 86 additions & 32 deletions reactapp/features/hydroFabric/components/hydroFabricLinePlot.js
Original file line number Diff line number Diff line change
@@ -1,54 +1,108 @@
import { useEffect ,useRef } from "react";
import { useEffect, useRef } from 'react';
import { useHydroFabricContext } from "../hooks/useHydroFabricContext";
import { initializeChart, updateSeries } from "../lib/chartAuxiliary";
// import Chartist from 'chartist';
import { LineChart, FixedScaleAxis, easings } from 'chartist';
import { makeAxis,addAnimationToLineChart,makeTitle } from '../lib/chartAuxiliary';
import 'chartist/dist/index.css';
import '../css/chart.css';



const chartOptions = {
axisX: {
type: FixedScaleAxis,
divisor: 5,
labelInterpolationFnc: function(value) {
return new Date(value).toLocaleDateString();
}
},
axisY: {
offset: 20
},
showArea: true,
fullWidth: true,
showPoint: false,
chartPadding: {
left: 70,
top: 50
}
};


const HydroFabricLinePlot = (props) => {
// Reference to the chart container
const chartRef = useRef(null);
const {state, actions} = useHydroFabricContext();
const variable = `${state.catchment.id ? state.catchment.variable : 'streamflow'}`;
const chartInstance = useRef(null);

useEffect(() => {
chartRef.current = initializeChart('chartdiv', props.title , props.subtitle) // initialize the chart
return () => {
// if full screen has changed, reset the chart
if (chartRef.current && props.singleRowOn ){
chartRef.current && chartRef.current.dispose();
actions.reset();
}
}
}, []);
const { state, actions } = useHydroFabricContext();

useEffect(() => {
if (!state.nexus.series) return
updateSeries(chartRef.current,state.nexus,props.title, props.subtitle,variable)
if (!state.nexus.series) return;
const nexusSeries = state.nexus.series.map(point => ({x: new Date(point.x), y: point.y}));
const chartData = {
series: [
{ name: 'Nexus', data: nexusSeries },
]
};

chartInstance.current = new LineChart (chartRef.current, chartData, chartOptions);

addAnimationToLineChart(chartInstance.current, easings)
makeAxis(chartRef.current,'Date', 'StreamFlow' )
makeTitle(chartRef.current, `StreamFlow: ${state.nexus.id}`)


return () => {
if (chartRef.current && props.singleRowOn) {
chartRef.current.dispose();
if(chartInstance && props.singleRowOn){
actions.reset_nexus();
chartInstance.current.detach();
document.getElementById('x-axis-title')?.remove();
document.getElementById('y-axis-title')?.remove();
}
};
}, [state.nexus.series]);


useEffect(() => {
if (!state.catchment.series) return
updateSeries(chartRef.current,state.catchment,props.title, props.subtitle, variable)
if (!state.catchment.series) return;
if (chartRef.current) {
const catchmentSeries = state.catchment.series.map(point => ({x: new Date(point.x), y: point.y}));

const chartData = {
series: [
{ name: 'Catchment', data: catchmentSeries }
]
};

chartInstance.current = new LineChart(chartRef.current, chartData, chartOptions);

addAnimationToLineChart(chartInstance.current, easings)
makeAxis(
chartRef.current,
'Time (Date)',
`${state.catchment.variable ? state.catchment.variable.toLowerCase().replace(/_/g, " ") : state.catchment.variable_list ? state.catchment.variable_list[0].label : null}`
)


makeTitle(
chartRef.current,
`${state.catchment.variable ? state.catchment.variable.toLowerCase().replace(/_/g, " ") : state.catchment.variable_list ? state.catchment.variable_list[0].label : null}: ${state.catchment.id} `)
}

return () => {
if (chartRef.current && props.singleRowOn) {
chartRef.current.dispose();
if(props.singleRowOn){
actions.reset_catchment();
chartRef.current.detach();
document.getElementById('x-axis-title')?.remove();
document.getElementById('y-axis-title')?.remove();
}
};
}, [state.catchment.series]);


return (
<div id="chartdiv" style={{ width: "100%", height: "100%" }}></div>

)

}
export default HydroFabricLinePlot;
};
}, [state.catchment.series]); // Re-run effect if series data changes

return (
<div ref={chartRef} style={{ width: "100%", height: "90%", position: "relative"}}></div>
);
};

export default HydroFabricLinePlot;
8 changes: 6 additions & 2 deletions reactapp/features/hydroFabric/components/selectComponent.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,16 @@ const customStyles = {
})
};


// Usage of the Select component with the custom Option component
const SelectComponent = ({ optionsList, onChangeHandler,defaultValue }) => {
// Handler for when an option is selected, wrapped in useCallback
const handleChange = useCallback((option) => {
onChangeHandler(`${option.value}`)
}, [onChangeHandler,defaultValue]);
}, [onChangeHandler]);
// const handleChange = (option) =>{
// onChangeHandler(`${option.value}`)
// }

return (
<Select
Expand All @@ -54,7 +58,7 @@ const SelectComponent = ({ optionsList, onChangeHandler,defaultValue }) => {
filterOption={createFilter({ ignoreAccents: false })} // this makes all the difference!
options={optionsList}
onChange={handleChange}
defaultValue={defaultValue}
value={defaultValue}
/>
);
};
Expand Down
8 changes: 8 additions & 0 deletions reactapp/features/hydroFabric/css/chart.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
.ct-series-a .ct-line {
stroke: #2c3e50; /* Blue */
stroke-width: 2px;
}

.ct-series-a .ct-area {
fill: #2c3e50;
}
Loading

0 comments on commit 1612efb

Please sign in to comment.