Jump to content
Changes to the Jaspersoft community edition download ×

Handling null values in charts


subashmy
Go to solution Solved by augarte,

Recommended Posts

  • Replies 10
  • Created
  • Last Reply

Top Posters In This Topic

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 < rc; 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
Link to comment
Share on other sites

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 : ' Summary

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

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

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

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???

Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

 

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.
Link to comment
Share on other sites

  • Solution

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

Link to comment
Share on other sites

  • 1 year later...

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.

 

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...