Skip to content

Commit

Permalink
[charts] Remove colors prop from SparkLineChart. (#16494)
Browse files Browse the repository at this point in the history
Co-authored-by: alex <[email protected]>
  • Loading branch information
bernardobelchior and alexfauquette authored Feb 11, 2025
1 parent 50933fe commit e71e831
Show file tree
Hide file tree
Showing 11 changed files with 211 additions and 27 deletions.
4 changes: 2 additions & 2 deletions docs/data/charts/sparkline/CustomYAxis.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export default function CustomYAxis() {
<Typography>Without fixed y-range</Typography>
<Stack sx={{ width: '100%', mb: 2 }} direction="row" spacing={2}>
<Box sx={{ flexGrow: 1 }}>
<SparkLineChart data={smallValues} colors={['red']} {...settings} />
<SparkLineChart data={smallValues} color="red" {...settings} />
</Box>
<Box sx={{ flexGrow: 1 }}>
<SparkLineChart data={largeValues} {...settings} />
Expand All @@ -34,7 +34,7 @@ export default function CustomYAxis() {
min: 0,
max: 100,
}}
colors={['red']}
color="red"
{...settings}
/>
</Box>
Expand Down
4 changes: 2 additions & 2 deletions docs/data/charts/sparkline/CustomYAxis.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export default function CustomYAxis() {
<Typography>Without fixed y-range</Typography>
<Stack sx={{ width: '100%', mb: 2 }} direction="row" spacing={2}>
<Box sx={{ flexGrow: 1 }}>
<SparkLineChart data={smallValues} colors={['red']} {...settings} />
<SparkLineChart data={smallValues} color="red" {...settings} />
</Box>
<Box sx={{ flexGrow: 1 }}>
<SparkLineChart data={largeValues} {...settings} />
Expand All @@ -34,7 +34,7 @@ export default function CustomYAxis() {
min: 0,
max: 100,
}}
colors={['red']}
color="red"
{...settings}
/>
</Box>
Expand Down
19 changes: 19 additions & 0 deletions docs/data/migration/migration-charts-v7/migration-charts-v7.md
Original file line number Diff line number Diff line change
Expand Up @@ -265,3 +265,22 @@ The `useSeries` hook family has been stabilized and renamed accordingly.
+ useHeatmapSeries,
} from '@mui/x-charts-pro/hooks';
```

## Rename `colors` prop in `SparkLineChart`

The `colors` prop in `SparkLineChart` has been renamed to `color`. It now accepts a single color or a function that returns a color.

```diff
<SparkLineChart
- colors={['#000', '#fff']}
+ color="#000"
/>
```

We provide a codemod to fix simple cases of this change, which you can run as follows:

```bash
npx @mui/x-codemod@latest v8.0.0/charts/rename-sparkline-colors-to-color <path>
```

For more complex cases, you may need to adjust the code manually. To aid you in finding these cases, the codemod adds a comment prefixed by `mui-x-codemod`, which you can search for in your codebase.
6 changes: 0 additions & 6 deletions docs/pages/x/api/charts/spark-line-chart.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,6 @@
"type": { "name": "union", "description": "func<br>&#124;&nbsp;string" },
"default": "rainbowSurgePalette[0]"
},
"colors": {
"type": { "name": "union", "description": "Array&lt;string&gt;<br>&#124;&nbsp;func" },
"default": "rainbowSurgePalette",
"deprecated": true,
"deprecationInfo": "use the <code>color</code> prop instead"
},
"dataset": { "type": { "name": "arrayOf", "description": "Array&lt;object&gt;" } },
"disableAxisListener": { "type": { "name": "bool" }, "default": "false" },
"disableVoronoi": { "type": { "name": "bool" } },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
"description": "Set to <code>true</code> to fill spark line area. Has no effect if plotType=&#39;bar&#39;."
},
"color": { "description": "Color used to colorize the sparkline." },
"colors": { "description": "Color palette used to colorize multiple series." },
"data": { "description": "Data to plot." },
"dataset": {
"description": "An array of objects that can be used to populate series and axes data using their <code>dataKey</code> property."
Expand Down
21 changes: 5 additions & 16 deletions packages/x-charts/src/SparkLineChart/SparkLineChart.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,10 @@ export interface SparkLineChartSlotProps
ChartsTooltipSlotProps {}

export interface SparkLineChartProps
extends Omit<ChartContainerProps, 'series' | 'xAxis' | 'yAxis' | 'zAxis' | 'margin' | 'plugins'> {
extends Omit<
ChartContainerProps,
'series' | 'xAxis' | 'yAxis' | 'zAxis' | 'margin' | 'plugins' | 'colors'
> {
/**
* The xAxis configuration.
* Notice it is a single [[AxisConfig]] object, not an array of configuration.
Expand Down Expand Up @@ -108,13 +111,6 @@ export interface SparkLineChartProps
*/
slotProps?: SparkLineChartSlotProps;

/**
* Color palette used to colorize multiple series.
* @default rainbowSurgePalette
* @deprecated use the `color` prop instead
*/
colors?: ChartContainerProps['colors'];

/**
* Color used to colorize the sparkline.
* @default rainbowSurgePalette[0]
Expand Down Expand Up @@ -149,7 +145,6 @@ const SparkLineChart = React.forwardRef(function SparkLineChart(
height,
margin = SPARKLINE_DEFAULT_MARGIN,
color,
colors: deprecatedColors,
sx,
showTooltip,
showHighlight,
Expand Down Expand Up @@ -214,7 +209,7 @@ const SparkLineChart = React.forwardRef(function SparkLineChart(
...yAxis,
},
]}
colors={colors ?? deprecatedColors}
colors={colors}
sx={sx}
disableAxisListener={
(!showTooltip || slotProps?.tooltip?.trigger !== 'axis') &&
Expand Down Expand Up @@ -265,12 +260,6 @@ SparkLineChart.propTypes = {
* @default rainbowSurgePalette[0]
*/
color: PropTypes.oneOfType([PropTypes.func, PropTypes.string]),
/**
* Color palette used to colorize multiple series.
* @default rainbowSurgePalette
* @deprecated use the `color` prop instead
*/
colors: PropTypes.oneOfType([PropTypes.arrayOf(PropTypes.string), PropTypes.func]),
/**
* @default 'linear'
*/
Expand Down
30 changes: 30 additions & 0 deletions packages/x-codemod/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,36 @@ Remove `unstable_` prefix from `useSeries` and `use*Series` hooks, as they have
} from '@mui/x-charts-pro/hooks';
```

