Skip to content

Commit a025bb0

Browse files
authored
feat(react-chart): add palette plugin (#1408)
1 parent 1a8b1c3 commit a025bb0

File tree

28 files changed

+431
-55
lines changed

28 files changed

+431
-55
lines changed

packages/dx-chart-core/src/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,5 @@ export * from './plugins/series/computeds';
55
export * from './utils/scale';
66
export * from './constants';
77
export * from './plugins/stack/computeds';
8-
export * from './plugins/theme-manager/computeds';
8+
export * from './plugins/palette/computeds';
99
export * from './plugins/chart/computeds';
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { scaleOrdinal } from 'd3-scale';
2+
3+
export const palette = (series, scheme) => scaleOrdinal()
4+
.domain(series.map(({ uniqueName }) => uniqueName))
5+
.range(scheme);
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { palette } from './computeds';
2+
3+
describe('Palette', () => {
4+
it('should return palette series', () => {
5+
expect(palette(Array.from({ length: 6 }).map(() => ({})), [])).toEqual(expect.any(Function));
6+
});
7+
});

packages/dx-chart-core/src/plugins/series/computeds.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ export const pieAttributes = (
6161
const width = Math.max.apply(null, xScale.range());
6262
const height = Math.max.apply(null, yScale.range());
6363
const radius = Math.min(width, height) / 2;
64-
const pieData = pie().value(d => d[valueField])(data);
64+
const pieData = pie().sort(null).value(d => d[valueField])(data);
6565

6666
return pieData.map(({
6767
startAngle, endAngle, value, data: itemData,

packages/dx-chart-core/src/plugins/series/computeds.test.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ jest.mock('d3-shape', () => {
4444
value: jest.fn(func => data => data.map(d => ({
4545
startAngle: func(d), endAngle: func(d), value: 'value', data: d,
4646
}))),
47+
sort: jest.fn().mockReturnThis(),
4748
};
4849

4950
return {

packages/dx-chart-core/src/plugins/theme-manager/computeds.js

Lines changed: 0 additions & 8 deletions
This file was deleted.

packages/dx-chart-core/src/plugins/theme-manager/computeds.test.js

Lines changed: 0 additions & 7 deletions
This file was deleted.
Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,23 @@
1-
import { Chart as ChartBase, withComponents } from '@devexpress/dx-react-chart';
1+
import * as React from 'react';
2+
import * as PropTypes from 'prop-types';
3+
import { Chart as ChartBase, withComponents, Palette } from '@devexpress/dx-react-chart';
24
import { Root } from './templates/layout';
35

4-
export const Chart = withComponents({ Root })(ChartBase);
6+
const palette = ['#0070ff', '#d72e3d', '#249d3d', '#ffb90c', '#1698af', '#616a72'];
7+
8+
const ChartWithPalette = ({ children, ...props }) => (
9+
<ChartBase
10+
{...props}
11+
>
12+
<Palette scheme={palette} />
13+
{children}
14+
</ChartBase>
15+
);
16+
17+
ChartWithPalette.components = ChartBase.components;
18+
19+
ChartWithPalette.propTypes = {
20+
children: PropTypes.node.isRequired,
21+
};
22+
23+
export const Chart = withComponents({ Root })(ChartWithPalette);

packages/dx-react-chart-demos/package-lock.json

Lines changed: 30 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/dx-react-chart-demos/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
"core-js": "^2.5.7",
3636
"d3-format": "^1.3.2",
3737
"d3-scale": "^2.1.2",
38+
"d3-scale-chromatic": "^1.3.3",
3839
"d3-shape": "^1.2.2",
3940
"prismjs": "^1.15.0",
4041
"prop-types": "^15.6.2",
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
import * as React from 'react';
2+
import { Card } from 'reactstrap';
3+
import {
4+
Chart,
5+
PieSeries,
6+
} from '@devexpress/dx-react-chart-bootstrap4';
7+
import {
8+
schemeCategory10,
9+
schemeAccent,
10+
schemeDark2,
11+
schemePaired,
12+
schemePastel1,
13+
schemePastel2,
14+
schemeSet1,
15+
schemeSet2,
16+
schemeSet3,
17+
} from 'd3-scale-chromatic';
18+
19+
import { Scale, Palette } from '@devexpress/dx-react-chart';
20+
21+
const schemeCollection = [
22+
schemeCategory10,
23+
schemeAccent,
24+
schemeDark2,
25+
schemePaired,
26+
schemePastel1,
27+
schemePastel2,
28+
schemeSet1,
29+
schemeSet2,
30+
schemeSet3,
31+
];
32+
33+
const data = [];
34+
for (let i = 0; i < 15; i += 1) {
35+
data.push({ category: `item${i}`, val: 1 });
36+
}
37+
38+
export default class Demo extends React.PureComponent {
39+
constructor(props) {
40+
super(props);
41+
42+
this.state = {
43+
data,
44+
scheme: schemeCollection[0],
45+
};
46+
47+
this.changeScheme = this.changeScheme.bind(this);
48+
}
49+
50+
changeScheme(e) {
51+
this.setState({ scheme: schemeCollection[e.target.value] });
52+
}
53+
54+
render() {
55+
const { data: chartData, scheme } = this.state;
56+
57+
return (
58+
<Card>
59+
<Chart
60+
data={chartData}
61+
>
62+
<PieSeries
63+
valueField="val"
64+
argumentField="category"
65+
/>
66+
<Scale />
67+
<Palette scheme={scheme} />
68+
</Chart>
69+
<div className="d-flex justify-content-center mt-3">
70+
{scheme.map(color => <div key={color} style={{ width: '40px', height: '40px', backgroundColor: color }} />)}
71+
</div>
72+
<div className="pb-5 pl-5 w-200" style={{ width: '200px' }}>
73+
<h5>Scheme</h5>
74+
<select className="custom-select" onChange={this.changeScheme}>
75+
<option defaultValue value={0}>schemeCategory10</option>
76+
<option value={1}>schemeAccent</option>
77+
<option value={2}>schemeDark2</option>
78+
<option value={3}>schemePaired</option>
79+
<option value={4}>schemePastel1</option>
80+
<option value={5}>schemePastel2</option>
81+
<option value={6}>schemeSet1</option>
82+
<option value={7}>schemeSet2</option>
83+
<option value={8}>schemeSet3</option>
84+
</select>
85+
</div>
86+
</Card>
87+
);
88+
}
89+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import * as React from 'react';
2+
import { mount } from 'enzyme';
3+
import { setupConsole } from '@devexpress/dx-testing';
4+
import Demo from './theme';
5+
6+
setupConsole();
7+
8+
Element.prototype.getBBox = () => {};
9+
jest.spyOn(Element.prototype, 'getBBox').mockImplementation(() => ({
10+
x: 0,
11+
y: 0,
12+
width: 800,
13+
height: 600,
14+
}));
15+
16+
window.fetch = jest.fn().mockImplementation(() => new Promise(() => {}));
17+
18+
it('should not fail', () => {
19+
expect(() => { mount(<Demo />); })
20+
.not.toThrow();
21+
});
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
import * as React from 'react';
2+
import Paper from '@material-ui/core/Paper';
3+
import {
4+
Chart,
5+
PieSeries,
6+
} from '@devexpress/dx-react-chart-material-ui';
7+
import { withStyles } from '@material-ui/core/styles';
8+
import Typography from '@material-ui/core/Typography';
9+
import NativeSelect from '@material-ui/core/NativeSelect';
10+
import FormControl from '@material-ui/core/FormControl';
11+
import {
12+
schemeCategory10,
13+
schemeAccent,
14+
schemeDark2,
15+
schemePaired,
16+
schemePastel1,
17+
schemePastel2,
18+
schemeSet1,
19+
schemeSet2,
20+
schemeSet3,
21+
} from 'd3-scale-chromatic';
22+
23+
import { Scale, Palette } from '@devexpress/dx-react-chart';
24+
25+
const schemeCollection = [
26+
schemeCategory10,
27+
schemeAccent,
28+
schemeDark2,
29+
schemePaired,
30+
schemePastel1,
31+
schemePastel2,
32+
schemeSet1,
33+
schemeSet2,
34+
schemeSet3,
35+
];
36+
37+
const demoStyles = theme => ({
38+
typography: {
39+
marginTop: theme.spacing.unit * 0,
40+
marginBottom: theme.spacing.unit,
41+
},
42+
div: {
43+
width: '200px',
44+
marginLeft: '50px',
45+
paddingBottom: '30px',
46+
},
47+
item: {
48+
width: '40px',
49+
height: '40px',
50+
},
51+
schemeConteiner: {
52+
display: 'flex',
53+
justifyContent: 'center',
54+
marginTop: theme.spacing.unit,
55+
},
56+
});
57+
58+
const data = [];
59+
for (let i = 0; i < 15; i += 1) {
60+
data.push({ category: `item${i}`, val: 1 });
61+
}
62+
63+
class Demo extends React.PureComponent {
64+
constructor(props) {
65+
super(props);
66+
67+
this.state = {
68+
data,
69+
scheme: schemeCollection[0],
70+
};
71+
72+
this.changeScheme = this.changeScheme.bind(this);
73+
}
74+
75+
changeScheme(e) {
76+
this.setState({ scheme: schemeCollection[e.target.value] });
77+
}
78+
79+
render() {
80+
const { data: chartData, scheme } = this.state;
81+
const { classes } = this.props;
82+
83+
return (
84+
<Paper>
85+
<Chart
86+
data={chartData}
87+
>
88+
<PieSeries
89+
valueField="val"
90+
argumentField="category"
91+
/>
92+
<Scale />
93+
<Palette scheme={scheme} />
94+
</Chart>
95+
<div className={classes.schemeConteiner}>
96+
{scheme.map(color => (
97+
<div
98+
key={color}
99+
className={classes.item}
100+
style={{ backgroundColor: color }}
101+
/>
102+
))}
103+
</div>
104+
<div className={classes.div}>
105+
<Typography component="h5" variant="headline" className={classes.typography}>Scheme</Typography>
106+
<FormControl>
107+
<NativeSelect onChange={this.changeScheme} defaultValue={0}>
108+
<option value={0}>schemeCategory10</option>
109+
<option value={1}>schemeAccent</option>
110+
<option value={2}>schemeDark2</option>
111+
<option value={3}>schemePaired</option>
112+
<option value={4}>schemePastel1</option>
113+
<option value={5}>schemePastel2</option>
114+
<option value={6}>schemeSet1</option>
115+
<option value={7}>schemeSet2</option>
116+
<option value={8}>schemeSet3</option>
117+
</NativeSelect>
118+
</FormControl>
119+
</div>
120+
</Paper>
121+
);
122+
}
123+
}
124+
125+
export default withStyles(demoStyles, { name: 'Demo' })(Demo);
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import * as React from 'react';
2+
import { mount } from 'enzyme';
3+
import { setupConsole } from '@devexpress/dx-testing';
4+
import Demo from './theme';
5+
6+
setupConsole();
7+
8+
Element.prototype.getBBox = () => {};
9+
jest.spyOn(Element.prototype, 'getBBox').mockImplementation(() => ({
10+
x: 0,
11+
y: 0,
12+
width: 800,
13+
height: 600,
14+
}));
15+
16+
window.fetch = jest.fn().mockImplementation(() => new Promise(() => {}));
17+
18+
it('should not fail', () => {
19+
expect(() => { mount(<Demo />); })
20+
.not.toThrow();
21+
});

0 commit comments

Comments
 (0)