-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[charts] Allow the creation of custom HTML components using charts da…
…ta (mui#15511) Signed-off-by: Jose C Quintas Jr <juniorquintas@gmail.com> Signed-off-by: Alexandre Fauquette <45398769+alexfauquette@users.noreply.github.com> Co-authored-by: Alexandre Fauquette <45398769+alexfauquette@users.noreply.github.com>
- Loading branch information
Showing
26 changed files
with
503 additions
and
143 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import * as React from 'react'; | ||
import Box from '@mui/material/Box'; | ||
import { unstable_useBarSeries } from '@mui/x-charts/hooks'; | ||
import { ChartDataProvider } from '@mui/x-charts/context'; | ||
import { ChartsSurface } from '@mui/x-charts/ChartsSurface'; | ||
import { BarPlot } from '@mui/x-charts/BarChart'; | ||
import { ChartsXAxis } from '@mui/x-charts/ChartsXAxis'; | ||
import { ChartsYAxis } from '@mui/x-charts/ChartsYAxis'; | ||
|
||
function MyCustomLegend() { | ||
const s = unstable_useBarSeries(); | ||
return ( | ||
<table | ||
style={{ | ||
marginLeft: 40, | ||
marginRight: 40, | ||
}} | ||
> | ||
<tbody> | ||
{Object.values(s?.series ?? []).map((v) => { | ||
return ( | ||
<tr key={v.id}> | ||
<td aria-hidden> | ||
<div | ||
style={{ | ||
background: v.color, | ||
height: 10, | ||
width: 10, | ||
marginRight: 10, | ||
flexShrink: 0, | ||
borderRadius: 5, | ||
}} | ||
/> | ||
</td> | ||
<td>{`${v.label}`}</td> | ||
</tr> | ||
); | ||
})} | ||
</tbody> | ||
</table> | ||
); | ||
} | ||
|
||
const veryLongText = | ||
"Second Series. You should always try to avoid long sentences. But oftentimes, it's not possible. So, we need to handle them gracefully. This is a very long sentence that should be fully readable."; | ||
|
||
export default function HtmlLegend() { | ||
return ( | ||
<Box sx={{ height: 400, display: 'flex', flexDirection: 'column' }}> | ||
<ChartDataProvider | ||
series={[ | ||
{ label: 'First Series', type: 'bar', data: [100, 200] }, | ||
{ label: veryLongText, type: 'bar', data: [45, 333] }, | ||
]} | ||
xAxis={[{ data: ['A', 'B'], scaleType: 'band', id: 'x-axis' }]} | ||
> | ||
<ChartsSurface> | ||
<BarPlot /> | ||
<ChartsXAxis position="bottom" axisId="x-axis" /> | ||
<ChartsYAxis position="left" /> | ||
</ChartsSurface> | ||
<MyCustomLegend /> | ||
</ChartDataProvider> | ||
</Box> | ||
); | ||
} |
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,66 @@ | ||
import * as React from 'react'; | ||
import Box from '@mui/material/Box'; | ||
import { unstable_useBarSeries } from '@mui/x-charts/hooks'; | ||
import { ChartDataProvider } from '@mui/x-charts/context'; | ||
import { ChartsSurface } from '@mui/x-charts/ChartsSurface'; | ||
import { BarPlot } from '@mui/x-charts/BarChart'; | ||
import { ChartsXAxis } from '@mui/x-charts/ChartsXAxis'; | ||
import { ChartsYAxis } from '@mui/x-charts/ChartsYAxis'; | ||
|
||
function MyCustomLegend() { | ||
const s = unstable_useBarSeries(); | ||
return ( | ||
<table | ||
style={{ | ||
marginLeft: 40, | ||
marginRight: 40, | ||
}} | ||
> | ||
<tbody> | ||
{Object.values(s?.series ?? []).map((v) => { | ||
return ( | ||
<tr key={v.id}> | ||
<td aria-hidden> | ||
<div | ||
style={{ | ||
background: v.color, | ||
height: 10, | ||
width: 10, | ||
marginRight: 10, | ||
flexShrink: 0, | ||
borderRadius: 5, | ||
}} | ||
/> | ||
</td> | ||
<td>{`${v.label}`}</td> | ||
</tr> | ||
); | ||
})} | ||
</tbody> | ||
</table> | ||
); | ||
} | ||
|
||
const veryLongText = | ||
"Second Series. You should always try to avoid long sentences. But oftentimes, it's not possible. So, we need to handle them gracefully. This is a very long sentence that should be fully readable."; | ||
|
||
export default function HtmlLegend() { | ||
return ( | ||
<Box sx={{ height: 400, display: 'flex', flexDirection: 'column' }}> | ||
<ChartDataProvider | ||
series={[ | ||
{ label: 'First Series', type: 'bar', data: [100, 200] }, | ||
{ label: veryLongText, type: 'bar', data: [45, 333] }, | ||
]} | ||
xAxis={[{ data: ['A', 'B'], scaleType: 'band', id: 'x-axis' }]} | ||
> | ||
<ChartsSurface> | ||
<BarPlot /> | ||
<ChartsXAxis position="bottom" axisId="x-axis" /> | ||
<ChartsYAxis position="left" /> | ||
</ChartsSurface> | ||
<MyCustomLegend /> | ||
</ChartDataProvider> | ||
</Box> | ||
); | ||
} |
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,14 @@ | ||
<ChartDataProvider | ||
series={[ | ||
{ label: 'First Series', type: 'bar', data: [100, 200] }, | ||
{ label: veryLongText, type: 'bar', data: [45, 333] }, | ||
]} | ||
xAxis={[{ data: ['A', 'B'], scaleType: 'band', id: 'x-axis' }]} | ||
> | ||
<ChartsSurface> | ||
<BarPlot /> | ||
<ChartsXAxis position="bottom" axisId="x-axis" /> | ||
<ChartsYAxis position="left" /> | ||
</ChartsSurface> | ||
<MyCustomLegend /> | ||
</ChartDataProvider> |
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
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,23 @@ | ||
import * as React from 'react'; | ||
import ApiPage from 'docs/src/modules/components/ApiPage'; | ||
import mapApiPageTranslations from 'docs/src/modules/utils/mapApiPageTranslations'; | ||
import jsonPageContent from './chart-data-provider.json'; | ||
|
||
export default function Page(props) { | ||
const { descriptions, pageContent } = props; | ||
return <ApiPage descriptions={descriptions} pageContent={pageContent} />; | ||
} | ||
|
||
Page.getInitialProps = () => { | ||
const req = require.context( | ||
'docsx/translations/api-docs/charts/chart-data-provider', | ||
false, | ||
/\.\/chart-data-provider.*.json$/, | ||
); | ||
const descriptions = mapApiPageTranslations(req); | ||
|
||
return { | ||
descriptions, | ||
pageContent: jsonPageContent, | ||
}; | ||
}; |
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,70 @@ | ||
{ | ||
"props": { | ||
"series": { | ||
"type": { "name": "arrayOf", "description": "Array<object>" }, | ||
"required": true | ||
}, | ||
"colors": { | ||
"type": { "name": "union", "description": "Array<string><br>| func" }, | ||
"default": "blueberryTwilightPalette" | ||
}, | ||
"dataset": { "type": { "name": "arrayOf", "description": "Array<object>" } }, | ||
"height": { "type": { "name": "number" } }, | ||
"highlightedItem": { | ||
"type": { | ||
"name": "shape", | ||
"description": "{ dataIndex?: number, seriesId?: number<br>| string }" | ||
} | ||
}, | ||
"margin": { | ||
"type": { | ||
"name": "shape", | ||
"description": "{ bottom?: number, left?: number, right?: number, top?: number }" | ||
}, | ||
"default": "object Depends on the charts type." | ||
}, | ||
"onHighlightChange": { | ||
"type": { "name": "func" }, | ||
"signature": { | ||
"type": "function(highlightedItem: HighlightItemData | null) => void", | ||
"describedArgs": ["highlightedItem"] | ||
} | ||
}, | ||
"plugins": { "type": { "name": "arrayOf", "description": "Array<object>" } }, | ||
"skipAnimation": { "type": { "name": "bool" } }, | ||
"width": { "type": { "name": "number" } }, | ||
"xAxis": { | ||
"type": { | ||
"name": "arrayOf", | ||
"description": "Array<{ classes?: object, colorMap?: { colors: Array<string>, type: 'ordinal', unknownColor?: string, values?: Array<Date<br>| number<br>| string> }<br>| { color: Array<string><br>| func, max?: Date<br>| number, min?: Date<br>| number, type: 'continuous' }<br>| { colors: Array<string>, thresholds: Array<Date<br>| number>, type: 'piecewise' }, data?: array, dataKey?: string, disableLine?: bool, disableTicks?: bool, domainLimit?: 'nice'<br>| 'strict'<br>| func, fill?: string, hideTooltip?: bool, id?: number<br>| string, label?: string, labelFontSize?: number, labelStyle?: object, max?: Date<br>| number, min?: Date<br>| number, position?: 'bottom'<br>| 'top', reverse?: bool, scaleType?: 'band'<br>| 'linear'<br>| 'log'<br>| 'point'<br>| 'pow'<br>| 'sqrt'<br>| 'time'<br>| 'utc', slotProps?: object, slots?: object, stroke?: string, sx?: Array<func<br>| object<br>| bool><br>| func<br>| object, tickFontSize?: number, tickInterval?: 'auto'<br>| array<br>| func, tickLabelInterval?: 'auto'<br>| func, tickLabelPlacement?: 'middle'<br>| 'tick', tickLabelStyle?: object, tickMaxStep?: number, tickMinStep?: number, tickNumber?: number, tickPlacement?: 'end'<br>| 'extremities'<br>| 'middle'<br>| 'start', tickSize?: number, valueFormatter?: func }>" | ||
} | ||
}, | ||
"yAxis": { | ||
"type": { | ||
"name": "arrayOf", | ||
"description": "Array<{ classes?: object, colorMap?: { colors: Array<string>, type: 'ordinal', unknownColor?: string, values?: Array<Date<br>| number<br>| string> }<br>| { color: Array<string><br>| func, max?: Date<br>| number, min?: Date<br>| number, type: 'continuous' }<br>| { colors: Array<string>, thresholds: Array<Date<br>| number>, type: 'piecewise' }, data?: array, dataKey?: string, disableLine?: bool, disableTicks?: bool, domainLimit?: 'nice'<br>| 'strict'<br>| func, fill?: string, hideTooltip?: bool, id?: number<br>| string, label?: string, labelFontSize?: number, labelStyle?: object, max?: Date<br>| number, min?: Date<br>| number, position?: 'left'<br>| 'right', reverse?: bool, scaleType?: 'band'<br>| 'linear'<br>| 'log'<br>| 'point'<br>| 'pow'<br>| 'sqrt'<br>| 'time'<br>| 'utc', slotProps?: object, slots?: object, stroke?: string, sx?: Array<func<br>| object<br>| bool><br>| func<br>| object, tickFontSize?: number, tickInterval?: 'auto'<br>| array<br>| func, tickLabelInterval?: 'auto'<br>| func, tickLabelPlacement?: 'middle'<br>| 'tick', tickLabelStyle?: object, tickMaxStep?: number, tickMinStep?: number, tickNumber?: number, tickPlacement?: 'end'<br>| 'extremities'<br>| 'middle'<br>| 'start', tickSize?: number, valueFormatter?: func }>" | ||
} | ||
}, | ||
"zAxis": { | ||
"type": { | ||
"name": "arrayOf", | ||
"description": "Array<{ colorMap?: { colors: Array<string>, type: 'ordinal', unknownColor?: string, values?: Array<Date<br>| number<br>| string> }<br>| { color: Array<string><br>| func, max?: Date<br>| number, min?: Date<br>| number, type: 'continuous' }<br>| { colors: Array<string>, thresholds: Array<Date<br>| number>, type: 'piecewise' }, data?: array, dataKey?: string, id?: string, max?: number, min?: number }>" | ||
} | ||
} | ||
}, | ||
"name": "ChartDataProvider", | ||
"imports": [ | ||
"import { ChartDataProvider } from '@mui/x-charts/context';", | ||
"import { ChartDataProvider } from '@mui/x-charts';", | ||
"import { ChartDataProvider } from '@mui/x-charts-pro';" | ||
], | ||
"classes": [], | ||
"spread": false, | ||
"themeDefaultProps": false, | ||
"muiName": "MuiChartDataProvider", | ||
"forwardsRefTo": "SVGSVGElement", | ||
"filename": "/packages/x-charts/src/context/ChartDataProvider/ChartDataProvider.tsx", | ||
"inheritance": null, | ||
"demos": "<ul><li><a href=\"/x/react-charts/composition/\">Chart composition</a></li></ul>", | ||
"cssComponent": false | ||
} |
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
2 changes: 1 addition & 1 deletion
2
docs/translations/api-docs/charts/chart-container/chart-container.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
Oops, something went wrong.