Tuesday 24 March 2015

Conditional colors to the bars of HTML-5 Bar Chart - Applicable on other chart types like Line Chart.

This post is a re-blogging from Jasper WIKI pages on conditional coloring of bars for HTML-5 Bar Chart.


https://community.jaspersoft.com/wiki/dynamically-updating-bar-colors-html5-bar-charts

Quick Output Image :

JRXML: Run this JRXML on foodmart database.

<?xml version="1.0" encoding="UTF-8"?>
<!-- Created with Jaspersoft Studio version 6.0.0.final using JasperReports Library version 6.0.0  -->
<!-- 2015-03-24T10:39:11 -->
<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="HTML5BarChart-CustomiseColors" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="a146894c-9f63-42a0-bd25-21cce326e316">
    <property name="com.jaspersoft.studio.data.sql.tables" value="dGltZV9ieV9kYXkgQVMgLDI1LDc1O3NhbGVzX2ZhY3RfMTk5NyBBUyAsMTg0LDc5Ow=="/>
    <property name="com.jaspersoft.studio.data.defaultdataadapter" value="foodmart"/>
    <queryString language="SQL">
        <![CDATA[SELECT sum(sales_fact_1997.store_sales) as sales,
    time_by_day.the_month
FROM sales_fact_1997
    INNER JOIN time_by_day ON
     time_by_day.time_id = sales_fact_1997.time_id
     GROUP BY the_month, month_of_year
     ORDER BY month_of_year]]>
    </queryString>
    <field name="sales" class="java.math.BigDecimal"/>
    <field name="the_month" class="java.lang.String"/>
    <variable name="Variable_1" class="java.lang.String">
        <variableExpression><![CDATA[DATEFORMAT(new Date(),"MMMMM")]]></variableExpression>
    </variable>
    <background>
        <band splitType="Stretch"/>
    </background>
    <title>
        <band height="79" splitType="Stretch"/>
    </title>
    <summary>
        <band height="358" splitType="Stretch">
            <componentElement>
                <reportElement x="0" y="0" width="555" height="238" uuid="58498ba5-f781-4a59-8db0-70e30d626b77"/>
                <hc:chart xmlns:hc="http://jaspersoft.com/highcharts" xsi:schemaLocation="http://jaspersoft.com/highcharts http://jaspersoft.com/schema/highcharts.xsd" type="Column">
                    <hc:chartSetting name="default">
                        <hc:chartProperty name="chart.zoomType">
                            <hc:propertyExpression><![CDATA["xy"]]></hc:propertyExpression>
                        </hc:chartProperty>
                        <hc:chartProperty name="credits.enabled">
                            <hc:propertyExpression><![CDATA[false]]></hc:propertyExpression>
                        </hc:chartProperty>
                        <hc:chartProperty name="credits.href">
                            <hc:propertyExpression><![CDATA[""]]></hc:propertyExpression>
                        </hc:chartProperty>
                        <hc:chartProperty name="credits.text">
                            <hc:propertyExpression><![CDATA[""]]></hc:propertyExpression>
                        </hc:chartProperty>
                        <hc:chartProperty name="title.text">
                            <hc:propertyExpression><![CDATA[""]]></hc:propertyExpression>
                        </hc:chartProperty>
                        <hc:chartProperty name="yAxis.title.text">
                            <hc:propertyExpression><![CDATA[""]]></hc:propertyExpression>
                        </hc:chartProperty>
                    </hc:chartSetting>
                    <multiAxisData>
                        <multiAxisDataset/>
                        <dataAxis axis="Rows">
                            <axisLevel name="Month">
                                <labelExpression><![CDATA["Level Label expression"]]></labelExpression>
                                <axisLevelBucket order="None" class="java.lang.String">
                                    <bucketExpression><![CDATA[$F{the_month}]]></bucketExpression>
                                    <bucketProperty name="color"><![CDATA[IF($F{the_month}.toString().matches(DATEFORMAT(new Date(),"MMMMM")),"red",IF($F{sales}.intValue() >= 50000,"green","blue"))]]></bucketProperty>
                                </axisLevelBucket>
                            </axisLevel>
                        </dataAxis>
                        <dataAxis axis="Columns"/>
                        <multiAxisMeasure name="Sales" class="java.math.BigDecimal" calculation="Nothing">
                            <labelExpression><![CDATA["Sales"]]></labelExpression>
                            <valueExpression><![CDATA[$F{sales}]]></valueExpression>
                        </multiAxisMeasure>
                    </multiAxisData>
                    <hc:series name="Sales">
                        <hc:contributor name="SeriesItemProperty">
                            <hc:contributorProperty name="color" valueType="Bucket" value="Month.color"/>
                        </hc:contributor>
                    </hc:series>

                </hc:chart>
            </componentElement>
        </band>
    </summary>
</jasperReport>

:-)

No comments:

Post a Comment