Chore: line chart adjustments
This commit is contained in:
parent
fca38a5636
commit
d3cd5f08f1
2 changed files with 56 additions and 7 deletions
|
|
@ -28,7 +28,18 @@ const reducer = (acc, tx) => {
|
||||||
return { ...tx, profit: currentProfit + transactionProfit(tx) }
|
return { ...tx, profit: currentProfit + transactionProfit(tx) }
|
||||||
}
|
}
|
||||||
|
|
||||||
const RefLineChart = ({ data: realData, timeFrame }) => {
|
const timeFrameMS = {
|
||||||
|
Day: 24 * 3600 * 1000,
|
||||||
|
Week: 7 * 24 * 3600 * 1000,
|
||||||
|
Month: 30 * 24 * 3600 * 1000
|
||||||
|
}
|
||||||
|
|
||||||
|
const RefLineChart = ({
|
||||||
|
data: realData,
|
||||||
|
previousTimeData,
|
||||||
|
previousProfit,
|
||||||
|
timeFrame
|
||||||
|
}) => {
|
||||||
const svgRef = useRef()
|
const svgRef = useRef()
|
||||||
|
|
||||||
const drawGraph = useCallback(() => {
|
const drawGraph = useCallback(() => {
|
||||||
|
|
@ -43,14 +54,49 @@ const RefLineChart = ({ data: realData, timeFrame }) => {
|
||||||
|
|
||||||
const aggregatedTX = R.values(R.reduceBy(reducer, [], method, realData))
|
const aggregatedTX = R.values(R.reduceBy(reducer, [], method, realData))
|
||||||
// if no point exists, then return 2 points at y = 0
|
// if no point exists, then return 2 points at y = 0
|
||||||
if (aggregatedTX.length === 0) {
|
if (!aggregatedTX.length && !previousTimeData.length) {
|
||||||
const mockPoint1 = { created: new Date().toISOString(), profit: 0 }
|
const mockPoint1 = { created: new Date().toISOString(), profit: 0 }
|
||||||
const mockPoint2 = mockPoint(mockPoint1)
|
const mockPoint2 = mockPoint(mockPoint1)
|
||||||
return [[mockPoint1, mockPoint2], true]
|
return [[mockPoint1, mockPoint2], true]
|
||||||
}
|
}
|
||||||
|
// if this time period has no txs, but previous time period has, then % change is -100%
|
||||||
|
if (!aggregatedTX.length && previousTimeData.length) {
|
||||||
|
const mockPoint1 = {
|
||||||
|
created: new Date().toISOString(),
|
||||||
|
profit: 0
|
||||||
|
}
|
||||||
|
const mockPoint2 = {
|
||||||
|
created: new Date(Date.now() - 1).toISOString(),
|
||||||
|
profit: 1
|
||||||
|
}
|
||||||
|
return [[mockPoint1, mockPoint2], false]
|
||||||
|
}
|
||||||
|
// if this time period has txs, but previous doesn't, then % change is +100%
|
||||||
|
if (aggregatedTX.length && !previousTimeData.length) {
|
||||||
|
const mockPoint1 = {
|
||||||
|
created: new Date().toISOString(),
|
||||||
|
profit: 1
|
||||||
|
}
|
||||||
|
const mockPoint2 = {
|
||||||
|
created: new Date(Date.now() - timeFrameMS[timeFrame]).toISOString(),
|
||||||
|
profit: 0
|
||||||
|
}
|
||||||
|
return [[mockPoint1, mockPoint2], false]
|
||||||
|
}
|
||||||
// if only one point exists, create point on the left - otherwise the line won't be drawn
|
// if only one point exists, create point on the left - otherwise the line won't be drawn
|
||||||
if (aggregatedTX.length === 1) {
|
if (aggregatedTX.length === 1) {
|
||||||
return [R.append(mockPoint(aggregatedTX[0]), aggregatedTX), false]
|
return [
|
||||||
|
R.append(
|
||||||
|
{
|
||||||
|
created: new Date(
|
||||||
|
Date.now() - timeFrameMS[timeFrame]
|
||||||
|
).toISOString(),
|
||||||
|
profit: previousProfit
|
||||||
|
},
|
||||||
|
aggregatedTX
|
||||||
|
),
|
||||||
|
false
|
||||||
|
]
|
||||||
}
|
}
|
||||||
// the boolean value is for zeroProfit. It makes the line render at y = 0 instead of y = 50% of container height
|
// the boolean value is for zeroProfit. It makes the line render at y = 0 instead of y = 50% of container height
|
||||||
return [aggregatedTX, false]
|
return [aggregatedTX, false]
|
||||||
|
|
@ -148,7 +194,7 @@ const RefLineChart = ({ data: realData, timeFrame }) => {
|
||||||
.attr('stroke-width', '2')
|
.attr('stroke-width', '2')
|
||||||
.attr('stroke-linejoin', 'round')
|
.attr('stroke-linejoin', 'round')
|
||||||
.attr('stroke', primaryColor)
|
.attr('stroke', primaryColor)
|
||||||
}, [realData, timeFrame])
|
}, [realData, timeFrame, previousTimeData, previousProfit])
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
// first we clear old chart DOM elements on component update
|
// first we clear old chart DOM elements on component update
|
||||||
|
|
|
||||||
|
|
@ -157,8 +157,6 @@ const SystemPerformance = () => {
|
||||||
|
|
||||||
const percentChange = getPercentChange()
|
const percentChange = getPercentChange()
|
||||||
|
|
||||||
console.log(percentChange)
|
|
||||||
|
|
||||||
const percentageClasses = {
|
const percentageClasses = {
|
||||||
[classes.percentDown]: percentChange < 0,
|
[classes.percentDown]: percentChange < 0,
|
||||||
[classes.percentUp]: percentChange > 0,
|
[classes.percentUp]: percentChange > 0,
|
||||||
|
|
@ -216,7 +214,12 @@ const SystemPerformance = () => {
|
||||||
{`${percentChange}%`}
|
{`${percentChange}%`}
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<LineChart timeFrame={selectedRange} data={transactionsToShow} />
|
<LineChart
|
||||||
|
timeFrame={selectedRange}
|
||||||
|
data={transactionsToShow}
|
||||||
|
previousTimeData={transactionsLastTimePeriod}
|
||||||
|
previousProfit={getProfit(transactionsLastTimePeriod)}
|
||||||
|
/>
|
||||||
</Grid>
|
</Grid>
|
||||||
<Grid item xs={4}>
|
<Grid item xs={4}>
|
||||||
<Grid container>
|
<Grid container>
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue