Bug report: if there is only one set of data, chart does not

A discussion forum for JFreeChart (a 2D chart library for the Java platform).
Locked
Wei

Bug report: if there is only one set of data, chart does not

Post by Wei » Wed Jul 24, 2002 5:47 pm

//This program demos that if there is only one set of data, the chart does
//not show anything.

import java.awt.*;
import java.awt.event.*;
import java.awt.print.*;
import java.util.*;
import javax.swing.*;

import com.jrefinery.chart.*;
import com.jrefinery.chart.tooltips.SymbolicXYToolTipGenerator;
import com.jrefinery.data.*;
import com.jrefinery.ui.*;

public class BugChartPanel extends JPanel {
JButton printButton;
JButton closeButton;

String[] nameArray;
CombinedDataset combinedDataset;
ChartPanel chartPanel;

public BugChartPanel() {
}

public ChartPanel createChart(String title, String xLabel,
String yLabel, String[] nameArray, CombinedDataset combinedDataset) {
ValueAxis valueAxis = new HorizontalDateAxis(xLabel);
VerticalSymbolicAxis symbolicAxis =
new VerticalSymbolicAxis(yLabel, nameArray);
OverlaidXYPlot mainPlot = new OverlaidXYPlot(valueAxis, symbolicAxis);

XYItemRenderer renderer =
new StandardXYItemRenderer(StandardXYItemRenderer.SHAPES, null);

for (int i = 0; i < nameArray.length; i++) {
XYDataset xyDataset = new SubSeriesDataset(combinedDataset, i);
XYPlot subplot = new XYPlot(xyDataset, null, null, renderer);
mainPlot.add(subplot);
}

JFreeChart chart =
new JFreeChart(title, JFreeChart.DEFAULT_TITLE_FONT, mainPlot, true);
chart.setBackgroundPaint(new GradientPaint(0, 0, Color.white,0, 1000,
Color.blue));
chart.setLegend(null);
return new ChartPanel(chart);
}

boolean prepareData() {
long now = System.currentTimeMillis();
long then = now + 600000;
Vector timeVector1 = new Vector();
timeVector1.add(new Long(now));
Vector timeVector2 = new Vector();
timeVector2.add(new Long(then));

combinedDataset = new CombinedDataset();
ScheduledDataset dataset1 = new ScheduledDataset(0, "First", timeVector1);
ScheduledDataset dataset2 = new ScheduledDataset(0, "Second", timeVector2);

combinedDataset.add(dataset1);

nameArray = new String[] {"First"};

// If you un-comment the following two lines, the chart works
//nameArray = new String[] {"First", "Second"};
//combinedDataset.add(dataset2);
return true;
}


class ScheduledDataset extends AbstractSeriesDataset
implements XYDataset, YisSymbolic {
int index;
String name;
Vector timeVector;

public ScheduledDataset(int index, String name, Vector timeVector) {
this.index = index;
this.name = name;
this.timeVector = timeVector;
}

public int getItemCount(int series) {
return timeVector.size();
}

public int getSeriesCount() {
return 1;
}

public String getSeriesName(int series) {
return name;
}

public Number getXValue(int series, int item) {
return (Number) timeVector.elementAt(item);
}

public Number getYValue(int series, int item) {
return new Integer(index);
}

public String getYSymbolicValue(Integer val) {
return name;
}

public String getYSymbolicValue(int series, int item) {
return name;
}

public String[] getYSymbolicValues() {
String[] s = {name};
return s;
}

}


public static void main(String[] args) {
BugChartPanel bugChartPanel = new BugChartPanel();
bugChartPanel.prepareData();
bugChartPanel.chartPanel = bugChartPanel.createChart("Test", "x label",
"y label", bugChartPanel.nameArray, bugChartPanel.combinedDataset);
JDialog dialog = new JDialog(new Frame(), "dialog", true);
dialog.setContentPane(bugChartPanel.chartPanel);
dialog.pack();
dialog.setVisible(true);
}
}

David Gilbert

Re: Bug report: if there is only one set of data, chart does

Post by David Gilbert » Wed Jul 24, 2002 10:29 pm

