ChartData.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. import React, { Component } from "react";
  2. import ContentWrapper from "@/components/Layout/ContentWrapper";
  3. import { Container, Row, Col, Card, CardHeader, CardBody } from "reactstrap";
  4. import FlotChart from "@/components/Charts/Flot.js";
  5. import { connect } from "react-redux";
  6. import { getPengunjung } from "@/actions/pengunjung";
  7. class ChartData extends Component {
  8. constructor(props) {
  9. super(props);
  10. this.state = {
  11. dataPengunjung: [],
  12. dataChart: this.ChartSpline,
  13. };
  14. }
  15. async componentDidMount() {
  16. const { token } = this.props;
  17. const dataPengunjung = await getPengunjung(token);
  18. this.setState({ dataPengunjung: dataPengunjung.data.map((e) => [this.convertMonth(e._id.bulan), e.jumlah_pengunjung]) });
  19. this.setState((prevState) => ({ dataChart: { ...prevState.dataChart, data: [{ ...prevState.dataChart.data[0], data: prevState.dataPengunjung }] } }));
  20. }
  21. ChartSpline = {
  22. data: [
  23. {
  24. // label: "Uniques",
  25. color: "#3e3a8e",
  26. data: [],
  27. },
  28. ],
  29. options: {
  30. series: {
  31. lines: {
  32. show: false,
  33. },
  34. points: {
  35. show: true,
  36. radius: 4,
  37. },
  38. splines: {
  39. show: true,
  40. tension: 0.4,
  41. lineWidth: 1,
  42. fill: 0.5,
  43. },
  44. },
  45. grid: {
  46. borderColor: "#eee",
  47. borderWidth: 1,
  48. hoverable: true,
  49. backgroundColor: "#fcfcfc",
  50. },
  51. tooltip: true,
  52. tooltipOpts: {
  53. content: (label, x, y) => x + " : " + y,
  54. },
  55. xaxis: {
  56. tickColor: "#fcfcfc",
  57. mode: "categories",
  58. },
  59. yaxis: {
  60. min: 0,
  61. max: 150, // optional: use it for a clear represetation
  62. tickColor: "#eee",
  63. //position: 'right' or 'left',
  64. tickFormatter: (v) => v /* + ' visitors'*/,
  65. },
  66. shadowSize: 0,
  67. },
  68. };
  69. convertMonth = (int) => {
  70. switch (int) {
  71. case 1:
  72. return "Januari";
  73. break;
  74. case 2:
  75. return "Februari";
  76. break;
  77. case 3:
  78. return "Maret";
  79. break;
  80. case 4:
  81. return "April";
  82. break;
  83. case 5:
  84. return "Mei";
  85. break;
  86. case 6:
  87. return "Juni";
  88. break;
  89. case 7:
  90. return "Juli";
  91. break;
  92. case 8:
  93. return "Agustus";
  94. break;
  95. case 9:
  96. return "September";
  97. break;
  98. case 10:
  99. return "Oktober";
  100. break;
  101. case 11:
  102. return "November";
  103. break;
  104. case 12:
  105. return "Desember";
  106. break;
  107. default:
  108. break;
  109. }
  110. };
  111. render() {
  112. return (
  113. <Col lg={12}>
  114. <Card className="card-default">
  115. <CardBody>
  116. <FlotChart options={this.ChartSpline.options} data={this.state.dataChart.data} className="flot-chart" height="250px" />
  117. </CardBody>
  118. <div align="center">
  119. <span>Data Statistik Pengunjung Tahun {new Date().getFullYear()}</span>
  120. </div>
  121. </Card>
  122. </Col>
  123. );
  124. }
  125. }
  126. const mapStateToProps = (state) => ({ token: state.token });
  127. export default connect(mapStateToProps)(ChartData);