-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4f4da57
commit 16f1afd
Showing
11 changed files
with
516 additions
and
73 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,81 +1,205 @@ | ||
import React, { useRef, useState, useEffect } from 'react'; | ||
import { select, scaleTime, axisLeft } from 'd3'; | ||
import React, { useRef, useEffect } from 'react'; | ||
import { select, scaleTime, axisLeft, timeYear, timeFormat, min } from 'd3'; | ||
import { DarkLinenPaper } from '../layout/StyledLayoutComponents'; | ||
|
||
export interface Occupation { | ||
id: string; | ||
selected: boolean; | ||
title: string; | ||
category: keyof Category; | ||
tag: Array<keyof Tag>; | ||
start: Date; | ||
end: Date; | ||
} | ||
|
||
export interface LifeEvent { | ||
id: string; | ||
title: string; | ||
date: Date; | ||
} | ||
|
||
interface Category { | ||
Education: boolean; | ||
Work: boolean; | ||
Volunteer: boolean; | ||
} | ||
|
||
interface Tag { | ||
architect: boolean; | ||
landscaper: boolean; | ||
baker: boolean; | ||
coder: boolean; | ||
farmer: boolean; | ||
manager: boolean; | ||
} | ||
const dim = { | ||
width: 400, | ||
height: 800, | ||
marginLeft: 100, | ||
background: '#f6f3f0', | ||
}; | ||
|
||
const TimeLine = () => { | ||
interface Props { | ||
width: number; | ||
height: number; | ||
occupations: Array<Occupation>; | ||
events: Array<LifeEvent>; | ||
background: string; | ||
} | ||
|
||
const TimeLine: React.FC<Props> = ({ | ||
width, | ||
height, | ||
occupations, | ||
events, | ||
background, | ||
}) => { | ||
const svgRef = useRef(null); | ||
const [data] = useState(timelineData); | ||
|
||
const lookupInRange = ( | ||
corner: Date, | ||
allValues: Array<Occupation> | ||
): boolean => { | ||
let result: boolean = false; | ||
allValues.forEach((i) => { | ||
if (corner > i.start && corner < i.end) { | ||
result = true; | ||
return true; | ||
} | ||
}); | ||
return result; | ||
}; | ||
|
||
const categoryColor = (category: keyof Category) => { | ||
switch (category) { | ||
case 'Work': | ||
return '#CBE0F2'; | ||
case 'Education': | ||
return '#EECEC9'; | ||
case 'Volunteer': | ||
return '#F0E2CE'; | ||
} | ||
}; | ||
|
||
const orderInRange = ( | ||
value: Occupation, | ||
backArg: any, | ||
midArg: any, | ||
frontArg: any | ||
): boolean | number | string => { | ||
if ( | ||
lookupInRange(value.end, occupations) && | ||
lookupInRange(value.start, occupations) | ||
) { | ||
return backArg; | ||
} else if (lookupInRange(value.start, occupations)) { | ||
return midArg; | ||
} else { | ||
return frontArg; | ||
} | ||
}; | ||
|
||
useEffect(() => { | ||
const svg = select(svgRef.current); | ||
|
||
const yScale = scaleTime() | ||
.domain([new Date('2018-01-01'), new Date('2020-07-22')]) | ||
.range([dim.height, 0]); | ||
.domain([ | ||
min(occupations, (d) => d.start) || new Date('1988-04-18'), | ||
new Date(), | ||
]) | ||
.range([height, 0]); | ||
|
||
const yAxis = axisLeft<any>(yScale); | ||
const yAxis = axisLeft<any>(yScale) | ||
.ticks(timeYear, 1) | ||
.tickFormat(timeFormat('%Y')); | ||
|
||
const Axis = svg.append('g'); | ||
Axis.style('transform', `translateX(${dim.marginLeft}px)`).call(yAxis); | ||
|
||
const Boxes = svg.append('g'); | ||
Boxes.selectAll('rect') | ||
.data(data) | ||
.join('rect') | ||
.attr('x', 100) | ||
const BackBoxes = svg | ||
.append('g') | ||
.selectAll('rect') | ||
.data(occupations.filter((d) => orderInRange(d, true, false, false))); | ||
BackBoxes.join('rect') | ||
.transition() | ||
.attr('x', 115) | ||
.attr('y', (value) => yScale(+value.end)) | ||
.attr('width', 35) | ||
.attr('height', (value) => yScale(+value.start) - yScale(+value.end)) | ||
.attr('fill', (value) => | ||
value.selected ? categoryColor(value.category) : 'none' | ||
) | ||
.attr('stroke', (value) => | ||
value.selected ? background : categoryColor(value.category) | ||
) | ||
.attr('stroke-width', '2px'); | ||
|
||
const MidBoxes = svg | ||
.append('g') | ||
.selectAll('rect') | ||
.data(occupations.filter((d) => orderInRange(d, false, true, false))); | ||
MidBoxes.join('rect') | ||
.transition() | ||
.attr('x', 110) | ||
.attr('y', (value) => yScale(+value.end)) | ||
.attr('width', 30) | ||
.attr('height', (value) => yScale(+value.start) - yScale(+value.end)) | ||
.attr('fill', (value) => | ||
value.selected ? categoryColor(value.category) : 'none' | ||
) | ||
.attr('stroke', (value) => | ||
value.selected ? background : categoryColor(value.category) | ||
) | ||
.attr('stroke-width', '2px'); | ||
|
||
const FrontBoxes = svg | ||
.append('g') | ||
.selectAll('rect') | ||
.data(occupations.filter((d) => orderInRange(d, false, false, true))); | ||
FrontBoxes.join('rect') | ||
.transition() | ||
.attr('x', 105) | ||
.attr('y', (value) => yScale(+value.end)) | ||
.attr('width', (_, i) => i * 10 + 25) | ||
.attr('width', 25) | ||
.attr('height', (value) => yScale(+value.start) - yScale(+value.end)) | ||
.attr('fill', 'teal') | ||
.attr('stroke', 'lightgrey'); | ||
}, [data]); | ||
.attr('fill', (value) => | ||
value.selected ? categoryColor(value.category) : 'none' | ||
) | ||
.attr('stroke', (value) => | ||
value.selected ? background : categoryColor(value.category) | ||
) | ||
.attr('stroke-width', '2px'); | ||
|
||
const LifeEvents = svg.append('g').selectAll('line').data(events); | ||
LifeEvents.join('line') | ||
.transition() | ||
.attr('x1', 25) | ||
.attr('x2', width - 25) | ||
.attr('y1', (value) => yScale(value.date)) | ||
.attr('y2', (value) => yScale(value.date)) | ||
.attr('stroke', 'black') | ||
.attr('stroke-dasharray', '5,10,5'); | ||
|
||
LifeEvents.join('text') | ||
.transition() | ||
.attr('x', 5) | ||
.attr('y', (value) => yScale(value.date) - 5) | ||
.text((value) => value.title) | ||
.attr('font-family', 'Josefin Sans, serif') | ||
.attr('font-size', '0.6em') | ||
.attr('fill', DarkLinenPaper); | ||
}, [occupations]); | ||
|
||
return ( | ||
<div> | ||
<svg | ||
style={{ background: 'lightgrey', overflow: 'visible' }} | ||
width={dim.width} | ||
height={dim.height} | ||
style={{ background, overflow: 'visible' }} | ||
width={width} | ||
height={height} | ||
ref={svgRef} | ||
/> | ||
</div> | ||
); | ||
}; | ||
|
||
export default TimeLine; | ||
|
||
const timelineData = [ | ||
{ | ||
id: '001', | ||
title: 'number 1', | ||
category: 'Code', | ||
start: new Date('2020-01-01'), | ||
end: new Date('2020-03-23'), | ||
}, | ||
{ | ||
id: '002', | ||
title: 'number 2', | ||
category: 'Design', | ||
start: new Date('2018-06-02'), | ||
end: new Date('2019-01-01'), | ||
}, | ||
{ | ||
id: '003', | ||
title: 'number 3', | ||
category: 'Other', | ||
start: new Date('2019-04-29'), | ||
end: new Date('2019-12-31'), | ||
}, | ||
{ | ||
id: '004', | ||
title: 'number 4', | ||
category: 'Other', | ||
start: new Date('2019-02-29'), | ||
end: new Date('2019-5-31'), | ||
}, | ||
]; |
Oops, something went wrong.