Hello,

i have used ireport tool for creating jrxml template for creating chart(XYLine chart).
and i used this jrxml file in java code to create chart.
Here i passed csv file as data source.
Now i can freely change color,title,subtitle,font ect from java code .
what i want is how to draw a horizontal line in that chart at particular point through java code.
Please suggest me in a right direction.

Thanks in Advance.

Dani AI

Generated

Short answer: use a chart customizer and add a JFreeChart ValueMarker to the plot (a range marker draws a horizontal line). was right to point at the API — JasperReports calls your customizer at fill time so you can change the JFreeChart object before it is rendered. (jasperreports.sourceforge.net)

Example customizer (compute or pass the Y value as a parameter/variable, then add a ValueMarker):

public class HorizontalLineCustomizer extends net.sf.jasperreports.engine.JRAbstractChartCustomizer {
  @Override
  public void customize(org.jfree.chart.JFreeChart chart, net.sf.jasperreports.engine.JRChart jasperChart) {
    org.jfree.chart.plot.XYPlot plot = (org.jfree.chart.plot.XYPlot) chart.getPlot();

    // obtain a number computed in the report (variable) or passed as a parameter
    Object v = getParameterValue("AVG_Y"); // or getVariableValue("AVG_Y")
    double y = (v instanceof Number) ? ((Number)v).doubleValue() : 0.0;

    org.jfree.chart.plot.ValueMarker marker = new org.jfree.chart.plot.ValueMarker(y);
    marker.setPaint(java.awt.Color.RED);
    marker.setStroke(new java.awt.BasicStroke(1.5f));
    marker.setLabel("Avg: " + y);
    // add to foreground so it is drawn over the series
    plot.addRangeMarker(marker, org.jfree.chart.ui.Layer.FOREGROUND);
  }
}

Notes and gotchas:

  • Extend JRAbstractChartCustomizer to access report parameters/variables from the customizer. (infosys.ars.usda.gov)
  • If your report parameter does not show up in the customizer, map it as a chart/dataset parameter (use a <datasetRun>/<datasetParameter> mapping in JRXML) so the chart-level code can read it. (stackoverflow.com)
  • Make sure the compiled customizer jar is on JasperReports/iReport classpath and set the chart element's customizerClass attribute in the JRXML. You may also need to adjust chart evaluationTime/evaluationGroup so the value is available when the chart is filled. (jasperreports.sourceforge.net)
  • Imports for label anchors/text anchors moved between JFreeChart versions (older builds use org.jfree.ui.; newer 1.5.x use org.jfree.chart.ui.). If you get a NoClassDefFoundError, check which package your JFreeChart exposes. (javadoc.io)

Alternative: if you prefer not to use markers, add a constant series (two X points at min/max with same Y) to the dataset and style it; but ValueMarker is simpler and does not affect the dataset. (javadoc.io)

This approach answers and also addresses 's request for a dynamic "average" line driven from report data.

Recommended Answers

All 3 Replies

I guess I'd read the API doc for the package and see what it offers.

ok.
Can any one tell me how to edit series expression of chart through java code?
thanks in advance.

Hi,

I'm new to Jasper reporting and it will be very helpful to me to know how to add a horizontal line (Showing average value in Y axis) dynamically to a Line chart in jasper reporting. I am familiar with customizer class and I want to clarify whether it can be done through cutomizer class.

Any help is appriciated

Thanks...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.