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 all 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
85 changes: 70 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,74 @@ export const computeCellSize = ({
}
}

export const ARRAY_OF_WEEKDAYS = [
'Sunday',
'Monday',
'Tuesday',
'Wednesday',
'Thursday',
'Friday',
'Saturday',
]

export function getFirstWeekdayIndex(weekday: Weekday) {
return ARRAY_OF_WEEKDAYS.findIndex(item => item.toLowerCase() === weekday)
}

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

const getTimeInterval = (firstWeekday: Weekday) => {
return [
timeSunday,
timeMonday,
timeTuesday,
timeWednesday,
timeThursday,
timeFriday,
timeSaturday,
][getFirstWeekdayIndex(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 +232,7 @@ export const computeCellPositions = ({
cellHeight,
daySpacing,
offset,
firstWeekday,
}: ComputeCellPositions) => {
let x = daySpacing
let y = daySpacing
Expand Down Expand Up @@ -201,6 +262,7 @@ export const computeCellPositions = ({
startDate,
date: day.date,
direction,
firstWeekday,
})

const coordinates = {
Expand Down Expand Up @@ -244,15 +306,8 @@ export const computeWeekdays = ({
direction,
daySpacing,
ticks = [1, 3, 5],
arrayOfWeekdays = [
'Sunday',
'Monday',
'Tuesday',
'Wednesday',
'Thursday',
'Friday',
'Saturday',
],
firstWeekday,
arrayOfWeekdays = shiftArray(ARRAY_OF_WEEKDAYS, getFirstWeekdayIndex(firstWeekday)),
}: ComputeWeekdays) => {
const sizes = {
width: cellWidth + daySpacing,
Expand Down Expand Up @@ -282,7 +337,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
1 change: 1 addition & 0 deletions packages/calendar/src/props.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,5 @@ export const timeRangeDefaultProps = {
dayRadius: 0,
square: true,
weekdayLegendOffset: 75,
firstWeekday: 'sunday',
} as const
10 changes: 10 additions & 0 deletions packages/calendar/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,15 @@ export type TimeRangeTooltipProps = Omit<TimeRangeDayData, 'date' | 'value'> & {
value: string
}

export type Weekday =
| 'sunday'
| 'monday'
| 'tuesday'
| 'wednesday'
| 'thursday'
| 'friday'
| 'saturday'

export type TimeRangeSvgProps = Dimensions & { data: CalendarDatum[] } & Partial<
Omit<CalendarData, 'data'>
> &
Expand All @@ -212,6 +221,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
16 changes: 16 additions & 0 deletions website/src/data/components/time-range/props.ts
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,22 @@ const props: ChartProperty[] = [
6 = Saturday\n
`,
},
{
key: 'firstWeekday',
help: `define the first weekday: 'sunday', 'monday', etc.`,
flavors: allFlavors,
type: 'Weekday',
required: false,
defaultValue: defaults.firstWeekday,
group: 'Weekday',
control: {
type: 'radio',
choices: [
{ label: `'sunday'`, value: 'sunday' },
{ label: `'monday'`, value: 'monday' },
],
},
},
// Days
{
key: 'square',
Expand Down
1 change: 1 addition & 0 deletions website/src/pages/time-range/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ const initialProperties = {
monthLegendOffset: 10,

weekdayLegendOffset: 75,
firstWeekday: 'sunday',

square: true,
dayRadius: 0,
Expand Down