Barchart - Category/Series - null bars

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
mausbull
Posts: 15
Joined: Wed Feb 22, 2006 3:36 pm
Contact:

Barchart - Category/Series - null bars

Post by mausbull » Thu Jan 03, 2008 3:41 pm

Hi,

I need to display a barchart using a dataset like the one shown below:

Code: Select all

DefaultStatisticalCategoryDataset dataset = new DefaultStatisticalCategoryDataset();

dataset.add(10, 1, "S1", "C1");      
dataset.add(11, 1, "S2", "C1");
dataset.add(12, 1, "S3", "C1");

dataset.add(9, 1, "S4", "C2");      
dataset.add(8, 1, "S5", "C2");
dataset.add(7, 1, "S6", "C2");
Problem: I've different series in different categories.
As David has mentioned in a thread (http://www.jfree.org/phpBB2/viewtopic.p ... ry+dataset) JfreeChart reserves space for the null bars.
Is there a way I can get rid of this space?

I also tried using multiple datasets but I encountered the problem of overlapping bars (http://www.jfree.org/phpBB2/viewtopic.p ... up+dataset); however this suggestion didn't solve my problem.

Thanks for your answers

Stephan

mausbull
Posts: 15
Joined: Wed Feb 22, 2006 3:36 pm
Contact:

Post by mausbull » Mon Jan 14, 2008 9:09 am

Can anyone give me a hint where to start changing the code so I get the desired output?

Is it right that I need to manipulate the BarRenderer class?

Thanks for your help
Stephan

david.gilbert
JFreeChart Project Leader
Posts: 11734
Joined: Fri Mar 14, 2003 10:29 am
antibot: No, of course not.
Contact:

Post by david.gilbert » Tue Jan 15, 2008 12:37 pm

Here's what I'd do (using multiple datasets and null values):

Code: Select all

import java.awt.Color;
import java.awt.Dimension;
import java.awt.GradientPaint;

import javax.swing.JPanel;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.labels.ItemLabelAnchor;
import org.jfree.chart.labels.ItemLabelPosition;
import org.jfree.chart.labels.StandardCategoryItemLabelGenerator;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.StatisticalBarRenderer;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.statistics.DefaultStatisticalCategoryDataset;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RefineryUtilities;
import org.jfree.ui.TextAnchor;

/**
 * A simple demonstration application showing how to create a "statistical" 
 * bar chart using data from a {@link CategoryDataset}.
 */
public class StatisticalBarChartTest extends ApplicationFrame {

    /**
     * Creates a new demo.
     *
     * @param title  the frame title.
     */
    public StatisticalBarChartTest(String title) {
        super(title);
        JPanel chartPanel = createDemoPanel();
        chartPanel.setPreferredSize(new Dimension(500, 270));
        setContentPane(chartPanel);
    }

    /**
     * Creates a sample dataset.
     * 
     * @return The dataset.
     */
    private static CategoryDataset createDataset1() {
        
        DefaultStatisticalCategoryDataset dataset 
            = new DefaultStatisticalCategoryDataset();
        dataset.add(10, 1, "S1", "C1");     
        dataset.add(11, 1, "S2", "C1");
        dataset.add(12, 1, "S3", "C1");

        dataset.add(null, null, "S1", "C2");     
        dataset.add(null, null, "S2", "C2");
        dataset.add(null, null, "S3", "C2"); 
        return dataset;
                
    }
    
    /**
     * Creates a sample dataset.
     * 
     * @return The dataset.
     */
    private static CategoryDataset createDataset2() {
        
        DefaultStatisticalCategoryDataset dataset 
            = new DefaultStatisticalCategoryDataset();
        dataset.add(null, null, "S4", "C1");     
        dataset.add(null, null, "S5", "C1");
        dataset.add(null, null, "S6", "C1");

        dataset.add(9, 1, "S4", "C2");     
        dataset.add(8, 1, "S5", "C2");
        dataset.add(7, 1, "S6", "C2"); 
        return dataset;
                
    }

    /**
     * Creates a sample chart.
     * 
     * @param dataset  a dataset.
     * 
     * @return The chart.
     */
    private static JFreeChart createChart() {
        
        // create the chart...
        JFreeChart chart = ChartFactory.createLineChart(
            "Statistical Bar Chart Demo 1", // chart title
            "Type",                         // domain axis label
            "Value",                        // range axis label
            createDataset1(),                        // data
            PlotOrientation.VERTICAL,       // orientation
            true,                           // include legend
            true,                           // tooltips
            false                           // urls
        );

        chart.setBackgroundPaint(Color.white);

        CategoryPlot plot = (CategoryPlot) chart.getPlot();
        plot.setBackgroundPaint(Color.lightGray);
        plot.setRangeGridlinePaint(Color.white);

        plot.setDataset(1, createDataset2());
        StatisticalBarRenderer renderer2 = new StatisticalBarRenderer();
        plot.setRenderer(1, renderer2);
        
        // customise the range axis...
        NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
        rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
        rangeAxis.setAutoRangeIncludesZero(false);

        // customise the renderer...
        StatisticalBarRenderer renderer = new StatisticalBarRenderer();
        renderer.setDrawBarOutline(false);
        renderer.setErrorIndicatorPaint(Color.black);
        renderer.setIncludeBaseInRange(false);
        plot.setRenderer(renderer);

        renderer.setBaseItemLabelGenerator(
                new StandardCategoryItemLabelGenerator());
        renderer.setBaseItemLabelsVisible(true);
        renderer.setBaseItemLabelPaint(Color.yellow);
        renderer.setBasePositiveItemLabelPosition(new ItemLabelPosition(
                ItemLabelAnchor.INSIDE6, TextAnchor.BOTTOM_CENTER));
        
        // set up gradient paints for series...
        GradientPaint gp0 = new GradientPaint(0.0f, 0.0f, Color.blue, 
                0.0f, 0.0f, new Color(0, 0, 64));
        GradientPaint gp1 = new GradientPaint(0.0f, 0.0f, Color.green, 
                0.0f, 0.0f, new Color(0, 64, 0));
        renderer.setSeriesPaint(0, gp0);
        renderer.setSeriesPaint(1, gp1);
        return chart;
    }
    
    /**
     * Creates a panel for the demo (used by SuperDemo.java).
     * 
     * @return A panel.
     */
    public static JPanel createDemoPanel() {
        JFreeChart chart = createChart();
        return new ChartPanel(chart);
    }
    
    /**
     * Starting point for the demonstration application.
     *
     * @param args  ignored.
     */
    public static void main(String[] args) {
        StatisticalBarChartTest demo = new StatisticalBarChartTest(
                "Statistical Bar Chart Demo");
        demo.pack();
        RefineryUtilities.centerFrameOnScreen(demo);
        demo.setVisible(true);
    }

}
David Gilbert
JFreeChart Project Leader

:idea: Read my blog
:idea: Support JFree via the Github sponsorship program

blajevardi
Posts: 9
Joined: Tue Nov 21, 2006 2:19 am

Running 1.0.0 version having problem running your code

Post by blajevardi » Tue Jan 22, 2008 9:30 pm

David,

I have tried your code under 1.0.0 version and the code did not like the following line:
renderer.setIncludeBaseInRange(false);
I commented it out, but the example did not run.
I have the same problem when using stacked bar chart with subcategories.
There are many zeros in my data set. I do not want to show them in my plot since the plot would look awful with big spaces between the bars and I cannot show a lot of data in one shot.
Is there any way I can force the plot not to allocate space for 0 value bars?

Thanks,

Brian

Locked