import React,{useState} from 'react';
import {View, Text, TextStyle} from 'react-native';
import {PieChartMain} from './main';
import {PieChartPropsType, pieColors, usePieChart} from 'gifted-charts-core';
export const PieChart = (props: PieChartPropsType) => {
const {
radius,
extraRadius,
selectedIndex,
setSelectedIndex,
startAngle,
total,
donut,
isThreeD,
semiCircle,
inwardExtraLengthForFocused,
canvasWidth,
canvasHeight,
innerRadius,
innerCircleColor,
innerCircleBorderWidth,
innerCircleBorderColor,
shiftInnerCenterX,
shiftInnerCenterY,
tiltAngle,
isDataShifted,
paddingHorizontal,
paddingVertical,
data,
showTooltip,
tooltipHorizontalShift,
tooltipVerticalShift,
tooltipComponent,
getTooltipText,
tooltipBackgroundColor,
tooltipBorderRadius,
tooltipWidth,
tooltipTextNoOfLines,
textColor,
textSize,
font,
fontWeight,
fontStyle,
} = usePieChart(props);
// 2. 新增:声明4个必选属性的兜底值(仅这几行新增)
const [touchX, setTouchX] = useState(0);
const [touchY, setTouchY] = useState(0);
const [tooltipSelectedIndex, setTooltipSelectedIndex] = useState(-1);
const renderInnerCircle = (
innerRadius: number,
innerCircleBorderWidth: number,
) => {
if (props.centerLabelComponent || (donut && !isDataShifted)) {
return (
{props.centerLabelComponent?.(selectedIndex) ?? null}
);
}
return null;
};
const renderTooltip = () => {
return (
(radius + extraRadius) * 1.5
? props.tooltipHorizontalShift
? touchX - tooltipHorizontalShift
: touchX -
(tooltipWidth ??
getTooltipText(tooltipSelectedIndex).length * 10)
: touchX - tooltipHorizontalShift,
top:
touchY < 30
? props.tooltipVerticalShift
? touchY - tooltipVerticalShift
: touchY
: touchY - tooltipVerticalShift,
}}>
{data[tooltipSelectedIndex].tooltipComponent ? (
data[tooltipSelectedIndex].tooltipComponent?.()
) : tooltipComponent ? (
tooltipComponent(tooltipSelectedIndex)
) : (
{getTooltipText(tooltipSelectedIndex)}
)}
);
};
// if (!total) return null;
return (
{renderInnerCircle(innerRadius, innerCircleBorderWidth)}
{props.data.length > 1 &&
props.data[selectedIndex] && // don't forget to add this one so there are no errors when the data is empty / updating
(props.focusOnPress || props.sectionAutoFocus) &&
selectedIndex !== -1 && (
)}
{renderInnerCircle(
innerRadius - inwardExtraLengthForFocused,
inwardExtraLengthForFocused ? 0 : innerCircleBorderWidth,
)}
{showTooltip && tooltipSelectedIndex !== -1 ? renderTooltip() : null}
);
};