import React, { Component } from "react"; import { Col, Card, CardBody, FormGroup, Label, Input } from "reactstrap"; import FlotChart from "@/components/Charts/Flot.js"; import { getMonthDiskSpace } from "../../actions/disk"; import moment from "moment"; import "moment/locale/id"; class ChartLineDisk extends Component { constructor(props) { super(props); this.state = { dataChart: this.ChartLine, selectedMonth: moment().month(), // Current month (0 = January, 11 = December) selectedYear: moment().year(), // Current year }; } // Fetch data when component mounts or when date is changed componentDidMount = async () => { moment.locale('id'); this.fetchDiskSpaceData(); }; componentDidUpdate(prevProps, prevState) { // Fetch data if selected month or year has changed if (prevState.selectedMonth !== this.state.selectedMonth || prevState.selectedYear !== this.state.selectedYear) { this.fetchDiskSpaceData(); } } // Function to fetch the disk space data fetchDiskSpaceData = async () => { const { token } = this.props; const { selectedMonth, selectedYear } = this.state; const selectedDate = `${selectedYear}-${(selectedMonth + 1).toString().padStart(2, "0")}`; // Format as YYYY-MM const dataDisk = await getMonthDiskSpace(token, selectedDate); const sortedDataDisk = dataDisk.data.sort((a, b) => new Date(a.date) - new Date(b.date)); const chartData = sortedDataDisk.map(item => { const day = new Date(item.date).getDate(); const used = (item.used / (1024 ** 2)).toFixed(2); return [day.toString(), used]; }); const updatedChartLine = { ...this.ChartLine, data: [ { label: "Use Disk Space", color: "#3e3a8e", data: chartData, }, ], }; this.setState({ dataChart: updatedChartLine }); }; handleMonthChange = (e) => { this.setState({ selectedMonth: parseInt(e.target.value) }); }; handleYearChange = (e) => { this.setState({ selectedYear: parseInt(e.target.value) }); }; // Chart configuration ChartLine = { data: [ { label: "Use Disk Space", color: "#3e3a8e", data: [], }, ], options: { series: { lines: { show: true, fill: 0.01, }, points: { show: true, radius: 4, }, }, grid: { borderColor: "#eee", borderWidth: 1, hoverable: true, backgroundColor: "#fcfcfc", }, tooltip: true, tooltipOpts: { content: (label, x, y) => `${x} : ${y} GB`, }, xaxis: { tickColor: "#eee", mode: "categories", }, yaxis: { tickColor: "#eee", }, shadowSize: 0, }, }; render() { const { selectedMonth, selectedYear, dataChart } = this.state; const months = moment.months(); const years = Array.from({ length: 11 }, (_, i) => moment().year() - i); return ( {months.map((month, index) => ( ))} {years.map((year) => ( ))}
Data Statistik Penggunaan Hardisk Bulan {months[selectedMonth]} {selectedYear}
); } } export default ChartLineDisk;