Skip to content

Commit 25d808d

Browse files
authored
Merge pull request #54 from mayinrain/master
upd: legend调整,新增最近更新,国际化引入,样式名优化
2 parents 08310cf + 45c0eae commit 25d808d

File tree

5 files changed

+98
-16
lines changed

5 files changed

+98
-16
lines changed

packages/traction-widget/components/Charts/Charts.vue

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,23 @@
11
<template>
2-
<div class="wd-content-body">
3-
<h4 class="chart-title">{{ config.title }}</h4>
2+
<div class="chart-body">
3+
<div class="chart-header">
4+
<div class="title-section">
5+
<h4 class="chart-title">{{ config.title }}</h4>
6+
<div class="update-info">
7+
<span class="update-time">{{ chartsLocale.lastUpdate }}{{ lastUpdateTime }}</span>
8+
<ReloadOutlined class="refresh-icon" @click="handleRefresh" />
9+
</div>
10+
</div>
11+
</div>
412
<div class="date-range">
513
<div class="mr16 my-date-picker">
614
<FDatePicker type="daterange" v-model="dateRange" @change="updateDateRange" />
715
</div>
816
<FButton key="btn-1" class="mr16" :class="{ 'my-btn': true, active: days === 7 }" @click="updateDays(7)">
9-
最近7天
17+
{{ chartsLocale.last7Days }}
1018
</FButton>
1119
<FButton key="btn-2" :class="{ 'my-btn': true, active: days === 30 }" @click="updateDays(30)">
12-
最近30天
20+
{{ chartsLocale.last30Days }}
1321
</FButton>
1422
</div>
1523
<div :id="chartId" class="chart-container">
@@ -18,15 +26,18 @@
1826
</template>
1927

