How do I create a jfreechart xyblock chart via Jasper Studio?


I'm trying to create a chart similar to the following: The chart in question uses an XYZDataset, however the Jasper Reports API only refers to the Bubble chart. There is no mention of XYBlock or XYShape charts.

To date I have used chart customisers to tweak charts where required but I'm wondering if I need to use a different approach in this instance. I've attempted to create a cusomiser for a bubble chart but need to work out how to override the default rendering of bubbles. I'm not sure if this is best use of my time.

I have been reading documentation on rendering images using third party APIs and it looks like I should be able to create a chart of my choice using a scriptlet. Is a valid approach?

Cheers's picture
Joined: Apr 14 2015 - 11:19pm
Last seen: 2 years 1 month ago

2 Answers:

The answer to my question is create a report (that uses an image element) with an appropriate scriptlet class. Create a variable with class attribute set to net.sf.jasperreports.engine.JRRenderable.

I used the afterReportInit() method where I set the variable using JCommonDrawableRenderer().

The chart uses a DateAxis that ranges between the first and last days of a month. I've tested the chart building code in a separate Java program and used the code in the scriptlet. The following statement "xAxis.setRange(end.getTime(), start.getTime());" works in my Java code but fails in the scriptlet with "net.sf.jasperreports.engine.JRException: java.lang.IllegalArgumentException: Requires 'lower' < 'upper'.". The relevant scriptlet code is as follows:

    public void afterReportInit() throws JRScriptletException {
        int year = 2016;
        int month = 1;
        int day = 1;
        Calendar[] dates = dateRange(year, month, day);
        Calendar start = dates[0];
        Calendar end = dates[1];
        XYZDataset dataset = createDataset(start, end);
        DateAxis xAxis = new DateAxis();
        xAxis.setRange(start.getTime(), end.getTime());
        NumberAxis yAxis = new NumberAxis();
        XYBlockRenderer renderer = new XYBlockRenderer();
        renderer.setBlockWidth(1000.0 * 60.0 * 60.0 * 24.0);
        XYPlot plot = new XYPlot(dataset, xAxis, yAxis, renderer);
        plot.setAxisOffset(new RectangleInsets(5, 5, 5, 5));
        JFreeChart chart = new JFreeChart(null, plot);
        this.setVariableValue("Chart", new JCommonDrawableRenderer(chart));
     private static Calendar[] dateRange(int year, int month, int day) {
        Calendar start = new GregorianCalendar(year, month, day);
        start.set(Calendar.DAY_OF_MONTH, 1);
        start.set(Calendar.HOUR_OF_DAY, 0);
        start.set(Calendar.MINUTE, 0);
        start.set(Calendar.SECOND, 0);
        start.set(Calendar.MILLISECOND, 0);
        int numberOfDays = start.getActualMaximum(Calendar.DAY_OF_MONTH);
        Calendar end = (Calendar) start.clone();
        end.set(Calendar.DAY_OF_MONTH, numberOfDays);
        end.set(Calendar.HOUR_OF_DAY, 23);
        end.set(Calendar.MINUTE, 59);
        end.set(Calendar.SECOND, 59);
        end.set(Calendar.MILLISECOND, 999);
        return new Calendar[] { start, end };

What possible reason could there be for this inconsistency?

On a different note, my scriplet are not picked up when I re-preview in Jasper Studio unless I restart the IDE which now entails having to exit. Can anyone tell me what happened to the restart menu option? I am running under Ubuntu 14.04.'s picture
Joined: Apr 14 2015 - 11:19pm
Last seen: 2 years 1 month ago

Problem solved! The method createDataset() was modifying the Calendar objects.'s picture
Joined: Apr 14 2015 - 11:19pm
Last seen: 2 years 1 month ago