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

fix: Rendering the cursor before the shapes #4200

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
40 changes: 20 additions & 20 deletions src/chart/generateCategoricalChart.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2173,26 +2173,26 @@ export const generateCategoricalChart = ({
}

renderMap = {
CartesianGrid: { handler: renderAsIs, once: true },
ReferenceArea: { handler: this.renderReferenceElement },
ReferenceLine: { handler: renderAsIs },
ReferenceDot: { handler: this.renderReferenceElement },
XAxis: { handler: renderAsIs },
YAxis: { handler: renderAsIs },
Brush: { handler: this.renderBrush, once: true },
Bar: { handler: this.renderGraphicChild },
Line: { handler: this.renderGraphicChild },
Area: { handler: this.renderGraphicChild },
Radar: { handler: this.renderGraphicChild },
RadialBar: { handler: this.renderGraphicChild },
Scatter: { handler: this.renderGraphicChild },
Pie: { handler: this.renderGraphicChild },
Funnel: { handler: this.renderGraphicChild },
Tooltip: { handler: this.renderCursor, once: true },
PolarGrid: { handler: this.renderPolarGrid, once: true },
PolarAngleAxis: { handler: this.renderPolarAxis },
PolarRadiusAxis: { handler: this.renderPolarAxis },
Customized: { handler: this.renderCustomized },
CartesianGrid: { handler: renderAsIs, once: true, priority: 0 },
ReferenceArea: { handler: this.renderReferenceElement, priority: 1 },
ReferenceLine: { handler: renderAsIs, priority: 1 },
ReferenceDot: { handler: this.renderReferenceElement, priority: 1 },
XAxis: { handler: renderAsIs, priority: 0 },
YAxis: { handler: renderAsIs, priority: 0 },
Brush: { handler: this.renderBrush, once: true, priority: 1 },
Bar: { handler: this.renderGraphicChild, priority: 1 },
Line: { handler: this.renderGraphicChild, priority: 1 },
Area: { handler: this.renderGraphicChild, priority: 1 },
Radar: { handler: this.renderGraphicChild, priority: 1 },
RadialBar: { handler: this.renderGraphicChild, priority: 1 },
Scatter: { handler: this.renderGraphicChild, priority: 1 },
Pie: { handler: this.renderGraphicChild, priority: 1 },
Funnel: { handler: this.renderGraphicChild, priority: 1 },
Tooltip: { handler: this.renderCursor, once: true, priority: 0 },
PolarGrid: { handler: this.renderPolarGrid, once: true, priority: 0 },
PolarAngleAxis: { handler: this.renderPolarAxis, priority: 0 },
PolarRadiusAxis: { handler: this.renderPolarAxis, priority: 0 },
Customized: { handler: this.renderCustomized, priority: 1 },
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why would we want to extend this with a number field, if really it can only be 0 or 1?
It seems to me that what instead care for is: Foreground vs Background.

I would always prefer types to be a simple as possible, and here number is needlessly open to extension. Soon, we might have someone add a new value, i.e. priority 2, or -1.

What do you think? Could we implement this with only a boolean flag? Something like isBackground: boolean?

};

render() {
Expand Down
21 changes: 16 additions & 5 deletions src/util/ReactUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -417,27 +417,38 @@ export const isSingleChildEqual = (nextChild: React.ReactElement, prevChild: Rea
return false;
};

type Priority = number;
type Index = number;
export const orderByPriorityAndIndex = <T>(items: [Priority, Index, T][]): [Priority, Index, T][] => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would prefer an implementation that uses a object, to benefit from named keys.

This would both make the code more readable as well as safer to use, i.e. one could not accidentally pass index and priority in the wrong order without noticing (both are number after all.)

return items.sort((a, b) => {
if (a[0] < b[0]) return -1;
if (a[0] > b[0]) return 1;

return a[1] - b[1];
});
};

export const renderByOrder = (children: React.ReactElement[], renderMap: any) => {
const elements: React.ReactElement[] = [];
const elementsWithPriority: [number, number, React.ReactElement][] = [];
const record: Record<string, boolean> = {};

toArray(children).forEach((child, index) => {
if (isSvgElement(child)) {
elements.push(child);
elementsWithPriority.push([0, index, child]);
} else if (child) {
const displayName = getDisplayName(child.type);
const { handler, once } = renderMap[displayName] || {};
const { handler, once, priority } = renderMap[displayName] || {};

if (handler && (!once || !record[displayName])) {
const results = handler(child, displayName, index);

elements.push(results);
elementsWithPriority.push([priority, index, results]);
record[displayName] = true;
}
}
});

return elements;
return orderByPriorityAndIndex(elementsWithPriority).map(entry => entry[2]);
};

export const getReactEventByType = (e: { type?: string }): string => {
Expand Down
17 changes: 17 additions & 0 deletions storybook/stories/Examples/BarChart.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -811,3 +811,20 @@ export const WithMinPointSize = {
);
},
};

export const ActiveTooltip = {
render: () => {
return (
<ResponsiveContainer width="100%" height="100%">
<BarChart data={pageData}>
<XAxis dataKey="pv" />
<YAxis dataKey="uv" />
<ReferenceLine y={pageData[0].pv} />
<Bar dataKey="uv" />
<CartesianGrid strokeDasharray="3 3" />
<Tooltip active defaultIndex={1} />
</BarChart>
</ResponsiveContainer>
);
},
};
21 changes: 21 additions & 0 deletions test/util/ReactUtils.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
getDisplayName,
isChildrenEqual,
isValidSpreadableProp,
orderByPriorityAndIndex,
toArray,
validateWidthHeight,
withoutType,
Expand Down Expand Up @@ -406,4 +407,24 @@ describe('ReactUtils untest tests', () => {
expect(lineChildren.map(child => child.key)).toEqual(['a', 'b', 'c', 'd']);
});
});

describe('orderByPriorityAndIndex', () => {
test('orderByPriorityAndIndex should return in ascending order by priority and index', () => {
const sortedValue = orderByPriorityAndIndex([
[1, 1, 'a'],
[0, 1, 'b'],
[4, 2, 'c'],
[4, 0, 'd'],
[3, 1, 'e'],
]);

expect(sortedValue).toEqual([
[0, 1, 'b'],
[1, 1, 'a'],
[3, 1, 'e'],
[4, 0, 'd'],
[4, 2, 'c'],
]);
});
});
});