Search code examples
javaswinguser-interfacejfreechart

JFreechart program running, but window not displayed


I am running code based on the example shown here using Eclipse on OS X, and I am using the JFreeChart and JCommon libraries.

As i said in the title, the program is running but not displaying anything. As check I have tried to print the data using the method printData() and it's working perfectly. It correctly takes the data from a yahoo csv file.

I have even tried using a small bunch of data (20 rows), but it keeps not showing.

The problem may be just in displaying the JFrame window.

The code I have inserted trying to fix it is between //BEGIN and //END comments.

Waiting for a reply,

Thank you.

SEMI-SOLVED: The code is fine, the problem is related to the JFreeChart Library.

I tried to "Build Path> Add my JFreeChart lib" in another previously working JFrame class.

When i insert the JFreeChart library, it inhibits the window display. Even if i don't import anything; just by adding the library to the building path.

As default system library i'm using "JRE System Library [JavaSE-1.7]".

Code:

package marketAnalyzer;
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.chart.renderer.xy.XYLineAndShapeRenderer;
import org.jfree.data.time.MovingAverage;
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 CandlestickDemo extends JFrame {


    public CandlestickDemo(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);

        final long ONE_DAY = 24 * 60 * 60 * 1000;
        XYLineAndShapeRenderer maRenderer = new XYLineAndShapeRenderer(true, false);
        XYDataset              maSataset  = MovingAverage.createMovingAverage(dataset, "MA", 30 * ONE_DAY, 0);
        mainPlot.setRenderer(1, maRenderer);
        mainPlot.setDataset (1, maSataset);

        //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);
        chartPanel.setPreferredSize(new Dimension(600, 300));

        this.add(chartPanel);
        this.pack();

        //BEGIN

        //TRYING TO DISPLAY THE WINDOW

        chartPanel.setLayout(new java.awt.BorderLayout());
        chartPanel.validate();
        this.setLayout(new java.awt.BorderLayout());
        this.validate();
        chartPanel.repaint();
        this.repaint();


        //END
    }
    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 {
            //INTERNET DATA
            //              String strUrl= "http://ichart.finance.yahoo.com/table.csv?s="+stockSymbol+"&a=0&b=1&c=2008&d=3&e=30&f=2008&ignore=.csv";
            //              URL url = new URL(strUrl);
            //              BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
            //              DateFormat df = new SimpleDateFormat("y-M-d");


            //BEGIN
            FileReader r;
            BufferedReader in;

            r = new FileReader("nasdaqSmall.csv");

            in = new BufferedReader(r);
            DateFormat df = new SimpleDateFormat("y-M-d");

            //END


            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()]);



        //BEGIN
        //DEBUG
        printData(data);
        //END

        return data;
    }

    public static void main(String[] args) {
        new CandlestickDemo("MSFT").setVisible(true);

    }

    //PRINT DATA
    public static void printData(OHLCDataItem[] data){
        for(OHLCDataItem el: data){
            System.out.println(""+el.getDate()+" "+el.getOpen()+" "+el.getHigh()+" "+el.getLow()+" "+el.getClose()+
                    " "+el.getVolume());

        }
    }


}

Solution

  • I was having this issue as well. It comes from adding all of the jars in the JFreeChart lib to the build path. By removing the unnecessary and adding only the jfreechart-1.0.19 and jcommon-1.0.23 jars, my applications started running with the window and chart displayed.