使用Yahoo Finance Quotes时,jfreechart崩溃了 [英] jfreechart crashes when using Yahoo Finance Quotes

查看:106
本文介绍了使用Yahoo Finance Quotes时,jfreechart崩溃了的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




问题解决:解决方案正在改为JFreeChart v1.0.15






我有一个非常奇怪的问题。



我设置的是一个向Yahoo Finance网站发送网址请求然后使用结果绘制JFreeChart的文件在JFrame中。



我无法理解的是以下几点:



对于某些网址请求,JFrame崩溃



它启动,但只显示白屏。而对于其他请求,我的程序工作正常。



示例



例如:



此请求:

 http://ichart.yahoo.com/table.csv ?S = GOOG&安培; A = 0和b = 1和C = 2011&安培; d = 6&安培; E = 24&安培; F = 2013&安培; G = d&安培;忽略=的.csv; 

工作正常。



但是这个请求:

 http://ichart.yahoo.com/table.csv?s=GOOG&a=2&b= 1和C = 2012&安培; d = 6&安培; E = 24&安培; F = 2013&安培; G = d&安培;忽略=的.csv; 

导致错误。



如何这可能吗?



注意



我知道以下内容:




  • JFrame崩溃但JVM没有记录它(没有注意到它崩溃)

  • 下载股票报价(信息)
  • 下载数据并在JFreeChart中显示它的代码来自有关。但这没有任何意义,因为 1/1/2011 (请求1)属于星期六而 3/1/2012 (请求2)属于星期四,请求1成功,请求2失败。



    我绝对不知道该怎么办。



    所有帮助非常感谢。



    更新



    根据要求,这是 SSCCE trashgod 的建议(使用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;

    公共类CandlestickDemo2扩展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);

    //做一些设置,参见API Doc
    renderer.setSeriesPaint(0,Color.BLACK);
    renderer.setDrawVolume(false);
    rangeAxis.setAutoRangeIncludesZero(false);
    domainAxis.setTimeline(SegmentedTimeline.newMondayThroughFridayTimeline());

    //现在创建图表和图表面板
    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){
    //这是我们要创建的数据集
    DefaultOHLCDataset result = null;
    //这是数据集
    OHLCDataItem []数据所需的数据;

    //这是我们获取数据的地方,替换为您自己的数据源
    data = getData(stockSymbol);

    //创建一个数据集,一个Open,High,Low,Close数据集
    result = new DefaultOHLCDataset(stockSymbol,data);

    返回结果;
    }
    //此方法使用yahoo finance获取OHLC数据
    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&安培;忽略=的.csv;
    URL url =新URL(strUrl);
    BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
    DateFormat df = new SimpleDateFormat(y-M-d);

    String inputLine;
    in.readLine();
    while((inputLine = in.readLine())!= null){
    StringTokenizer st = new StringTokenizer(inputLine,,);

    日期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(例外e){
    e.printStackTrace();
    }
    //来自雅虎的数据是从最新到最旧的。反转所以它是最旧的最新的
    Collections.reverse(dataItems);

    //将列表转换为数组
    OHLCDataItem [] data = dataItems.toArray(new OHLCDataItem [dataItems.size()]);

    返回数据;
    }

    public static void main(String [] args){
    EventQueue.invokeLater(new Runnable(){
    @Override
    public void run (){
    new CandlestickDemo2(GOOG)。setVisible(true);
    }
    });
    }
    }

    这会生成一个只显示白屏的JFrame 。尝试将 strUrl 更改为

     http://ichart.finance .yahoo.com / table.csv S = GOOG&安培; A = 0和b = 1和C = 2011&安培; d = 6&安培; E = 24&安培; F = 2013&安培; G = d&安培;忽略=的.csv; 

    你会发现它运作正常。



    更新



    解决了!问题是JFreeChart版本。从v1.0.14更改为v1.0.15解决了所有问题。感谢 trashgod (在不知不觉中)通过广泛回答并提及所使用的库版本来解决我的问题。



    有人知道如何帮助其他有同样问题的人吗?是否有一个门户我可以记下这个错误?

    解决方案

    CandlestickDemo 适用于 jfreechart v1.0.15以及您的任何一个查询。我做了两处更改:




    • 事件派发线程;如果不这样做会导致数据竞争具有非确定性结果:

        EventQueue.invokeLater(new Runnable(){
      @Override
      public void run(){
      new CandlestickDemo(GOOG)。setVisible(true);
      }
      });


    • 省略后备缓冲区,虽然它可能无关紧要:

        ChartPanel chartPanel = new ChartPanel(chart,false); 


    • 附录:作为参考,它看起来像是对 ParamChecks的调用.nu​​llNotPermitted()已应用 r2692 ,取代了显式检查,但渲染器未更改。


    • 附录:在v1.0.14下,示例正常工作使用<$ href =http://sourceforge.net/p/jfreechart/code/2900/tree/branches/jfreechart-1.0.x-branch/的 DefaultTimeline source / org / jfree / chart / axis / DateAxis.javarel =nofollow noreferrer> DateAxis ,其中已更改基本上临时。


    • 附录:@David Gilbert注意到相关的变化可以在 r2465






    QUESTION RESOLVED: solution is changing to JFreeChart v1.0.15


    I have a very peculiar problem.

    What I have set up is a file that sends a url request to the Yahoo Finance website and then uses the results to draw a JFreeChart in a JFrame.

    What I just can't get my head around is the following:

    For certain url requests, the JFrame crashes

    It starts, but only shows a white screen. Whereas for other requests, my program works fine.

    Example

    For example:

    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 fine.

    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?

    Note

    I know the following:

    • JFrame crashes but JVM doesn't make note of it (doesn't notice that it crashes)
    • Downloading of the stock quotes (the information that JFreeChart uses) goes perfect in both cases
    • The code for downloading the data and displaying it in the JFreeChart comes from this site (the code posted by RoyW » Sat May 10, 2008 7:52 am).
    • The amount of data coming in doesn't seem a problem because in request 1, there are more data points than in request 2
    • The way I download the data doesn't matter (I tried both reading directly from the url and I tried saving the .csv file to a folder and then reading from the folder)
    • The problem doesn't occur randomly: I've tried launching the JFrame (recompiling the code and then running it) multiple times but the 'wrong' quote crashes the JFrame everytime.

    Extra information

    Something that might have to do with it:

    • When I launch the graph with a correct quote and I drag the graph around, something weird happens. At a certain interval, it seems every weekend, the candlesticks get smaller and smaller untill they are only a stripe. Then when I drag across the weekend, they become thicker again until they are normal size again. This seems to happen for every weekend. Refer to the following pictures:

    So it got me thinking that it might have something to do with trading days. But this doesn't make any sense either because 1/1/2011 (request 1) falls on a Saturday and 3/1/2012 (request 2) falls on a Thursday while request 1 was successful and request 2 failed.

    I am absolutely clueless as to what I should do.

    All help greatly appreciated.

    Update

    On request, here is the SSCCE with trashgod's suggestions (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("y-M-d");
    
                    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 generates a JFrame that displays nothing but a white screen. Try changing the 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 works fine.

    Update

    Solved it! The problem was the JFreeChart version. Changing from v1.0.14 to v1.0.15 solved everything. Kudos to trashgod for (unknowingly) solving my issue by answering extensively and mentioning the library version used.

    Does anybody know how I can help others that are having the same issue? Is there a portal somewhere where I can make note of this bug?

    解决方案

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

    • Construct the GUI on the event dispatch thread; failure to do so causes a data race with non-deterministic results:

      EventQueue.invokeLater(new Runnable() {
          @Override
          public void run() {
              new CandlestickDemo("GOOG").setVisible(true);
          }
      });
      

    • Omit the backing buffer, although it's probably irrelevant:

      ChartPanel chartPanel = new ChartPanel(chart, false);
      

    • Addendum: For reference, it looks like a call to ParamChecks.nullNotPermitted() was applied in r2692, replacing an explicit check, but the renderer was otherwise unchanged.

    • Addendum: Under v1.0.14, the example works correctly with the DefaultTimeline of DateAxis, which changed substantially in the interim.

    • Addendum: @David Gilbert notes that the relevant changes may be found in r2465.

    这篇关于使用Yahoo Finance Quotes时,jfreechart崩溃了的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