Hello,
I encountered a bug when using setMinimumBarLength while trying to display negative values.
We're using the minimum bar length in order for values not to "disappear" since they're comparatively too small, and sometime negative values are displayed, which causes the whole barchart to appear... Weird.
The code which shows the problem (4 modifications to BarChartDemo11 from the examples):
===============================================
/* -------------------
* BarChartDemo11.java
* -------------------
* (C) Copyright 2006, by Object Refinery Limited.
*
*/
package demo;
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.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.BarRenderer;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RefineryUtilities;
/**
* A bar chart showing licence statistics for open source projects listed
* at Freshmeat.
*/
public class BarChartDemo11 extends ApplicationFrame {
/**
* Creates a new demo instance.
*
* @param title the frame title.
*/
public BarChartDemo11(String title) {
super(title);
CategoryDataset dataset = createDataset();
JFreeChart chart = createChart(dataset);
ChartPanel chartPanel = new ChartPanel(chart, false);
chartPanel.setPreferredSize(new Dimension(500, 270));
setContentPane(chartPanel);
}
/**
* Returns a sample dataset.
*
* @return The dataset.
*/
private static CategoryDataset createDataset() {
DefaultCategoryDataset dataset = new DefaultCategoryDataset();
dataset.addValue(/*2*/3192, "S1", "GNU General Public Licence");
dataset.addValue(3157, "S1", "GNU Lesser General Public Licence");
dataset.addValue(1506, "S1", "BSD Licence (Original)");
dataset.addValue(1283, "S1", "BSD Licence (Revised)");
dataset.addValue(738, "S1", "MIT/X Consortium Licence");
dataset.addValue(-1000, "S1", "Artistic Licence");
dataset.addValue(585, "S1", "Public Domain");
dataset.addValue(349, "S1", "Apache Licence 2.0");
dataset.addValue(317, "S1", "Apache Licence");
dataset.addValue(309, "S1", "Mozilla Public Licence");
dataset.addValue(918, "S1", "Other");
return dataset;
}
/**
* Creates a sample chart.
*
* @param dataset the dataset.
*
* @return The chart.
*/
private static JFreeChart createChart(CategoryDataset dataset) {
// create the chart...
JFreeChart chart = ChartFactory.createBarChart(
"Open Source Projects By Licence", // chart title
"Licence", // domain axis label
"Project Count", // range axis label
dataset, // data
PlotOrientation.VERTICAL, // orientation
false, // include legend
true, // tooltips?
false // URLs?
);
// NOW DO SOME OPTIONAL CUSTOMISATION OF THE CHART...
// set the background color for the chart...
chart.setBackgroundPaint(Color.white);
// get a reference to the plot for further customisation...
CategoryPlot plot = (CategoryPlot) chart.getPlot();
plot.setBackgroundPaint(Color.lightGray);
plot.setDomainGridlinePaint(Color.white);
plot.setDomainGridlinesVisible(true);
plot.setRangeGridlinePaint(Color.white);
plot.getDomainAxis().setMaximumCategoryLabelWidthRatio(0.4f);
// set the range axis to display integers only...
NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
// disable bar outlines...
BarRenderer renderer = (BarRenderer) plot.getRenderer();
renderer.setDrawBarOutline(false);
// This code causes problems when displaying negative values!
renderer.setMinimumBarLength(100.0);
// 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));
renderer.setSeriesPaint(0, gp0);
return chart;
}
/**
* Creates a panel for the demo (used by SuperDemo.java).
*
* @return A panel.
*/
public static JPanel createDemoPanel() {
JFreeChart chart = createChart(createDataset());
return new ChartPanel(chart);
}
/**
* Starting point for the demonstration application.
*
* @param args ignored.
*/
public static void main(String[] args) {
BarChartDemo11 demo = new BarChartDemo11("Bar Chart Demo 11");
demo.pack();
RefineryUtilities.centerFrameOnScreen(demo);
demo.setVisible(true);
}
}
===============================================
Thanks in advanced,
Morwen.
Bug using MinimumBarLength and Negative values in Barcharts
BTW Technicalities
Checked with Java 1.6_03, with Java 1.5_08, with JFree 1.0.9 (Latest), and previous versions.
-
- JFreeChart Project Leader
- Posts: 11734
- Joined: Fri Mar 14, 2003 10:29 am
- antibot: No, of course not.
- Contact:
Thanks for the report. I see what is happening...when JFreeChart makes the bar longer than it really should be (because you asked it to by setting the minimumBarLength), it isn't taking into account that the 'base' of the bar could be either at the top or the bottom, depending on the sign of the value (and also whether or not the axis is inverted). I'll fix that.
David Gilbert
JFreeChart Project Leader
Read my blog
Support JFree via the Github sponsorship program
JFreeChart Project Leader


-
- JFreeChart Project Leader
- Posts: 11734
- Joined: Fri Mar 14, 2003 10:29 am
- antibot: No, of course not.
- Contact:
I'm pretty sure I included a fix in 1.0.10, but I don't have the time to check right now.
David Gilbert
JFreeChart Project Leader
Read my blog
Support JFree via the Github sponsorship program
JFreeChart Project Leader

