Jump to content
Changes to the Jaspersoft community edition download ×
  • Sample Report to Create a Monthly Calendar


    tchen
    • Version: v6.3 Product: JasperReports® Library

    General Discussion

    This sample report will show user how to use existing JasperReports (JR) feature to create a monthly calendar based on a user provided date without custom coding (such as using a Scriptlet).

    A monthly calendar consists of rows of 7 columns each representing a day of the week from Sunday to Saturday. Total number of rows ranges between 4 to 6 depending upon number of days for the month and the day of the week when month starts.

    Technique Used in the Report Design

    This sample report uses a (any) PostgreSQL data source to feed the report. The query uses a PostgresSQL generate function to create number of records reflecting the total cells needed to create the monthly calendar.

    SELECT i as id FROM generate_series(1, $P!{Limit} ) i
    

    For other database sources, user will need to consult with their in house query expert or review relevant DB reference material to find feature similar to create the report query.

    Each record in the query resultant data set will create one cell in the monthly calendar. This report has a 7 column layout reflecting a week in the monthly calendar.

    Other Design Considerations

    • Figure out which day of the week the first of the month is;

    • Correctly calculate number of rows (weeks) needed for the calendar layout;

    • Avoid using setter for date related calculation since JR is not able to process in line statement that returns no class object or null (such as void).

    Sample Report Design

    <?xml version="1.0" encoding="UTF-8"?>
    <!-- Created with Jaspersoft Studio version 6.3.0.final using JasperReports Library version 6.3.0  -->
    <!-- 2016-09-19T11:42:55 -->
    <jasperReport xmlns="http://jasperreports.sourceforge.net/jasperreports"
                  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                  xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports http://jasperreports.sourceforge.net/xsd/jasperreport.xsd"
                  name="demo_calendar"
                  columnCount="7"
                  printOrder="Horizontal"
                  pageWidth="792"
                  pageHeight="612"
                  orientation="Landscape"
                  whenNoDataType="AllSectionsNoDetail"
                  columnWidth="110"
                  leftMargin="11"
                  rightMargin="11"
                  topMargin="6"
                  bottomMargin="6"
                  uuid="32abf871-39b8-4cc7-b03c-d950a55e2bfb">
        <property name="com.jaspersoft.studio.data.sql.tables"         value=""       />
        <property name="com.jaspersoft.studio.data.defaultdataadapter" value="JRSrepo"/>
        <property name="com.jaspersoft.studio.unit."                   value="pixel"  />
        <style name="Style1" forecolor="#050505">
            <conditionalStyle>
                <conditionExpression><![CDATA[$F{id}.intValue()%7==1]]></conditionExpression>
                <style forecolor="#FA0702"/>
            </conditionalStyle>
        </style>
        <parameter name="Date" class="java.sql.Timestamp">
            <defaultValueExpression><![CDATA[new Date()]]></defaultValueExpression>
        </parameter>
        <parameter name="DOW" class="java.lang.Integer" isForPrompting="false">
            <defaultValueExpression>
                <![CDATA[new Date($P{Date}.getYear(),$P{Date}.getMonth(),1).getDay()+1]]>
            </defaultValueExpression>
        </parameter>
        <parameter name="DOM" class="java.lang.Integer" isForPrompting="false">
            <defaultValueExpression>
                <![CDATA[$P{Date}.getMonth()+1]]>
            </defaultValueExpression>
        </parameter>
        <parameter name="DOY" class="java.lang.Integer" isForPrompting="false">
            <defaultValueExpression>
                <![CDATA[$P{Date}.getYear()+1900]]>
            </defaultValueExpression>
        </parameter>
        <parameter name="Days" class="java.lang.Integer" isForPrompting="false">
            <defaultValueExpression>
                <![CDATA[java.time.YearMonth.of($P{DOY}.intValue(), $P{DOM}.intValue()).lengthOfMonth()]]>
            </defaultValueExpression>
        </parameter>
        <parameter name="Cells" class="java.lang.Integer" isForPrompting="false">
            <defaultValueExpression>
                <![CDATA[$P{DOW}.intValue()-1+$P{Days}.intValue()]]>
            </defaultValueExpression>
        </parameter>
        <parameter name="Limit" class="java.lang.Integer" isForPrompting="false">
            <defaultValueExpression>
                <![CDATA[$P{Cells}.intValue()>28?($P{Cells}.intValue()>35?42:35):28]]>
            </defaultValueExpression>
        </parameter>
        <queryString>
            <![CDATA[SELECT i as id FROM generate_series(1, $P!{Limit} ) i]]>
        </queryString>
        <field name="id" class="java.lang.Integer"/>
        <variable name="Date" class="java.lang.Integer">
            <variableExpression>
                <![CDATA[$F{id}.intValue()-$P{DOW}.intValue()+1]]>
            </variableExpression>
        </variable>
        <title>
            <band height="60">
                <textField pattern="yyyy MMMM">
                    <reportElement x="0" y="0" width="770" height="30" 
                                   uuid="21293131-3839-43aa-b449-b19867eee2da"/>
                    <textElement textAlignment="Center">
                        <font size="16" isBold="true"/>
                    </textElement>
                    <textFieldExpression><![CDATA[$P{Date}]]></textFieldExpression>
                </textField>
                <staticText>
                    <reportElement mode="Opaque" x="0" y="30" width="110" height="30"
                                   forecolor="#FFFCFC" backcolor="#FC0703"
                                   uuid="11cb3d1f-777b-43cb-a053-11f27f46d99b">
                        <property name="com.jaspersoft.studio.unit.y" value="pixel"/>
                    </reportElement>
                    <box>
                        <topPen    lineWidth="0.0" lineStyle="Solid"/>
                        <leftPen   lineWidth="0.0" lineStyle="Solid"/>
                        <bottomPen lineWidth="0.0" lineStyle="Solid"/>
                        <rightPen  lineWidth="0.0" lineStyle="Solid" lineColor="#FFFCFC"/>
                    </box>
                    <textElement textAlignment="Center">
                        <font size="14" isBold="true"/>
                    </textElement>
                    <text><![CDATA[Sunday]]></text>
                </staticText>
                <staticText>
                    <reportElement mode="Opaque" x="110" y="30" width="110" height="30"
                                   forecolor="#FFFCFC" backcolor="#403D3D"
                                   uuid="35ef4490-9b76-4251-bc43-7f59a9ab19c2">
                        <property name="com.jaspersoft.studio.unit.y" value="pixel"/>
                    </reportElement>
                    <box>
                        <topPen    lineWidth="1.0" lineStyle="Solid"/>
                        <leftPen   lineWidth="1.0" lineStyle="Solid"/>
                        <bottomPen lineWidth="1.0" lineStyle="Solid"/>
                        <rightPen  lineWidth="1.0" lineStyle="Solid"/>
                    </box>
                    <textElement textAlignment="Center">
                        <font size="14" isBold="true"/>
                    </textElement>
                    <text><![CDATA[Monday]]></text>
                </staticText>
                <staticText>
                    <reportElement mode="Opaque" x="220" y="30" width="110" height="30"
                                   forecolor="#FFFCFC" backcolor="#403D3D"
                                   uuid="33eb161b-40bb-4a2d-9b85-6ba67461b4f6">
                        <property name="com.jaspersoft.studio.unit.y" value="pixel"/>
                    </reportElement>
                    <box>
                        <topPen    lineWidth="1.0" lineStyle="Solid"/>
                        <leftPen   lineWidth="1.0" lineStyle="Solid"/>
                        <bottomPen lineWidth="1.0" lineStyle="Solid"/>
                        <rightPen  lineWidth="1.0" lineStyle="Solid"/>
                    </box>
                    <textElement textAlignment="Center">
                        <font size="14" isBold="true"/>
                    </textElement>
                    <text><![CDATA[Tuesday]]></text>
                </staticText>
                <staticText>
                    <reportElement mode="Opaque" x="330" y="30" width="110" height="30"
                                   forecolor="#FFFCFC" backcolor="#403D3D"
                                   uuid="5e4c7405-352c-4058-b0ef-bd52d766e00b">
                        <property name="com.jaspersoft.studio.unit.y" value="pixel"/>
                    </reportElement>
                    <box>
                        <topPen    lineWidth="1.0" lineStyle="Solid"/>
                        <leftPen   lineWidth="1.0" lineStyle="Solid"/>
                        <bottomPen lineWidth="1.0" lineStyle="Solid"/>
                        <rightPen  lineWidth="1.0" lineStyle="Solid"/>
                    </box>
                    <textElement textAlignment="Center">
                        <font size="14" isBold="true"/>
                    </textElement>
                    <text><![CDATA[Wednesday]]></text>
                </staticText>
                <staticText>
                    <reportElement mode="Opaque" x="440" y="30" width="110" height="30"
                                   forecolor="#FFFCFC" backcolor="#403D3D"
                                   uuid="81be3ac1-c1c1-4573-86a7-67c891288f1c">
                        <property name="com.jaspersoft.studio.unit.y" value="pixel"/>
                    </reportElement>
                    <box>
                        <topPen    lineWidth="1.0" lineStyle="Solid"/>
                        <leftPen   lineWidth="1.0" lineStyle="Solid"/>
                        <bottomPen lineWidth="1.0" lineStyle="Solid"/>
                        <rightPen  lineWidth="1.0" lineStyle="Solid"/>
                    </box>
                    <textElement textAlignment="Center">
                        <font size="14" isBold="true"/>
                    </textElement>
                    <text><![CDATA[Thursday]]></text>
                </staticText>
                <staticText>
                    <reportElement mode="Opaque" x="550" y="30" width="110" height="30"
                                   forecolor="#FFFCFC" backcolor="#403D3D"
                                   uuid="9442d83e-3726-4db9-8746-04b2fea1449b">
                        <property name="com.jaspersoft.studio.unit.y" value="pixel"/>
                    </reportElement>
                    <box>
                        <topPen lineWidth="1.0" lineStyle="Solid"/>
                        <leftPen lineWidth="1.0" lineStyle="Solid"/>
                        <bottomPen lineWidth="1.0" lineStyle="Solid"/>
                        <rightPen lineWidth="1.0" lineStyle="Solid"/>
                    </box>
                    <textElement textAlignment="Center">
                        <font size="14" isBold="true"/>
                    </textElement>
                    <text><![CDATA[Friday]]></text>
                </staticText>
                <staticText>
                    <reportElement mode="Opaque" x="660" y="30" width="110" height="30"
                                   forecolor="#FFFCFC" backcolor="#403D3D"
                                   uuid="edc883b5-0e35-4e17-8a45-56efc9dacf6a">
                        <property name="com.jaspersoft.studio.unit.y" value="pixel"/>
                    </reportElement>
                    <box>
                        <topPen    lineWidth="1.0" lineStyle="Solid"/>
                        <leftPen   lineWidth="1.0" lineStyle="Solid"/>
                        <bottomPen lineWidth="1.0" lineStyle="Solid"/>
                        <rightPen  lineWidth="1.0" lineStyle="Solid"/>
                    </box>
                    <textElement textAlignment="Center">
                        <font size="14" isBold="true"/>
                    </textElement>
                    <text><![CDATA[Saturday]]></text>
                </staticText>
            </band>
        </title>
        <detail>
            <band height="90" splitType="Stretch">
                <frame>
                    <reportElement x="0" y="0" width="110" height="90"
                                   uuid="75440275-c439-4c3e-bb77-f9f4d8f48415">
                        <property name="com.jaspersoft.studio.unit.x" value="pixel"/>
                        <property name="com.jaspersoft.studio.unit.y" value="pixel"/>
                        <property name="com.jaspersoft.studio.unit.width" value="pixel"/>
                    </reportElement>
                    <box>
                        <topPen lineWidth="1.0" lineStyle="Solid"/>
                        <leftPen lineWidth="1.0" lineStyle="Solid"/>
                        <bottomPen lineWidth="1.0" lineStyle="Solid"/>
                        <rightPen lineWidth="1.0" lineStyle="Solid"/>
                    </box>
                    <textField>
                        <reportElement style="Style1" x="60" y="1" width="50" height="30"
                                       isRemoveLineWhenBlank="true"
                                       uuid="733ff549-0109-4f55-b9de-476f0ad9fbeb">
                            <printWhenExpression>
                                <![CDATA[$V{Date}.intValue()>0&&$V{Date}.intValue()<=$P{Days}.intValue()]]>
                            </printWhenExpression>
                        </reportElement>
                        <textElement textAlignment="Right">
                            <font size="16" isBold="true"/>
                        </textElement>
                        <textFieldExpression><![CDATA[$V{Date}]]></textFieldExpression>
                    </textField>
                </frame>
            </band>
        </detail>
    </jasperReport>
    

    Report Output

    1. Date entered - 9/19/2016

      20160919.png.37e9e71a143a74b1e0d1da178b0a3df8.png

    2. Date entered - 10/15/2016

      20161015.png.9ee7d91ba84537ecb2e541190de4c1f1.png

    3. Date entered - 2/15/2015

      20150215(2).png.c48fcfb9e4d353174c4fd5546aee2897.png


    TTC-20160919

    20160919.png.3bb1a36c7591497370f8afb7a1eac8f8.png

    20161015.png.eab49da18f6420a82afc349ccd487a44.png

    20150215(2).png.937971e7870395cb659dcd40443d20b2.png


    User Feedback

    Recommended Comments

    This is very useful. Could you please let me know whether there is a way to breakdown a single day's cell to display different details. For example, JAN 1 is NEW YEAR and it will be a holiday in countries like USA, IND, UK etc. I want to show under JAN 1 cell the list of coutries that are having that day as Holiday and differentiate them with some color codings. Is this possbile ? 

    Link to comment
    Share on other sites

    Not the article author here, but here are a few suggestions:

    - You should be able to achieve most of your requirements using Conditional Styles
    - If you are not receiving a response to an article comment, please consider posting your question in the forum section (called 'Answers'). Wiki comment notifications are only sent to article author, who may or may not be available. The Answers section always has active users and you are more likely to receive a response.

    Link to comment
    Share on other sites

    You can use a similar logic as shown for Sunday to designate a different color for a specific date - just use equal to instead the modulo operation to meet your needs. As for a relative date holiday such as US Thanksgiving Day for every fourth Thursday of November, you will have to make a decision based on month and date ranging from 23rd to 28th of November to see which one has remainder of 4 when divided by 7 with weekday offset on the first day of the month.  

    Link to comment
    Share on other sites



    Create an account or sign in to comment

    You need to be a member in order to leave a comment

    Create an account

    Sign up for a new account in our community. It's easy!

    Register a new account

    Sign in

    Already have an account? Sign in here.

    Sign In Now

×
×
  • Create New...