ChartLineDisk.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. import React, { Component } from "react";
  2. import { Col, Card, CardBody, FormGroup, Label, Input } from "reactstrap";
  3. import FlotChart from "@/components/Charts/Flot.js";
  4. import { getMonthDiskSpace } from "../../actions/disk";
  5. import moment from "moment";
  6. import "moment/locale/id";
  7. class ChartLineDisk extends Component {
  8. constructor(props) {
  9. super(props);
  10. this.state = {
  11. dataChart: this.ChartLine,
  12. selectedMonth: moment().month(), // Current month (0 = January, 11 = December)
  13. selectedYear: moment().year(), // Current year
  14. };
  15. }
  16. // Fetch data when component mounts or when date is changed
  17. componentDidMount = async () => {
  18. moment.locale('id');
  19. this.fetchDiskSpaceData();
  20. };
  21. componentDidUpdate(prevProps, prevState) {
  22. // Fetch data if selected month or year has changed
  23. if (prevState.selectedMonth !== this.state.selectedMonth || prevState.selectedYear !== this.state.selectedYear) {
  24. this.fetchDiskSpaceData();
  25. }
  26. }
  27. // Function to fetch the disk space data
  28. fetchDiskSpaceData = async () => {
  29. const { token } = this.props;
  30. const { selectedMonth, selectedYear } = this.state;
  31. const selectedDate = `${selectedYear}-${(selectedMonth + 1).toString().padStart(2, "0")}`; // Format as YYYY-MM
  32. const dataDisk = await getMonthDiskSpace(token, selectedDate);
  33. const sortedDataDisk = dataDisk.data.sort((a, b) => new Date(a.date) - new Date(b.date));
  34. const chartData = sortedDataDisk.map(item => {
  35. const day = new Date(item.date).getDate();
  36. const used = (item.used / (1024 ** 2)).toFixed(2);
  37. return [day.toString(), used];
  38. });
  39. const updatedChartLine = {
  40. ...this.ChartLine,
  41. data: [
  42. {
  43. label: "Available Disk Space",
  44. color: "#3e3a8e",
  45. data: chartData,
  46. },
  47. ],
  48. };
  49. this.setState({ dataChart: updatedChartLine });
  50. };
  51. handleMonthChange = (e) => {
  52. this.setState({ selectedMonth: parseInt(e.target.value) });
  53. };
  54. handleYearChange = (e) => {
  55. this.setState({ selectedYear: parseInt(e.target.value) });
  56. };
  57. // Chart configuration
  58. ChartLine = {
  59. data: [
  60. {
  61. label: "Available Disk Space",
  62. color: "#3e3a8e",
  63. data: [],
  64. },
  65. ],
  66. options: {
  67. series: {
  68. lines: {
  69. show: true,
  70. fill: 0.01,
  71. },
  72. points: {
  73. show: true,
  74. radius: 4,
  75. },
  76. },
  77. grid: {
  78. borderColor: "#eee",
  79. borderWidth: 1,
  80. hoverable: true,
  81. backgroundColor: "#fcfcfc",
  82. },
  83. tooltip: true,
  84. tooltipOpts: {
  85. content: (label, x, y) => `${x} : ${y} GB`,
  86. },
  87. xaxis: {
  88. tickColor: "#eee",
  89. mode: "categories",
  90. },
  91. yaxis: {
  92. tickColor: "#eee",
  93. },
  94. shadowSize: 0,
  95. },
  96. };
  97. render() {
  98. const { selectedMonth, selectedYear, dataChart } = this.state;
  99. const months = moment.months();
  100. const years = Array.from({ length: 11 }, (_, i) => moment().year() - i);
  101. return (
  102. <Col lg={12}>
  103. <Card className="card-default">
  104. <CardBody>
  105. <FormGroup row>
  106. <Label for="month" sm={1}>Bulan:</Label>
  107. <Col sm={4}>
  108. <Input
  109. type="select"
  110. name="month"
  111. id="month"
  112. value={selectedMonth}
  113. onChange={this.handleMonthChange}
  114. >
  115. {months.map((month, index) => (
  116. <option key={index} value={index}>
  117. {month}
  118. </option>
  119. ))}
  120. </Input>
  121. </Col>
  122. <Col sm={4}>
  123. <Input
  124. type="select"
  125. name="year"
  126. id="year"
  127. value={selectedYear}
  128. onChange={this.handleYearChange}
  129. >
  130. {years.map((year) => (
  131. <option key={year} value={year}>
  132. {year}
  133. </option>
  134. ))}
  135. </Input>
  136. </Col>
  137. </FormGroup>
  138. <FlotChart
  139. options={dataChart.options}
  140. data={dataChart.data}
  141. className="flot-chart"
  142. height="300px"
  143. />
  144. </CardBody>
  145. <div align="center" className="mb-3">
  146. <span>
  147. Data Statistik Penggunaan Hardisk Bulan {months[selectedMonth]} {selectedYear}
  148. </span>
  149. </div>
  150. </Card>
  151. </Col>
  152. );
  153. }
  154. }
  155. export default ChartLineDisk;