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
|
import React, { useMemo } from 'react';
import { fetchData } from '../api/traffic';
import useLineChart from '../hooks/useLineChart';
import { connect } from './StateProvider';
import { getClashAPIConfig, getSelectedChartStyleIndex } from '../store/app';
import {
chartJSResource,
commonDataSetProps,
chartStyles
} from '../misc/chart';
const chartWrapperStyle = {
// make chartjs chart responsive
position: 'relative',
maxWidth: 1000
};
const mapState = s => ({
apiConfig: getClashAPIConfig(s),
selectedChartStyleIndex: getSelectedChartStyleIndex(s)
});
export default connect(mapState)(TrafficChart);
function TrafficChart({ apiConfig, selectedChartStyleIndex }) {
const Chart = chartJSResource.read();
const { hostname, port, secret } = apiConfig;
const traffic = fetchData({ hostname, port, secret });
const data = useMemo(
() => ({
labels: traffic.labels,
datasets: [
{
...commonDataSetProps,
...chartStyles[selectedChartStyleIndex].up,
label: 'Up',
data: traffic.up
},
{
...commonDataSetProps,
...chartStyles[selectedChartStyleIndex].down,
label: 'Down',
data: traffic.down
}
]
}),
[traffic, selectedChartStyleIndex]
);
useLineChart(Chart, 'trafficChart', data, traffic);
return (
<div style={chartWrapperStyle}>
<canvas id="trafficChart" />
</div>
);
}
|