Hi,
I am trying to create a sample histogram with the following code:
HistogramDataset dataset = new HistogramDataset();
double[] values = {1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0};
HistogramBin bin = new HistogramBin(4,9);
dataset.addSeries("Bar",values,(values.length)/6);
JFreeChart chart = ChartFactory.createHistogram("title", null,null, dataset, PlotOrientation.HORIZONTAL, true, false, false);
When I run the code, it returns a NullPointerException exception (stack trace at the bottom of the post) with no chart created. Strangely, this error seems to be related to the chart legend. Everything runs fine if I turn off the "create legend" flag in ChartFactory.createHistogram method. I am using a linux box with Java 1.6.0_16 installed. I tried to google this error but couldn't find anything. I am wondering if anyone here have the similar problem?
Thanks
Derek
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at org.jfree.chart.labels.AbstractXYItemLabelGenerator.createItemArray(AbstractXYItemLabelGenerator.java:245)
at org.jfree.chart.labels.AbstractXYItemLabelGenerator.generateLabelString(AbstractXYItemLabelGenerator.java:227)
at org.jfree.chart.labels.StandardXYToolTipGenerator.generateToolTip(StandardXYToolTipGenerator.java:154)
at org.jfree.chart.renderer.xy.XYBarRenderer.drawItem(XYBarRenderer.java:551)
at org.jfree.chart.plot.XYPlot.render(XYPlot.java:2590)
at org.jfree.chart.plot.XYPlot.draw(XYPlot.java:2168)
at org.jfree.chart.JFreeChart.draw(JFreeChart.java:1058)
at org.jfree.chart.ChartPanel.paintComponent(ChartPanel.java:1271)
at javax.swing.JComponent.paint(JComponent.java:1029)
at javax.swing.JComponent.paintChildren(JComponent.java:864)
at javax.swing.JComponent.paint(JComponent.java:1038)
at javax.swing.JLayeredPane.paint(JLayeredPane.java:567)
at javax.swing.JComponent.paintChildren(JComponent.java:864)
at javax.swing.JComponent.paintToOffscreen(JComponent.java:5131)
at javax.swing.BufferStrategyPaintManager.paint(BufferStrategyPaintManager.java:278)
at javax.swing.RepaintManager.paint(RepaintManager.java:1220)
at javax.swing.JComponent.paint(JComponent.java:1015)
at java.awt.GraphicsCallback$PaintCallback.run(GraphicsCallback.java:21)
at sun.awt.SunGraphicsCallback.runOneComponent(SunGraphicsCallback.java:60)
at sun.awt.SunGraphicsCallback.runComponents(SunGraphicsCallback.java:97)
at java.awt.Container.paint(Container.java:1780)
at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:814)
at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:714)
at javax.swing.RepaintManager.seqPaintDirtyRegions(RepaintManager.java:694)
at javax.swing.SystemEventQueueUtilities$ComponentWorkRequest.run(SystemEventQueueUtilities.java:128)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:209)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:597)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)
Cannot show legend for histogram
-
- Posts: 1634
- Joined: Sat Feb 17, 2007 1:51 pm
Re: Cannot show legend for histogram
What version of JFreeChart are you using? The line numbers do apparently not match with the current version for which the API is online available. E. g. AbstractXYItemLabelGenerator.createItemArray starts in line 275 (current version) and does not contain line 245.
In any case, I do not see too many things that could go wrong in this method, only if you had a null dataset or the dataset had a null series key.
Furthermore, if this problem was really connected to the "createLegend" flag, I would expect it to fail earlier during the rendering of the legend, but according to the stack trace, you have already passed that point.
Can you provide a runnable piece of code that is showing the issue?
In any case, I do not see too many things that could go wrong in this method, only if you had a null dataset or the dataset had a null series key.
Furthermore, if this problem was really connected to the "createLegend" flag, I would expect it to fail earlier during the rendering of the legend, but according to the stack trace, you have already passed that point.
Can you provide a runnable piece of code that is showing the issue?
Re: Cannot show legend for histogram
The code is attached below. The offending line is marked with a comment.
The version of JFreeChart I am using is 1.0.13. The library was downloaded and used without any modification. Thinking that the downloaded file may not be up-to-date, I even built my own from the source code with the provided ant script. However, the problem persists.
You brought up an interesting point when you mentioned the line number mismatch. I read from the javadoc that the JFreeChart class should have an addLegend method; however, when I tried to access the addLegend method, I got a compilation error telling me that the method is not defined.
-----------------------------------------------------------------------------------------------
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.statistics.HistogramDataset;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RefineryUtilities;
public class HisTest extends ApplicationFrame {
public HisTest(String title) {
super(title);
HistogramDataset dataset = new HistogramDataset();
double[] values = { 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0 };
dataset.addSeries("Bar", values, 10);
JFreeChart chart = ChartFactory.createHistogram("title", null, null,
dataset, PlotOrientation.HORIZONTAL, true, false, false); //if I turn the flag to false, everything runs fine
ChartPanel chartPanel = new ChartPanel(chart);
setContentPane(chartPanel);
}
public static void main(String[] args) {
HisTest t = new HisTest("Histogram Test");
t.pack();
RefineryUtilities.centerFrameOnScreen(t);
t.setVisible(true);
}
}
------------------------------------------------------------------------------------------------------------------
**Update**
I somehow managed to fix the problem by moving the JfreeChart and JCommon library to the top of the "build class path order" in Eclipse. I still don't know why it works tho.
The version of JFreeChart I am using is 1.0.13. The library was downloaded and used without any modification. Thinking that the downloaded file may not be up-to-date, I even built my own from the source code with the provided ant script. However, the problem persists.
You brought up an interesting point when you mentioned the line number mismatch. I read from the javadoc that the JFreeChart class should have an addLegend method; however, when I tried to access the addLegend method, I got a compilation error telling me that the method is not defined.
-----------------------------------------------------------------------------------------------
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.statistics.HistogramDataset;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RefineryUtilities;
public class HisTest extends ApplicationFrame {
public HisTest(String title) {
super(title);
HistogramDataset dataset = new HistogramDataset();
double[] values = { 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0 };
dataset.addSeries("Bar", values, 10);
JFreeChart chart = ChartFactory.createHistogram("title", null, null,
dataset, PlotOrientation.HORIZONTAL, true, false, false); //if I turn the flag to false, everything runs fine
ChartPanel chartPanel = new ChartPanel(chart);
setContentPane(chartPanel);
}
public static void main(String[] args) {
HisTest t = new HisTest("Histogram Test");
t.pack();
RefineryUtilities.centerFrameOnScreen(t);
t.setVisible(true);
}
}
------------------------------------------------------------------------------------------------------------------
**Update**
I somehow managed to fix the problem by moving the JfreeChart and JCommon library to the top of the "build class path order" in Eclipse. I still don't know why it works tho.
-
- JFreeChart Project Leader
- Posts: 11734
- Joined: Fri Mar 14, 2003 10:29 am
- antibot: No, of course not.
- Contact:
Re: Cannot show legend for histogram
You probably have an older version of JFreeChart on your runtime classpath somewhere.
David Gilbert
JFreeChart Project Leader
Read my blog
Support JFree via the Github sponsorship program
JFreeChart Project Leader
Read my blog
Support JFree via the Github sponsorship program
-
- Posts: 2
- Joined: Thu Jan 29, 2015 8:31 am
- antibot: No, of course not.
Re: Cannot show legend for histogram
I am also using a linux box with java but I am new to java.I am using JFreeChart to render a stacked area chart. By default, the chart legend is rendered below the plot with the elements laid out horizontally. I would like the legend to appear on the right of the plot with the elements laid out as a vertical list.
Is this possible and, if so, how do I do it?
Is this possible and, if so, how do I do it?