Jfreechart crashes when using Yahoo Finance Quotes


QUESTION SOLVED: the solution changes to JFreeChart v1.0.15


I have a very strange problem.

What I created is a file that sends the URL to the Yahoo Finance website and then uses the results to draw the JFreeChart in the JFrame.

What I just cannot understand is the following:

JFrame crashes for some URL requests

It starts, but only a white screen is displayed. If for other requests my program works fine.

example

For instance:

This request:

"http://ichart.yahoo.com/table.csv?s=GOOG&a=0&b=1&c=2011&d=6&e=24&f=2013&g=d&ignore=.csv"; 

works great.

But this request:

 "http://ichart.yahoo.com/table.csv?s=GOOG&a=2&b=1&c=2012&d=6&e=24&f=2013&g=d&ignore=.csv"; 

causes an error.

How is this possible?

The note

I know the following:

  • The JFrame crashes, but the JVM does not notice this (does not notice that it is crashing)
  • Downloading stock quotes (information that JFreeChart uses) is ideal in both cases
  • The code for downloading data and displaying it in JFreeChart comes from this site (the code sent by RoyW "Sat May 10, 2008 7:52 a.m.).
  • The amount of incoming data does not seem to be a problem, since there are more data points in query 1 than in query 2
  • The way I load the data does not matter (I tried both reads directly from the URL and I tried to save the .csv file to a folder and then read from the folder)
  • The problem does not occur randomly: I tried to run the JFrame (recompile the code and then run it) several times, but a β€œwrong” quote resets the JFrame every time.

Additional Information

Something that may be relevant to this:

  • When I run the chart with the correct quote, and I drag the chart around, something strange happens. In a certain period of time, it seems, every weekend, candlesticks become smaller and smaller, until they are only a strip. Then, when I pull over the weekend, they get thicker again until they become normal. It seems to happen every weekend. See the following figures:

enter image description hereenter image description hereenter image description here

Therefore, it seemed to me that this might have something to do with trading days . But this makes no sense, because 1/1/2011 (request 1) falls on Saturday and 3/1/2012 (request 2) falls on Thursday, while request 1 was successful and request 2 failed.

I absolutely do not know what I should do.

All help is much appreciated.

Refresh

Upon request, there is SSCCE with trashgod offers (using JFreechart lib v1.0.14).

  import org.jfree.chart.*; import org.jfree.chart.axis.*; import org.jfree.chart.plot.XYPlot; import org.jfree.chart.renderer.xy.CandlestickRenderer; import org.jfree.data.xy.*; import javax.swing.*; import java.awt.*; import java.io.*; import java.net.URL; import java.text.*; import java.util.*; import java.util.List; public class CandlestickDemo2 extends JFrame { public CandlestickDemo2(String stockSymbol) { super("CandlestickDemo"); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); DateAxis domainAxis = new DateAxis("Date"); NumberAxis rangeAxis = new NumberAxis("Price"); CandlestickRenderer renderer = new CandlestickRenderer(); XYDataset dataset = getDataSet(stockSymbol); XYPlot mainPlot = new XYPlot(dataset, domainAxis, rangeAxis, renderer); //Do some setting up, see the API Doc renderer.setSeriesPaint(0, Color.BLACK); renderer.setDrawVolume(false); rangeAxis.setAutoRangeIncludesZero(false); domainAxis.setTimeline( SegmentedTimeline.newMondayThroughFridayTimeline() ); //Now create the chart and chart panel JFreeChart chart = new JFreeChart(stockSymbol, null, mainPlot, false); ChartPanel chartPanel = new ChartPanel(chart, false); chartPanel.setPreferredSize(new Dimension(600, 300)); this.add(chartPanel); this.pack(); } protected AbstractXYDataset getDataSet(String stockSymbol) { //This is the dataset we are going to create DefaultOHLCDataset result = null; //This is the data needed for the dataset OHLCDataItem[] data; //This is where we go get the data, replace with your own data source data = getData(stockSymbol); //Create a dataset, an Open, High, Low, Close dataset result = new DefaultOHLCDataset(stockSymbol, data); return result; } //This method uses yahoo finance to get the OHLC data protected OHLCDataItem[] getData(String stockSymbol) { List<OHLCDataItem> dataItems = new ArrayList<OHLCDataItem>(); try { String strUrl= "http://ichart.finance.yahoo.com/table.csv?s=GOOG&a=2&b=1&c=2012&d=6&e=24&f=2013&g=d&ignore=.csv"; URL url = new URL(strUrl); BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream())); DateFormat df = new SimpleDateFormat("yMd"); String inputLine; in.readLine(); while ((inputLine = in.readLine()) != null) { StringTokenizer st = new StringTokenizer(inputLine, ","); Date date = df.parse( st.nextToken() ); double open = Double.parseDouble( st.nextToken() ); double high = Double.parseDouble( st.nextToken() ); double low = Double.parseDouble( st.nextToken() ); double close = Double.parseDouble( st.nextToken() ); double volume = Double.parseDouble( st.nextToken() ); double adjClose = Double.parseDouble( st.nextToken() ); OHLCDataItem item = new OHLCDataItem(date, open, high, low, close, volume); dataItems.add(item); } in.close(); } catch (Exception e) { e.printStackTrace(); } //Data from Yahoo is from newest to oldest. Reverse so it is oldest to newest Collections.reverse(dataItems); //Convert the list into an array OHLCDataItem[] data = dataItems.toArray(new OHLCDataItem[dataItems.size()]); return data; } public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { @Override public void run() { new CandlestickDemo2("GOOG").setVisible(true); } }); } } 

This creates a JFrame that only displays a white screen. Try changing strUrl to

  "http://ichart.finance.yahoo.com/table.csv?s=GOOG&a=0&b=1&c=2011&d=6&e=24&f=2013&g=d&ignore=.csv"; 

and you will notice that it is working fine.

Refresh

I decided! The problem was in the JFreeChart version. Changing from v1.0.14 to v1.0.15 solved everything. Kudos to trashgod for (unknowingly) solving my problem by answering widely and mentioning the version of the library used.

Does anyone know how I can help others who have the same problem? Is there a portal somewhere where I can flag this error?

+2
source share
1 answer

CandlestickDemo works for me with v1.0.15 and any of your requests. I made two changes:

  • Build the GUI in the event dispatch thread ; Failure to do this causes a data race with uncertain results:

     EventQueue.invokeLater(new Runnable() { @Override public void run() { new CandlestickDemo("GOOG").setVisible(true); } }); 
  • Omit the support buffer, although it probably doesn't matter:

     ChartPanel chartPanel = new ChartPanel(chart, false); 
  • Appendix: for reference, it looks like the call to ParamChecks.nullNotPermitted() was applied in r2692 , replacing the explicit check, but otherwise the rendering has not changed.

  • Application: in section v1.0.14, the example works correctly with DefaultTimeline DateAxis , which changed significantly in the interim.

  • Addendum: @David Gilbert notes that the corresponding changes can be found in r2465 .

image

+4
source

Source: https://habr.com/ru/post/1498728/


All Articles