Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 | 1x 1x 2x 2x 2x 6x 6x 2x 6x 4x 4x 2x 2x 2x 2x 2x 2x 6x | /**
* 营养成分饼图 - 可视化展示各营养成分占比
* 响应式设计,适配不同屏幕尺寸
*/
import { Dimensions, View } from 'react-native';
import { PieChart } from 'react-native-chart-kit';
import { Text, XStack, YStack } from 'tamagui';
import { IconSymbol } from '@/src/components/ui/IconSymbol';
import { useResponsiveLayout } from '@/src/hooks/useResponsiveLayout';
import { neutralScale, successScale, primaryScale } from '@/src/design-system/tokens';
interface NutritionChartSectionProps {
percentData: Record<string, number | null>;
}
// 专业饼图配色方案 - 柔和渐变色系
const CHART_COLORS = [
'#FF7B54', // 橙红 - 蛋白质
'#4CAF50', // 绿色 - 脂肪
'#2196F3', // 蓝色 - 碳水
'#FFC107', // 金黄 - 纤维
'#9C27B0', // 紫色 - 灰分
'#00BCD4', // 青色 - 水分
'#E91E63', // 粉红 - 其他
'#607D8B', // 灰蓝
];
// 营养成分名称映射
const NUTRITION_NAME_MAP: Record<string, string> = {
protein: '粗蛋白',
crude_protein: '粗蛋白',
fat: '粗脂肪',
crude_fat: '粗脂肪',
carbohydrates: '碳水化合物',
fiber: '粗纤维',
crude_fiber: '粗纤维',
ash: '粗灰分',
crude_ash: '粗灰分',
moisture: '水分',
others: '其它',
};
// 准备饼图数据
function preparePieChartData(percentData: Record<string, number | null>) {
Iif (!percentData || typeof percentData !== 'object') return [];
const data: { name: string; value: number }[] = [];
Object.entries(percentData).forEach(([key, value]) => {
Eif (value !== null && value !== undefined && typeof value === 'number' && value > 0) {
data.push({ name: NUTRITION_NAME_MAP[key] || key, value });
}
});
Iif (data.length === 0) return [];
return data.map((item, index) => ({
name: item.name,
population: parseFloat(item.value.toFixed(1)),
color: CHART_COLORS[index % CHART_COLORS.length],
legendFontColor: neutralScale.neutral10,
legendFontSize: 11,
}));
}
export function NutritionChartSection({ percentData }: NutritionChartSectionProps) {
const { width: screenWidth, isExtraSmallScreen, isSmallScreen } = useResponsiveLayout();
if (!percentData || typeof percentData !== 'object' || Object.keys(percentData).length === 0) {
return null;
}
const chartData = preparePieChartData(percentData);
Iif (chartData.length === 0) return null;
// 响应式尺寸计算
const chartWidth = screenWidth - 32;
const chartHeight = isExtraSmallScreen ? 180 : isSmallScreen ? 200 : 220;
return (
<YStack
marginHorizontal="$3"
marginBottom="$3"
borderRadius={20}
backgroundColor="white"
overflow="hidden"
borderWidth={1}
borderColor={neutralScale.neutral3}
>
{/* 标题栏 */}
<XStack
padding="$4"
alignItems="center"
gap="$3"
borderBottomWidth={1}
borderBottomColor={neutralScale.neutral2}
>
<YStack
width={44}
height={44}
borderRadius={22}
backgroundColor={successScale.success2}
alignItems="center"
justifyContent="center"
>
<IconSymbol name="chart.pie.fill" size={22} color={successScale.success7} />
</YStack>
<YStack flex={1}>
<Text fontSize="$5" fontWeight="700" color={neutralScale.neutral12}>
营养成分分析
</Text>
<Text fontSize={11} color={neutralScale.neutral8} marginTop={2}>
Nutrition Analysis
</Text>
</YStack>
</XStack>
{/* 图表区域 */}
<YStack padding="$4" gap="$4">
{/* 饼图 */}
<View style={{ alignItems: 'center' }}>
<PieChart
data={chartData}
width={chartWidth}
height={chartHeight}
chartConfig={{
backgroundColor: 'transparent',
backgroundGradientFrom: '#FFFFFF',
backgroundGradientTo: '#FFFFFF',
color: (opacity = 1) => `rgba(0, 0, 0, ${opacity})`,
labelColor: () => neutralScale.neutral10,
strokeWidth: 2,
decimalPlaces: 1,
}}
accessor="population"
backgroundColor="transparent"
paddingLeft="0"
center={[chartWidth / 4, 0]}
absolute
hasLegend={false}
/>
</View>
{/* 自定义图例 */}
<YStack gap="$2" paddingTop="$2">
<XStack flexWrap="wrap" gap="$2" justifyContent="center">
{chartData.map((item, index) => (
<XStack
key={index}
alignItems="center"
gap="$2"
backgroundColor={neutralScale.neutral1}
paddingHorizontal="$3"
paddingVertical="$2"
borderRadius={20}
borderWidth={1}
borderColor={neutralScale.neutral3}
>
<View
style={{
width: 12,
height: 12,
borderRadius: 6,
backgroundColor: item.color,
}}
/>
<Text fontSize={12} fontWeight="600" color={neutralScale.neutral10}>
{item.name}
</Text>
<Text fontSize={12} fontWeight="700" color={primaryScale.primary9}>
{item.population}%
</Text>
</XStack>
))}
</XStack>
</YStack>
</YStack>
</YStack>
);
}
|