Customizing a chart - for newbies!

0
Hello, I need to customize a chart in my report. Just basic things like the range, line colors, position of legend, etc. I understand that I have to use JRChartCustomizer to do this. Unfortunately, I've never programmed a line of java in my life, although I've used other programming languages. As has been pointed out by others, there is no step by step guide for those who are computer literate, but not familiar with java. Over and over, I've read something like, "To customize a chart all you have to do is implement the JRChartCustomizer interface..." Unfortunately, there are still lots of gaps for me. So what I'd like to do is work out a step by step process for the non Java programmer. If you veteran programmers out there could help me along, it will benefit quite a few people I think. OK, here we go. From what I can see, I need to write a custom snippet of code that will modify certain aspects of my chart. You tell iReport which snippet of code is to be used by: a) right clicking on the chart and selecting "Chart Properties" b) entering the name of the JAR file that contains the snippet of code in the field marked: Customizer Class. This is where I get fuzzy! Based on various posts I've put together some java code that I think will stipulate the colors of the first 2 lines on my line chart. Its is called "LineChartCustomizer.java" and the contents are as follows:
Code:
import net.sf.jasperreports.engine.JRChartCustomizer; 
 
import net.sf.jasperreports.engine.JRChartDataset; 
 
import net.sf.jasperreports.engine.JRChartPlot; 
 
import net.sf.jasperreports.engine.JRChart; 
 
import org.jfree.chart.JFreeChart; 
 
import org.jfree.chart.renderer.category.BarRenderer; 
 
import org.jfree.chart.axis.CategoryLabelPositions; 
 
 
 
import java.awt.*; 
 
 
 
public class LineChartCustomizer implements JRChartCustomizer { 
 
 
 
	public void customize(JFreeChart chart, JRChart jasperChart) {
 
 
 
		ChartRenderer renderer = (ChartRenderer) chart.getCategoryPlot().getRenderer(); 
 
		renderer.setSeriesPaint(0, Color.green); 
 
		renderer.setSeriesPaint(1, Color.orange); 
 
	} 
 
}
 
</td></tr></tbody></table>
 
 
 
Please, advise if there are any problems in the code above.
 
 
 
OK, I know this is all BASIC stuff for you Java programmers, but not for the rest of us.  Apparently, creating a JAR file requires 2 commands from the command prompt:
 
a) javac LineChartCustomizer.java (which will create LineChartCustomizer.class)
 
b) jar cf LineChartCustomizer (which will create LineChartCustomizer.jar)
 
 
 
Then, in iReport you go to Options->Class Path and tell iReport where the LineChartCustomizer.jar file is.  Apparently, you can add a single jar file or an entire folder of jar files.
 
 
 
Again, please correct my mistakes above.  I haven't actually successfully made it through these steps yet!
 
 
 
Here is where I am failing:
 
When I run javac LineChartCustomizer.java, I get the following errors:
 
<table cellpadding="0" cellspacing="0" align="center" width="525" bgcolor="#cccccc"><tr><td>
 
C:\Program Files\JasperSoft\iReport-1.2.6>javac LineChartCustomizer.java
 
LineChartCustomizer.java:1: package net.sf.jasperreports.engine does not exist
 
import net.sf.jasperreports.engine.JRChartCustomizer;
 
                                   ^
 
LineChartCustomizer.java:2: package net.sf.jasperreports.engine does not exist
 
import net.sf.jasperreports.engine.JRChartDataset;
 
                                   ^
 
LineChartCustomizer.java:3: package net.sf.jasperreports.engine does not exist
 
import net.sf.jasperreports.engine.JRChartPlot;
 
                                   ^
 
LineChartCustomizer.java:4: package net.sf.jasperreports.engine does not exist
 
import net.sf.jasperreports.engine.JRChart;
 
                                   ^
 
LineChartCustomizer.java:5: package org.jfree.chart does not exist
 
import org.jfree.chart.JFreeChart;
 
                       ^
 
LineChartCustomizer.java:6: package org.jfree.chart.renderer.category does not e
 
xist
 
import org.jfree.chart.renderer.category.BarRenderer;
 
                                         ^
 