#### `rename-sparkline-colors-to-color`

Renames the `colors` prop of a `SparkLineChart` to `color` and accesses its first element.

```diff
<SparkLineChart
- colors={['red']}
+ color={'red'}
/>
```

If `colors` is a function, it will be wrapped in another function that returns its first element.

```diff
<SparkLineChart
- colors={fn}
+ color={typeof fn === 'function' ? mode => fn(mode)[0] : fn[0]}
/>
```

If there are cases that the codemod cannot handle, you should see a comment with a `mui-x-codemod` prefix in the code.

```diff
<SparkLineChart
- colors={(mode) => (mode === 'light' ? ['black'] : ['white'])}
+ /* mui-x-codemod: We renamed the `colors` prop to `color`, but didn't change the value. Please ensure sure this prop receives a string or a function that returns a string. */
+ color={(mode) => (mode === 'light' ? ['black'] : ['white'])}
/>
```

### Data Grid codemods

#### `preset-safe` for Data Grid v8.0.0
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// @ts-nocheck
/* eslint-disable no-restricted-imports */
import * as React from 'react';
import { SparkLineChart } from '@mui/x-charts';

const data = [1, 2];

function Chart() {
const fn = (mode) => (mode === 'light' ? ['black'] : ['white']);

// prettier-ignore
return (
<React.Fragment>
<SparkLineChart data={data} colors={['red']} />
<SparkLineChart data={data} colors={fn} />
<SparkLineChart data={data} colors={(mode) => (mode === 'light' ? ['black'] : ['white'])} />
<SparkLineChart data={data} colors="red" />
</React.Fragment>
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// @ts-nocheck
/* eslint-disable no-restricted-imports */
import * as React from 'react';
import { SparkLineChart } from '@mui/x-charts';

const data = [1, 2];

function Chart() {
const fn = (mode) => (mode === 'light' ? ['black'] : ['white']);

// prettier-ignore
return (
(<React.Fragment>
<SparkLineChart data={data} color={'red'} />
<SparkLineChart data={data} color={typeof fn === "function" ? mode => fn(mode)?.[0] : fn} />
<SparkLineChart
data={data}
/* mui-x-codemod: We renamed the `colors` prop to `color`, but didn't change the value. Please ensure sure this prop receives a string or a function that returns a string. */
color={(mode) => (mode === 'light' ? ['black'] : ['white'])} />
<SparkLineChart
data={data}
/* mui-x-codemod: We renamed the `colors` prop to `color`, but didn't change the value. Please ensure sure this prop receives a string or a function that returns a string. */
color="red" />
</React.Fragment>)
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import { JsCodeShiftAPI, JsCodeShiftFileInfo } from '../../../types';
/**
* @param {import('jscodeshift').FileInfo} file
* @param {import('jscodeshift').API} api
*/
export default function transformer(file: JsCodeShiftFileInfo, api: JsCodeShiftAPI, options: any) {
const j = api.jscodeshift;

const printOptions = options.printOptions;

const root = j(file.source);
const componentNames = ['SparkLineChart'];
const props = { colors: 'color' };

const colorAttributes = root
.find(j.JSXElement)
.filter((path) => {
return componentNames.includes((path.value.openingElement.name as any).name);
})
.find(j.JSXAttribute)
.filter((attribute) => Object.keys(props).includes(attribute.node.name.name as string));

return colorAttributes
.forEach((attribute) => {
const colorsAttributeExpression =
attribute.node.value?.type === 'JSXExpressionContainer'
? attribute.node.value.expression
: null;

let colorAttributeExpression;

if (colorsAttributeExpression?.type === 'ArrayExpression') {
colorAttributeExpression = colorsAttributeExpression.elements[0];
} else if (colorsAttributeExpression?.type === 'Identifier') {
colorAttributeExpression = j.conditionalExpression(
j.binaryExpression(
'===',
j.unaryExpression('typeof', colorsAttributeExpression),
j.literal('function'),
),
j.arrowFunctionExpression(
[j.identifier('mode')],
j.chainExpression(
j.optionalMemberExpression(
j.callExpression(colorsAttributeExpression, [j.identifier('mode')]),
j.literal(0),
),
),
),
colorsAttributeExpression,
);
} else {
// Don't know how to handle this case
}

// Only transform the value if we know how to handle it, otherwise rename the prop and add a comment
if (colorAttributeExpression) {
j(attribute).replaceWith(
j.jsxAttribute(
j.jsxIdentifier(props[attribute.node.name.name as string]),
j.jsxExpressionContainer(colorAttributeExpression),
),
);
} else {
j(attribute)
.replaceWith(
j.jsxAttribute(
j.jsxIdentifier(props[attribute.node.name.name as string]),
attribute.node.value,
),
)
.insertBefore(
j.commentBlock(
" mui-x-codemod: We renamed the `colors` prop to `color`, but didn't change the value. Please ensure sure this prop receives a string or a function that returns a string. ",
),
);
}
})
.toSource(printOptions);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import path from 'path';
import { expect } from 'chai';
import jscodeshift from 'jscodeshift';
import transform from '.';
import readFile from '../../../util/readFile';

function read(fileName) {
return readFile(path.join(__dirname, fileName));
}

describe('v8.0.0/charts', () => {
describe('rename-sparkline-colors-to-color', () => {
it('transforms code as needed', () => {
const actual = transform(
{
source: read('./actual.spec.tsx'),
path: require.resolve('./actual.spec.tsx'),
},
{ jscodeshift: jscodeshift.withParser('tsx') },
{},
);

const expected = read('./expected.spec.tsx');
expect(actual).to.equal(expected, 'The transformed version should be correct');
});
});
});

0 comments on commit e71e831

Please sign in to comment.