Jump to content
JasperReports Library 7.0 is now available ×

Dual Y axis in line chart


cho

Recommended Posts

  • Replies 9
  • Created
  • Last Reply

Top Posters In This Topic

Right now JasperReports doesn't have built in support for dual-axis charts - you have to write some custom code in a scriplet to get one. Funny - this is the fourth time somebody asked me about this this week, and the average was about 4x a year up until now. I'm working on some charting enhancements so I'll take a look at adding a second y axis, potentially with a seperate type of chart too.

 

-Barry

Link to comment
Share on other sites

I spent some time playing around with this over the weekend and have most of it working. I need to do some clean up around plot options, and add the ability to label each axis. The hard part is done though, so I'll clean it up soon and get it in a future build.

 

size=270]http://www.jasperforge.org/components/com_joomlaboard/uploaded/images/img_0_10_8.jpg

 

-Barry

Link to comment
Share on other sites

Thanks for the reply. We were trying to plot the dual Y axis graph writing a scriptlet.

 

We successfully created dual Y axis, but we need to loop thru the data set to plot the Y values in the graph. Can you post the code how to loop thru the dataset and plot the graph using scriptlets?

 

Attached is the sample report we need to generate.

 

-For each ID the graph needs to be generated in new page based on the IDs data.

 

-The X-Axis Category names need to be modified based on user parameter. For EX: If user selects day the X_axis values are 2/1,2/1.. If parameter is Quarter , the X_axis values are June2006, July2006...Right now, this is String data type in our report.

 

Post edited by: cho, at: 2006/08/22 20:22 [file name=quarter.doc size=76288]http://www.jasperforge.org/components/com_joomlaboard/uploaded/files/quarter.doc[/file]

Post edited by: cho, at: 2006/08/22 20:23

Link to comment
Share on other sites

//scriptlet.java

 

import java.awt.Color;

import java.awt.Font;

 

import net.sf.jasperreports.engine.JRDefaultScriptlet;

import net.sf.jasperreports.engine.JRScriptletException;

import net.sf.jasperreports.renderers.JCommonDrawableRenderer;

 

import org.jfree.chart.ChartFactory;

import org.jfree.chart.JFreeChart;

import org.jfree.chart.axis.AxisLocation;

import org.jfree.chart.axis.NumberAxis;

import org.jfree.chart.plot.CategoryPlot;

import org.jfree.chart.plot.PlotOrientation;

import org.jfree.chart.renderer.category.LineAndShapeRenderer;

import org.jfree.chart.title.TextTitle;

import org.jfree.data.category.DefaultCategoryDataset;

import org.jfree.ui.HorizontalAlignment;

import org.jfree.ui.RectangleEdge;

 

 

public class retail_cvr_by_loc3Scriptlet extends it.businesslogic.ireport.IReportScriptlet {

 

/** Creates a new instance of JRIreportDefaultScriptlet */

public retail_cvr_by_loc3Scriptlet() {

 

 

}

 

 

/** Begin EVENT_AFTER_COLUMN_INIT This line is generated by iReport. Don't modify or move please! */

public void afterColumnInit() throws JRScriptletException

{

super.beforeColumnInit();

}

/** End EVENT_AFTER_COLUMN_INIT This line is generated by iReport. Don't modify or move please! */

/** Begin EVENT_AFTER_DETAIL_EVAL This line is generated by iReport. Don't modify or move please! */

public void afterDetailEval() throws JRScriptletException

{

super.afterDetailEval();

}

/** End EVENT_AFTER_DETAIL_EVAL This line is generated by iReport. Don't modify or move please! */

/** Begin EVENT_AFTER_GROUP_INIT This line is generated by iReport. Don't modify or move please! */

public void afterGroupInit(String groupName) throws JRScriptletException

{

//super.afterGroupInit(groupName);

 

DefaultCategoryDataset dataset = new DefaultCategoryDataset();

 

dataset.addValue((java.math.BigDecimal)getFieldValue("TRAFFIC"), "Traffic", (java.lang.String)getFieldValue("GRAPH_X_AXIS"));

 

 

// create the chart...

JFreeChart chart = ChartFactory.createLineChart(

"Java Standard Class Library", // chart title

(java.lang.String)getParameterValue("p_period"), // domain axis label

"Traffic", // range axis label

dataset, // data

PlotOrientation.VERTICAL, // orientation

false, // include legend

true, // tooltips

false // urls

);

 

chart.addSubtitle(new TextTitle("Number of Classes By Release"));

TextTitle source = new TextTitle("Source: Java In A Nutshell (4th Edition) by David Flanagan (O’Reilly)");

source.setFont(new Font("SansSerif", Font.PLAIN, 10));

//source.setPosition(RectangleEdge.BOTTOM);

//source.setHorizontalAlignment(HorizontalAlignment.RIGHT);

chart.addSubtitle(source);

chart.setBackgroundPaint(Color.white);

 

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

plot.setBackgroundPaint(Color.lightGray);

plot.setRangeGridlinePaint(Color.white);

 

 

// customise the range axis...

NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();

rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());

 

NumberAxis axis2 = new NumberAxis("Conversion");

axis2.setRange(5, 500);

plot.setRangeAxis(1, axis2);

//plot.setRangeAxisLocation(1, AxisLocation.BOTTOM_OR_RIGHT);

 

 

// customise the renderer...

LineAndShapeRenderer renderer = (LineAndShapeRenderer) plot.getRenderer();

renderer.setShapesVisible(true);

renderer.setDrawOutlines(true);

renderer.setUseFillPaint(true);

renderer.setFillPaint(Color.white);

 

this.setVariableValue("Chart", new JCommonDrawableRenderer(chart));

}

