Exemple #1
0
def make_calendar(year, month, firstweekday="Mon"):
    firstweekday = list(day_abbrs).index(firstweekday)
    calendar = Calendar(firstweekday=firstweekday)

    month_days = [
        None if not day else str(day)
        for day in calendar.itermonthdays(year, month)
    ]
    month_weeks = len(month_days) // 7

    workday = "linen"
    weekend = "lightsteelblue"

    def weekday(date):
        return (date.weekday() - firstweekday) % 7

    def pick_weekdays(days):
        return [days[i % 7] for i in range(firstweekday, firstweekday + 7)]

    day_names = pick_weekdays(day_abbrs)
    week_days = pick_weekdays([workday] * 5 + [weekend] * 2)

    source = ColumnDataSource(data=dict(
        days=list(day_names) * month_weeks,
        weeks=sum([[str(week)] * 7 for week in range(month_weeks)], []),
        month_days=month_days,
        day_backgrounds=sum([week_days] * month_weeks, []),
    ))

    holidays = [
        (date, summary.replace("(US-OPM)", "").strip())
        for (date, summary) in us_holidays
        if date.year == year and date.month == month and "(US-OPM)" in summary
    ]

    holidays_source = ColumnDataSource(data=dict(
        holidays_days=[day_names[weekday(date)] for date, _ in holidays],
        holidays_weeks=[
            str((weekday(date.replace(day=1)) + date.day) // 7)
            for date, _ in holidays
        ],
        month_holidays=[summary for _, summary in holidays],
    ))

    xdr = FactorRange(factors=list(day_names))
    ydr = FactorRange(
        factors=list(reversed([str(week) for week in range(month_weeks)])))

    plot = Plot(title=month_names[month],
                x_range=xdr,
                y_range=ydr,
                plot_width=300,
                plot_height=300,
                outline_line_color=None)
    plot.title_text_align = "left"
    plot.title_text_font_size = "12pt"
    plot.title_text_color = "darkolivegreen"
    plot.title_standoff = 25
    plot.min_border_left = 0
    plot.min_border_bottom = 5

    rect = Rect(x="days",
                y="weeks",
                width=0.9,
                height=0.9,
                fill_color="day_backgrounds",
                line_color="silver")
    plot.add_glyph(source, rect)

    rect = Rect(x="holidays_days",
                y="holidays_weeks",
                width=0.9,
                height=0.9,
                fill_color="pink",
                line_color="indianred")
    rect_renderer = plot.add_glyph(holidays_source, rect)

    text = Text(x="days",
                y="weeks",
                text="month_days",
                text_align="center",
                text_baseline="middle")
    plot.add_glyph(source, text)

    xaxis = CategoricalAxis()
    xaxis.major_label_text_font_size = "8pt"
    xaxis.major_label_standoff = 0
    xaxis.major_tick_line_color = None
    xaxis.axis_line_color = None
    plot.add_layout(xaxis, 'above')

    hover_tool = HoverTool(plot=plot,
                           renderers=[rect_renderer],
                           tooltips=[("Holiday", "@month_holidays")])
    plot.tools.append(hover_tool)

    return plot
Exemple #2
0
def make_calendar(year, month, firstweekday="Mon"):
    firstweekday = list(day_abbrs).index(firstweekday)
    calendar = Calendar(firstweekday=firstweekday)

    month_days  = [ None if not day else str(day) for day in calendar.itermonthdays(year, month) ]
    month_weeks = len(month_days)//7

    workday = "linen"
    weekend = "lightsteelblue"

    def weekday(date):
        return (date.weekday() - firstweekday) % 7

    def pick_weekdays(days):
        return [ days[i % 7] for i in range(firstweekday, firstweekday+7) ]

    day_names = pick_weekdays(day_abbrs)
    week_days = pick_weekdays([workday]*5 + [weekend]*2)

    source = ColumnDataSource(data=dict(
        days            = list(day_names)*month_weeks,
        weeks           = sum([ [str(week)]*7 for week in range(month_weeks) ], []),
        month_days      = month_days,
        day_backgrounds = sum([week_days]*month_weeks, []),
    ))

    holidays = [ (date, summary.replace("(US-OPM)", "").strip()) for (date, summary) in us_holidays
        if date.year == year and date.month == month and "(US-OPM)" in summary ]

    holidays_source = ColumnDataSource(data=dict(
        holidays_days  = [ day_names[weekday(date)] for date, _ in holidays ],
        holidays_weeks = [ str((weekday(date.replace(day=1)) + date.day) // 7) for date, _ in holidays ],
        month_holidays = [ summary for _, summary in holidays ],
    ))

    xdr = FactorRange(factors=list(day_names))
    ydr = FactorRange(factors=list(reversed([ str(week) for week in range(month_weeks) ])))

    plot = Plot(title=month_names[month], x_range=xdr, y_range=ydr, plot_width=300, plot_height=300, outline_line_color=None)
    plot.title_text_align = "left"
    plot.title_text_font_size = "12pt"
    plot.title_text_color = "darkolivegreen"
    plot.title_standoff = 25
    plot.min_border_left = 0
    plot.min_border_bottom = 5

    rect = Rect(x="days", y="weeks", width=0.9, height=0.9, fill_color="day_backgrounds", line_color="silver")
    plot.add_glyph(source, rect)

    rect = Rect(x="holidays_days", y="holidays_weeks", width=0.9, height=0.9, fill_color="pink", line_color="indianred")
    rect_renderer = plot.add_glyph(holidays_source, rect)

    text = Text(x="days", y="weeks", text="month_days", text_align="center", text_baseline="middle")
    plot.add_glyph(source, text)

    xaxis = CategoricalAxis()
    xaxis.major_label_text_font_size = "8pt"
    xaxis.major_label_standoff = 0
    xaxis.major_tick_line_color = None
    xaxis.axis_line_color = None
    plot.add_layout(xaxis, 'above')

    hover_tool = HoverTool(plot=plot, renderers=[rect_renderer], tooltips=[("Holiday", "@month_holidays")])
    plot.tools.append(hover_tool)

    return plot