Advertisement
Guest User

SimpleCalculator

a guest
Jun 14th, 2022
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.28 KB | None | 0 0
  1. //Here is SimpleCalculator.js
  2.  
  3. import React, {useState} from 'react';
  4. import {SimpleCalculatorControls} from './SimpleCalculatorControls';
  5. import {handleInterest} from './handleInterest';
  6. import {SimpleGraph} from './SimpleGraph';
  7. import {HeaderFigures} from './HeaderFigures';
  8.  
  9. export const SimpleCalculator = () => {
  10. const [deposit, setDeposit] = useState(100000);
  11. const [interestRate, setInterestRate] = useState(5);
  12. const [duration, setDuration] = useState(20);
  13.  
  14. const yearlyBalance = handleInterest(deposit, interestRate, duration);
  15.  
  16.  
  17.  
  18. return(
  19. <>
  20. <HeaderFigures
  21. deposit={deposit}
  22. interestRate={interestRate}
  23. duration={duration}
  24. />
  25. <SimpleCalculatorControls
  26. deposit={deposit}
  27. setDeposit={setDeposit}
  28. interestRate={interestRate}
  29. setInterestRate={setInterestRate}
  30. duration={duration}
  31. setDuration={setDuration}
  32. />
  33. <SimpleGraph
  34. yearlyBalance={yearlyBalance}
  35. />
  36.  
  37. </>
  38. )
  39. }
  40.  
  41. // Here is SimpleGraph.js
  42.  
  43.  
  44. import React, {useState, useEffect} from 'react';
  45. import { Bar } from 'react-chartjs-2';
  46. import { Line } from 'react-chartjs-2';
  47. import { PointElement } from 'chart.js';
  48. import {
  49. Chart as ChartJS,
  50. CategoryScale,
  51. LinearScale,
  52. BarElement,
  53. LineElement,
  54. Title,
  55. Tooltip,
  56. Legend,
  57. } from 'chart.js';
  58.  
  59.  
  60. ChartJS.register(
  61. CategoryScale,
  62. LinearScale,
  63. BarElement,
  64. LineElement,
  65. PointElement,
  66. Title,
  67. Tooltip,
  68. Legend
  69. );
  70.  
  71. export function SimpleGraph(props) {
  72. const [graphData, setGraphData] = useState ({
  73. datasets: [],
  74. });
  75.  
  76. const [graphOptions, setGraphOptions] = useState ({});
  77.  
  78. useEffect(() => {
  79. setGraphData ({
  80. labels: props.yearlyBalance.year,
  81. datasets: [
  82. {
  83. label: "Balance",
  84. data: props.yearlyBalance.balance,
  85. borderColor: "rgb(53, 162, 235)",
  86. backgroundColor: "rgb(53, 162, 235, 0.4)",
  87. },
  88. ]
  89. });
  90. setGraphOptions ({
  91. responsive: true,
  92. plugins: {
  93. legend: {
  94. postion: "top"
  95. },
  96. title: {
  97. display: true,
  98. text: "Duration",
  99. }
  100. }
  101. })
  102.  
  103. }, [])
  104.  
  105.  
  106.  
  107. return(
  108.  
  109. <div className="SimpleGraph">
  110. <Bar
  111. data={graphData}
  112. options={graphOptions}
  113. />
  114. </div>
  115. )
  116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement