Skip to content

Commit

Permalink
new feature - graph
Browse files Browse the repository at this point in the history
  • Loading branch information
cardanians committed Oct 17, 2020
1 parent b50e25d commit 4f55df0
Showing 1 changed file with 76 additions and 0 deletions.
76 changes: 76 additions & 0 deletions src/components/CardRoa.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/* eslint-disable indent */
import React from 'react';
import styled from 'styled-components';

const RoaElement = styled.div`
height: 26px;
text-align: center;
padding: 3px 0;
border: 1px solid #dee2ea;
border-radius: 8px;
color: #2B2C32;
font-size: 14px;
`;

function getColor($value, h = 80) {
const $hue = (1 - $value) * 120;
return `hsl(${$hue}, ${h}%, ${h}%)`
}

function sum(input){

if (toString.call(input) !== "[object Array]")
return false;

var total = 0;
for(var i=0;i<input.length;i++)
{
if(isNaN(input[i])){
continue;
}
total += Number(input[i]);
}
return total;
}

function graph(data) {
let json = JSON.parse(JSON.stringify(data));
let arr = [];

arr = Object.keys(json).map((key => json[key].val));

let avg = sum(arr) / arr.length;

const trend = arr.slice(0, 10).map((val,key) => {

return (
<div key={key}
style={{
width: 2,
display : 'inline-block',
height: Math.min(12,Math.max(2, Math.ceil(12 * val / avg))),
backgroundColor: getColor(1 - Math.min(1,val / avg), 45)
}}
></div>
)
});

return trend;

}

function CardRoa({ roa, data, description }: Props) {


return (
<div>
<RoaElement>
{description}
{parseFloat(roa) === 0 ? 'unknown' : parseFloat(roa).toFixed(2)+'% '}
{graph(data)}
</RoaElement>
</div>
)
}

export default CardRoa;

0 comments on commit 4f55df0

Please sign in to comment.