novice question

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

novice question

Post by Edward » Thu Mar 14, 2002 1:26 pm

excuse my ignorence ....

I want to make a candlestick plot. I have my data in double[] arrays, meaning the open, high,low,close,volume,day,month,year.

I was used to easy methods to just incorporate these data inside a plot. The programming used here seems a little too advanced for my current knowledge of JAVA unfortunately. I have looked at the example JFreeChartDEmo.java:

the data is created using the method createSampleHighLowDataset(); etc.

HighLowDataset data = DemoDatasetFactory.createSampleHighLowDataset();
JFreeChart chart = ChartFactory.createCandlestickChart(title,
timeAxisLabel, valueAxisLabel,
data, true);

I haven't managed to get it working yet. But my question is: is this the only way to do it or is there a way in which I can easily transfer the double[] arrays which I already have available,

many thanks,

Edward

Edward

Re: novice question

Post by Edward » Thu Mar 14, 2002 1:42 pm

or maybe I could better formulate my questions differently:

if I have my data in the following arrays:

double[] open
double[] high
double[] low
double[] close
double[] volume
double[] day
double[] month
double[] year

what is then the easiest way to make a candle plot,

again, many thanks, Edward

David Gilbert

Re: novice question

Post by David Gilbert » Thu Mar 14, 2002 1:44 pm

Writing your own class that implements the HighLowDataset interface is the only option at present. It's not that hard...take a look at the SampleHighLowDataset class for an example...by changing the hard-coded arrays to references passed in via a constructor, you would have a reasonable dataset (although more work would be required if you want to dynamically update the data).

Regards,

DG.

Edward

Re: novice question

Post by Edward » Thu Mar 14, 2002 1:59 pm

thanks David. That's what I have been doing the last few hours .... I'll succeed eventually.

But now I know that at least I'm doing the right thing and that there is not a much easier way I overlooked ...... for example a method looking like ..... candleDataSet(double[] open, double[] high, double[] low, double[] close, double[] day, double[] month, double[] year);

rgds, Edward

David Gilbert

Re: novice question

Post by David Gilbert » Fri Mar 15, 2002 4:01 pm

Hi Edward,

I've done a quick implementation of a DefaultHighLowDataset that is along the lines of what you are looking for.

There are a couple of limitations: (1) it only allows for one data series when most of the other datasets allow any number of series, and (2) you can't update the data after you construct the dataset, so it is only good for static charts.
But it should get you started...

Regards,

DG.

P.S. Sorry about the formatting, I haven't worked out how to preserve spaces in forum posts yet!!

package com.jrefinery.data;

import java.util.Date;

/**
* A simple implementation of the HighLowDataset.
*/
public class DefaultHighLowDataset extends AbstractSeriesDataset implements HighLowDataset {

/** The series name. */
protected String seriesName;

/** Storage for the dates. */
protected Date[] date;

/** Storage for the high values. */
protected Number[] high;

/** Storage for the low values. */
protected Number[] low;

/** Storage for the open values. */
protected Number[] open;

/** Storage for the close values. */
protected Number[] close;

/** Storage for the volume values. */
protected Number[] volume;

/**
* Constructs a new high/low/open/close dataset.
* <p>
* The current implementation allows only one series in the dataset. This may be extended in
* a future version.
*/
public DefaultHighLowDataset(String seriesName,
Date[] date,
double[] high, double[] low,
double[] open, double[] close,
double[] volume) {

this.seriesName = seriesName;
this.date = date;
this.high = this.createNumberArray(high);
this.low = this.createNumberArray(low);
this.open = this.createNumberArray(open);
this.close = this.createNumberArray(close);
this.volume = this.createNumberArray(volume);;

}

/**
* Returns the name of the series stored in this dataset.
*/
public String getSeriesName(int i) {
return this.seriesName;
}

/**
* Returns the x-value for one item in a series.
* <p>
* The value returned is a Long object generated from the underlying Date object.
*
* @param series The series (zero-based index).
* @param item The item (zero-based index).
* @return The x-value.
*/
public Number getXValue(int series, int item) {
return new Long(date[item].getTime());
}

/**
* Returns the y-value for one item in a series.
* <p>
* This method (from the XYDataset interface) is mapped to the getCloseValue(...) method.
*
* @param series The series (zero-based index).
* @param item The item (zero-based index).
* @return The y-value.
*/
public Number getYValue(int series, int item) {
return this.getCloseValue(series, item);
}

/**
* Returns the high-value for one item in a series.
*
* @param series The series (zero-based index).
* @param item The item (zero-based index).
* @return The high-value.
*/
public Number getHighValue(int series, int item) {
return high[item];
}

/**
* Returns the low-value for one item in a series.
*
* @param series The series (zero-based index).
* @param item The item (zero-based index).
* @return The low-value.
*/
public Number getLowValue(int series, int item) {
return low[item];
}

/**
* Returns the open-value for one item in a series.
*
* @param series The series (zero-based index).
* @param item The item (zero-based index).
* @return The open-value.
*/
public Number getOpenValue(int series, int item) {
return open[item];
}

/**
* Returns the close-value for one item in a series.
*
* @param series The series (zero-based index).
* @param item The item (zero-based index).
* @return The close-value.
*/
public Number getCloseValue(int series, int item) {
return close[item];
}

/**
* Returns the volume-value for one item in a series.
*
* @param series The series (zero-based index).
* @param item The item (zero-based index).
* @return The volume-value.
*/
public Number getVolumeValue(int series, int item) {
return volume[item];
}

/**
* Returns the number of series in the dataset.
* <p>
* This implementation only allows one series.
*
* @return The number of series.
*/
public int getSeriesCount() {
return 1;
}

/**
* Returns the number of items in the specified series.
* @param series The index (zero-based) of the series;
* @return The number of items in the specified series.
*/
public int getItemCount(int series) {
return date.length;
}

/**
* Constructs an array of Number objects from an array of doubles.
*/
public static Number[] createNumberArray(double[] data) {

Number[] result = new Number[data.length];

for (int i=0; i<data.length; i++) {
result = new Double(data);
}

return result;

}

}

Locked