forked from hypertrons/hypertrons-crx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathContributorChart.tsx
157 lines (146 loc) · 3.34 KB
/
ContributorChart.tsx
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
import React, { useEffect, useRef } from 'react';
import * as echarts from 'echarts';
import { formatNum, numberWithCommas } from '../../../../helpers/formatter';
import { getInterval, judgeInterval } from '../../../../helpers/judge-interval';
const LIGHT_THEME = {
FG_COLOR: '#24292F',
BG_COLOR: '#ffffff',
SPLIT_LINE_COLOR: '#D0D7DE',
BAR_COLOR: '#3E90F1',
LINE_COLOR: '#267FE8',
};
const DARK_THEME = {
FG_COLOR: '#c9d1d9',
BG_COLOR: '#0d1118',
SPLIT_LINE_COLOR: '#30363D',
BAR_COLOR: '#3E90F1',
LINE_COLOR: '#82BBFF',
};
interface ContributorChartProps {
theme: 'light' | 'dark';
width: number;
height: number;
data: [string, number][];
}
const ContributorChart = (props: ContributorChartProps): JSX.Element => {
const { theme, width, height, data } = props;
const { timeLength, minInterval } = getInterval(data);
const divEL = useRef(null);
const TH = theme == 'light' ? LIGHT_THEME : DARK_THEME;
const option: echarts.EChartsOption = {
tooltip: {
trigger: 'axis',
textStyle: {
color: TH.FG_COLOR,
},
backgroundColor: TH.BG_COLOR,
formatter: tooltipFormatter,
},
grid: {
top: '10%',
bottom: '5%',
left: '8%',
right: '5%',
containLabel: true,
},
xAxis: {
type: 'time',
// 30 * 3600 * 24 * 1000 milliseconds
minInterval: minInterval,
splitLine: {
show: false,
},
axisLabel: {
color: TH.FG_COLOR,
formatter: {
year: '{yearStyle|{yy}}',
month: '{MMM}',
},
rich: {
yearStyle: {
fontWeight: 'bold',
},
},
},
},
yAxis: [
{
type: 'value',
position: 'left',
axisLabel: {
color: TH.FG_COLOR,
formatter: formatNum,
},
splitLine: {
lineStyle: {
color: TH.SPLIT_LINE_COLOR,
},
},
},
],
dataZoom: [
{
type: 'inside',
start: 0,
end: 100,
minValueSpan: 3600 * 24 * 1000 * 180,
},
],
series: [
{
type: 'bar',
data: data,
itemStyle: {
color: '#ff8061',
},
emphasis: {
focus: 'series',
},
yAxisIndex: 0,
},
{
type: 'line',
symbol: 'none',
lineStyle: {
color: '#ff8061',
},
data: data,
emphasis: {
focus: 'series',
},
yAxisIndex: 0,
},
],
animationEasing: 'elasticOut',
animationDelayUpdate: function (idx: any) {
return idx * 5;
},
};
useEffect(() => {
let chartDOM = divEL.current;
const instance = echarts.init(chartDOM as any);
return () => {
instance.dispose();
};
}, []);
useEffect(() => {
let chartDOM = divEL.current;
const instance = echarts.getInstanceByDom(chartDOM as any);
if (instance) {
judgeInterval(instance, option, timeLength);
instance.setOption(option);
}
}, []);
return <div ref={divEL} style={{ width, height }}></div>;
};
const tooltipFormatter = (params: any) => {
const res = `
${params[0].data[0]}<br/>
${params[0].marker}
<span style="font-weight:bold;">
${numberWithCommas(params[0].data[1])}
</span>
`;
return res;
};
export default ContributorChart;