'use client'; import { Card } from '@/components/ui/card'; import { Button } from '@/components/ui/button'; import { Slider } from '@/components/ui/slider'; import { Badge } from '@/components/ui/badge'; import { Progress } from '@/components/ui/progress'; import { Dialog, DialogContent, DialogHeader, DialogTitle, } from '@/components/ui/dialog'; import { Sliders, RotateCcw, Info, Scale } from 'lucide-react'; import { SpatialAnalysisState, SpatialAnalysisAction } from './spatialAnalysisReducer'; interface WeightConfigurationProps { state: SpatialAnalysisState; dispatch: React.Dispatch; isDialog?: boolean; } export default function WeightConfiguration({ state, dispatch, isDialog = false }: WeightConfigurationProps) { const categories = [ { id: 'soil', name: '土壤条件', icon: '🌱', color: 'blue' }, { id: 'climate', name: '气候条件', icon: '🌤️', color: 'green' }, { id: 'topography', name: '地形条件', icon: '⛰️', color: 'orange' }, { id: 'infrastructure', name: '基础设施', icon: '🏗️', color: 'purple' } ]; const handleUpdateWeight = (category: string, value: number) => { const newWeightConfig = { ...state.weightConfig, [category]: value / 100 }; dispatch({ type: 'SET_WEIGHT_CONFIG', payload: newWeightConfig }); }; const handleResetToDefaults = () => { const defaultConfig = { soil: 0.35, climate: 0.30, topography: 0.20, infrastructure: 0.15 }; dispatch({ type: 'SET_WEIGHT_CONFIG', payload: defaultConfig }); }; const handleBalanceWeights = () => { // 自动平衡权重,确保总和为100% const total = Object.values(state.weightConfig).reduce((sum, val) => sum + val, 0); if (total > 0) { const balancedConfig = Object.fromEntries( Object.entries(state.weightConfig).map(([key, value]) => [key, value / total]) ) as typeof state.weightConfig; dispatch({ type: 'SET_WEIGHT_CONFIG', payload: balancedConfig }); } }; const getTotalWeight = () => { return Object.values(state.weightConfig).reduce((sum, val) => sum + val, 0); }; const categoryConfigs = categories.map(category => ({ ...category, weight: state.weightConfig[category.id as keyof typeof state.weightConfig], percentage: Math.round(state.weightConfig[category.id as keyof typeof state.weightConfig] * 100) })); const content = (
{/* 权重总览 */}

权重分配总览

{Math.round(getTotalWeight() * 100)}%
总权重
{/* 权重可视化 */}
{categoryConfigs.map(category => (
{category.icon} {category.name}
{category.percentage}% {category.weight.toFixed(2)}
))} {Math.abs(getTotalWeight() - 1) > 0.01 && (
偏差 {Math.abs((getTotalWeight() - 1) * 100).toFixed(1)}%

权重总和不为100%,建议点击“自动平衡”调整

)}
{/* 权重调节面板 */}

权重调节

{categoryConfigs.map(category => (
{category.icon}

{category.name}

{category.id === 'soil' && '包括土壤pH、有机质、养分含量等指标'} {category.id === 'climate' && '包括温度、降水、光照等气象条件'} {category.id === 'topography' && '包括海拔、坡度等地形特征'} {category.id === 'infrastructure' && '包括灌溉、交通等设施条件'}

{category.percentage}%
当前权重
handleUpdateWeight(category.id, value)} max={60} min={5} step={1} className="w-full" />
5% 60%
))}
{/* 权重配置说明 */}

权重配置说明:

  • 土壤条件 (35%): 土壤是农业生产的基础,影响作物生长和养分供应
  • 气候条件 (30%): 气候决定了作物的生长环境和适宜种植范围
  • 地形条件 (20%): 地形影响水土保持、机械化作业和微气候
  • 基础设施 (15%): 灌溉、交通等设施影响生产效率和成本
  • • 权重总和应保持100%,可根据实际情况调整各维度的重要性
  • • 建议在农业专家指导下进行权重配置,确保评价结果科学合理
{/* 预设配置 */}

快速预设配置

); if (isDialog) { return ( dispatch({ type: 'TOGGLE_WEIGHT_DIALOG' })}> 权重配置 {content} ); } return (

权重配置

总权重: {Math.round(getTotalWeight() * 100)}%
{content}
); }