Problem with ClusteredXYBarRenderer

A free public discussion forum for the JFreeChart class library.

Problem with ClusteredXYBarRenderer

Postby Daniil » Tue Apr 15, 2008 3:16 pm

I use the following class for bar charts generation. If renderer is set to StackedXYBarRenderer, everything works fine. If renderer is set to ClusteredXYBarRenderer, empty chart is generated and there are no bars on it. Can anyone help me with it?


Code: Select all
package chart;

import org.jfree.data.time.*;
import org.jfree.chart.renderer.xy.*;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.plot.PlotOrientation;

import java.util.Iterator;
import java.util.Map;
import java.awt.*;

public class ChartBar2D extends Chart {

    public ChartBar2D(String title, String categoryAxisLabel, String valueAxisLabel, boolean autoRange, double lowerLimit, double upperLimit, DateDoubleDataSource data) {

        TimeTableXYDataset dataset = new TimeTableXYDataset();
        XYBarRenderer renderer = new StackedXYBarRenderer();
//        XYBarRenderer renderer = new ClusteredXYBarRenderer();
        renderer.setBaseSeriesVisible(true);

        for (Iterator iter = data.map.entrySet().iterator(); iter.hasNext();) {
            Map.Entry entry = (Map.Entry) iter.next();
            String name = (String) entry.getKey();
            DateDoubleDataSource.DateDouble[] points = (DateDoubleDataSource.DateDouble[]) entry.getValue();
            for (int i = 0; i < points.length; ++i) {
                dataset.add(new FixedMillisecond(points[i].date), points[i].value.doubleValue(), name);
            }
            renderer.setSeriesPaint(dataset.getSeriesCount() - 1, (Color)data.colorMap.get(name));
        }

        chart = ChartFactory.createXYBarChart(
                title,                      // title
                categoryAxisLabel,          // x-axis label
                true,                       // make the domain axis display dates
                valueAxisLabel,             // y-axis label
                dataset,                    // data
                PlotOrientation.VERTICAL,   // bar orientation
                true,                       // create legend?
                true,                       // generate tooltips?
                false                       // generate URLs?
        );

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

    }
}
Daniil
 
Posts: 5
Joined: Tue Apr 15, 2008 3:07 pm
Location: Saratov, Russia

Postby david.gilbert » Wed Apr 16, 2008 4:36 pm

It may well be a bug, but it would be easier to tell if you could post a small self-contained demo program that shows the problem. The code you've posted won't compile outside of your environment. It gives me an idea of what you're doing, but not the detail.
David Gilbert
JFreeChart Project Leader

:idea: Read my blog
:idea: Ask your company to buy the JFreeChart Developer Guide
:idea: Check out other products sold by my company Object Refinery Limited
david.gilbert
JFreeChart Project Leader
 
Posts: 11367
Joined: Fri Mar 14, 2003 10:29 am

Postby Daniil » Thu Apr 17, 2008 10:59 am

Here is a sample program:

Code: Select all
import org.jfree.chart.ChartFactory;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.encoders.EncoderUtil;
import org.jfree.chart.encoders.ImageFormat;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.StackedXYBarRenderer;
import org.jfree.chart.renderer.xy.ClusteredXYBarRenderer;
import org.jfree.chart.renderer.xy.XYBarRenderer;
import org.jfree.data.time.FixedMillisecond;
import org.jfree.data.time.TimeTableXYDataset;

import java.awt.image.BufferedImage;
import java.util.Calendar;
import java.util.Date;
import java.io.FileOutputStream;
import java.io.IOException;

public class Sample {

    public static void main(String[] args) {

        TimeTableXYDataset dataset = new TimeTableXYDataset();
//        XYBarRenderer renderer = new StackedXYBarRenderer();
        XYBarRenderer renderer = new ClusteredXYBarRenderer();
        renderer.setBaseSeriesVisible(true);

        double value = 0.0;
        for (Calendar calendar = Calendar.getInstance(); value < 30; calendar.add(Calendar.DATE, 1)) {
            Date date = calendar.getTime();
            value++;
            dataset.add(new FixedMillisecond(date), value, "Name 1");
            dataset.add(new FixedMillisecond(date), 30.0 - value, "Name 2");
        }


        JFreeChart chart = ChartFactory.createXYBarChart(
                "Sample Chart",             // title
                "X-axis",                   // x-axis label
                true,                       // make the domain axis display dates
                "Y-axis",                   // y-axis label
                dataset,                    // data
                PlotOrientation.VERTICAL,   // bar orientation
                true,                       // create legend?
                true,                       // generate tooltips?
                false                       // generate URLs?
        );

        XYPlot plot = (XYPlot) chart.getPlot();
        plot.setRenderer(renderer);
        chart.setAntiAlias(true);
        BufferedImage image = chart.createBufferedImage(600, 400, BufferedImage.SCALE_DEFAULT, null);

        try {
            EncoderUtil.writeBufferedImage(image, ImageFormat.PNG, new FileOutputStream("sample.png"), true);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
Daniil
 
Posts: 5
Joined: Tue Apr 15, 2008 3:07 pm
Location: Saratov, Russia

Postby david.gilbert » Fri Apr 18, 2008 4:39 pm

Thanks. This does look like a bug, caused by the bar widths getting calculated as zero (due to the time period being 1 unit (millisecond)). If you change the lines where you add the values to the dataset as follows, you'll get a more sensible result:

Code: Select all
            dataset.add(new Day(date), value, "Name 1");
            dataset.add(new Day(date), 30.0 - value, "Name 2");


I opened this bug report to track it, since I don't know what the best fix is just yet:

http://sourceforge.net/tracker/index.ph ... tid=115494
David Gilbert
JFreeChart Project Leader

:idea: Read my blog
:idea: Ask your company to buy the JFreeChart Developer Guide
:idea: Check out other products sold by my company Object Refinery Limited
david.gilbert
JFreeChart Project Leader
 
Posts: 11367
Joined: Fri Mar 14, 2003 10:29 am

Postby Daniil » Mon Apr 21, 2008 2:37 pm

Thank you! I'm fine with suggested solution.
Daniil
 
Posts: 5
Joined: Tue Apr 15, 2008 3:07 pm
Location: Saratov, Russia


Return to JFreeChart - General

Who is online

Users browsing this forum: Bing [Bot], Google [Bot], Yahoo [Bot] and 18 guests