-
Notifications
You must be signed in to change notification settings - Fork 2.8k
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
feat(react-charting): make charts responsive #33723
Open
krkshitij
wants to merge
9
commits into
microsoft:master
Choose a base branch
from
krkshitij:responsive-charts-v8-2
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+27,814
−26,890
Open
Changes from 2 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
0bcb930
make cartesian charts responsive
krkshitij bdf306b
add change file
krkshitij 340db7a
make non-cartesian charts responsive
krkshitij bf8d0ba
add comments + update api
krkshitij dd7b69d
Merge branch 'master' of https://github.com/microsoft/fluentui into r…
krkshitij 6dece1d
update snapshots
krkshitij c6d1ce0
use responsive container in declarative chart
krkshitij eb776d6
remove default width for donut chart
krkshitij 786dfec
update snapshots
krkshitij 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
7 changes: 7 additions & 0 deletions
7
change/@fluentui-react-charting-54da258a-d516-4f4d-a974-d05d46893ca6.json
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 |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"type": "patch", | ||
"comment": "feat: make charts responsive", | ||
"packageName": "@fluentui/react-charting", | ||
"email": "[email protected]", | ||
"dependentChangeType": "patch" | ||
} |
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 |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './components/ResponsiveContainer/index'; |
22 changes: 22 additions & 0 deletions
22
...es/charts/react-charting/src/components/ResponsiveContainer/ResponsiveContainer.styles.ts
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 |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { IResponsiveContainerStyles } from './ResponsiveContainer.types'; | ||
|
||
export const getStyles = (): IResponsiveContainerStyles => { | ||
return { | ||
root: { | ||
width: '100%', | ||
height: '100%', | ||
|
||
'& [class^="chartWrapper-"]': { | ||
width: '100%', // optional | ||
// To prevent chart height from collapsing while resizing | ||
height: '100%', // optional | ||
|
||
'> svg': { | ||
// This overrides the pixel width and height of svg allowing it to resize properly within flexbox | ||
width: '100%', | ||
height: '100%', | ||
}, | ||
}, | ||
}, | ||
}; | ||
}; |
64 changes: 64 additions & 0 deletions
64
packages/charts/react-charting/src/components/ResponsiveContainer/ResponsiveContainer.tsx
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 |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import * as React from 'react'; | ||
import { classNamesFunction, getWindow } from '@fluentui/react'; | ||
import { IResponsiveContainerProps, IResponsiveContainerStyles } from './ResponsiveContainer.types'; | ||
import { getStyles } from './ResponsiveContainer.styles'; | ||
|
||
const getClassNames = classNamesFunction<{}, IResponsiveContainerStyles>(); | ||
|
||
export const ResponsiveContainer: React.FC<IResponsiveContainerProps> = props => { | ||
const containerRef = React.useRef<HTMLDivElement>(null); | ||
const onResizeRef = React.useRef<IResponsiveContainerProps['onResize']>(); | ||
const classNames = React.useMemo(() => getClassNames(getStyles()), []); | ||
|
||
const [size, setSize] = React.useState<{ containerWidth?: number; containerHeight?: number }>({}); | ||
|
||
onResizeRef.current = props.onResize; | ||
|
||
React.useEffect(() => { | ||
const _window = getWindow(containerRef.current) as (Window & typeof globalThis) | undefined; | ||
let animationFrameId: number | undefined; | ||
let resizeObserver: ResizeObserver | undefined; | ||
|
||
const resizeCallback = (entries: ResizeObserverEntry[]) => { | ||
const { width: containerWidth, height: containerHeight } = entries[0].contentRect; | ||
// rAF is an alternative to the throttle function. For more info, see: | ||
// https://css-tricks.com/debouncing-throttling-explained-examples/#aa-requestanimationframe-raf | ||
animationFrameId = _window?.requestAnimationFrame(() => { | ||
setSize(prevSize => { | ||
const roundedWidth = Math.floor(containerWidth); | ||
const roundedHeight = Math.floor(containerHeight); | ||
if (prevSize.containerWidth === roundedWidth && prevSize.containerHeight === roundedHeight) { | ||
return prevSize; | ||
} | ||
|
||
return { containerWidth: roundedWidth, containerHeight: roundedHeight }; | ||
}); | ||
}); | ||
onResizeRef.current?.(containerWidth, containerHeight); | ||
}; | ||
|
||
if (_window && _window.ResizeObserver) { | ||
resizeObserver = new _window.ResizeObserver(resizeCallback); | ||
if (containerRef.current) { | ||
resizeObserver.observe(containerRef.current); | ||
} | ||
} | ||
|
||
return () => { | ||
if (animationFrameId) { | ||
_window?.cancelAnimationFrame(animationFrameId); | ||
} | ||
|
||
resizeObserver?.disconnect(); | ||
}; | ||
}, []); | ||
|
||
return ( | ||
<div ref={containerRef} className={classNames.root} style={{ width: props.width, height: props.height }}> | ||
{React.cloneElement(props.children, { | ||
width: size.containerWidth, | ||
height: size.containerHeight, | ||
})} | ||
</div> | ||
); | ||
}; |
31 changes: 31 additions & 0 deletions
31
...ges/charts/react-charting/src/components/ResponsiveContainer/ResponsiveContainer.types.ts
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 |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import * as React from 'react'; | ||
import { IStyle } from '@fluentui/react/lib/Styling'; | ||
|
||
export interface IResponsiveContainerProps { | ||
/** | ||
* | ||
*/ | ||
width?: number | string; | ||
|
||
/** | ||
* | ||
*/ | ||
height?: number | string; | ||
|
||
/** | ||
* | ||
*/ | ||
onResize?: (width: number, height: number) => void; | ||
|
||
/** | ||
* | ||
*/ | ||
children: React.ReactElement<{ width?: number; height?: number }>; | ||
} | ||
|
||
export interface IResponsiveContainerStyles { | ||
/** | ||
* | ||
*/ | ||
root: IStyle; | ||
} |
2 changes: 2 additions & 0 deletions
2
packages/charts/react-charting/src/components/ResponsiveContainer/index.ts
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 |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from './ResponsiveContainer'; | ||
export * from './ResponsiveContainer.types'; |
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
132 changes: 132 additions & 0 deletions
132
...eact-examples/src/react-charting/VerticalBarChart/VerticalBarChart.Responsive.Example.tsx
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 |
---|---|---|
@@ -0,0 +1,132 @@ | ||
import * as React from 'react'; | ||
import { | ||
VerticalBarChart, | ||
IVerticalBarChartDataPoint, | ||
DataVizPalette, | ||
getColorFromToken, | ||
ILineChartLineOptions, | ||
ResponsiveContainer, | ||
} from '@fluentui/react-charting'; | ||
import { classNamesFunction, DefaultPalette, IStyle } from '@fluentui/react'; | ||
|
||
interface IVBCResponsiveExampleStyles { | ||
resizableArea: IStyle; | ||
} | ||
|
||
const getStyles = (): IVBCResponsiveExampleStyles => { | ||
return { | ||
resizableArea: { | ||
display: 'flex', | ||
flexWrap: 'nowrap', | ||
overflow: 'hidden', | ||
|
||
minWidth: '200px', | ||
maxWidth: '800px', | ||
border: `2px solid ${DefaultPalette.blue}`, | ||
padding: '20px 10px 10px 10px', | ||
position: 'relative', | ||
resize: 'horizontal', | ||
'::after': { | ||
content: `'Resizable Area'`, | ||
position: 'absolute', | ||
padding: '1px 4px 1px', | ||
top: '-2px', | ||
left: '-2px', | ||
fontFamily: 'monospace', | ||
fontSize: '15px', | ||
fontWeight: 900, | ||
letterSpacing: '1px', | ||
color: DefaultPalette.white, | ||
backgroundColor: DefaultPalette.blue, | ||
}, | ||
}, | ||
}; | ||
}; | ||
const points: IVerticalBarChartDataPoint[] = [ | ||
{ | ||
x: 0, | ||
y: 10000, | ||
legend: 'Oranges', | ||
color: DefaultPalette.accent, | ||
lineData: { | ||
y: 7000, | ||
}, | ||
}, | ||
{ | ||
x: 10000, | ||
y: 50000, | ||
legend: 'Dogs', | ||
color: DefaultPalette.blueDark, | ||
lineData: { | ||
y: 30000, | ||
}, | ||
}, | ||
{ | ||
x: 25000, | ||
y: 30000, | ||
legend: 'Apples', | ||
color: DefaultPalette.blueMid, | ||
lineData: { | ||
y: 3000, | ||
}, | ||
}, | ||
{ | ||
x: 40000, | ||
y: 13000, | ||
legend: 'Bananas', | ||
color: getColorFromToken(DataVizPalette.color6), | ||
}, | ||
{ | ||
x: 52000, | ||
y: 43000, | ||
legend: 'Giraffes', | ||
color: getColorFromToken(DataVizPalette.color11), | ||
lineData: { | ||
y: 30000, | ||
}, | ||
}, | ||
{ | ||
x: 68000, | ||
y: 30000, | ||
legend: 'Cats', | ||
color: DefaultPalette.blueDark, | ||
lineData: { | ||
y: 5000, | ||
}, | ||
}, | ||
{ | ||
x: 80000, | ||
y: 20000, | ||
legend: 'Elephants', | ||
color: getColorFromToken(DataVizPalette.color11), | ||
lineData: { | ||
y: 16000, | ||
}, | ||
}, | ||
{ | ||
x: 92000, | ||
y: 45000, | ||
legend: 'Monkeys', | ||
color: getColorFromToken(DataVizPalette.color6), | ||
lineData: { | ||
y: 40000, | ||
}, | ||
}, | ||
]; | ||
const lineOptions: ILineChartLineOptions = { lineBorderWidth: '2' }; | ||
|
||
const getClassNames = classNamesFunction<{}, IVBCResponsiveExampleStyles>(); | ||
|
||
export class VerticalBarChartResponsiveExample extends React.Component { | ||
private _classNames = getClassNames(getStyles()); | ||
|
||
public render(): JSX.Element { | ||
return ( | ||
<div className={this._classNames.resizableArea} style={{ height: '350px' }}> | ||
<ResponsiveContainer> | ||
<VerticalBarChart data={points} lineLegendText={'Line'} lineLegendColor={'brown'} lineOptions={lineOptions} /> | ||
</ResponsiveContainer> | ||
</div> | ||
); | ||
} | ||
} |
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
Oops, something went wrong.
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.
🕵🏾♀️ visual regressions to review in the fluentuiv8 Visual Regression Report
Keytip 1 screenshots
react-charting-AreaChart 1 screenshots
react-charting-LineChart 1 screenshots