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] Decouple margin and axis-size #16418

Merged
merged 75 commits into from
Feb 20, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
75 commits
Select commit Hold shift + click to select a range
644cd1f
general improvements
JCQuintas Feb 5, 2025
7ee2fe4
multiple axis + axis offset + axis size
JCQuintas Feb 5, 2025
977dd34
Merge branch 'master' into axis-size-poc
JCQuintas Feb 6, 2025
f48e90b
todo
JCQuintas Feb 6, 2025
caab7bb
calculate margins + axis size
JCQuintas Feb 6, 2025
1b961f7
defaultize margin fn
JCQuintas Feb 6, 2025
74f320f
remove "calculatelegendmargin" behavior
JCQuintas Feb 6, 2025
45d9e94
fix missing filechange
JCQuintas Feb 6, 2025
a73e755
try document
JCQuintas Feb 6, 2025
acc67bd
Merge commit 'd8f269155f3ca08f72cb3f40a142005db0e1f6e1' into axis-siz…
JCQuintas Feb 7, 2025
0747b9e
use params not state
JCQuintas Feb 7, 2025
ec16952
add comment
JCQuintas Feb 7, 2025
6424b51
types
JCQuintas Feb 7, 2025
5bd7329
Merge commit '798b73c92c614b042514050c85edbdfccfce0584' into axis-siz…
JCQuintas Feb 7, 2025
1cf7de6
Revert "[charts] Document plugins for internal use"
JCQuintas Feb 7, 2025
75916db
empty sides
JCQuintas Feb 7, 2025
f1eb5dd
render based on state rather than props
JCQuintas Feb 7, 2025
0daad2b
Merge commit '587f7c5757e0d66222ea1f3d37c923a0814e9d86' into axis-siz…
JCQuintas Feb 7, 2025
f1c99ba
Merge commit 'e71e83180666ae867c95b2dccad6653619aa7e2c' into axis-siz…
JCQuintas Feb 11, 2025
e21062d
fix cycle
JCQuintas Feb 11, 2025
ec5f0a7
Merge axisprops
JCQuintas Feb 11, 2025
50d753c
none
JCQuintas Feb 11, 2025
419a688
fix pie charts
JCQuintas Feb 11, 2025
cd1b875
react to svg width/height changes
JCQuintas Feb 11, 2025
fb5e934
[charts][docs] Remove or improve margins on examples
JCQuintas Feb 11, 2025
7b14f1f
Remove left/right/top/bottomAxis props
JCQuintas Feb 12, 2025
7ae9799
define offset on defaultize axis
JCQuintas Feb 12, 2025
3d228db
move params to axisconfig instead of axisprops
JCQuintas Feb 12, 2025
90cc7f1
move selectors
JCQuintas Feb 12, 2025
818a4ed
Merge commit '9b36487c9bf35b9166b7c83909164893cb92b4b4' into axis-siz…
JCQuintas Feb 12, 2025
5a51ef7
fix flicker
JCQuintas Feb 12, 2025
9e30455
add docs
JCQuintas Feb 13, 2025
e9863f5
Merge branch 'master' into axis-size-poc
JCQuintas Feb 13, 2025
e4beced
fix types
JCQuintas Feb 13, 2025
f3b4c70
fix types cycle
JCQuintas Feb 13, 2025
2b8a0f3
fix failing tests
JCQuintas Feb 13, 2025
d8617cf
Apply suggestions from code review
JCQuintas Feb 13, 2025
c76f6c2
Merge commit 'be0c6abf8d3a199ae3f1b81c275b56a7e31fe173' into axis-siz…
JCQuintas Feb 17, 2025
53bb88b
simplify chart dimension state
JCQuintas Feb 17, 2025
885d07c
use drawing area selector
JCQuintas Feb 17, 2025
70081a3
add axisSize selector
JCQuintas Feb 17, 2025
8e48af7
fix undefined height or width
JCQuintas Feb 17, 2025
ba70987
add comment to ignore type
JCQuintas Feb 17, 2025
e361689
fix tests
JCQuintas Feb 17, 2025
287f78b
set cartesian axis as optional
JCQuintas Feb 17, 2025
b65c152
slice selectors to get single values
alexfauquette Feb 18, 2025
549de97
fix docs demo
alexfauquette Feb 18, 2025
91a8a2a
continue fixing position props
alexfauquette Feb 18, 2025
e3ea4d7
proptypes
alexfauquette Feb 18, 2025
f5c0551
fix spark line
alexfauquette Feb 18, 2025
86e7f29
docs:api
alexfauquette Feb 18, 2025
7a96dcd
remove memo
JCQuintas Feb 18, 2025
142447d
remove extra reduce
JCQuintas Feb 18, 2025
d6f7dda
Merge branch 'master' into axis-size-poc
JCQuintas Feb 18, 2025
924f47b
fix js demos
JCQuintas Feb 18, 2025
bef8768
remove/fix margins in demos
JCQuintas Feb 18, 2025
c7a3a88
fix types in js files
JCQuintas Feb 18, 2025
f069df6
Merge commit 'c66eb26d20e8b79dcb9ea813c61e3b585cc4f1dd' into axis-siz…
JCQuintas Feb 18, 2025
4a2ffb2
fix readonly
JCQuintas Feb 18, 2025
5ccc70e
add migration info
JCQuintas Feb 18, 2025
b92566e
Merge branch 'master' into axis-size-poc
JCQuintas Feb 18, 2025
48fa5f2
fix spakline axis size being counted
JCQuintas Feb 18, 2025
c3dd3ca
Merge commit 'd7984447f4c7a095bd03d2ac8c4cb61e7065b1a6' into axis-siz…
JCQuintas Feb 18, 2025
e24a2c1
fix missing theme
JCQuintas Feb 18, 2025
2560b7a
make default height/width different
JCQuintas Feb 18, 2025
d2f3634
Fix missed positions
JCQuintas Feb 18, 2025
d21e7b9
remove position= reference
JCQuintas Feb 18, 2025
0c167e3
typos
JCQuintas Feb 18, 2025
d720a79
Merge branch 'master' into axis-size-poc
JCQuintas Feb 18, 2025
efbe589
update more margins
JCQuintas Feb 18, 2025
1c9a8ca
force sparkline
JCQuintas Feb 18, 2025
d92f6d3
Merge commit 'dbc7561dfd33656bfd50d3bc3c22e5c5f34da1df' into axis-siz…
JCQuintas Feb 19, 2025
a3d1e5c
code review
JCQuintas Feb 19, 2025
e6f8f58
Merge branch 'master' into axis-size-poc
JCQuintas Feb 19, 2025
acbc0eb
Merge branch 'master' into axis-size-poc
JCQuintas Feb 19, 2025
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
2 changes: 0 additions & 2 deletions docs/data/charts/areas-demo/AreaChartConnectNulls.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,11 @@ export default function AreaChartConnectNulls() {
xAxis={[{ data: xData, scaleType: 'point' }]}
series={[{ data, showMark: false, area: true }]}
height={200}
margin={{ top: 10, bottom: 20 }}
/>
<LineChart
xAxis={[{ data: xData, scaleType: 'point' }]}
series={[{ data, showMark: false, area: true, connectNulls: true }]}
height={200}
margin={{ top: 10, bottom: 20 }}
/>
</Stack>
);
Expand Down
2 changes: 0 additions & 2 deletions docs/data/charts/areas-demo/AreaChartConnectNulls.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,11 @@ export default function AreaChartConnectNulls() {
xAxis={[{ data: xData, scaleType: 'point' }]}
series={[{ data, showMark: false, area: true }]}
height={200}
margin={{ top: 10, bottom: 20 }}
/>
<LineChart
xAxis={[{ data: xData, scaleType: 'point' }]}
series={[{ data, showMark: false, area: true, connectNulls: true }]}
height={200}
margin={{ top: 10, bottom: 20 }}
/>
</Stack>
);
Expand Down
2 changes: 0 additions & 2 deletions docs/data/charts/areas-demo/AreaChartConnectNulls.tsx.preview
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,9 @@
xAxis={[{ data: xData, scaleType: 'point' }]}
series={[{ data, showMark: false, area: true }]}
height={200}
margin={{ top: 10, bottom: 20 }}
/>
<LineChart
xAxis={[{ data: xData, scaleType: 'point' }]}
series={[{ data, showMark: false, area: true, connectNulls: true }]}
height={200}
margin={{ top: 10, bottom: 20 }}
/>
26 changes: 13 additions & 13 deletions docs/data/charts/areas-demo/AreaChartFillByValue.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ import { LineChart, areaElementClasses } from '@mui/x-charts/LineChart';
const data = [4000, 3000, -1000, 500, -2100, -250, 3490];
const xData = ['Page A', 'Page B', 'Page C', 'Page D', 'Page E', 'Page F', 'Page G'];

