antvis/G2

The issue has been closed
feat: dodgeX support different sort methods for different fields of the x-axis #4191
pepper-nice posted onGitHub
背景
<img width="500" alt="image" src="https://user-images.githubusercontent.com/109653633/194706570-13d1ebe8-dcd0-4639-95a0-2d3e8827ee1e.png"> G2 4.0 的组内排序无法支持不同 x 轴字段使用不同的排序方式。这一需求在复杂数据分析场景十分需要。
API 设计
{
type: 'interval',
data: [
{ year: '2014', type: 'Sales', sales: 1000 },
{ year: '2015', type: 'Sales', sales: 1170 },
{ year: '2016', type: 'Sales', sales: 660 },
{ year: '2017', type: 'Sales', sales: 1030 },
{ year: '2014', type: 'Expenses', sales: 400 },
{ year: '2015', type: 'Expenses', sales: 460 },
{ year: '2016', type: 'Expenses', sales: 1120 },
{ year: '2017', type: 'Expenses', sales: 540 },
{ year: '2014', type: 'Profit', sales: 300 },
{ year: '2015', type: 'Profit', sales: 300 },
{ year: '2016', type: 'Profit', sales: 300 },
{ year: '2017', type: 'Profit', sales: 350 },
],
transform: [
{
type: 'dodgeX',
orderBy: {
2014: 'ascend',
2015: 'descend',
2016: ['Sales', 'Profit', 'Expenses'],
},
},
],
encode: {
x: 'year',
y: 'sales',
color: 'type',
},
};
实现方法
export function normalizeComparator(order: Order): IndexComparatorFactory {
if(typeof order === 'object') return createCustomOrder(order);
// ......
return () => null;
}
// fields 获取x、y、color 的字段名称
function createCustomOrder(order, fields) {
const xField = fields.x;
return (data, Y, S) => {
return ascendingComparator((i) => {
const x = data[xField];
if(order[x]) {
// todo
}
});
};
}