Skip to content

Commit

Permalink
feat: add responsive chart option (#256)
Browse files Browse the repository at this point in the history
  • Loading branch information
wadjih-bencheikh18 authored Oct 30, 2022
1 parent 79f9147 commit 6d7b5a0
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 49 deletions.
28 changes: 20 additions & 8 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@
"devDependencies": {
"@babel/core": "^7.19.3",
"@babel/eslint-parser": "^7.19.1",
"@ladle/react": "^2.4.4",
"@ladle/react": "^2.4.5",
"@playwright/experimental-ct-react": "^1.27.1",
"@playwright/test": "^1.27.1",
"@types/lodash": "^4.14.186",
Expand All @@ -87,6 +87,7 @@
"ml-spectra-processing": "^11.13.0",
"prettier": "^2.7.1",
"react": "^18.2.0",
"react-d3-utils": "^0.6.0",
"react-dom": "^18.2.0",
"react-icons": "^4.6.0",
"react-inspector": "^6.0.1",
Expand Down
14 changes: 12 additions & 2 deletions src/app/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -182,9 +182,19 @@ function DropZoneArea() {
// reset the state of your app so the error doesn't happen again
}}
>
<div style={{ padding: 5 }}>
<div
style={{
padding: 5,
width: '100%',
height: '100%',
}}
>
{measurement ? (
<MeasurementExplorer measurement={measurement} />
<MeasurementExplorer
measurement={measurement}
width="100%"
height="100%"
/>
) : (
<span>No data, add them with drag and drop</span>
)}
Expand Down
6 changes: 3 additions & 3 deletions src/app/components/MeasurementExplorer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,15 @@ export function MeasurementExplorer(props: MeasurementExplorerProps) {
});
return (
<div
style={{ width, height }}
css={css`
width: ${width}px;
height: ${height}px;
display: flex;
flex-direction: column;
`}
>
<div
css={css`
display: flex;
gap: 40px;
justify-content: space-around;
margin-bottom: 20px;
`}
Expand Down
85 changes: 52 additions & 33 deletions src/app/components/MeasurementPlot.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
/** @jsxImportSource @emotion/react */
import { css } from '@emotion/react';
import { xyToXYObject } from 'ml-spectra-processing';
import { useMemo } from 'react';
import { ResponsiveChart } from 'react-d3-utils';
import {
Annotations,
Axis,
Expand All @@ -25,8 +28,8 @@ export interface MeasurementPlotProps {
dataIndex?: number;
xVariableName?: string;
yVariableName?: string;
width?: number;
height?: number;
width?: number | `${number}%`;
height?: number | `${number}%`;
zoom?: 'horizontal' | 'vertical' | 'rectangular' | '';
wheelZoom?: 'vertical' | 'horizontal' | '';
crossHair?: boolean;
Expand Down Expand Up @@ -104,36 +107,52 @@ function MeasurementComponent(props: MeasurementPlotProps) {
});
usePan({ horizontalAxisId: xAxis, verticalAxisId: yAxis });
return (
<Plot width={width} height={height}>
<Heading title={title} />
<LineSeries
data={xyToXYObject({
x: x.data,
y: y.data,
})}
xAxis={xAxis}
yAxis={yAxis}
/>
<Annotations>
{rectZoom.annotations}
{axisZoom.annotations}
{crossHairAnnot.annotations}
</Annotations>
<Axis
id={xAxis}
hidden={!showHorizontalAxis}
displayPrimaryGridLines={showVerticalGrid}
flip={flipHorizontalAxis}
position="bottom"
label={`${x.label}${x.units ? `(${x.units})` : ''}`}
/>
<Axis
id={yAxis}
hidden={!showVerticalAxis}
displayPrimaryGridLines={showHorizontalGrid}
position="left"
label={`${y.label}${y.units ? `(${y.units})` : ''}`}
/>
</Plot>
<div
style={{ width, height }}
css={css`
user-drag: none;
-webkit-user-drag: none;
user-select: none;
-moz-user-select: none;
-webkit-user-select: none;
-ms-user-select: none;
`}
>
<ResponsiveChart width={width} height={height}>
{({ width, height }) => (
<Plot width={width} height={height}>
<Heading title={title} />
<LineSeries
data={xyToXYObject({
x: x.data,
y: y.data,
})}
xAxis={xAxis}
yAxis={yAxis}
/>
<Annotations>
{rectZoom.annotations}
{axisZoom.annotations}
{crossHairAnnot.annotations}
</Annotations>
<Axis
id={xAxis}
hidden={!showHorizontalAxis}
displayPrimaryGridLines={showVerticalGrid}
flip={flipHorizontalAxis}
position="bottom"
label={`${x.label}${x.units ? `(${x.units})` : ''}`}
/>
<Axis
id={yAxis}
hidden={!showVerticalAxis}
displayPrimaryGridLines={showHorizontalGrid}
position="left"
label={`${y.label}${y.units ? `(${y.units})` : ''}`}
/>
</Plot>
)}
</ResponsiveChart>
</div>
);
}
4 changes: 2 additions & 2 deletions stories/app/measurements/measurement-plot.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ Control.args = {
dataIndex: 0,
xVariableName: 'x',
yVariableName: 'y',
width: 800,
height: 400,
width: '100%',
height: 300,
crossHair: true,
showHorizontalAxis: true,
showVerticalAxis: true,
Expand Down

0 comments on commit 6d7b5a0

Please sign in to comment.