读取CSV文件,并使用MVC在图形中绘制值 [英] Reading in a CSV file, and plotting the values in a graph using MVC
问题描述
我尝试使用Java,JFreeChart和使用MVC概念将值从csv文件绘制到图形中。在一分钟,我创建了一个按钮,当这个按钮被点击它添加一个新的情节,但是,而不是这样做,我想它读取的值从csv文件,这是读入一个csv文件适配器并存储在模型中。我想知道是否有人可以帮助我这个。我将感谢任何可以提供的帮助。谢谢
I am trying to plot values from a csv file into a graph, using Java, JFreeChart and using the MVC concept. At the minute, I have created a button, and when this button is clicked it adds a new plot to the graph, however instead of doing this, I would like it to read in the values from the csv file, which is read in by a csv file adapter and stored in the model. I wondering if anyone could help me with this. I would appreciate any help that could be given. Thank you
//主类
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文件适配器
//csv file adapter
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 Model {
private int x;
public Model(){
x = 0;
}
public Model(int x){
this.x = x;
}
public void incX(){
x++;
}
public int getX(){
return x;
}
public void addDataset(final List<Double> data)
{
System.out.println("Added data to model");
for (Double d : data)
{
System.out.println("Value: " + d.toString());
}
}
}
//查看
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"));
}
}
//图表显示小部件
//Chart Display Widget
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;
}
}
推荐答案
观察者模式 :更新模型 ,未指定的实现 XYDataset
,并且监听视图将自动更新。您可以在此处和此处查看示例。由于文件延迟本质上是不可预测的,请阅读 SwingWorker
, publish()
中间结果,并更新 code> process(); <此处显示 JFreeChart
示例。
Use the observer pattern: update the model, an unspecified implementation XYDataset
, and the listening view will update itself in response. Examples are seen here and here. Because file latency is inherently unpredictable, read the file in the background thread of a SwingWorker
, publish()
intermediate results, and update the model in process()
; a JFreeChart
example is shown here.
这篇关于读取CSV文件,并使用MVC在图形中绘制值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!