Skip to content

Commit

Permalink
fix(calendar): update TimeRange component for consistency
Browse files Browse the repository at this point in the history
- Update typings for props to be correct
- Adjusted defaults to be a little better
- Adjusted data to be same as Calendar component
  • Loading branch information
wyze committed Jun 10, 2021
1 parent 3435784 commit 23d569c
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 64 deletions.
21 changes: 13 additions & 8 deletions packages/calendar/src/TimeRange.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React from 'react'
import React, { useMemo } from 'react'

import { Container, SvgWrapper, useValueFormatter, useTheme, useDimensions } from '@nivo/core'
import { BoxLegendSvg } from '@nivo/legends'
Expand All @@ -24,7 +24,7 @@ const InnerTimeRange = ({
square = timeRangeDefaultProps.square,
colors = timeRangeDefaultProps.colors,
colorScale,
data,
data: _data,
direction = timeRangeDefaultProps.direction,
minValue = timeRangeDefaultProps.minValue,
maxValue = timeRangeDefaultProps.maxValue,
Expand All @@ -35,13 +35,12 @@ const InnerTimeRange = ({
monthLegendOffset = timeRangeDefaultProps.monthLegendOffset,
monthLegendPosition = timeRangeDefaultProps.monthLegendPosition,

weekdayLegendsOffset = timeRangeDefaultProps.weekdayLegendsOffset,
weekdayLegendOffset = timeRangeDefaultProps.weekdayLegendOffset,

dayBorderColor = timeRangeDefaultProps.dayBorderColor,
dayBorderWidth = timeRangeDefaultProps.dayBorderWidth,
daySpacing = timeRangeDefaultProps.daySpacing,
dayRadius = timeRangeDefaultProps.dayRadius,
daysInRange = timeRangeDefaultProps.daysInRange,

isInteractive = timeRangeDefaultProps.isInteractive,
tooltip = timeRangeDefaultProps.tooltip,
Expand All @@ -59,12 +58,20 @@ const InnerTimeRange = ({
partialMargin
)

const data = useMemo(
() =>
_data
.map(data => ({ ...data, date: new Date(`${data.day}T00:00:00`) }))
.sort((left, right) => left.day.localeCompare(right.day)),
[_data]
)

const theme = useTheme()
const colorScaleFn = useColorScale({ data, minValue, maxValue, colors, colorScale })

const { cellHeight, cellWidth } = computeCellSize({
square,
offset: weekdayLegendsOffset,
offset: weekdayLegendOffset,
totalDays: data.length + data[0].date.getDay(),
width: innerWidth,
height: innerHeight,
Expand All @@ -73,8 +80,7 @@ const InnerTimeRange = ({
})

const days = computeCellPositions({
offset: weekdayLegendsOffset,
daysInRange,
offset: weekdayLegendOffset,
colorScale: colorScaleFn,
cellHeight,
cellWidth,
Expand All @@ -91,7 +97,6 @@ const InnerTimeRange = ({
cellHeight,
cellWidth,
days,
daysInRange,
}).months
)

Expand Down
17 changes: 8 additions & 9 deletions packages/calendar/src/compute/timeRange.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import { timeWeek } from 'd3-time'
import { timeRangeDefaultProps } from '../props'

// Interfaces
interface ComputeBaseProps {
daysInRange: number
direction: 'horizontal' | 'vertical'
}

Expand Down Expand Up @@ -90,8 +88,8 @@ export const computeCellSize = ({
totalDays,
width,
height,
}: Omit<ComputeCellSize, 'daysInRange'>) => {
const { daysInRange } = timeRangeDefaultProps
}: ComputeCellSize) => {
const daysInRange = 7
let rows
let columns
let widthRest = width
Expand Down Expand Up @@ -184,6 +182,8 @@ export const computeCellPositions = ({
year,
date,
color: colorScale(dateValue.value),
width: cellWidth,
height: cellHeight,
}
})

Expand Down Expand Up @@ -213,15 +213,14 @@ export const computeWeekdays = ({
return ticks.map(day => ({
value: arrayOfWeekdays[day],
rotation: direction === 'horizontal' ? 0 : -90,
y: direction === 'horizontal' ? sizes.height * (day + 1) - daySpacing / 2 : 0,
x: direction === 'horizontal' ? 0 : sizes.width * (day + 1) - daySpacing / 2,
y: direction === 'horizontal' ? sizes.height * (day + 1) - sizes.height / 3 : 0,
x: direction === 'horizontal' ? 0 : sizes.width * (day + 1) - sizes.width / 3,
}))
}

export const computeMonthLegends = ({
direction,
daySpacing,
daysInRange,
days,
cellHeight,
cellWidth,
Expand All @@ -245,12 +244,12 @@ export const computeMonthLegends = ({

if (direction === 'horizontal') {
bbox.x = day.coordinates.x - daySpacing
bbox.height = daysInRange * cellHeight + daySpacing
bbox.height = cellHeight + daySpacing
bbox.width = cellWidth + daySpacing * 2
} else {
bbox.y = day.coordinates.y - daySpacing
bbox.height = cellHeight + daySpacing * 2
bbox.width = daysInRange * cellWidth + daySpacing * 2
bbox.width = cellWidth + daySpacing * 2
}

acc.months[key] = {
Expand Down
6 changes: 1 addition & 5 deletions packages/calendar/src/props.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,8 @@ export const calendarCanvasDefaultProps = {

export const timeRangeDefaultProps = {
...calendarDefaultProps,
dayBorderWidth: 0,
dayBorderColor: '#fff',
dayRadius: 0,
daySpacing: 10,
daysInRange: 7,
monthLegendOffset: 0,
square: true,
weekdayLegendsOffset: 65,
weekdayLegendOffset: 75,
} as const
32 changes: 19 additions & 13 deletions packages/calendar/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,39 +170,45 @@ export type CalendarCanvasProps = Dimensions &
Partial<
CommonCalendarProps &
InteractivityProps<Omit<Datum, 'data' | 'value'> | Datum, HTMLCanvasElement> & {
pixelRatio?: number
pixelRatio: number
}
>

export type TimeRangeDatum = {
day: string
date: Date
value: number
}

export type TimeRangeDayData = TimeRangeDatum & {
export type TimeRangeDayData = CalendarDatum & {
coordinates: {
x: number
y: number
}
date: Date
firstWeek: number
month: number
year: number
color: string
width: number
height: number
}

export type TimeRangeTooltipProps = Omit<TimeRangeDayData, 'date' | 'value'> & {
value: string
}

export type TimeRangeSvgProps = Dimensions & { data: TimeRangeDatum[] } & Partial<
CommonCalendarProps &
InteractivityProps<TimeRangeDatum, SVGRectElement> & {
export type TimeRangeSvgProps = Dimensions & { data: CalendarDatum[] } & Partial<
Omit<
CommonCalendarProps,
| 'emptyColor'
| 'yearLegend'
| 'yearSpacing'
| 'yearLegendOffset'
| 'yearLegendPosition'
| 'monthSpacing'
| 'monthBorderWidth'
| 'monthBorderColor'
> &
InteractivityProps<TimeRangeDayData, SVGRectElement> & {
dayRadius: number
daysInRange: number
square: boolean
role: string
weekdayLegendsOffset: number
weekdayLegendOffset: number
}
>

Expand Down
37 changes: 8 additions & 29 deletions packages/calendar/stories/timeRange.stories.tsx
Original file line number Diff line number Diff line change
@@ -1,24 +1,10 @@
import React from 'react'
import { storiesOf } from '@storybook/react'
import { withKnobs, number, date, boolean } from '@storybook/addon-knobs'
import { timeDays } from 'd3-time'
import { timeFormat } from 'd3-time-format'
import { generateOrderedDayCounts } from '@nivo/generators'

import { TimeRange, ResponsiveTimeRange } from '../src'

const generateOrderedDayCounts = ({ from, to }: { from: Date; to: Date }) => {
const days = timeDays(from, to)
const dayFormat = timeFormat('%Y-%m-%d')

return days.map(day => {
return {
value: Math.round(Math.random() * 400),
date: day,
day: dayFormat(day),
}
})
}

const stories = storiesOf('TimeRange', module)

stories.addDecorator(withKnobs)
Expand All @@ -38,12 +24,11 @@ stories.add('TimeRange horizontal', () => {
bottom: number('margin-bottom', 40),
left: number('margin-left', 40),
},
data: generateOrderedDayCounts({
from,
to,
}),
daySpacing: number('daySpacing', 10),
data: generateOrderedDayCounts(from, to),
daySpacing: number('daySpacing', 0),
}}
monthLegendOffset={10}
dayBorderWidth={1}
height={number('height', 250)}
width={number('width', 655)}
legendFormat={value => value / 10 + 'M'}
Expand Down Expand Up @@ -95,10 +80,7 @@ stories.add('responsive', () => {
bottom: number('margin-bottom', 40),
left: number('margin-left', 40),
},
data: generateOrderedDayCounts({
from,
to,
}),
data: generateOrderedDayCounts(from, to),
daySpacing: number('daySpacing', 10),
}}
/>
Expand All @@ -120,13 +102,10 @@ stories.add('TimeRange vertical', () => {
bottom: number('margin-bottom', 40),
left: number('margin-left', 40),
},
data: generateOrderedDayCounts({
from,
to,
}),
data: generateOrderedDayCounts(from, to),
daySpacing: number('daySpacing', 10),
}}
weekdayLegendsOffset={0}
weekdayLegendOffset={0}
height={number('height', 900)}
width={number('width', 250)}
direction="vertical"
Expand Down

0 comments on commit 23d569c

Please sign in to comment.