-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
[charts] Add RadarAxis
component to render labels
#19240
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
Changes from all commits
6ae7640
e6a36f3
1961268
1b231ff
e927f63
40d45a9
550b079
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
import * as React from 'react'; | ||
import Box from '@mui/material/Box'; | ||
import ChartsUsageDemo from 'docsx/src/modules/components/ChartsUsageDemo'; | ||
import { RadarChart, RadarAxis } from '@mui/x-charts/RadarChart'; | ||
|
||
function getTextAnchor(textAnchor) { | ||
switch (textAnchor) { | ||
case 'default': | ||
return undefined; | ||
case 'function': | ||
return (angle) => (angle < 180 ? 'start' : 'end'); | ||
default: | ||
return textAnchor; | ||
} | ||
} | ||
|
||
function getTextAnchorCode(textAnchor) { | ||
switch (textAnchor) { | ||
case 'default': | ||
return ''; | ||
case 'function': | ||
return `textAnchor={(angle) => angle < 180 ? 'start' : 'end'}`; | ||
default: | ||
return `textAnchor="${textAnchor}"`; | ||
} | ||
} | ||
|
||
export default function DemoRadarAxis() { | ||
return ( | ||
<ChartsUsageDemo | ||
componentName="RadarChart" | ||
data={{ | ||
angle: { | ||
knob: 'number', | ||
defaultValue: 30, | ||
min: -360, | ||
max: 360, | ||
step: 10, | ||
}, | ||
divisions: { | ||
knob: 'number', | ||
defaultValue: 4, | ||
min: 1, | ||
max: 5, | ||
}, | ||
labelOrientation: { | ||
knob: 'radio', | ||
options: ['horizontal', 'rotated'], | ||
defaultValue: 'horizontal', | ||
}, | ||
textAnchor: { | ||
knob: 'select', | ||
options: ['default', 'start', 'end', 'function'], | ||
defaultValue: 'default', | ||
}, | ||
}} | ||
renderDemo={(props) => ( | ||
<Box sx={{ width: '100%', maxWidth: 400 }}> | ||
<RadarChart | ||
height={250} | ||
margin={{ top: 20 }} | ||
series={[{ data: [100, 98, 86, 99, 85, 55] }]} | ||
divisions={4} | ||
radar={{ | ||
max: 100, | ||
metrics: [ | ||
'Math', | ||
'Chinese', | ||
'English', | ||
'Geography', | ||
'Physics', | ||
'History', | ||
], | ||
}} | ||
> | ||
<RadarAxis | ||
metric="Math" | ||
labelOrientation={props.labelOrientation} | ||
angle={props.angle} | ||
textAnchor={getTextAnchor(props.textAnchor)} | ||
divisions={props.divisions} | ||
/> | ||
</RadarChart> | ||
</Box> | ||
)} | ||
getCode={({ props }) => { | ||
const textAnchorCode = getTextAnchorCode(props.textAnchor); | ||
return [ | ||
`import { RadarChart, RadarAxis } from '@mui/x-charts/RadarChart';`, | ||
'', | ||
'<RadarChart>', | ||
` <RadarAxis`, | ||
` metric="Math"`, | ||
` divisions={${props.divisions}}`, | ||
` labelOrientation="${props.labelOrientation}"`, | ||
` angle="${props.angle}"`, | ||
...(textAnchorCode ? [` ${textAnchorCode}`] : []), | ||
' />', | ||
'</RadarChart>', | ||
].join('\n'); | ||
}} | ||
/> | ||
); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
import * as React from 'react'; | ||
import Box from '@mui/material/Box'; | ||
import ChartsUsageDemo from 'docsx/src/modules/components/ChartsUsageDemo'; | ||
import { RadarChart, RadarAxis } from '@mui/x-charts/RadarChart'; | ||
|
||
function getTextAnchor(textAnchor: 'default' | 'start' | 'end' | 'function') { | ||
switch (textAnchor) { | ||
case 'default': | ||
return undefined; | ||
case 'function': | ||
return (angle: number) => (angle < 180 ? 'start' : 'end'); | ||
default: | ||
return textAnchor; | ||
} | ||
} | ||
|
||
function getTextAnchorCode(textAnchor: 'default' | 'start' | 'end' | 'function') { | ||
switch (textAnchor) { | ||
case 'default': | ||
return ''; | ||
case 'function': | ||
return `textAnchor={(angle) => angle < 180 ? 'start' : 'end'}`; | ||
default: | ||
return `textAnchor="${textAnchor}"`; | ||
} | ||
} | ||
|
||
export default function DemoRadarAxis() { | ||
return ( | ||
<ChartsUsageDemo | ||
componentName="RadarChart" | ||
data={{ | ||
angle: { | ||
knob: 'number', | ||
defaultValue: 30, | ||
min: -360, | ||
max: 360, | ||
step: 10, | ||
}, | ||
divisions: { | ||
knob: 'number', | ||
defaultValue: 4, | ||
min: 1, | ||
max: 5, | ||
}, | ||
labelOrientation: { | ||
knob: 'radio', | ||
options: ['horizontal', 'rotated'] as const, | ||
defaultValue: 'horizontal', | ||
}, | ||
textAnchor: { | ||
knob: 'select', | ||
options: ['default', 'start', 'end', 'function'] as const, | ||
defaultValue: 'default', | ||
}, | ||
}} | ||
renderDemo={(props) => ( | ||
<Box sx={{ width: '100%', maxWidth: 400 }}> | ||
<RadarChart | ||
height={250} | ||
margin={{ top: 20 }} | ||
series={[{ data: [100, 98, 86, 99, 85, 55] }]} | ||
divisions={4} | ||
radar={{ | ||
max: 100, | ||
metrics: [ | ||
'Math', | ||
'Chinese', | ||
'English', | ||
'Geography', | ||
'Physics', | ||
'History', | ||
], | ||
}} | ||
> | ||
<RadarAxis | ||
metric="Math" | ||
labelOrientation={props.labelOrientation} | ||
angle={props.angle} | ||
textAnchor={getTextAnchor(props.textAnchor)} | ||
divisions={props.divisions} | ||
/> | ||
</RadarChart> | ||
</Box> | ||
)} | ||
getCode={({ props }) => { | ||
const textAnchorCode = getTextAnchorCode(props.textAnchor); | ||
return [ | ||
`import { RadarChart, RadarAxis } from '@mui/x-charts/RadarChart';`, | ||
'', | ||
'<RadarChart>', | ||
` <RadarAxis`, | ||
` metric="Math"`, | ||
` divisions={${props.divisions}}`, | ||
` labelOrientation="${props.labelOrientation}"`, | ||
` angle="${props.angle}"`, | ||
...(textAnchorCode ? [` ${textAnchorCode}`] : []), | ||
' />', | ||
'</RadarChart>', | ||
].join('\n'); | ||
}} | ||
/> | ||
); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
--- | ||
title: React Radar chart | ||
productId: x-charts | ||
components: RadarChart, RadarChartPro, RadarGrid, RadarSeriesArea, RadarSeriesMarks, RadarSeriesPlot, RadarMetricLabels, RadarAxisHighlight, ChartsWrapper | ||
components: RadarChart, RadarChartPro, RadarGrid, RadarSeriesArea, RadarSeriesMarks, RadarSeriesPlot, RadarMetricLabels, RadarAxisHighlight, RadarAxis, ChartsWrapper | ||
--- | ||
|
||
# Charts - Radar | ||
|
@@ -53,6 +53,18 @@ The radar chart displays a grid behind the series that can be configured with: | |
|
||
{{"demo": "DemoRadar.js" }} | ||
|
||
## Axis values | ||
|
||
You can add labels to metrics with the `<RadarAxis />`. | ||
This component requires a `metric` prop and can be configured with: | ||
|
||
- `angle` The angle used to display labels. By default it's the one associated to the given metric. | ||
- `labelOrientation` The orientation strategy. Either horizontal labels with moving anchor point, or label rotating with the axis. | ||
- `divisions` The number of labels to display. | ||
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. Why is this different than 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. I think most of the user will have it sync. But I do not enforce it such that user can have 4 divisions and 2 labels 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. My point was more to show them that you can have different values between them. I was confused at first when I saw that the axis had its own "divisions". It makes sense that they are separate, but the example should threat them as separate things so it is clear to user. |
||
- `textAnchor`/`dominantBaseline` The label placement. Can either be a string, or a function with the `angle` value (in degree) as an argument. | ||
|
||
{{"demo": "DemoRadarAxis.js" }} | ||
|
||
## Highlight | ||
|
||
### Axis highlight | ||
|
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 './radar-axis.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/radar-axis', | ||
false, | ||
/\.\/radar-axis.*.json$/, | ||
); | ||
const descriptions = mapApiPageTranslations(req); | ||
|
||
return { | ||
descriptions, | ||
pageContent: jsonPageContent, | ||
}; | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
{ | ||
"props": { | ||
"angle": { "type": { "name": "number" } }, | ||
"classes": { "type": { "name": "object" }, "additionalInfo": { "cssApi": true } }, | ||
"divisions": { "type": { "name": "number" }, "default": "1" }, | ||
"dominantBaseline": { | ||
"type": { "name": "union", "description": "func<br>| string" } | ||
}, | ||
"labelOrientation": { | ||
"type": { "name": "enum", "description": "'horizontal'<br>| 'rotated'" }, | ||
"default": "'horizontal'" | ||
}, | ||
"metric": { "type": { "name": "string" } }, | ||
"textAnchor": { "type": { "name": "union", "description": "func<br>| string" } } | ||
}, | ||
"name": "RadarAxis", | ||
"imports": [ | ||
"import { RadarAxis } from '@mui/x-charts/RadarChart';", | ||
"import { RadarAxis } from '@mui/x-charts';", | ||
"import { RadarAxis } from '@mui/x-charts-pro';" | ||
], | ||
"classes": [ | ||
{ | ||
"key": "label", | ||
"className": "MuiRadarAxis-label", | ||
"description": "Styles applied to every label element.", | ||
"isGlobal": false | ||
}, | ||
{ | ||
"key": "line", | ||
"className": "MuiRadarAxis-line", | ||
"description": "Styles applied to the line element.", | ||
"isGlobal": false | ||
}, | ||
{ | ||
"key": "root", | ||
"className": "MuiRadarAxis-root", | ||
"description": "Styles applied to the root element.", | ||
"isGlobal": false | ||
} | ||
], | ||
"muiName": "MuiRadarAxis", | ||
"filename": "/packages/x-charts/src/RadarChart/RadarAxis/RadarAxis.tsx", | ||
"inheritance": null, | ||
"demos": "<ul><li><a href=\"/x/react-charts/radar/\">Charts - Radar</a></li></ul>", | ||
"cssComponent": false | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
{ | ||
"componentDescription": "", | ||
"propDescriptions": { | ||
"angle": { | ||
"description": "The absolute rotation angle of the metrics (in degree) If not defined the metric angle will be used." | ||
}, | ||
"classes": { "description": "Override or extend the styles applied to the component." }, | ||
"divisions": { "description": "The number of divisions with label." }, | ||
"dominantBaseline": { | ||
"description": "The labels dominant baseline or a function returning the dominant baseline for a given axis angle (in degree)." | ||
}, | ||
"labelOrientation": { | ||
"description": "Defines how label align with the axis. - 'horizontal': labels stay horizontal and their placement change with the axis angle. - 'rotated': labels are rotated 90deg relatively to their axis." | ||
}, | ||
"metric": { | ||
"description": "The metric to get. If <code>undefined</code>, the hook returns <code>null</code>" | ||
}, | ||
"textAnchor": { | ||
"description": "The labels text anchor or a function returning the text anchor for a given axis angle (in degree)." | ||
} | ||
}, | ||
"classDescriptions": { | ||
"label": { | ||
"description": "Styles applied to {{nodeName}}.", | ||
"nodeName": "every label element" | ||
}, | ||
"line": { "description": "Styles applied to {{nodeName}}.", "nodeName": "the line element" }, | ||
"root": { "description": "Styles applied to the root element." } | ||
} | ||
} |
Uh oh!
There was an error while loading. Please reload this page.