Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add zoom btns #94

Merged
merged 3 commits into from
Aug 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
105 changes: 105 additions & 0 deletions frontend/src/charts/Chart.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
import { React, useRef, useState, useMemo } from 'react';
import { Line } from 'react-chartjs-2';
import 'chartjs-adapter-luxon';
import PropTypes from 'prop-types';
import { Box, ToggleButton } from '@mui/material';
import zoom from '../assets/zoom.svg';
import reset from '../assets/reset.svg';
import pan from '../assets/pan.svg';

function Chart(props) {
const chartRef = useRef([]);
const [zoomSelected, setZoomSelected] = useState(false);
const [panSelected, setPanSelected] = useState(true);
const handleResetZoom = () => {
console.log('reset zoom');
if (chartRef.current) {
chartRef.current.resetZoom();
}
};
const handleToggleZoom = () => {
console.log('toggle zoom');
if (chartRef.current) {
chartRef.current.options.plugins.zoom.zoom.wheel.enabled =
!chartRef.current.options.plugins.zoom.zoom.wheel.enabled;
chartRef.current.update();
setZoomSelected(!zoomSelected);
}
};
const handleTogglePan = () => {
console.log('toggle pan');
if (chartRef.current) {
const pan = chartRef.current.options.plugins.zoom.pan.enabled;
chartRef.current.options.plugins.zoom.pan.enabled =
!chartRef.current.options.plugins.zoom.pan.enabled;
chartRef.current.update();
console.log(chartRef.current.options.plugins.zoom.pan.enabled);
setPanSelected(!panSelected);
}
};

// const chart = useMemo(
// () => <Line ref={chartRef} data={props.data} options={props.options} />,
// [props.data, props.options]
// );
return (
<Box
sx={{
display: 'flex',
gap: '1%',
height: '100%',
}}
>
{/* {chart} */}
<Line ref={chartRef} data={props.data} options={props.options} />
<Box
sx={{
display: 'flex',
flexDirection: 'column',
gap: '1%',
}}
>
<ToggleButton
onClick={handleResetZoom}
variant='outlined'
sx={{ width: '32px', height: '32px' }}
>
<Box
component='img'
src={reset}
sx={{ width: '16px', height: '16px' }}
></Box>
</ToggleButton>
<ToggleButton
selected={zoomSelected}
onClick={handleToggleZoom}
sx={{ width: '32px', height: '32px' }}
>
<Box
component='img'
src={zoom}
sx={{ width: '16px', height: '16px' }}
></Box>
</ToggleButton>
<ToggleButton
selected={panSelected}
onClick={handleTogglePan}
sx={{ width: '32px', height: '32px' }}
>
<Box
component='img'
src={pan}
sx={{ width: '16px', height: '16px' }}
></Box>
</ToggleButton>
</Box>
</Box>
);
}

export default Chart;

Chart.propTypes = {
data: PropTypes.object,
options: PropTypes.object,
};
39 changes: 27 additions & 12 deletions frontend/src/charts/PwrChart/PwrChart.jsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { React } from "react";
import { Line } from "react-chartjs-2";
import "chartjs-adapter-luxon";
import { zoomOptions } from "../defaultChartOptions";
import PropTypes from "prop-types";
import { React } from 'react';
import 'chartjs-adapter-luxon';
import { zoomOptions } from '../defaultChartOptions';
import PropTypes from 'prop-types';
import Chart from '../Chart';

