Handling null values in charts

0

How to write a customizer class for bar chart, pie chart ?? (that should handle no data in subdataset & show proper No Data message) How to upload it to ireport ?? Anyone, please give some clear explanation on it.. please

subashmy's picture
Joined: Jan 3 2012 - 2:01am
Last seen: 4 years 11 months ago

5 Answers:

0

Hi subashmy, 

What you can do is to create a variable that depends on the value of $F{totalCt} and another one that depends on $F{totalMin}, the "Variable Expression" should be as follows: 

$F{totalCt} == null ? Double.valueOf("0.0") : $F{totalCt} --> For $V{TotalCt} variable

$F{totalMin} == null ? Double.valueOf("0.0") : $F{totalMin} --> For $V{TotalMin} variable

This way, if the value of the fields is null you'll have 0.00 in the variable, and otherwise you'll have the same value you have in the field. The you should change the variable expression in orther to use variables instead of fields: 

' Summary

Total # of calls: ' + new DecimalFormat("#,000").format($V{totalCt}) + '

Total minutes: ' + new DecimalFormat("#,000").format($V{totalMin})

Maybe there is an easier option but I think this should work for you. 

Let us know if you solve your problem.

Hope this helps,

Aitor

augarte's picture
521
Joined: Jan 27 2010 - 7:20am
Last seen: 1 year 3 months ago

Hey Aitor, I have integer for both fields, so I am using the following expression..
$F{totalCt} == null ? 0 : $F{totalCt} for variable $V{TotalCt} & similarly other expression also..but still

Error evaluating expression....

subashmy - 6 years 7 months ago

Hi,

This works with Integer type field/variable:

$F{totalCt} == null ? Integer.valueOf("0") : $F{totalCt}

augarte - 6 years 7 months ago
0

Hi, 

First of all you need to create a customize class. The following is an example of a Line Chart customizer: 

 

package com.almis.awe.chartcustomizer;
 
import java.awt.Color;
import java.awt.BasicStroke;
 
import net.sf.jasperreports.engine.JRChart;
import net.sf.jasperreports.engine.JRChartCustomizer;
 
import org.jfree.chart.JFreeChart;
import org.jfree.data.category.*;
import org.jfree.chart.renderer.category.LineAndShapeRenderer;
 
import java.util.HashMap;
/**
 *
 * @author augarte
 */
public class AweLineChartCustomizer implements JRChartCustomizer{
    public static HashMap engGroupColor = new HashMap();
    static {
        engGroupColor.put("A", new Color(0xE4, 0x1B, 0x1B));
        engGroupColor.put("B", new Color(0xF1, 0xAB, 0x10));
       }
 
 
 
 
    public void customize(JFreeChart chart, JRChart jasperChart) {
 
        // Category Plot: most commonly used to display bar chart
        // BarRenderer: create bar charts from data in a category dataset
 
        LineAndShapeRenderer renderer = (LineAndShapeRenderer) chart.getCategoryPlot().getRenderer();
 
        // Method required for reading the dataset.
        // Table of values that can be accessed using row and column keys.
        CategoryDataset cd = chart.getCategoryPlot().getDataset();
 
        // Row gives the series expression
        int rc = cd.getRowCount();
        for (int i = 0; i
           renderer.setSeriesStroke(i, new BasicStroke(2f));
        }
    }
 
}
 
Then you need to add it to iReport. What I do is to add all my classes to a jar file and the add this file to iReport classpath (Tools->Options->iReport, inside the "classpath" tab). 
 
The last step is to say to the chart to use this customize. If you select the chart (inside your report), in the right side you will see that there is a property "customizer class". You have to set the customizer here, in my case. 
 
com.almis.awe.chartcustomizer.AweLineChartCustomizer
 
Hope this helps. 
 
Best regards,
 
Aitor
augarte's picture
521
Joined: Jan 27 2010 - 7:20am
Last seen: 1 year 3 months ago

Thanks augarte, it helps... I wil get back to you on this.. meanwhile can you tellme how to deal with the following type of error??

Source text : '<b> Summary </b><p> Total # of calls: ' + new DecimalFormat("#,000").format($F{totalCt}) + '<p> Total minutes: ' + new DecimalFormat("#,000").format($F{totalMin})
net.sf.jasperreports.engine.fill.JRExpressionEvalException: Error evaluating expression :
Source text : '<b> Summary </b><p> Total # of calls: ' + new DecimalFormat("#,000").format($F{totalCt}) + '<p> Total minutes: ' + new DecimalFormat("#,000").format($F{totalMin}) ...........

I couldn't figure out how to resolve this... I have a report & four datasets, in two of the datasets, the data is coming as blank... so how should I deal with that???

subashmy - 6 years 7 months ago
0

What is that exactly? Where are you adding the following expression?

' Summary

Total # of calls: ' + new DecimalFormat("#,000").format($F{totalCt}) + '

Total minutes: ' + new DecimalFormat("#,000").format($F{totalMin})

Something is wrong here with this expression. If you check the DecimalFormat javadoc you will see that there is not any "format" function with one parameter: 

http://docs.oracle.com/javase/6/docs/api/java/text/DecimalFormat.html

So you have at least one error in this expression. Anyway, it would be easier to help you if you explain what do you want to do. From the expression I guess you need to add some data using "HTML markup" but don't know exactly what you want. 

Regards,

Aitor

 

augarte's picture
521
Joined: Jan 27 2010 - 7:20am
Last seen: 1 year 3 months ago
0

 

Hi augarte,
My main report query is...
select  COUNT(*) as totalCt ,SUM(callDurationTotalMinutes) as totalMin from vw_DMAProductionCalls where callType in ('VMR', 'Pt-pt outbound') and targetEndpoint 'gatekeeper-monitoring-check' and callStartMonth = $P{month} and callStartYear = $P{year}
 
& my variable expression is...
' Summary

Total # of calls: ' + new DecimalFormat("#,000").format($F{totalCt}) + '

Total minutes: ' + new DecimalFormat("#,000").format($F{totalMin})

 
This expression works well if I input '3 to 12' as value to $P{month}, but the above error comes if value is '1 or 2' as it doesn't have any data for january n february...
I have 4 other datasets, but the error occurs in the above expression only for 1,2 months..
Please help me out..
 
Regards,
Subash.
subashmy's picture
Joined: Jan 3 2012 - 2:01am
Last seen: 4 years 11 months ago

You need to post this new question as a new question. Please don't post it as an answer.

mdahlman - 6 years 7 months ago

Oh.. thnx mdahlman.. I will do that next time
I have figured out the problem is not with the expression, it is with the null data.. so I need to handle it in ireport or from database..

subashmy - 6 years 7 months ago
0

This one worked for me..

($V{member_id_MEASURE1} != 0 ? (new DecimalFormat(".00").format(($V{member_id_MEASURE1}/$V{member_id_MEASURE1_deny_reasons1_ALL}) * 100)) : 0 ) + "%"

I added the new DecimalFormat(".00").format( calculations)  the + "%" is to give it like a percent look.

 

mark.torres's picture
Joined: Mar 24 2014 - 9:26pm
Last seen: 5 years 3 months ago
Feedback