ChartData.js 2.8 KB

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