2028
<script setup lang="ts">
21-
import { ref, computed } from 'vue';
29+
import { ref } from 'vue';
2230
import { FDatePicker, FButton } from '@fesjs/fes-design';
31+
import { ReloadOutlined } from '@fesjs/fes-design/icon';
2332
import { useChart, type ChartConfig } from './useChart';
2433
import {
2534
getYear, getMonth, getDate, subDays, differenceInDays,
35+
format,
2636
} from 'date-fns';
37+
import { useLocale } from '../hooks/useLocale';
2738
28-
29-
39+
const locale = useLocale();
40+
const chartsLocale = locale.Charts;
3041
interface Props {
3142
chartId: string;
3243
config: ChartConfig;
@@ -42,7 +53,7 @@ const initialStartDate = subDays(new Date(initialEndDate), days.value - 1).getTi
4253
const endDate = ref(initialEndDate);
4354
const startDate = ref(initialStartDate);
4455
45-
// 初始化时,开始日期在前,结束日期在后
56+
4657
const dateRange = ref([initialStartDate, initialEndDate]);
4758
4859
const updateDateRange = (range: number[]) => {
@@ -73,10 +84,17 @@ const updateDays = (newDays: number) => {
7384
};
7485
7586
// 图表相关
76-
const { loading } = useChart(
87+
const { loading, updateChart } = useChart(
7788
props.chartId,
7889
props.config,
7990
startDate,
8091
endDate
8192
);
93+
94+
const lastUpdateTime = ref(format(new Date(), 'yyyy-MM-dd HH:mm:ss'));
95+
96+
const handleRefresh = async () => {
97+
await updateChart();
98+
lastUpdateTime.value = format(new Date(), 'yyyy-MM-dd HH:mm:ss');
99+
};
82100
</script>

packages/traction-widget/components/Charts/style/index.less

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,43 @@
1-
.wd-content-body {
1+
.chart-body {
22
width: 100%;
3+
4+
.chart-header {
5+
display: flex;
6+
justify-content: space-between;
7+
align-items: flex-start;
8+
margin-bottom: 16px;
9+
}
10+
11+
.title-section {
12+
display: flex;
13+
flex-direction: column;
14+
color: #999;
15+
gap: 8px;
16+
}
17+
18+
.update-info {
19+
display: flex;
20+
align-items: center;
21+
}
22+
23+
.update-time {
24+
font-size: 12px;
25+
}
26+
27+
.refresh-icon {
28+
margin-left: 8px;
29+
font-size: 12px;
30+
cursor: pointer;
31+
color: #999;
32+
&:hover {
33+
color: #5384ff;
34+
}
335
}
436

537
.chart-title {
6-
margin-bottom: 16px;
38+
margin: 0;
39+
font-size: 16px;
40+
color: #333;
741
}
842

943
.date-range {
@@ -38,4 +72,5 @@
3872
background-color: #f5f8ff;
3973
border-color: #5384ff;
4074
color: #5384ff;
41-
}
75+
}
76+
}

packages/traction-widget/components/Charts/useChart.ts

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
import { onMounted, onUnmounted, Ref, ref, watch, markRaw } from 'vue';
22
import echarts from './useEcharts';
33
import type { EChartsOption, TooltipComponentFormatterCallback } from 'echarts';
4+
import { useLocale } from '../hooks/useLocale';
5+
6+
const locale = useLocale();
7+
const chartsLocale = locale.Charts;
48

59
export interface BarStyle {
610
color: string;
@@ -47,6 +51,13 @@ export function useChart(
4751

4852
const transformData = (data: any[]) => {
4953
const xAxisData = data.map(item => item[config.xAxisField]);
54+
// 计算每个系列的总和
55+
const seriesTotal = config.series.reduce((acc, series) => {
56+
const total = data.reduce((sum, item) => sum + (Number(item[series.field]) || 0), 0);
57+
acc[series.name] = total;
58+
return acc;
59+
}, {} as Record<string, number>);
60+
5061
// 计算最大值来确定左侧留白
5162
let maxSum = 0;
5263
let leftGridSize = 16; // 基础留白大小
@@ -78,14 +89,15 @@ export function useChart(
7889
return {
7990
xAxisData,
8091
series,
81-
leftGridSize
92+
leftGridSize,
93+
seriesTotal
8294
};
8395
};
8496
const genTooltipStr = (tempData: any[]) => {
8597
const showList = tempData.map((item: { value: any; seriesName: any; }) => ({ value: item.value, name: item.seriesName }));
86-
const dateStr = showList.length ? `日期:${tempData[0].name} <br />` : '';
98+
const dateStr = showList.length ? `${chartsLocale.date}${tempData[0].name} <br />` : '';
8799
const total = showList.reduce((pre: any, cur: { value: any; }) => pre + cur.value, 0);
88-
const totalStr = showList.length ? `总计:${total} <br />` : '';
100+
const totalStr = showList.length ? `${chartsLocale.total}${total} <br />` : '';
89101
const showListStr = showList.reduce((pre: any, cur: { name: any; value: any; }) => `${pre}${cur.name}: ${cur.value}<br />`, '');
90102
return dateStr + totalStr + showListStr;
91103
};
@@ -95,7 +107,7 @@ export function useChart(
95107
try {
96108
loading.value = true;
97109
const data = await config.fetchData(startDate.value, endDate.value);
98-
const { xAxisData, series, leftGridSize } = transformData(data);
110+
const { xAxisData, series, leftGridSize, seriesTotal } = transformData(data);
99111

100112
const option: EChartsOption = {
101113
backgroundColor: '#fff',
@@ -142,6 +154,9 @@ export function useChart(
142154
itemStyle: {
143155
borderWidth: 1
144156
},
157+
formatter: (name) => {
158+
return `${name}(${seriesTotal[name]})`;
159+
},
145160
data: config.series.map(item => ({
146161
name: item.name,
147162
itemStyle: item.itemStyle

packages/traction-widget/components/locales/lang/enUS.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,5 +34,12 @@ export default {
3434
repeatTip: 'Cannot add duplicate tags!',
3535
numberLimitTip: 'Cannot add more than ',
3636
tag: 'tags'
37+
},
38+
Charts: {
39+
lastUpdate: 'Last Update: ',
40+
last30Days: 'Last 30 Days',
41+
last7Days: 'Last 7 Days',
42+
date: 'Date: ',
43+
total: 'Total: '
3744
}
3845
};

packages/traction-widget/components/locales/lang/zhCN.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,5 +33,12 @@ export default {
3333
repeatTip: '不能添加重复标签!',
3434
numberLimitTip: '不能添加超过',
3535
tag: '标签'
36+
},
37+
Charts: {
38+
lastUpdate: '最后更新:',
39+
last30Days: '最近30天',
40+
last7Days: '最近7天',
41+
date: '日期:',
42+
total: '总计:'
3643
}
3744
};

0 commit comments

Comments
 (0)