Overlaying HighLow chart with scatter chart

Discussion about JFreeChart related to stockmarket charts.
Locked
javageek
Posts: 11
Joined: Tue Apr 14, 2009 2:26 pm

Overlaying HighLow chart with scatter chart

Post by javageek » Tue Apr 14, 2009 6:05 pm

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?

david.gilbert
JFreeChart Project Leader
Posts: 11734
Joined: Fri Mar 14, 2003 10:29 am
antibot: No, of course not.
Contact:

Re: Overlaying HighLow chart with scatter chart

Post by david.gilbert » Wed Apr 15, 2009 9:35 am

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...
David Gilbert
JFreeChart Project Leader

:idea: Read my blog
:idea: Support JFree via the Github sponsorship program

david.gilbert
JFreeChart Project Leader
Posts: 11734
Joined: Fri Mar 14, 2003 10:29 am
antibot: No, of course not.
Contact:

Re: Overlaying HighLow chart with scatter chart

Post by david.gilbert » Wed Apr 15, 2009 9:44 am

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.
David Gilbert
JFreeChart Project Leader

:idea: Read my blog
:idea: Support JFree via the Github sponsorship program

javageek
Posts: 11
Joined: Tue Apr 14, 2009 2:26 pm

Re: Overlaying HighLow chart with scatter chart

Post by javageek » Wed Apr 15, 2009 3:35 pm

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: )

Locked