Hi,
I get null pointer exception while i make use of ChartUtilitues to save an OverlaidVerticalBarChart as a JPEG. I couldnt find the reason
can anyone explain
Re: can anyone explain
Let me give a true picture..actually i wanted an OverlaidHorizontalCategoryPlot. So i created a class file in same lines as OverlaidVerticalCategoryPlot. I have done away with getMaximumVerticalDataValue() and getMinimumVerticalDataValue() methods.now i create an instance of this.I get the null point error when i wanted to save as a JPEG or PNG with the use of chartutilities.
the code is
package detailed.trade;
import java.awt.Graphics2D;
import java.awt.geom.Rectangle2D;
import java.lang.Number;
import java.awt.Shape;
import java.util.List;
import java.util.Iterator;
import com.jrefinery.chart.*;
import com.jrefinery.chart.HorizontalCategoryPlot;
import com.jrefinery.data.DefaultCategoryDataset;
import com.jrefinery.data.Range;
/**
* An extension of HorizontalCategoryPlot that allows multiple
* HorizontalCategoryPlots to be overlaid in one space, using common axes.
*
* @author Jeremy Bowman
*/
public class OverlaidHorizontalCategoryPlot extends HorizontalCategoryPlot {
/** Storage for the subplot references. */
private List subplots;
/** The total number of series. */
private int seriesCount = 0;
/**
* Constructs a new overlaid vertical category plot.
*
* @param domainAxisLabel the label for the domain axis.
* @param rangeAxisLabel the label for the range axis.
* @param categories the categories to be shown on the domain axis.
*/
public OverlaidHorizontalCategoryPlot(String domainAxisLabel, String rangeAxisLabel,
Object[] categories) {
this(new VerticalCategoryAxis(domainAxisLabel),
new HorizontalNumberAxis(rangeAxisLabel),
categories);
}
/**
* Constructs a new overlaid vertical category plot.
*
* @param domain horizontal axis to use for all sub-plots.
* @param range vertical axis to use for all sub-plots.
* @param categories the categories to be shown on the domain axis.
*/
public OverlaidHorizontalCategoryPlot(CategoryAxis domain, ValueAxis range,
Object categories[]) {
super(null, domain, range, null);
// create an empty dataset to hold the category labels
double[][] emptyArray = new double[1][categories.length];
DefaultCategoryDataset empty = new DefaultCategoryDataset(emptyArray);
empty.setCategories(categories);
setDataset(empty);
this.subplots = new java.util.ArrayList();
}
/**
* Adds a subplot.
* <P>
* This method sets the axes of the subplot to null.
*
* @param subplot the subplot.
*/
public void add(HorizontalCategoryPlot subplot) {
subplot.setParent(this);
subplot.setDomainAxis(null);
subplot.setRangeAxis(null);
seriesCount = seriesCount + subplot.getSeriesCount();
subplots.add(subplot);
CategoryAxis domain = this.getDomainAxis();
if (domain != null) {
domain.configure();
}
ValueAxis range = this.getRangeAxis();
if (range != null) {
range.configure();
}
}
/**
* Returns an array of labels to be displayed by the legend.
*
* @return An array of legend item labels (or null).
*
* @deprecated use getLegendItems().
*/
public List getLegendItemLabels() {
List result = new java.util.ArrayList();
if (subplots != null) {
Iterator iterator = subplots.iterator();
while (iterator.hasNext()) {
HorizontalCategoryPlot plot = (HorizontalCategoryPlot) iterator.next();
List more = plot.getLegendItemLabels();
result.addAll(more);
}
}
return result;
}
/***
* Returns the legend items.
*
* @return the legend items.
*/
public LegendItemCollection getLegendItems() {
LegendItemCollection result = new LegendItemCollection();
if (subplots != null) {
Iterator iterator = subplots.iterator();
while (iterator.hasNext()) {
CategoryPlot plot = (CategoryPlot) iterator.next();
LegendItemCollection more = plot.getLegendItems();
result.addAll(more);
}
}
return result;
}
/**
* Performs the actual drawing of the data.
*
* @param g2 the graphics device.
* @param dataArea the data area.
* @param info the chart rendering info.
* @param backgroundPlotArea ??
*/
public void render(Graphics2D g2, Rectangle2D dataArea,
ChartRenderingInfo info, Shape backgroundPlotArea) {
Iterator iterator = subplots.iterator();
while (iterator.hasNext()) {
HorizontalCategoryPlot subplot = (HorizontalCategoryPlot) iterator.next();
subplot.render(g2, dataArea, info, backgroundPlotArea);
}
}
/**
* Returns a string for the plot type.
*
* @return a string for the plot type.
*/
public String getPlotType() {
return "Overlaid Horizontal Category Plot";
}
/**
* Returns the number of series in the plot.
*
* @return the series count.
*/
public int getSeriesCount() {
int result = 0;
Iterator iterator = subplots.iterator();
while (iterator.hasNext()) {
HorizontalCategoryPlot subplot = (HorizontalCategoryPlot) iterator.next();
result = result + subplot.getSeriesCount();
}
return result;
}
/**
* Returns the range of data values that will be plotted against the range
* axis.
* <P>
* If the dataset is null, this method returns null.
*
* @return the data range.
*/
public Range getHorizontalDataRange() {
Range result = null;
if (subplots != null) {
Iterator iterator = subplots.iterator();
while (iterator.hasNext()) {
HorizontalCategoryPlot plot = (HorizontalCategoryPlot) iterator.next();
result = Range.combine(result, plot.getHorizontalDataRange());
}
}
return result;
}
where am i going wrong?
the code is
package detailed.trade;
import java.awt.Graphics2D;
import java.awt.geom.Rectangle2D;
import java.lang.Number;
import java.awt.Shape;
import java.util.List;
import java.util.Iterator;
import com.jrefinery.chart.*;
import com.jrefinery.chart.HorizontalCategoryPlot;
import com.jrefinery.data.DefaultCategoryDataset;
import com.jrefinery.data.Range;
/**
* An extension of HorizontalCategoryPlot that allows multiple
* HorizontalCategoryPlots to be overlaid in one space, using common axes.
*
* @author Jeremy Bowman
*/
public class OverlaidHorizontalCategoryPlot extends HorizontalCategoryPlot {
/** Storage for the subplot references. */
private List subplots;
/** The total number of series. */
private int seriesCount = 0;
/**
* Constructs a new overlaid vertical category plot.
*
* @param domainAxisLabel the label for the domain axis.
* @param rangeAxisLabel the label for the range axis.
* @param categories the categories to be shown on the domain axis.
*/
public OverlaidHorizontalCategoryPlot(String domainAxisLabel, String rangeAxisLabel,
Object[] categories) {
this(new VerticalCategoryAxis(domainAxisLabel),
new HorizontalNumberAxis(rangeAxisLabel),
categories);
}
/**
* Constructs a new overlaid vertical category plot.
*
* @param domain horizontal axis to use for all sub-plots.
* @param range vertical axis to use for all sub-plots.
* @param categories the categories to be shown on the domain axis.
*/
public OverlaidHorizontalCategoryPlot(CategoryAxis domain, ValueAxis range,
Object categories[]) {
super(null, domain, range, null);
// create an empty dataset to hold the category labels
double[][] emptyArray = new double[1][categories.length];
DefaultCategoryDataset empty = new DefaultCategoryDataset(emptyArray);
empty.setCategories(categories);
setDataset(empty);
this.subplots = new java.util.ArrayList();
}
/**
* Adds a subplot.
* <P>
* This method sets the axes of the subplot to null.
*
* @param subplot the subplot.
*/
public void add(HorizontalCategoryPlot subplot) {
subplot.setParent(this);
subplot.setDomainAxis(null);
subplot.setRangeAxis(null);
seriesCount = seriesCount + subplot.getSeriesCount();
subplots.add(subplot);
CategoryAxis domain = this.getDomainAxis();
if (domain != null) {
domain.configure();
}
ValueAxis range = this.getRangeAxis();
if (range != null) {
range.configure();
}
}
/**
* Returns an array of labels to be displayed by the legend.
*
* @return An array of legend item labels (or null).
*
* @deprecated use getLegendItems().
*/
public List getLegendItemLabels() {
List result = new java.util.ArrayList();
if (subplots != null) {
Iterator iterator = subplots.iterator();
while (iterator.hasNext()) {
HorizontalCategoryPlot plot = (HorizontalCategoryPlot) iterator.next();
List more = plot.getLegendItemLabels();
result.addAll(more);
}
}
return result;
}
/***
* Returns the legend items.
*
* @return the legend items.
*/
public LegendItemCollection getLegendItems() {
LegendItemCollection result = new LegendItemCollection();
if (subplots != null) {
Iterator iterator = subplots.iterator();
while (iterator.hasNext()) {
CategoryPlot plot = (CategoryPlot) iterator.next();
LegendItemCollection more = plot.getLegendItems();
result.addAll(more);
}
}
return result;
}
/**
* Performs the actual drawing of the data.
*
* @param g2 the graphics device.
* @param dataArea the data area.
* @param info the chart rendering info.
* @param backgroundPlotArea ??
*/
public void render(Graphics2D g2, Rectangle2D dataArea,
ChartRenderingInfo info, Shape backgroundPlotArea) {
Iterator iterator = subplots.iterator();
while (iterator.hasNext()) {
HorizontalCategoryPlot subplot = (HorizontalCategoryPlot) iterator.next();
subplot.render(g2, dataArea, info, backgroundPlotArea);
}
}
/**
* Returns a string for the plot type.
*
* @return a string for the plot type.
*/
public String getPlotType() {
return "Overlaid Horizontal Category Plot";
}
/**
* Returns the number of series in the plot.
*
* @return the series count.
*/
public int getSeriesCount() {
int result = 0;
Iterator iterator = subplots.iterator();
while (iterator.hasNext()) {
HorizontalCategoryPlot subplot = (HorizontalCategoryPlot) iterator.next();
result = result + subplot.getSeriesCount();
}
return result;
}
/**
* Returns the range of data values that will be plotted against the range
* axis.
* <P>
* If the dataset is null, this method returns null.
*
* @return the data range.
*/
public Range getHorizontalDataRange() {
Range result = null;
if (subplots != null) {
Iterator iterator = subplots.iterator();
while (iterator.hasNext()) {
HorizontalCategoryPlot plot = (HorizontalCategoryPlot) iterator.next();
result = Range.combine(result, plot.getHorizontalDataRange());
}
}
return result;
}
where am i going wrong?