-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix: Scale the pie chart arc values
- Loading branch information
Showing
5 changed files
with
92 additions
and
1 deletion.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
79 changes: 79 additions & 0 deletions
79
...components/v5/frame/ColonyHome/partials/ReputationChart/partials/ChartCustomArcsLayer.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
import { ArcsLayer } from '@nivo/arcs'; | ||
import { type ComputedDatum, type PieCustomLayerProps } from '@nivo/pie'; | ||
import { useTooltip } from '@nivo/tooltip'; | ||
import { pie as d3Pie } from 'd3-shape'; | ||
import React, { type FC } from 'react'; | ||
|
||
import { useReputationChartContext } from '~context/ReputationChartContext/ReputationChartContext.ts'; | ||
import { ChartCustomTooltip } from '~v5/frame/ColonyHome/partials/ChartCustomTooltip/ChartCustomTooltip.tsx'; | ||
|
||
import { pieChartArcsLayerConfig } from '../consts.ts'; | ||
import { type ReputationChartDataItem } from '../types.ts'; | ||
|
||
interface ChartCustomBarGroupLayerProps | ||
extends PieCustomLayerProps<ReputationChartDataItem> {} | ||
|
||
export const ChartCustomArcsLayer: FC<ChartCustomBarGroupLayerProps> = ({ | ||
dataWithArc, | ||
arcGenerator, | ||
centerX, | ||
centerY, | ||
}) => { | ||
const { showTooltipFromEvent, hideTooltip } = useTooltip(); | ||
const { setActiveLegendItem, resetActiveLegendItem } = | ||
useReputationChartContext(); | ||
|
||
const getScaledPieArcValues = d3Pie< | ||
Omit<ComputedDatum<ReputationChartDataItem>, 'arc' | 'fill'> | ||
>() | ||
.value((d) => d.value * 100) | ||
.startAngle(pieChartArcsLayerConfig.startAngle) | ||
.endAngle(pieChartArcsLayerConfig.endAngle) | ||
// This needs to be smaller than the one used for nivo due to library internal adjustments | ||
.padAngle(0.15); | ||
|
||
const scaledPieArcValues = getScaledPieArcValues([...dataWithArc]); | ||
|
||
const updatedData = dataWithArc.map((datum, index) => ({ | ||
...datum, | ||
arc: { | ||
...datum.arc, | ||
startAngle: scaledPieArcValues[index].startAngle, | ||
endAngle: scaledPieArcValues[index].endAngle, | ||
}, | ||
})); | ||
|
||
const mouseInteractionHandler = (datum, event) => { | ||
setActiveLegendItem(datum.id); | ||
showTooltipFromEvent( | ||
<ChartCustomTooltip> | ||
{datum.label} {datum.value}% | ||
</ChartCustomTooltip>, | ||
event, | ||
); | ||
}; | ||
|
||
const mouseLeaveHandler = () => { | ||
hideTooltip(); | ||
resetActiveLegendItem(); | ||
}; | ||
|
||
const toggleTooltipHandler = (datum, event) => { | ||
mouseInteractionHandler(datum, event); | ||
}; | ||
|
||
return ( | ||
<ArcsLayer | ||
center={[centerX, centerY]} | ||
data={updatedData} | ||
arcGenerator={arcGenerator} | ||
borderWidth={pieChartArcsLayerConfig.borderWidth} | ||
borderColor={pieChartArcsLayerConfig.borderColor} | ||
transitionMode="startAngle" | ||
onClick={toggleTooltipHandler} | ||
onMouseEnter={mouseInteractionHandler} | ||
onMouseMove={mouseInteractionHandler} | ||
onMouseLeave={mouseLeaveHandler} | ||
/> | ||
); | ||
}; |