Jump to content
Changes to the Jaspersoft community edition download ×

Workaround-Passing vars to JRChartCustomizer


2004 IR Help

Recommended Posts

By: Kip Yeackley - yeackley

Workaround-Passing vars to JRChartCustomizer

2005-11-03 04:59

Currently implementations of the JRChartCustomizer interface cannot directly access variables or parameters defined in a report.

 

One way to send a variable or parameter value in is to leverage any one of the unused chart properties that are available on a specific chart type.

 

One constraint is that the object type of the variable value must be compatible with the JFreeChart attribute being used. Another is that the JFreeChart chart property should either be unset, overridden, or adjusted as necessary inside the JRChartCustomizer in order to behave as desired during chart rendering.

 

Here's an example.

 

I have a TimeSeriesChart in which I'd like to customize the DateTickUnits based upon a TimePeriod parameter defined in the report. In my case I'm not displaying a time axis label, so I use that as a way to pass my TimePeriod parameter value into the customizer class like this: <timeAxisLabelExpression><![CDATA[$P{TimePeriod}]]></timeAxisLabelExpression>

 

Now inside the customizer class I can obtain the runtime value of this parameter and use it to alter the customizations performed. Here's the relevant code from the customizer class:

 

XYPlot plot = (XYPlot) chart.getPlot();

 

// customize the domain axis to override standard date format

// and adjust the tick unit scale based on parameter value

DateAxis axis = (DateAxis) plot.getDomainAxis();

 

// we are utilizing the unused Label field in order to pass a

// runtime parameter value from the JasperReport environment

if (axis.getLabel().equalsIgnoreCase("day")) {

axis.setDateFormatOverride(new SimpleDateFormat("dd-MMM-yyyy"));

axis.setTickUnit(new DateTickUnit(DateTickUnit.DAY, 1));

axis.setVerticalTickLabels(true);

}

else if (axis.getLabel().equalsIgnoreCase("week")) {

axis.setDateFormatOverride(new SimpleDateFormat("dd-MMM-yyyy"));

axis.setTickUnit(new DateTickUnit(DateTickUnit.DAY, 7));

axis.setVerticalTickLabels(true);

}

else if (axis.getLabel().equalsIgnoreCase("month")) {

axis.setDateFormatOverride(new SimpleDateFormat("MMM-yyyy"));

axis.setTickUnit(new DateTickUnit(DateTickUnit.MONTH, 1));

}

 

// even though the Label field is unused, let's unset it just in case.

// alternatively you could override the value to something meaningful

// and use it if needed.

axis.setLabel(null);

 

Sometimes you may not have an available chart property to reuse in this way, but if you do you could also use a tokenization approach to send multiple values through a single chart property as needed.

 

 

 

 

 

 

By: Lucian Chirita - lucianc

RE: Workaround-Passing vars to JRChartCustomi

2005-11-18 01:03

Hi

 

An abstract implementation of JRChartCustomizer that provides access to parameter, variable and field value has been created and commited to CVS.

 

Whoever needs such values in the chart customizer should extend net.sf.jasperreports.engine.JRAbstractChartCustomizer and use the getParameterValue(..) et al. methods.

 

Regards,

Lucian

Link to comment
Share on other sites

  • 5 months later...
  • Replies 1
  • Created
  • Last Reply

Top Posters In This Topic

Top Posters In This Topic

Hi

This is Atanu. I am very new in ireport and also in java environment. In the ireport I have designed a template .like this.

please look on attach file

size=150C:Documents and SettingsAtanuMy Documentsgraph.jpeg[/img]

Now I want to plot a line graph on the basis of the each data like t1 t2 ---t5 and lcl, ucl, actual value for each row.

That I have done. Now my problem is that I have to change the color of points of t1 t2 ---t5 on the basis of Lcl and Ucl . If the value of t1 ,or t2 or ----t5 are between Ucl and Lcl the color will be blue. And if it out side of Lcl and Ucl then it will be red..

I want the following figure. Where the upper bold line is Ucl and lower bold line isLcl

size=150C:Documents and SettingsAtanuMy Documentsdata.jpeg[/img]

Actually I have got a information that for that I have to write a customizer class for chart. Also I have write it .

 

Code:

import net.sf.jasperreports.engine.*;
import net.sf.jasperreports.view.JasperViewer;
import net.sf.jasperreports.engine.JREmptyDataSource;
import java.util.*;
import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.SQLException;
import net.sf.jasperreports.engine.JRAbstractChartCustomizer;
import javax.sql.*;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.renderer.category.*;
import org.jfree.chart.renderer.*;
import org.jfree.chart.ChartRenderingInfo;
import org.jfree.chart.renderer.category.BarRenderer;
import org.jfree.chart.renderer.category.LevelRenderer;
import org.jfree.chart.renderer.category.LayeredBarRenderer;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Connection;
import java.util.HashMap;
import java.util.Map;
import java.lang.String;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperExportManager;
import net.sf.jasperreports.view.JasperViewer;
import net.sf.jasperreports.engine.xml.JRXmlLoader;
import net.sf.jasperreports.engine.JasperCompileManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.design.JasperDesign;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.engine.JRChartDataset;
import net.sf.jasperreports.engine.JRChartPlot;
import org.jfree.chart.renderer.category.BarRenderer3D;
import org.jfree.chart.renderer.category.LineAndShapeRenderer;
import org.jfree.chart.axis.CategoryLabelPositions;


import org.jfree.chart.renderer.category.LineRenderer3D;
import java.awt.*;
import net.sf.jasperreports.engine.JRChart;
import net.sf.jasperreports.engine.JRChartCustomizer;
import org.jfree.chart.JFreeChart;
import net.sf.jasperreports.engine.*;
import java.awt.Color;
import net.sf.jasperreports.engine.JRChart;
import net.sf.jasperreports.engine.JRChartCustomizer;

import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.axis.CategoryLabelPositions;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.renderer.category.BarRenderer3D;
import java.lang.Object;



public class LineChartCustomizer extends JRAbstractChartCustomizer {

public void customize(JFreeChart chart, JRChart jasperChart) {

LineAndShapeRenderer renderer = (LineAndShapeRenderer) chart.getCategoryPlot().getRenderer();


renderer.setSeriesPaint(1, Color.green);
renderer.setSeriesPaint(4, Color.orange);

chart.setTitle("Setting the title "«»);
}


}

Now this customize class ok running successfully with creating jar files.

Now I want to create a logic to check the value of t1 t2 t3 t5 with respect to UCL and LCL. How can I fetch data from report.

Or is there any other way to do this.

Is there any way to pass value from iReport to customizer class ?

 

What should I do. Please please help me. It’s urget for me.

 

Any help would be greatly appreciated.

Thanks in advance.

[file name=query-37ab101e3a539a71be9c962f72749d92.doc size=49152]http://www.jasperforge.org/components/com_joomlaboard/uploaded/files/query-37ab101e3a539a71be9c962f72749d92.doc[/file]

[file name=query-2a31efb2100005a59a4201182132d758.doc size=49152]http://www.jasperforge.org/components/com_joomlaboard/uploaded/files/query-2a31efb2100005a59a4201182132d758.doc[/file]

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