Skip to content

Commit

Permalink
feat(calendar): add ability to customize year/month legend
Browse files Browse the repository at this point in the history
  • Loading branch information
Raphaël Benitte authored and Raphaël Benitte committed Jun 4, 2018
1 parent 98106ab commit a43c708
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 24 deletions.
4 changes: 2 additions & 2 deletions packages/nivo-calendar/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,12 @@ declare module '@nivo/calendar' {
margin: Box

// years
yearLabel: (year: number) => string | number
yearLegend: (year: number) => string | number
yearSpacing: number
yearLegendOffset: number

// months
monthLabel: (year: number, month: number) => string | number
monthLegend: (year: number, month: number, date: Date) => string | number
monthBorderWidth: number
monthBorderColor: string
monthLegendOffset: number
Expand Down
23 changes: 14 additions & 9 deletions packages/nivo-calendar/src/Calendar.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
* file that was distributed with this source code.
*/
import React from 'react'
import { timeFormat } from 'd3-time-format'
import { BoxLegendSvg } from '@nivo/legends'
import computeCalendar from './computeCalendar'
import { CalendarPropTypes } from './props'
Expand All @@ -17,8 +16,6 @@ import CalendarMonthPath from './CalendarMonthPath'
import { Container, SvgWrapper } from '@nivo/core'
import enhance from './enhance'

const monthLegendFormat = timeFormat('%b')

const Calendar = ({
data,
from,
Expand All @@ -34,16 +31,24 @@ const Calendar = ({
outerHeight,

direction,
emptyColor,

// years
yearLegend,
yearSpacing,
yearLegendOffset,
daySpacing,
dayBorderWidth,
dayBorderColor,

// months
monthLegend,
monthBorderWidth,
monthBorderColor,
monthLegendOffset,

// days
daySpacing,
dayBorderWidth,
dayBorderColor,
emptyColor,

theme,

// interactivity
Expand Down Expand Up @@ -113,7 +118,7 @@ const Calendar = ({
transform={transform}
textAnchor="middle"
>
{monthLegendFormat(month.date)}
{monthLegend(month.year, month.month, month.date)}
</text>
)
})}
Expand All @@ -134,7 +139,7 @@ const Calendar = ({
transform={transform}
textAnchor="middle"
>
{year.year}
{yearLegend(year.year)}
</text>
)
})}
Expand Down
26 changes: 13 additions & 13 deletions packages/nivo-calendar/src/computeCalendar.js
Original file line number Diff line number Diff line change
Expand Up @@ -252,19 +252,19 @@ const CalendarLayout = ({
)
)

const yearMonths = timeMonths(yearStart, yearEnd).map(monthDate =>
assign(
{ date: monthDate },
memoMonthPathAndBBox({
date: monthDate,
direction,
yearIndex: i,
yearSpacing,
daySpacing,
cellSize,
})
)
)
const yearMonths = timeMonths(yearStart, yearEnd).map(monthDate => ({
date: monthDate,
year: monthDate.getFullYear(),
month: monthDate.getMonth(),
...memoMonthPathAndBBox({
date: monthDate,
direction,
yearIndex: i,
yearSpacing,
daySpacing,
cellSize,
}),
}))

months = months.concat(yearMonths)

Expand Down
7 changes: 7 additions & 0 deletions packages/nivo-calendar/src/props.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,13 @@
* file that was distributed with this source code.
*/
import PropTypes from 'prop-types'
import { timeFormat } from 'd3-time-format'
import { noop } from '@nivo/core'
import { LegendPropShape } from '@nivo/legends'
import { DIRECTION_HORIZONTAL, DIRECTION_VERTICAL } from './constants'

const monthLabelFormat = timeFormat('%b')

/**
* Calendar components propTypes.
*
Expand All @@ -35,10 +38,12 @@ export const CalendarPropTypes = {
emptyColor: PropTypes.string.isRequired,

// years
yearLegend: PropTypes.func.isRequired,
yearSpacing: PropTypes.number.isRequired,
yearLegendOffset: PropTypes.number.isRequired,

// months
monthLegend: PropTypes.func.isRequired,
monthBorderWidth: PropTypes.number.isRequired,
monthBorderColor: PropTypes.string.isRequired,
monthLegendOffset: PropTypes.number.isRequired,
Expand Down Expand Up @@ -74,10 +79,12 @@ export const CalendarDefaultProps = {
emptyColor: '#fff',

// years
yearLegend: year => year,
yearSpacing: 30,
yearLegendOffset: 10,

// months
monthLegend: (year, month, date) => monthLabelFormat(date),
monthBorderWidth: 2,
monthBorderColor: '#000',
monthLegendOffset: 6,
Expand Down
12 changes: 12 additions & 0 deletions website/src/components/charts/calendar/props.js
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,12 @@ export default [
controlGroup: 'Base',
},
// Years
{
key: 'yearLegend',
description: `can be used to customize years label, returns 'YYYY' by default.`,
type: '{(year: number) => string | number}',
required: false,
},
{
key: 'yearSpacing',
description: 'define spacing between each year row/column depending on the direction.',
Expand Down Expand Up @@ -174,6 +180,12 @@ export default [
},
},
// Months
{
key: 'monthLegend',
description: `can be used to customize months label, returns abbreviated month name (english) by default. This can be used to use a different language`,
type: '{(year: number, month: number, date: Date) => string | number}',
required: false,
},
{
key: 'monthBorderWidth',
description: 'width of month borders.',
Expand Down

0 comments on commit a43c708

Please sign in to comment.