LineChartCustomizer.java:7: package org.jfree.chart.axis does not exist
 
import org.jfree.chart.axis.CategoryLabelPositions;
 
                            ^
 
LineChartCustomizer.java:11: cannot find symbol
 
symbol: class JRChartCustomizer
 
public class LineChartCustomizer implements JRChartCustomizer {
 
                                            ^
 
LineChartCustomizer.java:13: cannot find symbol
 
symbol  : class JFreeChart
 
location: class LineChartCustomizer
 
        public void customize(JFreeChart chart, JRChart jasperChart) {
 
                              ^
 
LineChartCustomizer.java:13: cannot find symbol
 
symbol  : class JRChart
 
location: class LineChartCustomizer
 
        public void customize(JFreeChart chart, JRChart jasperChart) {
 
                                                ^
 
LineChartCustomizer.java:15: cannot find symbol
 
symbol  : class ChartRenderer
 
location: class LineChartCustomizer
 
                ChartRenderer renderer = (ChartRenderer) chart.getCategoryPlot()
 
.getRenderer();
 
                ^
 
LineChartCustomizer.java:15: cannot find symbol
 
symbol  : class ChartRenderer
 
location: class LineChartCustomizer
 
                ChartRenderer renderer = (ChartRenderer) chart.getCategoryPlot()
 
.getRenderer();
 
                                          ^
 
12 errors
 
 
 
C:\Program Files\JasperSoft\iReport-1.2.6>
 
</td></tr></table>
 
 
 
Clearly, Java has no idea where all the classes that I am importing are located.  Without them, the creation of the class file fails.
 
 
 
So here are my questions:
 
1) What do I need to do to successfully compile the class file?
 
2) After that, do I simply type "jar cf LineChartCustomizer" and the jar file will be ready to go?  Or is there more setup required?
 
 
 
Thanks for helping out the non-programmers who love iReport and JasperReports!
 
 
 
Sincerely,
 
 
 
Scott
scottward's picture
Joined: Sep 1 2006 - 9:26am
Last seen: 8 years 3 months ago

6 Answers:

0
Can anybody shed some light on this? It'd be a great help to quite a few of us, I think. Scott
scottward's picture
Joined: Sep 1 2006 - 9:26am
Last seen: 8 years 3 months ago
0
OK, if responses aren't coming because I should ask the question somewhere else, please advise. Or if I need to ask it differently, let me know. Scott
scottward's picture
Joined: Sep 1 2006 - 9:26am
Last seen: 8 years 3 months ago
0
Hi. You need to use javac's -classpath or -cp option to inlcude some .jar files in the classpath. These jars must contain all classes your code references. I don't exactly know which .jars you need to include, but there is a big chance that you only need jasperreports-1.2.x.jar. Open an editor and make a .bat / .sh script that will call javac with -classpath /some/path/jasperreports-1.2.x.jar:/some/path/someOther.jar... (making a script is good in case you need to change and recompile your code later.. you know that already.)
borut.hadzialic's picture
Joined: Oct 5 2006 - 3:43am
Last seen: 8 years 2 months ago
0

Post edited by: atanupanda, at: 2007/02/05 12:15
atanupanda's picture
Joined: Dec 21 2006 - 9:51pm
Last seen: 7 years 12 months ago
0
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 [img size=150]C:\Documents and Settings\Atanu\My Documents\graph.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 [img size=150]C:\Documents and Settings\Atanu\My Documents\data.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 "«»); 
 
	}        
 
 
 
 
 
}
 
</td></tr></tbody></table>
 
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]
 
 
 
Post edited by: atanupanda, at: 2007/02/06 10:06<br>Post edited by: atanupanda, at: 2007/02/06 10:09
atanupanda's picture
Joined: Dec 21 2006 - 9:51pm
Last seen: 7 years 12 months ago
0

no need html tag <br/> plz remove all the <br> and other html tags if exist .. then place ur generated .class file in the loc where the jrxml lies or export the java class as a jar file and include it in the  Tools > Option > classpath..

saradhasivaraman's picture
Joined: Oct 15 2012 - 9:15am
Last seen: 2 years 3 weeks ago
Feedback