function ColorSwich({ threshold, color1, color2, id }) {
function ColorSwitch({ threshold, color1, color2, id }) {
const { top, height, bottom } = useDrawingArea();
const svgHeight = top + bottom + height;

const scale = useYScale(); // You can provide the axis Id if you have multiple ones
const y0 = scale(threshold); // The coordinate of of the origine
const y0 = scale(threshold); // The coordinate of the origin
const off = y0 !== undefined ? y0 / svgHeight : 0;

return (
Expand All @@ -38,41 +38,41 @@ export default function AreaChartFillByValue() {
<Stack direction="column" width="100%" spacing={1}>
<LineChart
xAxis={[{ data: xData, scaleType: 'point' }]}
yAxis={[{ min: -3000, max: 4000 }]}
yAxis={[{ min: -3000, max: 4000, width: 40 }]}
series={[{ data, showMark: false, area: true }]}
height={200}
margin={{ top: 20, bottom: 30, left: 75 }}
margin={{ bottom: 0 }}
sx={{
[`& .${areaElementClasses.root}`]: {
fill: 'url(#swich-color-id-1)',
fill: 'url(#switch-color-id-1)',
filter: 'none', // Remove the default filter.
},
}}
>
<ColorSwich
<ColorSwitch
color1="#11B678" // green
color2="#FF3143" // red
threshold={0}
id="swich-color-id-1"
id="switch-color-id-1"
/>
<rect x={0} y={0} width={5} height="100%" fill="url(#swich-color-id-1)" />
<rect x={0} y={0} width={5} height="100%" fill="url(#switch-color-id-1)" />
</LineChart>

<LineChart
xAxis={[{ data: xData, scaleType: 'point' }]}
yAxis={[{ min: -3000, max: 4000 }]}
yAxis={[{ min: -3000, max: 4000, width: 40 }]}
series={[{ data, showMark: false, area: true }]}
height={200}
margin={{ top: 20, bottom: 30, left: 75 }}
margin={{ bottom: 0 }}
sx={{
[`& .${areaElementClasses.root}`]: {
fill: 'url(#swich-color-id-2)',
fill: 'url(#switch-color-id-2)',
filter: 'none', // Remove the default filter.
},
}}
>
<ColorPalette id="swich-color-id-2" />
<rect x={0} y={0} width={5} height="100%" fill="url(#swich-color-id-2)" />
<ColorPalette id="switch-color-id-2" />
<rect x={0} y={0} width={5} height="100%" fill="url(#switch-color-id-2)" />
</LineChart>
</Stack>
);
Expand Down
28 changes: 14 additions & 14 deletions docs/data/charts/areas-demo/AreaChartFillByValue.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,19 @@ import { LineChart, areaElementClasses } from '@mui/x-charts/LineChart';
const data = [4000, 3000, -1000, 500, -2100, -250, 3490];
const xData = ['Page A', 'Page B', 'Page C', 'Page D', 'Page E', 'Page F', 'Page G'];

type ColorSwichProps = {
type ColorSwitchProps = {
threshold: number;
color1: string;
color2: string;
id: string;
};

function ColorSwich({ threshold, color1, color2, id }: ColorSwichProps) {
function ColorSwitch({ threshold, color1, color2, id }: ColorSwitchProps) {
const { top, height, bottom } = useDrawingArea();
const svgHeight = top + bottom + height;

const scale = useYScale() as ScaleLinear<number, number>; // You can provide the axis Id if you have multiple ones
const y0 = scale(threshold); // The coordinate of of the origine
const y0 = scale(threshold); // The coordinate of the origin
const off = y0 !== undefined ? y0 / svgHeight : 0;

return (
Expand All @@ -45,42 +45,42 @@ export default function AreaChartFillByValue() {
<Stack direction="column" width="100%" spacing={1}>
<LineChart
xAxis={[{ data: xData, scaleType: 'point' }]}
yAxis={[{ min: -3000, max: 4000 }]}
yAxis={[{ min: -3000, max: 4000, width: 40 }]}
series={[{ data, showMark: false, area: true }]}
height={200}
margin={{ top: 20, bottom: 30, left: 75 }}
margin={{ bottom: 0 }}
sx={{
[`& .${areaElementClasses.root}`]: {
fill: 'url(#swich-color-id-1)',
fill: 'url(#switch-color-id-1)',
filter: 'none', // Remove the default filter.
},
}}
>
<ColorSwich
<ColorSwitch
color1="#11B678" // green
color2="#FF3143" // red
threshold={0}
id="swich-color-id-1"
id="switch-color-id-1"
/>
<rect x={0} y={0} width={5} height="100%" fill="url(#swich-color-id-1)" />
<rect x={0} y={0} width={5} height="100%" fill="url(#switch-color-id-1)" />
</LineChart>

<LineChart
xAxis={[{ data: xData, scaleType: 'point' }]}
yAxis={[{ min: -3000, max: 4000 }]}
yAxis={[{ min: -3000, max: 4000, width: 40 }]}
series={[{ data, showMark: false, area: true }]}
height={200}
margin={{ top: 20, bottom: 30, left: 75 }}
margin={{ bottom: 0 }}
sx={{
[`& .${areaElementClasses.root}`]: {
fill: 'url(#swich-color-id-2)',
fill: 'url(#switch-color-id-2)',
filter: 'none', // Remove the default filter.
},
}}
>
<ColorPalette id="swich-color-id-2" />
<ColorPalette id="switch-color-id-2" />

<rect x={0} y={0} width={5} height="100%" fill="url(#swich-color-id-2)" />
<rect x={0} y={0} width={5} height="100%" fill="url(#switch-color-id-2)" />
</LineChart>
</Stack>
);
Expand Down
53 changes: 27 additions & 26 deletions docs/data/charts/axis/AxisCustomizationNoSnap.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// @ts-check
import * as React from 'react';
import Box from '@mui/material/Box';
import ChartsUsageDemo from 'docsx/src/modules/components/ChartsUsageDemo';
Expand Down Expand Up @@ -28,7 +29,10 @@ export default function AxisCustomizationNoSnap() {
{ propName: 'label', knob: 'input', defaultValue: 'my axis' },
{ propName: 'tickSize', knob: 'number', defaultValue: 6 },
]}
renderDemo={(props) => (
renderDemo={(
/** @type {{ disableLine: boolean; disableTicks: boolean; label: string; tickSize: number; }} */
props,
) => (
<Box sx={{ width: '100%', maxWidth: 400 }}>
<ScatterChart
series={[
Expand All @@ -38,35 +42,32 @@ export default function AxisCustomizationNoSnap() {
data,
},
]}
leftAxis={null}
bottomAxis={{
...defaultXAxis,
...props,
}}
yAxis={[{ position: 'none' }]}
xAxis={[
{
...defaultXAxis,
...props,
},
]}
height={300}
margin={{ top: 10, left: 20, right: 20 }}
/>
</Box>
)}
getCode={({ props }) =>
[
`import { ScatterChart } from '@mui/x-charts/ScatterChart';`,
'',
`<ScatterChart`,
' {/** ... */}',
` bottomAxis={{`,
...Object.keys(props)
.filter((prop) => props[prop] !== defaultXAxis[prop])
.map(
(prop) =>
` ${prop}: ${
typeof props[prop] === 'string' ? `"${props[prop]}"` : props[prop]
},`,
),
' }}',
'/>',
].join('\n')
}
getCode={(
/** @type {{props: { disableLine: boolean; disableTicks: boolean; label: string; tickSize: number; }}} */
{ props },
) => `import { ScatterChart } from '@mui/x-charts/ScatterChart';

<ScatterChart
// ...
xAxis={{
disableLine: ${props.disableLine},
disableTicks: ${props.disableTicks},
label: "${props.label}",
tickSize: ${props.tickSize},
}}
/>
`}
/>
);
}
55 changes: 28 additions & 27 deletions docs/data/charts/axis/AxisTextCustomizationNoSnap.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// @ts-check
import * as React from 'react';
import Box from '@mui/material/Box';
import ChartsUsageDemo from 'docsx/src/modules/components/ChartsUsageDemo';
Expand All @@ -23,7 +24,10 @@ export default function AxisTextCustomizationNoSnap() {
{ propName: 'fontSize', knob: 'number', defaultValue: 12 },
{ propName: 'labelFontSize', knob: 'number', defaultValue: 14 },
]}
renderDemo={(props) => (
renderDemo={(
/** @type {{ labelFontSize: number; angle: number; textAnchor: 'start'| 'middle'| 'end'; fontSize: number; }} */
props,
) => (
<Box sx={{ width: '100%', maxWidth: 400 }}>
<BarChart
dataset={dataset}
Expand All @@ -32,11 +36,10 @@ export default function AxisTextCustomizationNoSnap() {
scaleType: 'band',
dataKey: 'month',
label: 'months',
height: 40,
labelStyle: {
fontSize: props.labelFontSize,
transform: `translateY(${
5 * Math.abs(Math.sin((Math.PI * props.angle) / 180))
}px)`,
transform: `translateY(10px)`,
},
tickLabelStyle: {
angle: props.angle,
Expand All @@ -51,33 +54,31 @@ export default function AxisTextCustomizationNoSnap() {
{ dataKey: 'newYork', label: 'New York', valueFormatter },
{ dataKey: 'seoul', label: 'Seoul', valueFormatter },
]}
margin={{ bottom: 30 }}
{...chartSetting}
/>
</Box>
)}
getCode={({ props }) =>
[
`import { ScatterChart } from '@mui/x-charts/ScatterChart';`,
'',
`<ScatterChart`,
' {/** ... */}',
` bottomAxis={{`,
` labelStyle: {`,
` fontSize: ${props.labelFontSize},`,
` transform: \`translateY(\${
// Hack that should be added in the lib latter.
5 * Math.abs(Math.sin((Math.PI * props.angle) / 180))
}px)\``,
` },`,
` tickLabelStyle: {`,
` angle: ${props.angle},`,
` textAnchor: '${props.textAnchor}',`,
` fontSize: ${props.fontSize},`,
` },`,
' }}',
'/>',
].join('\n')
}
getCode={(
/** @type {{props: { labelFontSize: number; angle: number; textAnchor: 'start'| 'middle'| 'end'; fontSize: number; }}} */
{ props },
) => `import { BarChart } from '@mui/x-charts/BarChart';

<ScatterChart
// ...
xAxis={[
{
labelStyle: {
fontSize: ${props.labelFontSize},
},
tickLabelStyle: {
angle: ${props.angle},
textAnchor: '${props.textAnchor}',
fontSize: ${props.fontSize},
},
},
]}
/>`}
/>
);
}
11 changes: 7 additions & 4 deletions docs/data/charts/axis/AxisWithComposition.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@ export default function AxisWithComposition() {
label: 'Quarters',
},
]}
yAxis={[{ id: 'money' }, { id: 'quantities' }]}
yAxis={[
{ id: 'money', position: 'right', width: 40 },
{ id: 'quantities', position: 'left', width: 40 },
]}
series={[
{
type: 'line',
Expand All @@ -41,13 +44,13 @@ export default function AxisWithComposition() {
},
]}
height={400}
margin={{ left: 70, right: 70 }}
margin={{ left: 25, right: 25 }}
sx={{
[`.${axisClasses.left} .${axisClasses.label}`]: {
transform: 'translate(-25px, 0)',
},
[`.${axisClasses.right} .${axisClasses.label}`]: {
transform: 'translate(30px, 0)',
transform: 'translate(25px, 0)',
},
}}
>
Expand All @@ -59,7 +62,7 @@ export default function AxisWithComposition() {
labelStyle={{ fontSize: 18 }}
/>
<ChartsYAxis axisId="quantities" label="# units sold" />
<ChartsYAxis axisId="money" position="right" label="revenue" />
<ChartsYAxis axisId="money" label="revenue" />
</ChartContainer>
</Box>
);
Expand Down
Loading