CombinedChart: Bug with HorizontalDataAxis - Help Please!!!!

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

CombinedChart: Bug with HorizontalDataAxis - Help Please!!!!

Post by Hari » Fri Feb 22, 2002 1:29 pm

Hi,

I'm really impressed with the CombinedPlots but I got stuck with the combined chart having different Date Ranges.

For example, I have a TimeSeriesDataset ranging from, say, 1st Jan 2002 to 15th Jan 2002 and an another TimeSeriesDataset ranging from 1st Feb 2002 to 10th Feb 2002. I expect the proper output, one plot showing graph on left side and another on rightside of the combinedchart. However, I'm getting only one plot output for the entire width and the other is completely missing.


Here is the sample code, I used for testing. Please give me solutions to solve this problem. Thanks in advance.

------------- source ---------------

import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import java.awt.print.*;
import java.io.*;
import java.util.*;
import javax.swing.*;
import com.jrefinery.data.*;
import com.jrefinery.layout.*;
import com.jrefinery.chart.*;
import com.jrefinery.chart.combination.*;
import com.jrefinery.chart.demo.*;
import com.jrefinery.chart.data.*;
import com.jrefinery.data.*;
import com.jrefinery.date.*;
import com.jrefinery.chart.ui.*;
import com.jrefinery.ui.*;

