-
Notifications
You must be signed in to change notification settings - Fork 7
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
Waitp 1316 Chart table #4720
Merged
alexd-bes
merged 10 commits into
epic-frontend-rewrite
from
waitp-1316-map-flippa-table
Jul 12, 2023
Merged
Waitp 1316 Chart table #4720
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
836fe4d
WAITP-1216: tab buttons
alexd-bes efba458
WIP
alexd-bes cbf6dd9
Merge branch 'epic-frontend-rewrite' into waitp-1316-map-flippa-table
alexd-bes 11450fe
Rename files
alexd-bes 57e968c
Merge branch 'epic-frontend-rewrite' into waitp-1316-map-flippa-table
alexd-bes 2e8fa53
rearrange files
alexd-bes 955ddeb
Remove unused types
alexd-bes 673252c
Update Chart.tsx
alexd-bes c317af7
Merge branch 'epic-frontend-rewrite' into waitp-1316-map-flippa-table
alexd-bes 2093828
Fix colors
alexd-bes File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,51 +3,130 @@ | |
* Copyright (c) 2017 - 2023 Beyond Essential Systems Pty Ltd | ||
*/ | ||
|
||
import React from 'react'; | ||
import React, { ChangeEvent, useState } from 'react'; | ||
import styled from 'styled-components'; | ||
import { Chart as ChartComponent, ViewContent } from '@tupaia/ui-chart-components'; | ||
import { Chart as ChartComponent, ChartTable, ViewContent } from '@tupaia/ui-chart-components'; | ||
import { BarChart, GridOn } from '@material-ui/icons'; | ||
import { Tabs, darken, lighten } from '@material-ui/core'; | ||
import { Tab } from '@material-ui/core'; | ||
import { TabContext, TabPanel } from '@material-ui/lab'; | ||
|
||
const Wrapper = styled.div<{ | ||
$isEnlarged: boolean; | ||
$hasData: boolean; | ||
}>` | ||
const Wrapper = styled.div` | ||
display: flex; | ||
position: relative; | ||
align-content: stretch; | ||
-webkit-box-align: stretch; | ||
align-items: stretch; | ||
height: ${({ $isEnlarged, $hasData }) => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this line is fine- just noticed that webkit selector above which is now redundant. |
||
if (!$hasData) return 'auto'; | ||
return $isEnlarged ? '22.5rem' : '14rem'; | ||
}}; | ||
flex-direction: column; | ||
.recharts-responsive-container { | ||
min-width: 0px; | ||
} | ||
.recharts-wrapper { | ||
font-size: 1rem !important; // this is to make sure the labels on the charts are relative to the base font size | ||
} | ||
// Make the charts conform to the parent container's size | ||
.recharts-wrapper, | ||
.recharts-wrapper svg { | ||
height: 100% !important; | ||
width: 100%; | ||
} | ||
li.recharts-legend-item { | ||
white-space: nowrap; // ensure there are no line breaks on the export legends | ||
} | ||
`; | ||
|
||
const TabsWrapper = styled.div` | ||
width: 100%; | ||
display: flex; | ||
justify-content: flex-end; | ||
|
||
.MuiTabs-indicator { | ||
display: none; | ||
} | ||
`; | ||
|
||
const TabsGroup = styled(Tabs)` | ||
border: 1px solid | ||
${({ theme }) => { | ||
const { | ||
text: { primary }, | ||
type, | ||
} = theme.palette; | ||
// This is to give the illusion of a thinner border, by blending it into the background more | ||
return type === 'light' ? lighten(primary, 0.5) : darken(primary, 0.6); | ||
}}; | ||
border-radius: 5px; | ||
min-height: 0; | ||
`; | ||
|
||
const TabButton = styled(Tab)` | ||
min-width: 0; | ||
padding: 0.3rem; | ||
min-height: 0; | ||
svg { | ||
width: 1.2rem; | ||
height: 1.2rem; | ||
} | ||
&[aria-selected='true'] { | ||
background-color: ${({ theme }) => theme.palette.primary.main}; | ||
} | ||
`; | ||
|
||
const ContentWrapper = styled.div<{ | ||
$isEnlarged: boolean; | ||
}>` | ||
padding: ${({ $isEnlarged }) => ($isEnlarged ? '1rem 0' : 'initial')}; | ||
`; | ||
|
||
interface ChartProps { | ||
viewContent: ViewContent; | ||
isEnlarged?: boolean; | ||
} | ||
|
||
const DISPLAY_TYPE_VIEWS = [ | ||
{ | ||
value: 'chart', | ||
Icon: BarChart, | ||
label: 'View chart', | ||
display: ChartComponent, | ||
}, | ||
{ | ||
value: 'table', | ||
Icon: GridOn, | ||
label: 'View table', | ||
display: ChartTable, | ||
}, | ||
]; | ||
|
||
export const Chart = ({ viewContent, isEnlarged = false }: ChartProps) => { | ||
const hasData = viewContent.data && viewContent.data.length > 0 ? true : false; | ||
const [displayType, setDisplayType] = useState(DISPLAY_TYPE_VIEWS[0].value); | ||
const handleChangeDisplayType = (_event: ChangeEvent<{}>, value: 'chart' | 'table') => { | ||
setDisplayType(value); | ||
}; | ||
|
||
const availableDisplayTypes = isEnlarged ? DISPLAY_TYPE_VIEWS : [DISPLAY_TYPE_VIEWS[0]]; | ||
|
||
return ( | ||
<Wrapper $isEnlarged={isEnlarged} $hasData={hasData}> | ||
<ChartComponent viewContent={viewContent} isEnlarged={isEnlarged} isExporting={false} /> | ||
<Wrapper> | ||
<TabContext value={displayType}> | ||
{isEnlarged && ( | ||
<TabsWrapper> | ||
<TabsGroup | ||
value={displayType} | ||
onChange={handleChangeDisplayType} | ||
variant="standard" | ||
aria-label="Toggle display type" | ||
> | ||
{DISPLAY_TYPE_VIEWS.map(({ value, Icon, label }) => ( | ||
<TabButton key={value} value={value} icon={<Icon />} aria-label={label} /> | ||
))} | ||
</TabsGroup> | ||
</TabsWrapper> | ||
)} | ||
{availableDisplayTypes.map(({ value, display: Content }) => ( | ||
<ContentWrapper | ||
key={value} | ||
value={value} | ||
as={isEnlarged ? TabPanel : 'div'} | ||
$isEnlarged={isEnlarged} | ||
> | ||
<Content viewContent={viewContent} isEnlarged={isEnlarged} isExporting={false} /> | ||
</ContentWrapper> | ||
))} | ||
</TabContext> | ||
</Wrapper> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This was because I was getting some weird TS errors, and this fixed it