export default function PwrChart(props) {
const data = props.data;
Expand All @@ -11,12 +11,12 @@ export default function PwrChart(props) {
responsive: true,
scales: {
x: {
position: "bottom",
position: 'bottom',
title: {
display: true,
text: "Time",
text: 'Time',
},
type: "time",
type: 'time',
ticks: {
autoSkip: false,
autoSkipPadding: 50,
Expand All @@ -27,24 +27,39 @@ export default function PwrChart(props) {
},
time: {
displayFormats: {
day: "D",
day: 'D',
},
},
},
y: {
beginAtZero: true,
title: {
display: true,
text: "Power (µV)",
text: 'Power (µV)',
},
},
},
plugins: {
zoom: zoomOptions,
zoom: {
zoom: {
wheel: {
enabled: false,
},
pinch: {
enabled: false,
},
mode: 'xy',
scaleMode: 'xy',
},
pan: {
enabled: true,
mode: 'xy',
},
},
},
};

return <Line data={data} options={chartOptions} />;
return <Chart data={data} options={chartOptions} />;
}

PwrChart.propTypes = {
Expand Down
45 changes: 30 additions & 15 deletions frontend/src/charts/TempChart/TempChart.jsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { React } from "react";
import { Line } from "react-chartjs-2";
import "chartjs-adapter-luxon";
import { zoomOptions } from "../defaultChartOptions";
import PropTypes from "prop-types";
import { React } from 'react';
import 'chartjs-adapter-luxon';
import { zoomOptions } from '../defaultChartOptions';
import PropTypes from 'prop-types';
import Chart from '../Chart';

export default function TempChart(props) {
const data = props.data;
Expand All @@ -11,12 +11,12 @@ export default function TempChart(props) {
responsive: true,
scales: {
x: {
position: "bottom",
position: 'bottom',
title: {
display: true,
text: "Time",
text: 'Time',
},
type: "time",
type: 'time',
ticks: {
autoSkip: false,
autoSkipPadding: 50,
Expand All @@ -27,28 +27,43 @@ export default function TempChart(props) {
},
time: {
displayFormats: {
hour: "hh:mm a",
day: "MM/dd",
hour: 'hh:mm a',
day: 'MM/dd',
},
},
},
y: {
type: "linear",
position: "left",
type: 'linear',
position: 'left',
beginAtZero: true,
suggestedMax: 35,
title: {
display: true,
text: "Temperature (°C)",
text: 'Temperature (°C)',
},
},
},
plugins: {
zoom: zoomOptions,
zoom: {
zoom: {
wheel: {
enabled: false,
},
pinch: {
enabled: false,
},
mode: 'xy',
scaleMode: 'xy',
},
pan: {
enabled: true,
mode: 'xy',
},
},
},
};

return <Line data={data} options={chartOptions} />;
return <Chart data={data} options={chartOptions} />;
}
TempChart.propTypes = {
data: PropTypes.object,
Expand Down
47 changes: 31 additions & 16 deletions frontend/src/charts/VChart/VChart.jsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { React } from "react";
import { Line } from "react-chartjs-2";
import "chartjs-adapter-luxon";
import { zoomOptions } from "../defaultChartOptions";
import PropTypes from "prop-types";
import { React } from 'react';
import 'chartjs-adapter-luxon';
import { zoomOptions } from '../defaultChartOptions';
import PropTypes from 'prop-types';
import Chart from '../Chart';

export default function VChart(props) {
const data = props.data;
Expand All @@ -11,12 +11,12 @@ export default function VChart(props) {
responsive: true,
scales: {
x: {
position: "bottom",
position: 'bottom',
title: {
display: true,
text: "Time",
text: 'Time',
},
type: "time",
type: 'time',
ticks: {
autoSkip: false,
autoSkipPadding: 50,
Expand All @@ -27,17 +27,17 @@ export default function VChart(props) {
},
time: {
displayFormats: {
hour: "hh:mm",
day: "D",
hour: 'hh:mm',
day: 'D',
},
},
},
vAxis: {
position: "left",
position: 'left',
beginAtZero: true,
title: {
display: true,
text: "Cell Voltage (V)",
text: 'Cell Voltage (V)',
},
suggestedMax: 0.28,
min: 0,
Expand All @@ -47,20 +47,35 @@ export default function VChart(props) {
},
},
cAxis: {
position: "right",
position: 'right',
beginAtZero: true,
title: {
display: true,
text: "Current (µA)",
text: 'Current (µA)',
},
},
},
plugins: {
zoom: zoomOptions,
zoom: {
zoom: {
wheel: {
enabled: false,
},
pinch: {
enabled: false,
},
mode: 'xy',
scaleMode: 'xy',
},
pan: {
enabled: true,
mode: 'xy',
},
},
},
};

return <Line data={data} options={chartOptions} />;
return <Chart data={data} options={chartOptions} />;
}

VChart.propTypes = {
Expand Down
Loading
Loading