Java 读取CSV文件,并使用MVC在图形中绘制值
我正在尝试使用Java、JFreeChart和MVC概念将csv文件中的值绘制成图形。目前,我已经创建了一个按钮,当单击该按钮时,它会向图形中添加一个新的绘图,但是我希望它从csv文件中读取值,该值由csv文件适配器读取并存储在模型中。我想知道是否有人能帮我做这件事。如果能给我任何帮助,我将不胜感激。多谢各位 //主类Java 读取CSV文件,并使用MVC在图形中绘制值,java,swing,csv,model-view-controller,jfreechart,Java,Swing,Csv,Model View Controller,Jfreechart,我正在尝试使用Java、JFreeChart和MVC概念将csv文件中的值绘制成图形。目前,我已经创建了一个按钮,当单击该按钮时,它会向图形中添加一个新的绘图,但是我希望它从csv文件中读取值,该值由csv文件适配器读取并存储在模型中。我想知道是否有人能帮我做这件事。如果能给我任何帮助,我将不胜感激。多谢各位 //主类 public class Main { public static void main(String[] args) { Swing
public class Main
{
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
Model model = new Model(0);
Controller controller = new Controller(model);
View view = new View(controller, "-");
view.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
view.setVisible(true);
}
});
}
}
//csv文件适配器
public List<Double> readFile(final String filename)
{
List<Double> result = new ArrayList<Double>();
String csvFile = filename;
BufferedReader br = null;
String line = "";
String splitBy = ",";
try {
br = new BufferedReader(new FileReader(csvFile));
while((line = br.readLine()) != null){
String[] test = line.split(splitBy);
System.out.println("csvFile [Value= " + test[0] + ", Value=" + test[1] + ", Value=" + test[2] + "]");
try
{
for (String val : test)
{
final Double valueToAdd = Double.parseDouble(val);
result.add(valueToAdd);
}
}
catch (NumberFormatException nfe)
{
System.out.println("Failed to parse line: " + line);
}
}
}
catch(FileNotFoundException e){
e.printStackTrace();
}catch (IOException e){
e.printStackTrace();
}finally{
if (br != null){
try{
br.close();
} catch (IOException e){
e.printStackTrace();
}
}
}
return result;
}
}
//图表显示小部件
public class ChartDisplayWidget extends JPanel
{
/**
*
*/
private static final long serialVersionUID = 1L;
private TimeSeriesCollection chartData;
private JFreeChart chart;
public ChartDisplayWidget()
{
init();
}
public void init()
{
final XYDataset dataset = getSampleData();
chart = ChartFactory.createTimeSeriesChart(
"Our test chart",
"Time",
"Some Value",
dataset);
final ChartPanel chartPanel = new ChartPanel(chart);
add(chartPanel);
}
public void addTimeSeriesPerformancePlot(final String plotName, final Double[] values)
{
final TimeSeries newSeries = new TimeSeries(plotName);
int arrLen = values.length;
int monthIndex = 2;
int yearIndex = 2001;
for (int index = 0; index < arrLen; index++)
{
newSeries.add(new Month(monthIndex++, yearIndex++), values[index]);
}
chartData.addSeries(newSeries);
}
private XYDataset getSampleData()
{
TimeSeries s1 = new TimeSeries("Max CPU");
s1.add(new Month(2, 2001), 181.5);
s1.add(new Month(3, 2001), 20.5);
s1.add(new Month(4, 2001), 1.1);
s1.add(new Month(5, 2001), 81.5);
s1.add(new Month(6, 2001), 1181.5);
s1.add(new Month(7, 2001), 1081.5);
TimeSeries s2 = new TimeSeries("Disk I/O");
s2.add(new Month(2, 2001), 50.0);
s2.add(new Month(3, 2001), 55.0);
s2.add(new Month(4, 2001), 60.6);
s2.add(new Month(5, 2001), 70.8);
s2.add(new Month(6, 2001), 1000.1);
s2.add(new Month(7, 2001), 1081.5);
chartData = new TimeSeriesCollection();
chartData.addSeries(s1);
chartData.addSeries(s2);
return chartData;
}
}
公共类ChartDisplayWidget扩展了JPanel
{
/**
*
*/
私有静态最终长serialVersionUID=1L;
私人时间体验收集图表数据;
私人海图;
公共ChartDisplayWidget()
{
init();
}
公共void init()
{
最终XYDataset数据集=getSampleData();
chart=ChartFactory.createTimeSeriesChart(
“我们的测试图表”,
“时间”,
“一些价值”,
数据集);
最终图表面板图表面板=新图表面板(图表);
添加(图表面板);
}
public void addTimeSeriesPerformancePlot(最终字符串plotName,最终双精度[]值)
{
final TimeSeries newSeries=新的TimeSeries(plotName);
int arrLen=values.length;
int-monthIndex=2;
国际年指数=2001;
对于(int index=0;index
使用:更新模型,一个未指定的实现XYDataset
,侦听视图将相应地更新自身。示例见和。由于文件延迟本质上是不可预测的,因此在中间结果的后台线程中读取文件,并在进程()中更新模型;下面显示了一个JFreeChart
示例
public class View extends JFrame
{
private Controller controller;
private JFrame frame;
private JLabel label;
private JButton button;
private ChartDisplayWidget myChart;
public View(Controller c, String text){
this.controller = c;
getContentPane().setLayout(new BorderLayout());
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(1000,1000);
//label = new JLabel(text);
//getContentPane().add(label, BorderLayout.CENTER);
button = new JButton("Button");
getContentPane().add(button, BorderLayout.SOUTH);
button.addActionListener(new Action());
myChart = new ChartDisplayWidget();
getContentPane().add(myChart, BorderLayout.CENTER);
}
public class Action implements ActionListener {
public void actionPerformed (ActionEvent e){
System.out.println("I was clicked");
controller.control();
myChart.addTimeSeriesPerformancePlot("NewPlot", new Double[] {60.0, 40.0, 500.0, 10.0});
/*this is where I would like to plot the values from the csv file*/
}
}
public JButton getButton(){
return button;
}
public void setText(String text){
label.setText(text);
}
}
//controller
public class Controller {
public Model model;
public ActionListener actionListener;
public Controller(Model model){
this.model = model;
}
public void control(){
CSVFileAdapter c = new CSVFileAdapter();
model.addDataset(c.readFile("C:/dstat.csv"));
}
}
public class ChartDisplayWidget extends JPanel
{
/**
*
*/
private static final long serialVersionUID = 1L;
private TimeSeriesCollection chartData;
private JFreeChart chart;
public ChartDisplayWidget()
{
init();
}
public void init()
{
final XYDataset dataset = getSampleData();
chart = ChartFactory.createTimeSeriesChart(
"Our test chart",
"Time",
"Some Value",
dataset);
final ChartPanel chartPanel = new ChartPanel(chart);
add(chartPanel);
}
public void addTimeSeriesPerformancePlot(final String plotName, final Double[] values)
{
final TimeSeries newSeries = new TimeSeries(plotName);
int arrLen = values.length;
int monthIndex = 2;
int yearIndex = 2001;
for (int index = 0; index < arrLen; index++)
{
newSeries.add(new Month(monthIndex++, yearIndex++), values[index]);
}
chartData.addSeries(newSeries);
}
private XYDataset getSampleData()
{
TimeSeries s1 = new TimeSeries("Max CPU");
s1.add(new Month(2, 2001), 181.5);
s1.add(new Month(3, 2001), 20.5);
s1.add(new Month(4, 2001), 1.1);
s1.add(new Month(5, 2001), 81.5);
s1.add(new Month(6, 2001), 1181.5);
s1.add(new Month(7, 2001), 1081.5);
TimeSeries s2 = new TimeSeries("Disk I/O");
s2.add(new Month(2, 2001), 50.0);
s2.add(new Month(3, 2001), 55.0);
s2.add(new Month(4, 2001), 60.6);
s2.add(new Month(5, 2001), 70.8);
s2.add(new Month(6, 2001), 1000.1);
s2.add(new Month(7, 2001), 1081.5);
chartData = new TimeSeriesCollection();
chartData.addSeries(s1);
chartData.addSeries(s2);
return chartData;
}
}