Excellent...working code makes it so much easier to trace bugs! The problem is the auto range calculation for the date axis, with only one point the range is zero, and that is messing up the translation of the data value to Java2D. I need to spend a little more time on it tomorrow, then I should have a fix committed to CVS.

Regards,

DG.

David Gilbert

Re: Bug report: if there is only one set of data, chart does

Post by David Gilbert » Thu Jul 25, 2002 3:57 pm

This is fixed in CVS now. I ended up making changes to all the subclasses of ValueAxis (I was making another change at the same time) so I can't just cut and paste the fix here.

One other thing, your program doesn't need to use CombinedDataset. Here's a modified version to illustrate:

package com.jrefinery.chart.demo;

//This program demos that if there is only one set of data, the chart does
//not show anything.

import java.awt.*;
import java.awt.event.*;
import java.awt.print.*;
import java.util.*;
import javax.swing.*;

import com.jrefinery.chart.*;
import com.jrefinery.chart.tooltips.SymbolicXYToolTipGenerator;
import com.jrefinery.data.*;
import com.jrefinery.ui.*;

public class BugChartPanel2 extends JPanel {

JButton printButton;
JButton closeButton;

String[] nameArray;
XYDataset[] datasetArray;

//CombinedDataset combinedDataset;
ChartPanel chartPanel;

public BugChartPanel2() {
}

public ChartPanel createChart(String title,
String xLabel,
String yLabel,
String[] nameArray,
XYDataset[] datasetArray) {

ValueAxis valueAxis = new HorizontalDateAxis(xLabel);
VerticalSymbolicAxis symbolicAxis = new VerticalSymbolicAxis(yLabel, nameArray);
OverlaidXYPlot mainPlot = new OverlaidXYPlot(valueAxis, symbolicAxis);

for (int i = 0; i < nameArray.length; i++) {
XYItemRenderer renderer = new StandardXYItemRenderer(StandardXYItemRenderer.SHAPES, null);
XYPlot subplot = new XYPlot(datasetArray, null, null, renderer);
mainPlot.add(subplot);
}

JFreeChart chart = new JFreeChart(title, JFreeChart.DEFAULT_TITLE_FONT, mainPlot, true);
chart.setBackgroundPaint(new GradientPaint(0, 0, Color.white,0, 1000,Color.blue));
chart.setLegend(null);
return new ChartPanel(chart);
}

boolean prepareData() {

long now = System.currentTimeMillis();
long then = now + 600000;
Vector timeVector1 = new Vector();
timeVector1.add(new Long(now));
Vector timeVector2 = new Vector();
timeVector2.add(new Long(then));

datasetArray = new XYDataset[2];
datasetArray[0] = new ScheduledDataset(0, "First", timeVector1);
datasetArray[1] = new ScheduledDataset(1, "Second", timeVector2);

nameArray = new String[] {"First"};
//nameArray = new String[] {"First", "Second"};
return true;
}


< snip ScheduledDataset ... >

public static void main(String[] args) {
BugChartPanel2 bugChartPanel = new BugChartPanel2();
bugChartPanel.prepareData();
bugChartPanel.chartPanel = bugChartPanel.createChart("Test", "x label", "y label",
bugChartPanel.nameArray,
bugChartPanel.datasetArray);
JDialog dialog = new JDialog(new Frame(), "dialog", true);
dialog.setContentPane(bugChartPanel.chartPanel);
dialog.pack();
dialog.setVisible(true);

}

}

Wei

Re: Bug report: if there is only one set of data, chart does

Post by Wei » Thu Jul 25, 2002 8:08 pm

I checked. The bug is fixed. It is fixed so quickly. Unbeliveable!

Also thanks for your advise on programming patterns.

David Gilbert

Re: Bug report: if there is only one set of data, chart does

Post by David Gilbert » Thu Jul 25, 2002 8:22 pm

One other thing you should check out...the EventFrequencyDemo.java file, I just committed to CVS a change that I made a few days back. It is a CategoryPlot that does something similar to the test chart you provided.

Regards,

DG.

Locked