/**
* The main frame in the chart demonstration application.
*/
public class TestChart {

private void displayVerticallyCombinedChart( boolean type) {


JFrame verticallyCombinedChartFrame = null;
JFreeChart chart = null;
// create a default chart based on some sample data...
String title = "Vertically Combined Chart";
String xAxisLabel = "Date";
String[] yAxisLabel = { "CCY per GBP", "Pounds", "IBM", "Bars" };
String yAxisLabelAlternate = "ALL";
int[] weight = { 1, 1, 1 }; // control vertical space allocated to each sub-plot

if( type) {
// calculate Time Series and Moving Average Dataset
XYDataset ds1 = createTimeSeriesCollection2();
XYDataset ds2 = createTimeSeriesCollection3();
XYDataset ds3 = createTimeSeriesCollection4();

// create master dataset
CombinedDataset data = new CombinedDataset();
data.add(ds1);
data.add(ds2);data

// test SubSeriesDataset and CombinedDataset operations

// decompose data into its two dataset series
SeriesDataset series0 = new SubSeriesDataset(data, 0);
SeriesDataset series1 = new SubSeriesDataset(data, 1);

// compose datasets for each sub-plot
CombinedDataset data0 = new CombinedDataset(new SeriesDataset[] {series0} );
CombinedDataset data1 = new CombinedDataset(new SeriesDataset[] {series1} );

// this code could probably go later in the ChartFactory class

Dataset[] dss = new Dataset[] { ds1, ds2,};

try {
// make one vertical axis for each sub-plot
NumberAxis[] valueAxis = new NumberAxis[2];
for (int i=0; i<valueAxis.length; i++) {
valueAxis = new VerticalNumberAxis(yAxisLabel);
if( dss != null) {
Number min = Datasets.getMinimumRangeValue( dss);
Number max = Datasets.getMaximumRangeValue( dss);
System.err.println( "Min: " + min + ", Max: " + max);
valueAxis.setAutoRange( false);
valueAxis.setMinimumAxisValue( min.doubleValue());
valueAxis.setMaximumAxisValue( max.doubleValue());
} else {
valueAxis.setAutoRange( true);
valueAxis.setAutoRangeIncludesZero( true);
}
}

// make one shared horizintal axis
HorizontalDateAxis timeAxis0 = new HorizontalDateAxis(xAxisLabel);

// make a vertically CombinedPlot that will contain the sub-plots
CombinedPlot combinedPlot = new CombinedPlot(timeAxis0, CombinedPlot.VERTICAL);
CombinedChart chartToCombine;

chartToCombine = ChartFactory.createCombinableTimeSeriesChart( timeAxis0, valueAxis[0], data0);
combinedPlot.add(chartToCombine, weight[0]);

chartToCombine = ChartFactory.createCombinableTimeSeriesChart( timeAxis0, valueAxis[1], data1);
combinedPlot.add(chartToCombine, weight[1]);

// this should be called after all sub-plots have been added
combinedPlot.adjustPlots();

// now make the top level JFreeChart that contains the CombinedPlot
chart = new JFreeChart(data, combinedPlot, title, JFreeChart.DEFAULT_TITLE_FONT, true);

}
catch (AxisNotCompatibleException e) {
// this won't happen unless you mess with the axis constructors above
System.err.println("axis not compatible.");
e.printStackTrace();
}
catch (PlotNotCompatibleException e) {
// this won't happen unless you mess with the axis constructors above
System.err.println("axis not compatible.");
}

// then customise it a little...
TextTitle subtitle = new TextTitle("Four Combined Plots: XY, TimeSeries, HighLow and VerticalXYBar",
new Font("SansSerif", Font.BOLD, 12));
chart.addTitle(subtitle);

} else {
XYDataset data = createTimeSeriesCollection1();
chart = ChartFactory.createTimeSeriesChart(title, xAxisLabel, yAxisLabelAlternate, data,
true);

// then customise it a little...
TextTitle subtitle = new TextTitle("Value of GBP in JPY", new Font("SansSerif", Font.BOLD, 12));
chart.addTitle(subtitle);
Plot plot = chart.getPlot();
}

// and present it in a frame...
verticallyCombinedChartFrame = new JFreeChartFrame("Vertically Combined Chart", chart);
verticallyCombinedChartFrame.pack();
JRefineryUtilities.positionFrameRandomly(verticallyCombinedChartFrame);
verticallyCombinedChartFrame.show();

}



public static TimeSeriesCollection createTimeSeriesCollection1() {

TimeSeriesCollection data = new TimeSeriesCollection();
data.addSeries(createJPYTimeSeries());
data.addSeries(createUSDTimeSeries());
data.addSeries(createEURTimeSeries());
return data;

}
public static TimeSeriesCollection createTimeSeriesCollection2() {

TimeSeriesCollection data = new TimeSeriesCollection();
data.addSeries(createJPYTimeSeries());
return data;

}

/**
* Creates a time series collection containing USD/GBP and EUR/GBP exchange rates.
*/
public static TimeSeriesCollection createTimeSeriesCollection3() {

TimeSeriesCollection collection = new TimeSeriesCollection();
collection.addSeries(createUSDTimeSeries());
return collection;

}
public static TimeSeriesCollection createTimeSeriesCollection4() {

TimeSeriesCollection collection = new TimeSeriesCollection();
collection.addSeries(createEURTimeSeries());
return collection;
}
public static BasicTimeSeries createUSDTimeSeries() {

BasicTimeSeries t1 = new BasicTimeSeries("USD/GBP");
try {
t1.add(new Day(2, SerialDate.JANUARY, 2001), new Double(14956));
t1.add(new Day(3, SerialDate.JANUARY, 2001), new Double(15047));
t1.add(new Day(4, SerialDate.JANUARY, 2001), new Double(14931));
t1.add(new Day(5, SerialDate.JANUARY, 2001), new Double(14955));
t1.add(new Day(8, SerialDate.JANUARY, 2001), new Double(14994));
t1.add(new Day(9, SerialDate.JANUARY, 2001), new Double(14911));
t1.add(new Day(10, SerialDate.JANUARY, 2001), new Double(14903));
t1.add(new Day(11, SerialDate.JANUARY, 2001), new Double(14947));
t1.add(new Day(12, SerialDate.JANUARY, 2001), new Double(14784));
t1.add(new Day(15, SerialDate.JANUARY, 2001), new Double(14787));
t1.add(new Day(16, SerialDate.JANUARY, 2001), new Double(14702));
t1.add(new Day(17, SerialDate.JANUARY, 2001), new Double(14729));
t1.add(new Day(18, SerialDate.JANUARY, 2001), new Double(14760));
t1.add(new Day(19, SerialDate.JANUARY, 2001), new Double(14685));
t1.add(new Day(22, SerialDate.JANUARY, 2001), new Double(14609));
t1.add(new Day(23, SerialDate.JANUARY, 2001), new Double(14709));
t1.add(new Day(24, SerialDate.JANUARY, 2001), new Double(14576));
t1.add(new Day(25, SerialDate.JANUARY, 2001), new Double(14589));
t1.add(new Day(26, SerialDate.JANUARY, 2001), new Double(14568));
t1.add(new Day(29, SerialDate.JANUARY, 2001), new Double(14566));
t1.add(new Day(30, SerialDate.JANUARY, 2001), new Double(14604));
t1.add(new Day(31, SerialDate.JANUARY, 2001), new Double(14616));
}
catch (Exception e) {
System.err.println(e.getMessage());
}
return t1;
}

/**
* Returns a time series of the daily EUR/GBP exchange rates in 2001 (to date), for use in
* the JFreeChart demonstration application.
* <P>
* You wouldn't normally create a time series in this way. Typically, values would
* be read from a database.
*
*/
public static BasicTimeSeries createEURTimeSeries() {

BasicTimeSeries t1 = new BasicTimeSeries("EUR/GBP");
try {
t1.add(new Day(2, SerialDate.JANUARY, 2001), new Double(15788));
t1.add(new Day(3, SerialDate.JANUARY, 2001), new Double(15913));
t1.add(new Day(4, SerialDate.JANUARY, 2001), new Double(15807));
t1.add(new Day(5, SerialDate.JANUARY, 2001), new Double(15711));
t1.add(new Day(8, SerialDate.JANUARY, 2001), new Double(15778));
t1.add(new Day(9, SerialDate.JANUARY, 2001), new Double(15851));
t1.add(new Day(10, SerialDate.JANUARY, 2001), new Double(15846));
t1.add(new Day(11, SerialDate.JANUARY, 2001), new Double(15727));
t1.add(new Day(12, SerialDate.JANUARY, 2001), new Double(15585));
t1.add(new Day(15, SerialDate.JANUARY, 2001), new Double(15694));
t1.add(new Day(16, SerialDate.JANUARY, 2001), new Double(15629));
t1.add(new Day(17, SerialDate.JANUARY, 2001), new Double(15831));
t1.add(new Day(18, SerialDate.JANUARY, 2001), new Double(15624));
t1.add(new Day(19, SerialDate.JANUARY, 2001), new Double(15694));
t1.add(new Day(22, SerialDate.JANUARY, 2001), new Double(15615));
t1.add(new Day(23, SerialDate.JANUARY, 2001), new Double(15656));
t1.add(new Day(24, SerialDate.JANUARY, 2001), new Double(15795));
t1.add(new Day(25, SerialDate.JANUARY, 2001), new Double(15852));
t1.add(new Day(26, SerialDate.JANUARY, 2001), new Double(15797));
t1.add(new Day(29, SerialDate.JANUARY, 2001), new Double(15862));
t1.add(new Day(30, SerialDate.JANUARY, 2001), new Double(15803));
t1.add(new Day(31, SerialDate.JANUARY, 2001), new Double(15714));
}
catch (Exception e) {
System.err.println(e.getMessage());
}
return t1;
}

/**
* Returns a time series of the daily EUR/GBP exchange rates in 2001 (to date), for use in
* the JFreeChart demonstration application.
* <P>
* You wouldn't normally create a time series in this way. Typically, values would
* be read from a database.
*
*/
public static BasicTimeSeries createJPYTimeSeries() {

BasicTimeSeries t1 = new BasicTimeSeries("JPY/GBP Exchange Rate");
try {
t1.add(new Day(2, SerialDate.MARCH, 2001), new Double(17112));
t1.add(new Day(3, SerialDate.MARCH, 2001), new Double(17276));
t1.add(new Day(4, SerialDate.MARCH, 2001), new Double(17285));
t1.add(new Day(5, SerialDate.MARCH, 2001), new Double(17323));
t1.add(new Day(8, SerialDate.MARCH, 2001), new Double(17453));
t1.add(new Day(9, SerialDate.MARCH, 2001), new Double(17386));
t1.add(new Day(10, SerialDate.MARCH, 2001), new Double(17323));
t1.add(new Day(11, SerialDate.MARCH, 2001), new Double(17519));
t1.add(new Day(12, SerialDate.MARCH, 2001), new Double(17442));
t1.add(new Day(15, SerialDate.MARCH, 2001), new Double(17583));
t1.add(new Day(16, SerialDate.MARCH, 2001), new Double(17319));
t1.add(new Day(17, SerialDate.MARCH, 2001), new Double(17305));
t1.add(new Day(18, SerialDate.MARCH, 2001), new Double(17475));
t1.add(new Day(19, SerialDate.MARCH, 2001), new Double(17238));
t1.add(new Day(22, SerialDate.MARCH, 2001), new Double(17016));
t1.add(new Day(23, SerialDate.MARCH, 2001), new Double(17236));
t1.add(new Day(24, SerialDate.MARCH, 2001), new Double(17254));
t1.add(new Day(25, SerialDate.MARCH, 2001), new Double(17015));
t1.add(new Day(26, SerialDate.MARCH, 2001), new Double(17028));
t1.add(new Day(29, SerialDate.MARCH, 2001), new Double(17011));
t1.add(new Day(30, SerialDate.MARCH, 2001), new Double(17095));
t1.add(new Day(31, SerialDate.MARCH, 2001), new Double(16910));
} catch (Exception e) {
System.err.println(e.getMessage());
}
return t1;
}

public static void main(String[] args) {
TestChart h = new TestChart();
h.displayVerticallyCombinedChart( args.length == 0);
}

}

David Gilbert

Re: CombinedChart: Bug with HorizontalDataAxis - Help Please

Post by David Gilbert » Mon Feb 25, 2002 6:31 pm

Hi Hari,

Your post is the model on which all requests for help should be based...a short self contained demo program that illustrates the problem exactly! I'm only sorry that I haven't managed to work out what is going wrong yet...the code for combination charts was written by another developer so I'm not so familiar with it. But I will keep trying (and I'm going to e-mail Bill Kelemen to see if he has time to look at it also).

Regards,

Dave Gilbert

Hari

Re: CombinedChart: Bug with HorizontalDataAxis - Help Please

Post by Hari » Tue Feb 26, 2002 5:44 am

Hi David,

I too went through the source and could not find the origin. I personally sent an e-mail to *Bill Kelemen* regarding this problem. Let's wait for his reply.

Regards,

Hari.

David Gilbert

Re: CombinedChart: Bug with HorizontalDataAxis - Help Please

Post by David Gilbert » Fri Mar 01, 2002 2:24 pm

Just an update on this one. Bill Kelemen has found and fixed the problem. The next version of JFreeChart (0.7.4) will incorporate the fix. Thanks to Bill and Hari for the time they've put into resolving this.

Regards,

Dave Gilbert.

Locked