-
-
Notifications
You must be signed in to change notification settings - Fork 280
/
usage_chart.jsx
60 lines (58 loc) · 1.51 KB
/
usage_chart.jsx
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
import { charts } from "@site/src/constants";
import {
Chart as ChartJS,
CategoryScale,
LinearScale,
BarElement,
TimeScale,
Title,
Tooltip,
Legend,
} from "chart.js";
import "chartjs-adapter-moment";
import moment from "moment";
import React, { useEffect, useState } from "react";
import { Bar } from "react-chartjs-2";
import styles from "./styles.module.css";
ChartJS.register(
CategoryScale,
LinearScale,
BarElement,
TimeScale,
Title,
Tooltip,
Legend
);
export default function UsageChart({ backgroundColor, onLoad, title, url }) {
const [state, setState] = useState({ rates: {}, repos: {}, total: 0 });
useEffect(() => {
void (async () => {
try {
const fetched = await (await fetch(url)).json();
setState((prev) => ({ ...prev, ...fetched }));
} catch (error) {
console.warn("error loading usage chart", error);
}
onLoad?.();
})();
}, []);
const datasets = [{ backgroundColor, data: [] }];
const labels = [];
Object.keys(state.rates).forEach((rate) => {
datasets[0].data.push(state.rates[rate]);
labels.push(moment(rate).local());
});
return (
<div className="card margin-top--xs">
<div className="card__header">
<h3>
{`${title} `}
<span className="badge badge--secondary">{state.total}</span>
</h3>
</div>
<div className={`card__body ${styles.status_dashboard_graph}`}>
<Bar data={{ labels, datasets }} options={charts.usage.options} />
</div>
</div>
);
}