-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
[charts] Allow setting a custom marker in legends and tooltips #16654
base: master
Are you sure you want to change the base?
Changes from 13 commits
91ef197
eb76428
c6681a2
b251241
f06319f
106132f
26995eb
89b53ad
d368e9f
8b22df6
f93758a
307ff39
48d4491
67e8283
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,38 @@ | ||
import { BarChart } from '@mui/x-charts/BarChart'; | ||
import * as React from 'react'; | ||
|
||
const seriesConfig = [ | ||
{ id: 0, data: [10], label: 'Series A' }, | ||
{ id: 1, data: [15], label: 'Series B' }, | ||
{ id: 2, data: [20], label: 'Series C' }, | ||
{ id: 3, data: [10], label: 'Series D' }, | ||
]; | ||
|
||
const cross = | ||
'M-5.35,-1.783L-1.783,-1.783L-1.783,-5.35L1.783,-5.35L1.783,-1.783L5.35,-1.783L5.35,1.783L1.783,1.783L1.783,5.35L-1.783,5.35L-1.783,1.783L-5.35,1.783Z'; | ||
const diamond = 'M0,-7.423L4.285,0L0,7.423L-4.285,0Z'; | ||
const star = | ||
'M0,-7.528L1.69,-2.326L7.16,-2.326L2.735,0.889L4.425,6.09L0,2.875L-4.425,6.09L-2.735,0.889L-7.16,-2.326L-1.69,-2.326Z'; | ||
const wye = | ||
'M2.152,1.243L2.152,5.547L-2.152,5.547L-2.152,1.243L-5.88,-0.91L-3.728,-4.638L0,-2.485L3.728,-4.638L5.88,-0.91Z'; | ||
const symbols = [cross, diamond, star, wye]; | ||
|
||
function CustomLabelMark({ className, seriesId, color }) { | ||
const symbol = symbols[seriesId]; | ||
return ( | ||
<svg className={className} viewBox="-8 -8 16 16" width={14} height={14}> | ||
<path d={symbol} fill={color} /> | ||
</svg> | ||
); | ||
} | ||
|
||
export default function LegendCustomLabelMark() { | ||
return ( | ||
<BarChart | ||
series={seriesConfig} | ||
xAxis={[{ scaleType: 'band', data: ['A'] }]} | ||
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. |
||
height={200} | ||
slots={{ labelMark: CustomLabelMark }} | ||
/> | ||
); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import { BarChart } from '@mui/x-charts/BarChart'; | ||
import * as React from 'react'; | ||
import { ChartsLabelMarkProps } from '@mui/x-charts/ChartsLabel'; | ||
|
||
const seriesConfig = [ | ||
{ id: 0, data: [10], label: 'Series A' }, | ||
{ id: 1, data: [15], label: 'Series B' }, | ||
{ id: 2, data: [20], label: 'Series C' }, | ||
{ id: 3, data: [10], label: 'Series D' }, | ||
]; | ||
|
||
const cross = | ||
'M-5.35,-1.783L-1.783,-1.783L-1.783,-5.35L1.783,-5.35L1.783,-1.783L5.35,-1.783L5.35,1.783L1.783,1.783L1.783,5.35L-1.783,5.35L-1.783,1.783L-5.35,1.783Z'; | ||
const diamond = 'M0,-7.423L4.285,0L0,7.423L-4.285,0Z'; | ||
const star = | ||
'M0,-7.528L1.69,-2.326L7.16,-2.326L2.735,0.889L4.425,6.09L0,2.875L-4.425,6.09L-2.735,0.889L-7.16,-2.326L-1.69,-2.326Z'; | ||
const wye = | ||
'M2.152,1.243L2.152,5.547L-2.152,5.547L-2.152,1.243L-5.88,-0.91L-3.728,-4.638L0,-2.485L3.728,-4.638L5.88,-0.91Z'; | ||
const symbols = [cross, diamond, star, wye]; | ||
|
||
function CustomLabelMark({ className, seriesId, color }: ChartsLabelMarkProps) { | ||
const symbol = symbols[seriesId as number]; | ||
return ( | ||
<svg className={className} viewBox="-8 -8 16 16" width={14} height={14}> | ||
<path d={symbol} fill={color} /> | ||
</svg> | ||
); | ||
} | ||
|
||
export default function LegendCustomLabelMark() { | ||
return ( | ||
<BarChart | ||
series={seriesConfig} | ||
xAxis={[{ scaleType: 'band', data: ['A'] }]} | ||
height={200} | ||
slots={{ labelMark: CustomLabelMark }} | ||
/> | ||
); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
<BarChart | ||
series={seriesConfig} | ||
xAxis={[{ scaleType: 'band', data: ['A'] }]} | ||
height={200} | ||
slots={{ labelMark: CustomLabelMark }} | ||
/> |
Original file line number | Diff line number | Diff line change | ||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -71,6 +71,16 @@ For the `pie` series, the `labelMarkType` property is available for each of the | |||||||||||||||||||
|
||||||||||||||||||||
{{"demo": "LegendMarkTypeNoSnap.js", "hideToolbar": true, "bg": "playground"}} | ||||||||||||||||||||
|
||||||||||||||||||||
For more advanced use cases, you can also provide a component to the `labelMark` slot to fully customize the mark. | ||||||||||||||||||||
|
||||||||||||||||||||
{{"demo": "LegendCustomLabelMark.js" }} | ||||||||||||||||||||
|
||||||||||||||||||||
Comment on lines
+74
to
+77
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. Maybe also adding a h4 title "Built-in shapes" in the previous section. It's mostly to simplify to visual processing of information. Titles are useful to split text and demos in coherent bloks. For example here without reading I can't guess to which demos the paragraph refers to
Suggested change
|
||||||||||||||||||||
Passing a component to `labelMark` or using `labelMarkType` affects not only the legend but other places where the label mark is shown, such as tooltips. | ||||||||||||||||||||
|
||||||||||||||||||||
When a `labelMark` slot is provided, the `labelMarkType` property is ignored. | ||||||||||||||||||||
|
||||||||||||||||||||
Customizing the mark shape of a pie chart depending on the series is slightly different. You can find how to do it in [this example](/x/react-charts/pie-demo/#pie-chart-with-custom-legend-and-tooltip). | ||||||||||||||||||||
|
||||||||||||||||||||
### Scrollable legend | ||||||||||||||||||||
|
||||||||||||||||||||
The legend can be made scrollable by setting the `overflowY` for vertical legends or `overflowX` for horizontal legends. | ||||||||||||||||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import * as React from 'react'; | ||
import { PieChart } from '@mui/x-charts/PieChart'; | ||
|
||
export default function PieChartWithCustomLegendTooltip() { | ||
return ( | ||
<PieChart | ||
series={[ | ||
{ | ||
data: [ | ||
{ id: 'cross', value: 10, label: 'Cross' }, | ||
{ id: 'diamond', value: 15, label: 'Diamond' }, | ||
{ id: 'star', value: 20, label: 'Star' }, | ||
], | ||
}, | ||
]} | ||
width={200} | ||
height={200} | ||
slots={{ labelMark: CustomMark }} | ||
/> | ||
); | ||
} | ||
|
||
const cross = | ||
'M-5.35,-1.783L-1.783,-1.783L-1.783,-5.35L1.783,-5.35L1.783,-1.783L5.35,-1.783L5.35,1.783L1.783,1.783L1.783,5.35L-1.783,5.35L-1.783,1.783L-5.35,1.783Z'; | ||
const diamond = 'M0,-7.423L4.285,0L0,7.423L-4.285,0Z'; | ||
const star = | ||
'M0,-7.528L1.69,-2.326L7.16,-2.326L2.735,0.889L4.425,6.09L0,2.875L-4.425,6.09L-2.735,0.889L-7.16,-2.326L-1.69,-2.326Z'; | ||
|
||
const symbols = [cross, diamond, star]; | ||
|
||
function CustomMark({ dataIndex, color, className }) { | ||
const data = symbols[dataIndex ?? 0]; | ||
|
||
return ( | ||
<svg className={className} width={14} height={14} viewBox="-8 -8 16 16"> | ||
<path d={data ?? symbols[0]} fill={color} /> | ||
</svg> | ||
); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import * as React from 'react'; | ||
import { PieChart } from '@mui/x-charts/PieChart'; | ||
import { ChartsLabelMarkProps } from '@mui/x-charts/ChartsLabel'; | ||
|
||
export default function PieChartWithCustomLegendTooltip() { | ||
return ( | ||
<PieChart | ||
series={[ | ||
{ | ||
data: [ | ||
{ id: 'cross', value: 10, label: 'Cross' }, | ||
{ id: 'diamond', value: 15, label: 'Diamond' }, | ||
{ id: 'star', value: 20, label: 'Star' }, | ||
], | ||
}, | ||
]} | ||
width={200} | ||
height={200} | ||
slots={{ labelMark: CustomMark }} | ||
/> | ||
); | ||
} | ||
|
||
const cross = | ||
'M-5.35,-1.783L-1.783,-1.783L-1.783,-5.35L1.783,-5.35L1.783,-1.783L5.35,-1.783L5.35,1.783L1.783,1.783L1.783,5.35L-1.783,5.35L-1.783,1.783L-5.35,1.783Z'; | ||
const diamond = 'M0,-7.423L4.285,0L0,7.423L-4.285,0Z'; | ||
const star = | ||
'M0,-7.528L1.69,-2.326L7.16,-2.326L2.735,0.889L4.425,6.09L0,2.875L-4.425,6.09L-2.735,0.889L-7.16,-2.326L-1.69,-2.326Z'; | ||
|
||
const symbols = [cross, diamond, star]; | ||
|
||
function CustomMark({ dataIndex, color, className }: ChartsLabelMarkProps) { | ||
const data = symbols[dataIndex ?? 0]; | ||
|
||
return ( | ||
<svg className={className} width={14} height={14} viewBox="-8 -8 16 16"> | ||
<path d={data ?? symbols[0]} fill={color} /> | ||
</svg> | ||
); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<PieChart | ||
series={[ | ||
{ | ||
data: [ | ||
{ id: 'cross', value: 10, label: 'Cross' }, | ||
{ id: 'diamond', value: 15, label: 'Diamond' }, | ||
{ id: 'star', value: 20, label: 'Star' }, | ||
], | ||
}, | ||
]} | ||
width={200} | ||
height={200} | ||
slots={{ labelMark: CustomMark }} | ||
/> |
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.