/** End EVENT_AFTER_GROUP_INIT This line is generated by iReport. Don't modify or move please! */

/** Begin EVENT_AFTER_PAGE_INIT This line is generated by iReport. Don't modify or move please! */

public void afterPageInit() throws JRScriptletException

{

super.afterPageInit();

}

/** End EVENT_AFTER_PAGE_INIT This line is generated by iReport. Don't modify or move please! */

/** Begin EVENT_AFTER_REPORT_INIT This line is generated by iReport. Don't modify or move please! */

public void afterReportInit() throws JRScriptletException

{

}

/** End EVENT_AFTER_REPORT_INIT This line is generated by iReport. Don't modify or move please! */

/** Begin EVENT_BEFORE_COLUMN_INIT This line is generated by iReport. Don't modify or move please! */

public void beforeColumnInit() throws JRScriptletException

{

 

}

/** End EVENT_BEFORE_COLUMN_INIT This line is generated by iReport. Don't modify or move please! */

/** Begin EVENT_BEFORE_DETAIL_EVAL This line is generated by iReport. Don't modify or move please! */

public void beforeDetailEval() throws JRScriptletException

{

 

}

/** end EVENT_BEFORE_DETAIL_EVAL Please don't touch or move this comment*/

 

/** End EVENT_BEFORE_DETAIL_EVAL This line is generated by iReport. Don't modify or move please! */

/** Begin EVENT_BEFORE_GROUP_INIT This line is generated by iReport. Don't modify or move please! */

public void beforeGroupInit(String groupName) throws JRScriptletException

{

}

/** End EVENT_BEFORE_GROUP_INIT This line is generated by iReport. Don't modify or move please! */

/** Begin EVENT_BEFORE_PAGE_INIT This line is generated by iReport. Don't modify or move please! */

public void beforePageInit() throws JRScriptletException

{

}

 

/** End EVENT_BEFORE_PAGE_INIT This line is generated by iReport. Don't modify or move please! */

/** Begin EVENT_BEFORE_REPORT_INIT This line is generated by iReport. Don't modify or move please! */

public void beforeReportInit() throws JRScriptletException

{

 

}

 

/** End EVENT_BEFORE_REPORT_INIT This line is generated by iReport. Don't modify or move please! */

 

}

Link to comment
Share on other sites

Is the data you need for both axes in the dataset? If so its pretty easy. When you create the second axis and add it to the plot you need to set the dataset to use for the axis too. Try adding

 

plot.setDataset(1, dataset);

plot.mapDatasetToRangeAxis(1, 1);

 

right after your call to plot.setRangeAxis.

 

-Barry

Link to comment
Share on other sites

timmyd wrote:

Thanks Barry.

The dual-y axis is a requirement that I have too, so I look forward to the final product.

Tim

 

I've got all the development work wrapped up, and as soon as JR/iR 1.2.6 get through QA and released I'll check the code in. I decided to hold off putting it into 1.2.6 since Giulio won't have time to add support in iReport. I'll post a followup when its checked in and folks can grab the latest code to play with it, assuming they can live without iReport support for a little while.

 

-Barry

Link to comment
Share on other sites

1.2.6 is out, so I just checked all the code into the main code tree, still on SourceForge. If you pull the latest source and build it, check out the new reports in the charts sample.

 

I haven't integrated the multiple axis charts with the new chart hyperlink support yet, so you can't create 1.2.6 style chart hyperlinks and have them work in a multiple axis chart.

 

-Barry

Link to comment
Share on other sites

  • 6 months later...

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