-
-
Notifications
You must be signed in to change notification settings - Fork 108
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #52 from MayGo/add-line-charts
Add line charts
- Loading branch information
Showing
14 changed files
with
418 additions
and
185 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import moment from 'moment'; | ||
import * as React from 'react'; | ||
import { TrackItemService } from './services/TrackItemService'; | ||
import { | ||
summariseLog, | ||
summariseOnline, | ||
summariseTimeOnline, | ||
} from './components/SummaryCalendar/SummaryCalendar.util'; | ||
|
||
export const SummaryContext = React.createContext<any>({}); | ||
|
||
export const SummaryProvider = ({ children }) => { | ||
const [isLoading, setIsLoading] = React.useState<any>(false); | ||
const [selectedDate, setSelectedDate] = React.useState<any>(moment()); | ||
const [selectedMode, setSelectedMode] = React.useState<any>('month'); | ||
|
||
const [logSummary, setLogSummary] = React.useState<any>([]); | ||
const [onlineSummary, setOnlineSummary] = React.useState<any>([]); | ||
const [onlineTimesSummary, setOnlineTimesSummary] = React.useState<any>([]); | ||
|
||
const loadData = React.useCallback(async () => { | ||
setIsLoading(true); | ||
const beginDate = moment(selectedDate).startOf(selectedMode); | ||
const endDate = moment(selectedDate).endOf(selectedMode); | ||
|
||
TrackItemService.findAllItems(beginDate, endDate).then( | ||
({ appItems, statusItems, logItems }) => { | ||
setLogSummary(summariseLog(logItems, selectedMode)); | ||
setOnlineSummary(summariseOnline(statusItems, selectedMode)); | ||
setOnlineTimesSummary(summariseTimeOnline(statusItems, selectedMode)); | ||
setIsLoading(false); | ||
}, | ||
); | ||
}, [selectedDate, selectedMode]); | ||
|
||
const defaultContext = { | ||
selectedDate, | ||
setSelectedDate, | ||
selectedMode, | ||
setSelectedMode, | ||
logSummary, | ||
onlineSummary, | ||
onlineTimesSummary, | ||
isLoading, | ||
}; | ||
|
||
React.useEffect(() => { | ||
loadData(); | ||
}, [selectedDate, selectedMode]); // eslint-disable-line react-hooks/exhaustive-deps | ||
|
||
return <SummaryContext.Provider value={defaultContext}>{children}</SummaryContext.Provider>; | ||
}; |
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 |
---|---|---|
@@ -0,0 +1,77 @@ | ||
import * as React from 'react'; | ||
import moment from 'moment'; | ||
import { VictoryBar, VictoryChart, VictoryAxis, VictoryTooltip } from 'victory'; | ||
import { convertDate, TIME_FORMAT, COLORS } from '../../constants'; | ||
import { chartTheme } from '../Timeline/ChartTheme'; | ||
import useWindowSize from '@rehooks/window-size'; | ||
import { SummaryContext } from '../../SummaryContext'; | ||
import { | ||
addToTimeDuration, | ||
formatToTimeEveryOther, | ||
formatToDay, | ||
toTimeDuration, | ||
} from './LineChart.util'; | ||
import { diffAndFormatShort } from '../../utils'; | ||
|
||
const scale = { x: 'time', y: 'time' }; | ||
const padding = { left: 50, top: 0, bottom: 20, right: 10 }; | ||
const domainPadding = { y: 10, x: 10 }; | ||
const labelComponent = () => ( | ||
<VictoryTooltip | ||
style={chartTheme.tooltip.style} | ||
cornerRadius={chartTheme.tooltip.cornerRadius} | ||
pointerLength={chartTheme.tooltip.pointerLength} | ||
flyoutStyle={chartTheme.tooltip.flyoutStyle} | ||
renderInPortal | ||
horizontal={false} | ||
/> | ||
); | ||
export const LineChart = () => { | ||
const { innerWidth: chartWidth } = useWindowSize(); | ||
const { onlineTimesSummary } = React.useContext(SummaryContext); | ||
|
||
return ( | ||
<VictoryChart | ||
theme={chartTheme} | ||
scale={scale} | ||
width={chartWidth} | ||
height={500} | ||
domainPadding={domainPadding} | ||
padding={padding} | ||
horizontal | ||
> | ||
<VictoryAxis | ||
orientation="bottom" | ||
tickCount={24} | ||
tickFormat={formatToTimeEveryOther} | ||
dependentAxis | ||
/> | ||
<VictoryAxis orientation="left" name="time-axis" tickFormat={formatToDay} /> | ||
<VictoryBar | ||
y={d => toTimeDuration(convertDate(d.beginDate), convertDate(d.beginDate))} | ||
y0={d => toTimeDuration(convertDate(d.beginDate), convertDate(d.endDate))} | ||
x={d => convertDate(d.beginDate).startOf('day')} | ||
barWidth={10} | ||
data={onlineTimesSummary} | ||
labelComponent={labelComponent()} | ||
labels={d => | ||
`Start time: ${convertDate(d.beginDate).format( | ||
TIME_FORMAT, | ||
)}\r\nEnd time: ${convertDate(d.endDate).format( | ||
TIME_FORMAT, | ||
)}\r\nDuration: ${diffAndFormatShort(d.beginDate, d.endDate)}` | ||
} | ||
/> | ||
<VictoryBar | ||
y={d => toTimeDuration(convertDate(d.beginDate), convertDate(d.beginDate))} | ||
y0={d => addToTimeDuration(convertDate(d.beginDate), d.online)} | ||
x={d => convertDate(d.beginDate).startOf('day')} | ||
barWidth={10} | ||
style={{ data: { fill: COLORS.green } }} | ||
data={onlineTimesSummary} | ||
labelComponent={labelComponent()} | ||
labels={d => `Worked: ${moment.duration(d.online).format()}`} | ||
/> | ||
</VictoryChart> | ||
); | ||
}; |
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 |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import moment from 'moment'; | ||
import { convertDate } from '../../constants'; | ||
|
||
export const generateTickValues = (date, ticks, unit, startOf) => { | ||
const day = convertDate(date).startOf(startOf); | ||
const dates = [...Array(ticks)].map((__, i) => { | ||
return day.clone().add(i, unit); | ||
}); | ||
|
||
return dates; | ||
}; | ||
|
||
export const toTimeDuration = (from, to) => { | ||
const start = moment(from).startOf('day'); | ||
|
||
return moment(moment(to).diff(start)); | ||
}; | ||
export const addToTimeDuration = (from, duration) => { | ||
const start = moment(from).startOf('day'); | ||
|
||
return moment(moment(from).diff(start) + duration); | ||
}; | ||
|
||
export const isOddHour = date => moment(date).get('hour') % 2; | ||
|
||
export const formatToTime = t => moment.utc(t).format('HH:mm'); | ||
|
||
export const formatToTimeEveryOther = t => { | ||
const hour = moment(t).startOf('hour'); | ||
return formatToTime(hour); | ||
}; | ||
|
||
export const formatToDay = t => moment(t).format('DD'); | ||
|
||
export const timeTickValues = t => { | ||
const ticks = 36; | ||
const day = moment(); | ||
const dates = [...Array(ticks)].map((__, i) => { | ||
return toTimeDuration(day, day.clone().add(i, 'hour')); | ||
}); | ||
|
||
return dates; | ||
}; | ||
|
||
export const dayTickValues = t => generateTickValues(t, 31, 'day', 'month'); |
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 |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import styled from 'styled-components'; | ||
|
||
export const Heading = styled.h3` | ||
text-align: center; | ||
color: rgba(0, 0, 0, 0.65); | ||
`; |
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
Oops, something went wrong.