Guest User

Untitled

a guest
Feb 24th, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.11 KB | None | 0 0
  1. import React from 'react';
  2. import { Doughnut, Line } from 'react-chartjs-2';
  3. import moment from 'moment';
  4. import cookie from 'react-cookies';
  5.  
  6. class Chart extends React.Component {
  7. constructor(props) {
  8. super(props);
  9. this.calculateData = this.calculateData.bind(this);
  10. this.state = {
  11. chartData: {}
  12. };
  13. }
  14. static defaultProps = {
  15. displayTitle: false,
  16. displayLegend: true,
  17. legendPosition: 'right'
  18. };
  19.  
  20. componentDidMount() {
  21. this.calculateData();
  22. }
  23.  
  24. componentWillReceiveProps(nextProps) {
  25. if (this.props.chartRate != nextProps.chartRate) {
  26. this.calculateData();
  27. }
  28. }
  29.  
  30. years = currentYear => {
  31. let year = [];
  32. for (let i = 0; i <= time; i++) {
  33. year.push(currentYear + i + '');
  34. }
  35. return year;
  36. };
  37.  
  38. gains = (amount, monthly, rate) => {
  39. let gain = [];
  40. for (let i = 0; i <= time; i++) {
  41. gain.push(
  42. amount * (1 + rate) ** (365 * i) + monthly / 365 * (1 + rate) * ((1 + rate) ** (365 * i) - 1) / rate
  43. );
  44. }
  45. //console.log(gain);
  46. return gain;
  47. };
  48.  
  49. calculateData() {
  50.  
  51. let time = parseInt(cookie.load('time'));
  52. let initialInvestment = parseInt(cookie.load('lump'));
  53. let monthlyInvestment = parseInt(cookie.load('monthly'));
  54.  
  55. const yAxis = this.gains(initialInvestment, monthlyInvestment, this.props.rate);
  56.  
  57. setLabel = this.years(
  58. moment()
  59. .utc()
  60. .year()
  61. );
  62.  
  63. this.setState({
  64. chartData: {
  65. labels: this.props.label,
  66. datasets: [
  67. {
  68. label: '',
  69. data: yAxis,
  70. backgroundColor: ['rgba(38, 114, 114, 0.8)', 'rgba(255, 99, 132, 0.6)'],
  71. borderWidth: 0
  72. }
  73. ]
  74. }
  75. });
  76. }
  77.  
  78. render() {
  79. return (
  80. <div className="chart">
  81. <div className="chart__line">
  82. <Line
  83. data={this.state.chartData}
  84. redraw={true}
  85. options={{
  86. title: {
  87. display: this.props.displayTitle,
  88. text: '' + this.props.label,
  89. fontSize: 25
  90. },
  91. legend: {
  92. display: this.props.displayLegend,
  93. position: this.props.legendPosition
  94. },
  95. maintainAspectRatio: false
  96. }}
  97. />
  98. </div>
  99. <div>
  100. <p className="btn btn--large">Create Account</p>
  101. </div>
  102. </div>
  103. );
  104. }
  105. }
  106.  
  107. export default Chart;
Add Comment
Please, Sign In to add comment