gaoluyang
9 天以前 f3cac4aa2db56d6017c1d1dc035bcfeaec76c3e2
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
import type { EChartsOption } from '@vben/plugins/echarts';
 
import type { HrmStatisticsApi } from '#/api/hrm/statistics';
 
/** 统一色板,保证同一页面的图表观感一致 */
const PALETTE = [
  '#3b82f6',
  '#10b981',
  '#f59e0b',
  '#ef4444',
  '#8b5cf6',
  '#06b6d4',
  '#ec4899',
  '#84cc16',
];
 
const BASE_TEXT_STYLE = {
  fontSize: 12,
};
 
interface AxisTooltipParams {
  dataIndex: number;
}
 
const TITLE_STYLE = {
  textStyle: { fontSize: 14, fontWeight: 'bold' as const },
  left: 'center' as const,
};
 
/** 柱状图:适用于固定区间分布(工龄、年龄) */
function getBarOption(title: string, data: HrmStatisticsApi.Item[]): EChartsOption {
  return {
    title: { text: title, ...TITLE_STYLE },
    color: PALETTE,
    grid: { bottom: 40, left: 50, right: 20, top: 60 },
    tooltip: {
      trigger: 'axis',
      axisPointer: { type: 'shadow' },
      formatter: '{b}<br/>{a}:{c} 人',
    },
    xAxis: {
      type: 'category',
      data: data.map((item) => item.name),
      axisLabel: { ...BASE_TEXT_STYLE, interval: 0 },
    },
    yAxis: {
      type: 'value',
      name: '人数',
      minInterval: 1,
      axisLabel: BASE_TEXT_STYLE,
    },
    series: [
      {
        name: title,
        type: 'bar',
        barMaxWidth: 42,
        itemStyle: { borderRadius: [6, 6, 0, 0] },
        label: { show: true, position: 'top', ...BASE_TEXT_STYLE },
        data: data.map((item) => item.value),
      },
    ],
  };
}
 
/** 横向条形图:适用于人数按名称排名(部门、岗位) */
function getRankingBarOption(
  title: string,
  data: HrmStatisticsApi.Item[],
  unit: string,
  seriesName: string,
): EChartsOption {
  return {
    title: { text: title, ...TITLE_STYLE },
    color: PALETTE,
    grid: { bottom: 30, left: 110, right: 50, top: 60 },
    tooltip: {
      trigger: 'axis',
      axisPointer: { type: 'shadow' },
      formatter: `{b}<br/>${seriesName}:{c} ${unit}`,
    },
    xAxis: {
      type: 'value',
      name: unit,
      axisLabel: BASE_TEXT_STYLE,
    },
    yAxis: {
      type: 'category',
      inverse: true,
      data: data.map((item) => item.name),
      axisLabel: BASE_TEXT_STYLE,
    },
    series: [
      {
        name: seriesName,
        type: 'bar',
        barMaxWidth: 26,
        itemStyle: { borderRadius: [0, 6, 6, 0] },
        label: {
          show: true,
          position: 'right',
          formatter: `{c} ${unit}`,
          ...BASE_TEXT_STYLE,
        },
        data: data.map((item) => item.value),
      },
    ],
  };
}
 
/** 环形图:适用于占比类分布(学历、性别、状态、婚姻、政治面貌) */
function getPieOption(title: string, data: HrmStatisticsApi.Item[]): EChartsOption {
  return {
    title: { text: title, ...TITLE_STYLE },
    color: PALETTE,
    tooltip: { trigger: 'item', formatter: '{b}:{c} 人({d}%)' },
    legend: {
      type: 'scroll',
      orient: 'vertical',
      left: 10,
      top: 50,
      textStyle: BASE_TEXT_STYLE,
    },
    series: [
      {
        name: title,
        type: 'pie',
        radius: ['42%', '68%'],
        center: ['58%', '58%'],
        avoidLabelOverlap: false,
        itemStyle: { borderRadius: 8, borderColor: '#fff', borderWidth: 2 },
        label: { show: true, formatter: '{b}\n{c} 人' },
        labelLine: { show: true },
        data: data.map((item) => ({ name: item.name, value: item.value })),
      },
    ],
  };
}
 
/** 合同到期预警:按紧急程度着色 */
function getContractWarningOption(
  data: HrmStatisticsApi.Item[],
): EChartsOption {
  const colorMap: Record<string, string> = {
    已到期: '#ef4444',
    '30天内到期': '#f59e0b',
    '31-90天到期': '#3b82f6',
    '90天以上': '#10b981',
    无固定期限: '#9ca3af',
  };
  return {
    title: { text: '合同到期预警', ...TITLE_STYLE },
    grid: { bottom: 40, left: 50, right: 20, top: 60 },
    tooltip: {
      trigger: 'axis',
      axisPointer: { type: 'shadow' },
      formatter: '{b}<br/>{c} 份',
    },
    xAxis: {
      type: 'category',
      data: data.map((item) => item.name),
      axisLabel: { ...BASE_TEXT_STYLE, interval: 0 },
    },
    yAxis: {
      type: 'value',
      name: '合同数',
      minInterval: 1,
      axisLabel: BASE_TEXT_STYLE,
    },
    series: [
      {
        name: '合同到期预警',
        type: 'bar',
        barMaxWidth: 42,
        label: { show: true, position: 'top', ...BASE_TEXT_STYLE },
        data: data.map((item) => ({
          name: item.name,
          value: item.value,
          itemStyle: {
            color: colorMap[item.name] ?? '#3b82f6',
            borderRadius: [6, 6, 0, 0],
          },
        })),
      },
    ],
  };
}
 
export function getTenureDistributionOption(data: HrmStatisticsApi.Item[]) {
  return getBarOption('工龄分布', data);
}
 
export function getAgeDistributionOption(data: HrmStatisticsApi.Item[]) {
  return getBarOption('年龄分布', data);
}
 
export function getStatusDistributionOption(data: HrmStatisticsApi.Item[]) {
  return getPieOption('人员状态构成', data);
}
 
export function getDeptDistributionOption(data: HrmStatisticsApi.Item[]) {
  return getRankingBarOption('部门人数分布', data, '人', '在岗人数');
}
 
export function getPostDistributionOption(data: HrmStatisticsApi.Item[]) {
  return getRankingBarOption('岗位分布', data, '人', '在岗人数');
}
 
export function getDeptTenureOption(data: HrmStatisticsApi.DeptTenure[]) {
  return {
    title: { text: '部门平均工龄', ...TITLE_STYLE },
    color: PALETTE,
    grid: { bottom: 30, left: 110, right: 50, top: 60 },
    tooltip: {
      trigger: 'axis',
      axisPointer: { type: 'shadow' },
      formatter: (params: AxisTooltipParams | AxisTooltipParams[]) => {
        const item = Array.isArray(params) ? params[0] : params;
        if (!item) {
          return '';
        }
        const dept = data[item.dataIndex];
        return `${dept?.deptName ?? ''}<br/>平均工龄:${dept?.averageTenureYears ?? 0} 年<br/>在岗人数:${dept?.count ?? 0} 人`;
      },
    },
    xAxis: {
      type: 'value',
      name: '年',
      axisLabel: BASE_TEXT_STYLE,
    },
    yAxis: {
      type: 'category',
      inverse: true,
      data: data.map((item) => item.deptName),
      axisLabel: BASE_TEXT_STYLE,
    },
    series: [
      {
        name: '平均工龄',
        type: 'bar',
        barMaxWidth: 26,
        itemStyle: { borderRadius: [0, 6, 6, 0] },
        label: {
          show: true,
          position: 'right',
          formatter: '{c} 年',
          ...BASE_TEXT_STYLE,
        },
        data: data.map((item) => item.averageTenureYears),
      },
    ],
  } as EChartsOption;
}
 
export function getEducationDistributionOption(data: HrmStatisticsApi.Item[]) {
  return getPieOption('学历分布', data);
}
 
export function getGenderDistributionOption(data: HrmStatisticsApi.Item[]) {
  return getPieOption('性别分布', data);
}
 
export function getMarriageDistributionOption(data: HrmStatisticsApi.Item[]) {
  return getPieOption('婚姻状况分布', data);
}
 
export function getPoliticalDistributionOption(data: HrmStatisticsApi.Item[]) {
  return getPieOption('政治面貌分布', data);
}
 
export function getHireResignTrendOption(data: HrmStatisticsApi.Trend[]) {
  return {
    title: { text: '近 12 个月入职 / 离职趋势', ...TITLE_STYLE },
    color: ['#10b981', '#ef4444'],
    grid: { bottom: 50, left: 50, right: 30, top: 70 },
    tooltip: { trigger: 'axis', formatter: '{b}<br/>{a}:{c} 人' },
    legend: { top: 30, data: ['入职', '离职'] },
    xAxis: {
      type: 'category',
      boundaryGap: false,
      data: data.map((item) => item.month.slice(2)),
      axisLabel: { ...BASE_TEXT_STYLE, rotate: 30 },
    },
    yAxis: {
      type: 'value',
      name: '人数',
      minInterval: 1,
      axisLabel: BASE_TEXT_STYLE,
    },
    series: [
      {
        name: '入职',
        type: 'line',
        smooth: true,
        symbolSize: 6,
        data: data.map((item) => item.hireCount),
      },
      {
        name: '离职',
        type: 'line',
        smooth: true,
        symbolSize: 6,
        data: data.map((item) => item.resignCount),
      },
    ],
  } as EChartsOption;
}
 
export function getContractWarningChartOption(data: HrmStatisticsApi.Item[]) {
  return getContractWarningOption(data);
}