attempting to understand charts

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Post Reply
Posts: 18
Joined: Sat Oct 14, 2006 5:02 am
Location: Victoria BC

attempting to understand charts

Post by lia » Fri Mar 16, 2007 2:38 am

I am trying to learn how to work with the charts by modifying the example code. I have changed the PriceVolumeDemo1 demo and I am trying to create a chart without using the ChartFactory method. I have created everything that I think a chart requires but when I run the code I get a null pointer exception at AWT Event Queue 0 at org.jfree.chart.plot.XYPlot.drawQuadrants( AM I MISSING

* Creates a chart.
* @return a chart.
private static JFreeChart createChart() {

XYDataset priceData = createPriceDataset();
String title = "Eurodollar Futures Contract (MAR03)";

JFreeChart chart = ChartFactory.createTimeSeriesChart(


// XYPlot plot = chart.getXYPlot();
XYPlot plot = new XYPlot ();

XYItemRenderer renderer1 = new StandardXYItemRenderer();
plot.setRenderer(0, renderer1);

NumberAxis rangeAxis1 = new NumberAxis();
rangeAxis1.setLowerMargin(0.40); // to leave room for volume bars
DecimalFormat format = new DecimalFormat("00.00");

plot.setRangeAxis(0, rangeAxis1);
plot.setDataset(0, priceData);

NumberAxis rangeAxis2 = new NumberAxis("Volume");
rangeAxis2.setUpperMargin(1.00); // to leave room for price line
plot.setRangeAxis(1, rangeAxis2);
plot.setDataset(1, createVolumeDataset());
plot.setRangeAxis(1, rangeAxis2);
plot.mapDatasetToRangeAxis(1, 1);
XYBarRenderer renderer2 = new XYBarRenderer(0.20);
plot.setRenderer(1, renderer2);

JFreeChart chart = new JFreeChart(plot);

return chart;

} :(

Posts: 844
Joined: Fri Oct 13, 2006 9:29 pm
Location: Sunnyvale, CA

Re: attempting to understand charts

Post by RichardWest » Fri Mar 16, 2007 3:32 am

lia wrote:org.jfree.chart.plot.XYPlot.drawQuadrants(
What version of JFreeChart are you using? I am using v1.0.4, and the line number you specified is not in drawQuadrants(). However, looking at your code and what the drawQuadrants() method does do, it seems your problem is you never specify a domain axis. As a result, when the XYPlot starts to draw itself, it queries the domain axis and throws a null-pointer exception. This holds true for all versions of JFreeChart. If that doesn't solve your problem, post a reply with the version you are using, and I will take a look again at that specific version's code.

Hope that helps,
Richard West
Design Engineer II
Advanced Micro Devices
Sunnyvale, CA

Posts: 3
Joined: Thu Mar 22, 2007 9:40 pm

Post by beena » Thu Mar 22, 2007 9:42 pm

were you able to solve the problem. I am getting the exact same exception Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at org.jfree.chart.plot.XYPlot.drawQuadrants(

It seems like the code is being called twice by the event dispatch thread , and the first time it paints properly and the next time I get the exception.
Any help is much appreciated

Posts: 3
Joined: Thu Mar 22, 2007 9:40 pm

Post by beena » Thu Mar 22, 2007 11:04 pm

Here is the code that is creating the chart, this is called from the done method in swingworker

public void drawBarChart(DefaultCategoryDataset dataSet, int totalHosts, int totalVulns) {

JFreeChart chart = ChartFactory.createStackedBarChart("Host Breakdown", "", "% of total",
dataSet, PlotOrientation.HORIZONTAL,
true, true, false);

CategoryPlot plot = chart.getCategoryPlot();
if (plot != null) {
ValueAxis numberAxis = plot.getRangeAxis();
// plot.setRangeGridlinesVisible(false);
// plot.setDomainGridlinesVisible(false);

ThreatBarRenderer barRenderer = new ThreatBarRenderer();

CategoryItemRenderer renderer = plot.getRenderer();
StringBuilder builder = new StringBuilder();
builder.append(new String("Total Hosts: "));
builder.append(new String(" Total Vulns: "));
Title title = new TextTitle(builder.toString());


ChartPanel barChart = new ChartPanel(chart);
barChart.setPreferredSize(new Dimension(250, 200));


Post Reply