| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170 |
- 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: "Available 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: "Available 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 (
- <Col lg={12}>
- <Card className="card-default">
- <CardBody>
- <FormGroup row>
- <Label for="month" sm={1}>Bulan:</Label>
- <Col sm={4}>
- <Input
- type="select"
- name="month"
- id="month"
- value={selectedMonth}
- onChange={this.handleMonthChange}
- >
- {months.map((month, index) => (
- <option key={index} value={index}>
- {month}
- </option>
- ))}
- </Input>
- </Col>
- <Col sm={4}>
- <Input
- type="select"
- name="year"
- id="year"
- value={selectedYear}
- onChange={this.handleYearChange}
- >
- {years.map((year) => (
- <option key={year} value={year}>
- {year}
- </option>
- ))}
- </Input>
- </Col>
- </FormGroup>
- <FlotChart
- options={dataChart.options}
- data={dataChart.data}
- className="flot-chart"
- height="300px"
- />
- </CardBody>
- <div align="center" className="mb-3">
- <span>
- Data Statistik Penggunaan Hardisk Bulan {months[selectedMonth]} {selectedYear}
- </span>
- </div>
- </Card>
- </Col>
- );
- }
- }
- export default ChartLineDisk;
|