java-JFreechart程序正在运行,但未显示窗口
作者:互联网
我在OS X上使用Eclipse运行基于示例here的代码,并且在使用JFreeChart和JCommon库.
正如我在标题中所述,该程序正在运行,但未显示任何内容.
作为检查,我尝试使用方法printData()打印数据,并且它运行良好.
它可以正确地从yahoo csv文件中获取数据.
我什至尝试使用一小束数据(20行),但一直没有显示.
问题可能仅在于显示JFrame窗口.
我插入来尝试修复的代码位于// BEGIN和// END注释之间.
等待回复,
谢谢.
半解决:
代码很好,问题与JFreeChart库有关.
我试图在另一个以前工作的JFrame类中“构建路径>添加我的JFreeChart库”.
当我插入JFreeChart库时,它将禁止窗口显示.即使我什么都不进口;只需将库添加到构建路径即可.
作为默认系统库,我使用的是“ JRE系统库[JavaSE-1.7]”.
码:
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());
}
}
}
解决方法:
我也有这个问题.它来自将JFreeChart lib中的所有jar添加到构建路径.通过删除不必要的内容并仅添加jfreechart-1.0.19和jcommon-1.0.23 jar,我的应用程序开始运行,并显示窗口和图表.
标签:jfreechart,swing,java,user-interface 来源: https://codeday.me/bug/20191120/2047169.html