Page 1 of 1

Overlaying HighLow chart with scatter chart

Posted: Tue Apr 14, 2009 6:05 pm
by javageek
I am trying to mark certain lines of a HighLow chart with a scatter chart. Basically I have identified certain lines that I want highlighted. So on the data consists of zero values and then non-zero values where I want a bar identified. What I am getting is wierd. Instead of the scatter chart symbol printed directy over or under a bar it is printed over and to the side of the bar. Plus it has a faint line coming up from zero. It appears that it is attempting to chart the line even though I have the stroke at 0.0f. So I figure I am missing something. Here is the code:

Code: Select all

DefaultHighLowDataset dataset1 = createStockDataset(stock, stockData, start, end);
TimeSeriesCollection dataset3 = createSignalsDataset(stock, stockData, start, end);
JFreeChart chart = createChart(dataset1, stock);
XYPlot plot = (XYPlot) chart.getPlot();
XYLineAndShapeRenderer renderer2 = new XYLineAndShapeRenderer();
renderer2.setUseOutlinePaint(false);
renderer2.setSeriesPaint(0, Color.green);
renderer2.setSeriesPaint(1, Color.red);
renderer2.setSeriesStroke(0, new BasicStroke(0.0f));
renderer2.setSeriesStroke(1, new BasicStroke(0.0f));
plot.setDataset(2, dataset3);
plot.setRenderer(2, renderer2);
NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxisForDataset(2);
rangeAxis.setAutoRangeIncludesZero(false);
rangeAxis.setTickMarkInsideLength(1.0f);
rangeAxis.setTickMarkOutsideLength(1.0f);
rangeAxis.setMinorTickCount(1);
rangeAxis.setMinorTickMarksVisible(true);
ChartPanel chartPanel = new ChartPanel(chart, false);
chartPanel.setPreferredSize(new Dimension(1000, 500));
setContentPane(chartPanel);
Here is the result:

Image

What I am really trying to get is:

(I used an image editor to get this)

Image

I am very, very close. Can anybody help get this right?

Re: Overlaying HighLow chart with scatter chart

Posted: Wed Apr 15, 2009 9:35 am
by david.gilbert
Three things about the lines:

(1) In Java, BasicStroke(0.0f) has a special meaning. It isn't a line with zero width, it is the thinnest visible line that the output device can show (which will be thinner on a high resolution printer than it is on the screen).

(2) You can set a flag in the XYLineAndShapeRenderer to turn off the lines (see the alternate constructors).

(3) You don't need to put items in your dataset with zero values - there's no need for the dataset to have an entry for every day.

Regarding the alignment of the x-values, this will be something to do with the x-values returned by the dataset. I'll go take a look and refresh my memory...

Re: Overlaying HighLow chart with scatter chart

Posted: Wed Apr 15, 2009 9:44 am
by david.gilbert
OK, for the x-value (date) alignment: the DefaultOHLCDataset class will return the dates exactly as you supplied them (as milliseconds since 1-Jan-1970) while the TimeSeriesCollection class will convert your dates into TimePeriod instances (probably Day objects in your case) and then return the start, middle or last millisecond in that time period depending on how the TimeSeriesCollection is configured (see the get/setXPosition() methods, the default value is TimePeriodAnchor.START).

If you use OHLCSeriesCollection instead of DefaultOHLCDataset, you'll have the same date mechanism as TimeSeriesCollection and it will be easier to synchronise the two datasets.

Re: Overlaying HighLow chart with scatter chart

Posted: Wed Apr 15, 2009 3:35 pm
by javageek
WOW!! Thank you !!! That worked wonderfully.

Everything is lining up perfectly now. I am beginning to think I might actually be able to manage what I need to do here!

Now I have another questions.

Is it possible to use a gif or jpg file as the image to use for the scatter plot? I am using the scatter plot to indicate specific situations and would like to control what image are displayed. Each image will go with a specific series so I'm not trying to control individual ticks. (YET :roll: )