|
| 1 | +import BigNumber from 'bignumber.js' |
| 2 | +import { useTranslation } from 'react-i18next' |
| 3 | +import dayjs from 'dayjs' |
| 4 | +import { DATA_ZOOM_CONFIG, assertIsArray, handleAxis } from '../../../utils/chart' |
| 5 | +import { tooltipColor, tooltipWidth, SmartChartPage } from '../common' |
| 6 | +import { ChartItem, explorerService } from '../../../services/ExplorerService' |
| 7 | +import { useCurrentLanguage } from '../../../utils/i18n' |
| 8 | +import { ChartColorConfig } from '../../../constants/common' |
| 9 | + |
| 10 | +const useOption = ( |
| 11 | + statisticKnowledgeSize: ChartItem.KnowledgeSize[], |
| 12 | + chartColor: ChartColorConfig, |
| 13 | + isMobile: boolean, |
| 14 | + isThumbnail = false, |
| 15 | +): echarts.EChartOption => { |
| 16 | + const { t } = useTranslation() |
| 17 | + const currentLanguage = useCurrentLanguage() |
| 18 | + const gridThumbnail = { |
| 19 | + left: '4%', |
| 20 | + right: '10%', |
| 21 | + top: '8%', |
| 22 | + bottom: '6%', |
| 23 | + containLabel: true, |
| 24 | + } |
| 25 | + const grid = { |
| 26 | + left: '3%', |
| 27 | + right: '3%', |
| 28 | + top: isMobile ? '3%' : '8%', |
| 29 | + bottom: '5%', |
| 30 | + containLabel: true, |
| 31 | + } |
| 32 | + return { |
| 33 | + color: chartColor.colors, |
| 34 | + tooltip: !isThumbnail |
| 35 | + ? { |
| 36 | + trigger: 'axis', |
| 37 | + formatter: dataList => { |
| 38 | + assertIsArray(dataList) |
| 39 | + const widthSpan = (value: string) => tooltipWidth(value, currentLanguage === 'en' ? 155 : 110) |
| 40 | + let result = `<div>${tooltipColor('#333333')}${widthSpan(t('statistic.date'))} ${dataList[0].data[0]}</div>` |
| 41 | + result += `<div>${tooltipColor(chartColor.colors[0])}\ |
| 42 | + ${widthSpan(t('statistic.knowledge_size'))} ${handleAxis(dataList[0].data[1], 2)}</div>` |
| 43 | + return result |
| 44 | + }, |
| 45 | + } |
| 46 | + : undefined, |
| 47 | + grid: isThumbnail ? gridThumbnail : grid, |
| 48 | + dataZoom: isThumbnail ? [] : DATA_ZOOM_CONFIG, |
| 49 | + xAxis: [ |
| 50 | + { |
| 51 | + name: isMobile || isThumbnail ? '' : t('statistic.date'), |
| 52 | + nameLocation: 'middle', |
| 53 | + nameGap: 30, |
| 54 | + type: 'category', |
| 55 | + boundaryGap: false, |
| 56 | + splitLine: { |
| 57 | + show: false, |
| 58 | + }, |
| 59 | + }, |
| 60 | + ], |
| 61 | + yAxis: [ |
| 62 | + { |
| 63 | + position: 'left', |
| 64 | + name: isMobile || isThumbnail ? '' : t('statistic.knowledge_size'), |
| 65 | + type: 'value', |
| 66 | + scale: true, |
| 67 | + nameTextStyle: { |
| 68 | + align: 'left', |
| 69 | + }, |
| 70 | + axisLine: { |
| 71 | + lineStyle: { |
| 72 | + color: chartColor.colors[0], |
| 73 | + }, |
| 74 | + }, |
| 75 | + axisLabel: { |
| 76 | + formatter: (value: string) => handleAxis(new BigNumber(value)), |
| 77 | + }, |
| 78 | + }, |
| 79 | + ], |
| 80 | + series: [ |
| 81 | + { |
| 82 | + name: t('statistic.knowledge_size'), |
| 83 | + type: 'line', |
| 84 | + yAxisIndex: 0, |
| 85 | + symbol: isThumbnail ? 'none' : 'circle', |
| 86 | + symbolSize: 3, |
| 87 | + }, |
| 88 | + ], |
| 89 | + dataset: { |
| 90 | + source: statisticKnowledgeSize.map(data => [ |
| 91 | + dayjs(+data.createdAtUnixtimestamp * 1000).format('YYYY/MM/DD'), |
| 92 | + new BigNumber(data.knowledgeSize).toNumber(), |
| 93 | + ]), |
| 94 | + }, |
| 95 | + } |
| 96 | +} |
| 97 | + |
| 98 | +const toCSV = (statisticKnowledge?: ChartItem.KnowledgeSize[]) => |
| 99 | + statisticKnowledge ? statisticKnowledge.map(data => [data.createdAtUnixtimestamp, data.knowledgeSize]) : [] |
| 100 | + |
| 101 | +export const KnowledgeSizeChart = ({ isThumbnail = false }: { isThumbnail?: boolean }) => { |
| 102 | + const [t] = useTranslation() |
| 103 | + return ( |
| 104 | + <SmartChartPage |
| 105 | + title={t('statistic.knowledge_size')} |
| 106 | + description={t('statistic.knowledge_size_description')} |
| 107 | + isThumbnail={isThumbnail} |
| 108 | + fetchData={explorerService.api.fetchStatisticKnowledgeSize} |
| 109 | + getEChartOption={useOption} |
| 110 | + toCSV={toCSV} |
| 111 | + queryKey="fetchStatisticKnowledgeSize" |
| 112 | + /> |
| 113 | + ) |
| 114 | +} |
| 115 | + |
| 116 | +export default KnowledgeSizeChart |
0 commit comments