gpt4 book ai didi

java - JFreechart 程序运行,但窗口不显示

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:45:32 25 4
gpt4 key购买 nike

我正在根据显示的示例运行代码 here在 OS X 上使用 Eclipse,我正在使用 JFreeChart 和 JCommon 库。

正如我在标题中所说,程序正在运行但没有显示任何内容。作为检查,我尝试使用 printData() 方法打印数据,它运行良好。它正确地从雅虎 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 库中的所有 jar 添加到构建路径。通过删除不必要的并仅添加 jfreechart-1.0.19jcommon-1.0.23 jar,我的应用程序开始运行并显示窗口和图表。

关于java - JFreechart 程序运行,但窗口不显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27918980/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com