Commit ad0f02e0 by 李伟康

refactor(markdown): 优化图表组件功能并移除调试日志

- 移除了Answer组件中的调试console.log语句
- 为图表组件添加了数值格式化功能支持万、亿单位显示
- 实现了饼图图例的显示/隐藏切换交互功能
- 优化了图表组件的数据处理和状态管理逻辑
- 添加了图表组件样式模块文件并整合CSS类名处理
- 修复了Think组件在思考未完成时的点击交互问题
- 增强了图表工具提示功能支持多轴数据展示
1 parent 99b46bee
...@@ -13,7 +13,6 @@ import { ...@@ -13,7 +13,6 @@ import {
} from '@/app/components/base/markdown/blocks' } from '@/app/components/base/markdown/blocks'
import { setGlobalOnThinkComplete } from '@/app/components/base/markdown/blocks/think' import { setGlobalOnThinkComplete } from '@/app/components/base/markdown/blocks/think'
import s from './index.module.scss' import s from './index.module.scss'
type AnswerProps = { type AnswerProps = {
chatAnswerContainerInner?: string chatAnswerContainerInner?: string
} }
...@@ -25,7 +24,6 @@ const Answer: FC<AnswerProps> = ({ chatAnswerContainerInner }) => { ...@@ -25,7 +24,6 @@ const Answer: FC<AnswerProps> = ({ chatAnswerContainerInner }) => {
// 设置全局回调 // 设置全局回调
useEffect(() => { useEffect(() => {
const handleThinkComplete = (isComplete: boolean) => { const handleThinkComplete = (isComplete: boolean) => {
console.log('chufa')
thinkingComplete.current = isComplete thinkingComplete.current = isComplete
} }
setGlobalOnThinkComplete(handleThinkComplete) setGlobalOnThinkComplete(handleThinkComplete)
......
.chart-canvas{
width: 100%;
}
.chart-box-pie {
width: 100%;
height: 230px;
.chart-canvas{
width: 42%;
}
}
.chart-legend {
width: calc(58% - 15px);
margin-left: 15px;
display: flex;
flex-direction: column;
justify-content: center;
&-item {
display: flex;
align-items: center;
margin-bottom: 8px;
color: #133368;
font-size: 12px;
transition: opacity 0.3s ease;
&-hidden {
opacity: 0.4;
color: #999;
.chart-legend-color{
background-color: #999 !important;
}
}
}
&-color {
width: 8px;
height: 8px;
margin-right: 4px;
}
&-text-box {
width: 50%;
}
&-text{
display: block;
width: 100%;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
&-value{
width: calc(50% - 23px);
text-align: right;
}
&-line {
width: 1px;
height: 100%;
background: #1333681a;
margin: 0 5px;
}
}
\ No newline at end of file \ No newline at end of file
import { FC } from 'react' import { FC, useState, useRef, useEffect, useMemo } from 'react'
import ReactECharts from 'echarts-for-react' import ReactECharts from 'echarts-for-react'
import cn from '@/utils/classnames'
import s from './chart.module.scss'
type ChartProps = { type ChartProps = {
content: any | null content: any | null
} }
const formatValue = (value: number) => {
if (value >= 100000000) {
return `${(Math.floor(value / 100000000 * 100) / 100).toFixed(2)}亿`
}
if (value >= 1000) {
return `${(Math.floor(value / 10000 * 100) / 100).toFixed(2)}万`
}
return `${value}`
}
const Chart: FC<ChartProps> = ({ content }) => { const Chart: FC<ChartProps> = ({ content }) => {
const chartData = content const chartData = content
if (!chartData || !chartData.chart?.[0]) { if (!chartData || !chartData.chart?.[0]) {
...@@ -17,8 +28,25 @@ const Chart: FC<ChartProps> = ({ content }) => { ...@@ -17,8 +28,25 @@ const Chart: FC<ChartProps> = ({ content }) => {
let title = '' let title = ''
let legendData = [metric] let legendData = [metric]
if (data_2) legendData = [metric, data_2.metric] if (data_2) legendData = [metric, data_2.metric]
const chartRef = useRef<any>(null)
const [visibleItems, setVisibleItems] = useState<Set<string>>(new Set())
// 存储原始数据
const originalDataRef = useRef<any[]>([])
if (type == 'bar' || type == 'line') { if (type == 'bar' || type == 'line') {
option = { option = {
tooltip: {
trigger: 'axis',
axisPointer: {
type: 'shadow'
},
formatter: (params: any) => {
let result = params[0].axisValue + '<br/>'
params.forEach((item: any) => {
result += `${item.marker} ${item.seriesName}: ${formatValue(item.value)}<br/>`
})
return result
}
},
grid: { grid: {
left: '20%', left: '20%',
right: '5%', right: '5%',
...@@ -130,28 +158,29 @@ const Chart: FC<ChartProps> = ({ content }) => { ...@@ -130,28 +158,29 @@ const Chart: FC<ChartProps> = ({ content }) => {
] as any ] as any
title = type == 'bar' ? '柱状图' : '折线图' title = type == 'bar' ? '柱状图' : '折线图'
} }
const pieColor = ['#2A82E4', '#00BAAD', '#43CF7C', '#A5D63F', '#FF8D1A', '#FFC300', '#7948EA', '#C6C6C6']
if (type == 'pie') { if (type == 'pie') {
option = { option = {
color: ['#2A82E4', '#00BAAD', '#43CF7C', '#A5D63F', '#FF8D1A', '#FFC300', '#7948EA', '#C6C6C6'], color: pieColor,
legend: { // legend: {
left: '52%', // left: '52%',
top: 'center', // top: 'center',
orient: 'vertical', // orient: 'vertical',
itemWidth: 8, // itemWidth: 8,
itemHeight: 8, // itemHeight: 8,
textStyle: { // textStyle: {
fontSize: 12, // fontSize: 12,
color: '#133368', // color: '#133368',
overflow: 'truncate', // overflow: 'truncate',
width: 130, // width: 130,
}, // },
itemGap: 8, // itemGap: 8,
}, // },
series: [ series: [
{ {
type: 'pie', type: 'pie',
radius: ['35%', '50%'], radius: ['70%', '100%'],
center: ['22%', '50%'], center: ['50%', '50%'],
padAngle: 3, padAngle: 3,
label: { label: {
show: false, show: false,
...@@ -166,7 +195,7 @@ const Chart: FC<ChartProps> = ({ content }) => { ...@@ -166,7 +195,7 @@ const Chart: FC<ChartProps> = ({ content }) => {
data: (() => { data: (() => {
const dataArray = data_1.data || [] const dataArray = data_1.data || []
const nameArray = data_1.name || [] const nameArray = data_1.name || []
if (dataArray.length <= 7) { if (dataArray.length <= 7) {
return dataArray.map((item: any, index: number) => ({ value: item, name: nameArray[index] })) return dataArray.map((item: any, index: number) => ({ value: item, name: nameArray[index] }))
} }
...@@ -184,12 +213,76 @@ const Chart: FC<ChartProps> = ({ content }) => { ...@@ -184,12 +213,76 @@ const Chart: FC<ChartProps> = ({ content }) => {
], ],
} }
title = '饼状图' title = '饼状图'
// 保存原始数据
originalDataRef.current = option.series[0].data
} }
if (!title) return null if (!title) return null
// 初始化可见项状态
useEffect(() => {
if (type === 'pie' && visibleItems.size === 0) {
const allNames = new Set<string>(option.series[0].data.map((item: any) => item.name))
setVisibleItems(allNames)
}
}, [type])
// 计算当前显示的图表数据
const currentOption = useMemo(() => {
if (type !== 'pie') return option
const updatedData = option.series[0].data.map((item: any) => {
const original = originalDataRef.current.find((d: any) => d.name === item.name)
return {
...item,
value: visibleItems.has(item.name) ? original.value : 0
}
})
return {
...option,
series: [{ ...option.series[0], data: updatedData }]
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [visibleItems, type, option.series[0]?.data?.length])
// 切换图例项显示/隐藏
const handleToggleLegend = (name: string) => {
setVisibleItems(prev => {
const next = new Set(prev)
if (next.has(name)) {
next.delete(name)
} else {
next.add(name)
}
return next
})
}
return ( return (
<div className="chart-container"> <div className="chart-container">
<div className="chart-title">{title}</div> <div className="chart-title">{title}</div>
<ReactECharts option={option} style={{ height: 240 }} /> <div className={cn('chart-box flex', type == 'pie' && s['chart-box-pie'])}>
<div className={cn(s['chart-canvas'])}>
<ReactECharts ref={chartRef} option={currentOption} style={{ height: 240 }} />
</div>
{type == 'pie' && option.series[0]?.data && (
<div className={cn(s['chart-legend'])}>
{option.series[0].data.map((item: any, index: number) => (
<div
key={item.name}
className={cn(s['chart-legend-item'], !visibleItems.has(item.name) && s['chart-legend-item-hidden'])}
onClick={() => handleToggleLegend(item.name)}
>
<div
className={cn(s['chart-legend-color'])}
style={{ backgroundColor: pieColor[index] }}></div>
<div className={cn(s['chart-legend-text-box'])}>
<span className={cn(s['chart-legend-text'])}>{item.name}</span>
</div>
<div className={cn(s['chart-legend-line'])}></div>
<div className={cn(s['chart-legend-value'])}>{formatValue(item.value)}</div>
</div>
))}
</div>
)}
</div>
</div> </div>
) )
} }
......
...@@ -90,7 +90,10 @@ export const Think = ({ children, ...props }: any) => { ...@@ -90,7 +90,10 @@ export const Think = ({ children, ...props }: any) => {
const handleSummaryClick = (e: React.MouseEvent<HTMLElement>) => { const handleSummaryClick = (e: React.MouseEvent<HTMLElement>) => {
e.preventDefault() e.preventDefault()
e.stopPropagation() e.stopPropagation()
// Disable toggle when thinking is not complete
if (!isComplete) return
// Toggle the open state manually // Toggle the open state manually
setIsOpen(!isOpen) setIsOpen(!isOpen)
} }
...@@ -98,14 +101,12 @@ export const Think = ({ children, ...props }: any) => { ...@@ -98,14 +101,12 @@ export const Think = ({ children, ...props }: any) => {
if (!(props['data-think'] ?? false)) return <details {...props}>{children}</details> if (!(props['data-think'] ?? false)) return <details {...props}>{children}</details>
return ( return (
<details <details open={isOpen} className="group">
open={isOpen} <summary
className="group" className={cn(
> 'flex cursor-pointer select-none list-none items-center whitespace-nowrap font-bold text-text-secondary',
<summary )}
className="flex cursor-pointer select-none list-none items-center whitespace-nowrap font-bold text-text-secondary" onClick={handleSummaryClick}>
onClick={handleSummaryClick}
>
<div className="flex shrink-0 items-center think-class"> <div className="flex shrink-0 items-center think-class">
{/* <svg {/* <svg
className="mr-2 h-3 w-3 transition-transform duration-500 group-open:rotate-90" className="mr-2 h-3 w-3 transition-transform duration-500 group-open:rotate-90"
...@@ -131,11 +132,9 @@ export const Think = ({ children, ...props }: any) => { ...@@ -131,11 +132,9 @@ export const Think = ({ children, ...props }: any) => {
<div className="pt-2 border-l border-components-panel-border pl-2 text-text-secondary think-class"> <div className="pt-2 border-l border-components-panel-border pl-2 text-text-secondary think-class">
{displayContent} {displayContent}
</div> </div>
) : ( ) : null}
null
)}
</details> </details>
) )
} }
export default Think
\ No newline at end of file \ No newline at end of file
export default Think
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!