Skip to content
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] Create color legend #13700

Merged
merged 13 commits into from
Jul 16, 2024
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions docs/data/charts-component-api-pages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,10 @@ const apiPages: MuiPage[] = [
pathname: '/x/api/charts/charts-y-axis',
title: 'ChartsYAxis',
},
{
pathname: '/x/api/charts/continuous-color-legend',
title: 'ContinuousColorLegend',
},
{
pathname: '/x/api/charts/default-charts-axis-tooltip-content',
title: 'DefaultChartsAxisTooltipContent',
Expand Down Expand Up @@ -161,6 +165,10 @@ const apiPages: MuiPage[] = [
pathname: '/x/api/charts/pie-arc-plot',
title: 'PieArcPlot',
},
{
pathname: '/x/api/charts/piecewise-color-legend',
title: 'PiecewiseColorLegend',
},
{
pathname: '/x/api/charts/pie-chart',
title: 'PieChart',
Expand Down
51 changes: 51 additions & 0 deletions docs/data/charts/legend/BasicColorLegend.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import * as React from 'react';
import Typography from '@mui/material/Typography';
import { LineChart } from '@mui/x-charts/LineChart';
import { ChartsReferenceLine } from '@mui/x-charts/ChartsReferenceLine';
import { PiecewiseColorLegend } from '@mui/x-charts/ChartsLegend';
import { dataset } from './tempAnomaly';

export default function BasicColorLegend() {
return (
<div style={{ width: '100%' }}>
<Typography variant="body1">
Global temperature anomaly relative to 1961-1990 average
</Typography>
<LineChart
dataset={dataset}
series={[
{
label: 'Global temperature anomaly relative to 1961-1990',
dataKey: 'anomaly',
showMark: false,
valueFormatter: (value) => `${value?.toFixed(2)}°`,
},
]}
xAxis={[
{
scaleType: 'time',
dataKey: 'year',
disableLine: true,
valueFormatter: (value) => value.getFullYear().toString(),
colorMap: {
type: 'piecewise',
thresholds: [new Date(1961, 0, 1), new Date(1990, 0, 1)],
colors: ['blue', 'gray', 'red'],
},
},
]}
yAxis={[{ disableLine: true, valueFormatter: (value) => `${value}°` }]}
height={300}
margin={{ top: 30, right: 150 }}
slotProps={{ legend: { hidden: true } }}
>
<PiecewiseColorLegend
axisDirection="x"
position={{ vertical: 'top', horizontal: 'right' }}
direction="column"
/>
<ChartsReferenceLine y={0} />
</LineChart>
</div>
);
}
51 changes: 51 additions & 0 deletions docs/data/charts/legend/BasicColorLegend.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import * as React from 'react';
import Typography from '@mui/material/Typography';
import { LineChart } from '@mui/x-charts/LineChart';
import { ChartsReferenceLine } from '@mui/x-charts/ChartsReferenceLine';
import { PiecewiseColorLegend } from '@mui/x-charts/ChartsLegend';
import { dataset } from './tempAnomaly';

export default function BasicColorLegend() {
return (
<div style={{ width: '100%' }}>
<Typography variant="body1">
Global temperature anomaly relative to 1961-1990 average
</Typography>
<LineChart
dataset={dataset}
series={[
{
label: 'Global temperature anomaly relative to 1961-1990',
dataKey: 'anomaly',
showMark: false,
valueFormatter: (value) => `${value?.toFixed(2)}°`,
},
]}
xAxis={[
{
scaleType: 'time',
dataKey: 'year',
disableLine: true,
valueFormatter: (value) => value.getFullYear().toString(),
colorMap: {
type: 'piecewise',
thresholds: [new Date(1961, 0, 1), new Date(1990, 0, 1)],
colors: ['blue', 'gray', 'red'],
},
},
]}
yAxis={[{ disableLine: true, valueFormatter: (value) => `${value}°` }]}
JCQuintas marked this conversation as resolved.
Show resolved Hide resolved
height={300}
margin={{ top: 30, right: 150 }}
slotProps={{ legend: { hidden: true } }}
>
<PiecewiseColorLegend
axisDirection="x"
position={{ vertical: 'top', horizontal: 'right' }}
direction="column"
/>
<ChartsReferenceLine y={0} />
</LineChart>
</div>
);
}
131 changes: 131 additions & 0 deletions docs/data/charts/legend/ContinuousInteractiveDemoNoSnap.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
import * as React from 'react';
import ChartsUsageDemo from 'docsx/src/modules/components/ChartsUsageDemo';
import { interpolateRdYlBu } from 'd3-scale-chromatic';
import { LineChart } from '@mui/x-charts/LineChart';
import { ContinuousColorLegend } from '@mui/x-charts/ChartsLegend';
import { dataset } from './tempAnomaly';

export default function ContinuousInteractiveDemoNoSnap() {
return (
<ChartsUsageDemo
componentName="Legend"
data={[
{
propName: 'direction',
knob: 'select',
defaultValue: 'row',
options: ['row', 'column'],
},
{
propName: 'length',
knob: 'number',
defaultValue: 50,
min: 10,
max: 80,
},
{
propName: 'thickness',
knob: 'number',
defaultValue: 5,
min: 1,
max: 20,
},
{
propName: 'align',
knob: 'select',
defaultValue: 'middle',
options: ['start', 'middle', 'end'],
},
{
propName: 'fontSize',
knob: 'number',
defaultValue: 10,
min: 8,
max: 20,
},
]}
renderDemo={(props) => (
<div style={{ width: '100%' }}>
<LineChart
dataset={dataset}
series={[
{
label: 'Global temperature anomaly relative to 1961-1990',
dataKey: 'anomaly',
showMark: false,
valueFormatter: (value) => `${value?.toFixed(2)}°`,
},
]}
xAxis={[
{
scaleType: 'time',
dataKey: 'year',
disableLine: true,
valueFormatter: (value) => value.getFullYear().toString(),
},
]}
yAxis={[
{
disableLine: true,
valueFormatter: (value) => `${value}°`,
colorMap: {
type: 'continuous',
min: -0.5,
max: 1.5,
color: (t) => interpolateRdYlBu(1 - t),
},
},
]}
height={300}
margin={{
top: props.direction === 'row' ? 50 : 20,
right: props.direction === 'row' ? 20 : 50,
}}
slotProps={{ legend: { hidden: true } }}
>
<ContinuousColorLegend
axisDirection="y"
position={
props.direction === 'row'
? { vertical: 'top', horizontal: 'middle' }
: { vertical: 'middle', horizontal: 'right' }
}
direction={props.direction}
length={`${props.length}%`}
thickness={props.thickness}
align={props.align}
labelStyle={{ fontSize: props.fontSize }}
/>
</LineChart>
</div>
)}
getCode={({ props }) => {
return [
`import { LineChart } from '@mui/x-charts/LineChart';`,
`import { ContinuousColorLegend } from '@mui/x-charts/ChartsLegend';`,
'',
`<LineChart`,
` margin={{ top: ${props.direction === 'row' ? 50 : 20}, right: ${
props.direction === 'row' ? 20 : 50
} }}`,
' {/** ... */}',
'>',
` <ContinuousColorLegend`,
` axisDirection="x"`,
` position={${
props.direction === 'row'
? `{ vertical: 'top', horizontal: 'middle' }`
: `{ vertical: 'middle', horizontal: 'right' }`
}}`,
` direction="${props.direction}"`,
` length="${props.length}%"`,
` thickness={${props.thickness}}`,
` align="${props.align}"`,
` labelStyle={{ fontSize: ${props.fontSize} }}`,
` />`,
'</LineChart>',
].join('\n');
}}
/>
);
}
111 changes: 111 additions & 0 deletions docs/data/charts/legend/PiecewiseInteractiveDemoNoSnap.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
import * as React from 'react';
import ChartsUsageDemo from 'docsx/src/modules/components/ChartsUsageDemo';
import { LineChart } from '@mui/x-charts/LineChart';
import { PiecewiseColorLegend } from '@mui/x-charts/ChartsLegend';
import { dataset } from './tempAnomaly';

export default function PiecewiseInteractiveDemoNoSnap() {
return (
<ChartsUsageDemo
componentName="Legend"
data={[
{
propName: 'hideFirst',
knob: 'switch',
},
{
propName: 'direction',
knob: 'select',
defaultValue: 'row',
options: ['row', 'column'],
},
{
propName: 'padding',
knob: 'number',
defaultValue: 0,
},
{
propName: 'fontSize',
knob: 'number',
defaultValue: 10,
min: 8,
max: 20,
},
]}
renderDemo={(props) => (
<div style={{ width: '100%' }}>
<LineChart
dataset={dataset}
series={[
{
label: 'Global temperature anomaly relative to 1961-1990',
dataKey: 'anomaly',
showMark: false,
valueFormatter: (value) => `${value?.toFixed(2)}°`,
},
]}
xAxis={[
{
scaleType: 'time',
dataKey: 'year',
disableLine: true,
valueFormatter: (value) => value.getFullYear().toString(),
colorMap: {
type: 'piecewise',
thresholds: [new Date(1961, 0, 1), new Date(1990, 0, 1)],
colors: ['blue', 'gray', 'red'],
},
},
]}
yAxis={[{ disableLine: true, valueFormatter: (value) => `${value}°` }]}
height={300}
margin={{
top: props.direction === 'row' ? 50 : 20,
right: props.direction === 'row' ? 20 : 150,
}}
slotProps={{ legend: { hidden: true } }}
>
<PiecewiseColorLegend
axisDirection="x"
position={
props.direction === 'row'
? { vertical: 'top', horizontal: 'middle' }
: { vertical: 'middle', horizontal: 'right' }
}
direction={props.direction}
padding={props.padding}
labelStyle={{ fontSize: props.fontSize }}
hideFirst={props.hideFirst}
/>
</LineChart>
</div>
)}
getCode={({ props }) => {
return [
`import { LineChart } from '@mui/x-charts/LineChart';`,
`import { PiecewiseColorLegend } from '@mui/x-charts/ChartsLegend';`,
'',
`<LineChart`,
` margin={{ top: ${props.direction === 'row' ? 50 : 20}, right: ${
props.direction === 'row' ? 20 : 150
} }}`,
' {/** ... */}',
'>',
` <PiecewiseColorLegend`,
` axisDirection="x"`,
` position={${
props.direction === 'row'
? `{ vertical: 'top', horizontal: 'middle' }`
: `{ vertical: 'middle', horizontal: 'right' }`
}}`,
` direction="${props.direction}"`,
...(props.hideFirst ? [' hideFirst'] : []),
` padding={${props.padding}}`,
` labelStyle={{ fontSize: ${props.fontSize} }}`,
` />`,
'</LineChart>',
].join('\n');
}}
/>
);
}
Loading