Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add firstWeekday attr to calendar/TimeRange #2299

Merged
merged 6 commits into from
Apr 28, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions packages/calendar/src/TimeRange.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ const InnerTimeRange = ({

legends = timeRangeDefaultProps.legends,
role = timeRangeDefaultProps.role,

firstWeekday = timeRangeDefaultProps.firstWeekday,
}: TimeRangeSvgProps) => {
const { margin, innerWidth, innerHeight, outerWidth, outerHeight } = useDimensions(
width,
Expand Down Expand Up @@ -98,6 +100,7 @@ const InnerTimeRange = ({
data,
direction,
daySpacing,
firstWeekday,
})

// map the days and reduce the month
Expand All @@ -117,6 +120,7 @@ const InnerTimeRange = ({
cellWidth,
daySpacing,
ticks: weekdayTicks,
firstWeekday,
})

const monthLegends = useMonthLegends({
Expand Down
74 changes: 59 additions & 15 deletions packages/calendar/src/compute/timeRange.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
import { timeWeek, timeDays, timeDay } from 'd3-time'
import {
timeDays,
timeDay,
timeMonday,
timeTuesday,
timeWednesday,
timeThursday,
timeFriday,
timeSaturday,
timeSunday,
} from 'd3-time'
import { timeFormat } from 'd3-time-format'
import { DateOrString } from '../types'
import { DateOrString, Weekday } from '../types'
import { isDate } from 'lodash'

// Interfaces
Expand Down Expand Up @@ -38,6 +48,7 @@ interface ComputeCellPositions
}[]
colorScale: (value: number) => string
emptyColor: string
firstWeekday: Weekday
}

interface ComputeWeekdays
Expand All @@ -46,6 +57,7 @@ interface ComputeWeekdays
ComputeBaseDimensionProps {
ticks?: number[]
arrayOfWeekdays?: string[]
firstWeekday: Weekday
}

interface Day {
Expand Down Expand Up @@ -135,26 +147,60 @@ export const computeCellSize = ({
}
}

export const getDayIndex = (date: Date, firstWeekday: Weekday) => {
const days = [0, 1, 2, 3, 4, 5, 6]
const day = date.getDay()
const offsetDay = day - firstWeekday
const [dayIndex] = days.slice(offsetDay)
return dayIndex
}

const getTimeInterval = (firstWeekday: Weekday) => {
return [
timeSunday,
timeMonday,
timeTuesday,
timeWednesday,
timeThursday,
timeFriday,
timeSaturday,
][firstWeekday]
}

function shiftArray<T>(arr: T[], x: number): T[] {
if (!arr.length || !x) return arr

for (let i = 0; i < x; i++) {
const shifted = arr.shift() as T
arr.push(shifted)
}

return arr
}

function computeGrid({
startDate,
date,
direction,
firstWeekday,
}: {
startDate: Date
date: Date
direction: 'horizontal' | 'vertical'
firstWeekday: Weekday
}) {
const firstWeek = timeWeek.count(startDate, date)
const timeInterval = getTimeInterval(firstWeekday)
const firstWeek = timeInterval.count(startDate, date)
const month = date.getMonth()
const year = date.getFullYear()

let currentColumn = 0
let currentRow = 0
if (direction === 'horizontal') {
currentColumn = firstWeek
currentRow = date.getDay()
currentRow = getDayIndex(date, firstWeekday)
} else {
currentColumn = date.getDay()
currentColumn = getDayIndex(date, firstWeekday)
currentRow = firstWeek
}

Expand All @@ -172,6 +218,7 @@ export const computeCellPositions = ({
cellHeight,
daySpacing,
offset,
firstWeekday,
}: ComputeCellPositions) => {
let x = daySpacing
let y = daySpacing
Expand Down Expand Up @@ -201,6 +248,7 @@ export const computeCellPositions = ({
startDate,
date: day.date,
direction,
firstWeekday,
})

const coordinates = {
Expand Down Expand Up @@ -244,15 +292,11 @@ export const computeWeekdays = ({
direction,
daySpacing,
ticks = [1, 3, 5],
arrayOfWeekdays = [
'Sunday',
'Monday',
'Tuesday',
'Wednesday',
'Thursday',
'Friday',
'Saturday',
],
firstWeekday,
arrayOfWeekdays = shiftArray(
['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'],
firstWeekday
),
}: ComputeWeekdays) => {
const sizes = {
width: cellWidth + daySpacing,
Expand Down Expand Up @@ -282,7 +326,7 @@ export const computeMonthLegends = ({
}

return days.reduce((acc, day) => {
if (acc.weeks.length === day.firstWeek) {
if (acc.weeks.length === day.firstWeek || (!acc.weeks.length && day.firstWeek === 1)) {
acc.weeks.push(day)

const key = `${day.year}-${day.month}`
Expand Down
3 changes: 2 additions & 1 deletion packages/calendar/src/props.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { timeFormat } from 'd3-time-format'
import { CalendarLegendProps } from './types'
import { CalendarLegendProps, Weekday } from './types'
import { CalendarTooltip } from './CalendarTooltip'

const monthLabelFormat = timeFormat('%b')
Expand Down Expand Up @@ -52,4 +52,5 @@ export const timeRangeDefaultProps = {
dayRadius: 0,
square: true,
weekdayLegendOffset: 75,
firstWeekday: Weekday.Sunday,
} as const
11 changes: 11 additions & 0 deletions packages/calendar/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,16 @@ export type TimeRangeTooltipProps = Omit<TimeRangeDayData, 'date' | 'value'> & {
value: string
}

export enum Weekday {
yzalvov marked this conversation as resolved.
Show resolved Hide resolved
Sunday,
Monday,
Tuesday,
Wednesday,
Thursday,
Friday,
Saturday,
}

export type TimeRangeSvgProps = Dimensions & { data: CalendarDatum[] } & Partial<
Omit<CalendarData, 'data'>
> &
Expand All @@ -212,6 +222,7 @@ export type TimeRangeSvgProps = Dimensions & { data: CalendarDatum[] } & Partial
role: string
weekdayLegendOffset: number
weekdayTicks: Array<0 | 1 | 2 | 3 | 4 | 5 | 6>
firstWeekday: Weekday
}
>

Expand Down
18 changes: 17 additions & 1 deletion website/src/data/components/time-range/props.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { timeRangeDefaultProps as defaults } from '@nivo/calendar'
import { Weekday, timeRangeDefaultProps as defaults } from '@nivo/calendar'
import { themeProperty, getLegendsProps, groupProperties } from '../../../lib/componentProperties'
import { chartDimensions, isInteractive } from '../../../lib/chart-properties'
import { ChartProperty, Flavor } from '../../../types'
Expand Down Expand Up @@ -215,6 +215,22 @@ const props: ChartProperty[] = [
6 = Saturday\n
`,
},
{
key: 'firstWeekday',
help: 'define the first weekday: Weekday.Sunday (default), Weekday.Monday, etc.',
flavors: allFlavors,
type: 'Weekday',
required: false,
defaultValue: defaults.firstWeekday,
group: 'Weekday',
control: {
type: 'radio',
choices: [
{ label: 'Sunday', value: String(Weekday.Sunday) },
{ label: 'Monday', value: String(Weekday.Monday) },
],
},
},
// Days
{
key: 'square',
Expand Down
4 changes: 3 additions & 1 deletion website/src/pages/time-range/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React from 'react'
import { ResponsiveTimeRange, timeRangeDefaultProps } from '@nivo/calendar'
import { ResponsiveTimeRange, timeRangeDefaultProps, Weekday } from '@nivo/calendar'
import { generateDayCounts } from '@nivo/generators'
import { ComponentTemplate } from '../../components/components/ComponentTemplate'
import meta from '../../data/components/time-range/meta.yml'
Expand Down Expand Up @@ -36,6 +36,8 @@ const initialProperties = {
monthLegendOffset: 10,

weekdayLegendOffset: 75,
// String for radio to display active option.
firstWeekday: String(Weekday.Sunday),

square: true,
dayRadius: 0,
Expand Down