attempting to understand charts

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Post Reply
lia
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(XYPlot.java:2312).WHAT 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(
title,
"Date",
"Price",
priceData,
true,
true,
false
);

*/


// 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");
rangeAxis1.setNumberFormatOverride(format);

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;

} :(

RichardWest
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(XYPlot.java:2312)
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

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

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

Lia,
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(XYPlot.java:2312)

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

beena
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();
numberAxis.setTickMarksVisible(false);
// plot.setRangeGridlinesVisible(false);
// plot.setDomainGridlinesVisible(false);

ThreatBarRenderer barRenderer = new ThreatBarRenderer();
barRenderer.setMaximumBarWidth(0.20);
barRenderer.setItemMargin(0.01);

plot.setRenderer(barRenderer);
CategoryItemRenderer renderer = plot.getRenderer();
renderer.setItemLabelsVisible(false);
StringBuilder builder = new StringBuilder();
builder.append(new String("Total Hosts: "));
builder.append(String.valueOf(totalHosts));
builder.append(new String(" Total Vulns: "));
builder.append(String.valueOf(totalVulns));
Title title = new TextTitle(builder.toString());

chart.addSubtitle(title);
plot.setBackgroundPaint(UIManager.getColor("Panel.background"));
plot.setOutlinePaint(null);




//renderer.setSeriesItemLabelsVisible(0,true);
ChartPanel barChart = new ChartPanel(chart);
//barChart.setChart(chart);
barChart.setPreferredSize(new Dimension(250, 200));
barPanel.removeAll();
barPanel.add(barChart);
barPanel.revalidate();

}

Post Reply