ahoj vedel by mi niekto pomoct ? robim program pre zobrazovanie grafu z tabulky pomocou JFreeChartu a nejak som sa zasekol. Zaciatok programu som zacal robit tak aby mi hned loadoval subor do tabulky a nasledne do grafu. Ale teraz to neviem dat na akciu tlacitka tak aby mi vedel nacitat stale novu tabulku a zobrazit ju. Vzdy mi ostane stara a novu nechce precitat.
Za akukolvek pomoc budem vdacny. Dakujem.
import java.io.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.*;
import javax.swing.table.*;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
import org.jfree.data.xy.XYDataset;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;
public class Main{
String[] vyberHD = {"-choose-"};
String hlavicka;
Vector<Vector<Object>> data;
Vector<Object> hldata;
int poc_riad;
JTable tabulka;
JComboBox volba1;
JComboBox volba2;
JButton add;
JLabel popis1;
JLabel popis2;
JLabel popis3;
JLabel x;
JLabel y;
JButton load;
JButton save;
JButton make;
JTextField zacInt;
JTextField konInt;
int zacIntNUM = 1000;
int konIntNUM = 1100;
JTextArea vyber;
JFrame ram;
JScrollPane scroller;
JScrollPane scroller2;
JFreeChart xygraf;
int pocetVyberov;
JPanel panel1;
JPanel panel2;
JTabbedPane taby;
DefaultTableModel dtm;
Vector getData() throws Exception{
Vector<Vector<Object>>Vdata = new Vector<Vector<Object>>();
JFileChooser chooser = new JFileChooser();
int retval = chooser.showOpenDialog(null);
if (retval == JFileChooser.APPROVE_OPTION){
File Sub = chooser.getSelectedFile();
BufferedReader vstup = new BufferedReader(new FileReader(Sub));
String riadok;
poc_riad=0;
hlavicka="#; "+vstup.readLine();
while ((riadok = vstup.readLine()) != null){
poc_riad=poc_riad+1;
Vector<Object> vriadok = new Vector<Object>(Arrays.asList(riadok.split(";")));
vriadok.insertElementAt(poc_riad,0);
Vdata.add(vriadok);
}
//System.out.println(poc_riad);
vstup.close();}
return Vdata;
}
public Object GetStlpec(JTable table_name, int row_index, int col_index){
return table_name.getValueAt(row_index, col_index);
}
public XYDataset createData(){
XYSeriesCollection dataset = new XYSeriesCollection();
for (int j=2;j<=10;j++){
XYSeries seria = new XYSeries(tabulka.getColumnName(j));
for (int i=zacIntNUM-1;i<=konIntNUM;i++){
Object obj1 = GetStlpec(tabulka,i,j);
String hodnotaSTR = obj1.toString();
double hodnotaNUM = Double.parseDouble(hodnotaSTR);
seria.add(i,hodnotaNUM);
}
dataset.addSeries(seria);
}
return dataset;
}
public void paintChart(){
createData();
XYDataset DataForChart = createData();
xygraf = ChartFactory.createXYLineChart(
null,//nadpis
"t/poradie vzoriek",//popis osy X
"hodnoty",//popis osy Y
DataForChart,//data pre graf
PlotOrientation.VERTICAL, //orientacia
true,//legenda
true,//tooltip
false);//URLs
XYPlot plot = (XYPlot) xygraf.getPlot();
XYLineAndShapeRenderer render = (XYLineAndShapeRenderer) plot.getRenderer();
render.setShapesVisible(true);
}
public void paintTable() throws Exception{
//vytvorenie a nastavenie tabulky
data = getData();
hldata = new Vector<Object>(Arrays.asList(hlavicka.split("; ")));
tabulka = new JTable(data,hldata){
public boolean isCellEditable(int row,int column){
if(column == 0) return false;
return true;}};
//tabulka.revalidate();
tabulka.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
//tabulka.setCellSelectionEnabled(true);
TableColumnModel bunkamodel = tabulka.getColumnModel();
for (int i=0; i<2; i++){
TableColumn bunka =bunkamodel.getColumn(i);
if(i<2) bunka.setPreferredWidth(125);
if(i<1) bunka.setPreferredWidth(50);
if(i<2) bunka.setResizable(false);
}
scroller = new JScrollPane(tabulka);
scroller.setBounds(10,105,770,410);
panel1.add(scroller);
}
class AkciaOpen implements ActionListener{
public void actionPerformed(ActionEvent e){
try {
paintTable();
} catch (Exception ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
class AkciaMakeChart implements ActionListener{
public void actionPerformed(ActionEvent e){
try {
paintChart();
} catch (Exception ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
AkciaOpen posluchacOpen = new AkciaOpen();
AkciaMakeChart posluchacMake = new AkciaMakeChart();
public void init() throws Exception{
//hldata = new Vector<Object>(Arrays.asList(vyberHD));
ram = new JFrame("Vizualizationer");
//vytvorenie horneho menu1
JMenu zalozka1 = new JMenu("File");
JMenuItem otvorit = new JMenuItem("Open");
otvorit.addActionListener(posluchacOpen);
JMenuItem ulozit = new JMenuItem("Save");
JMenuItem ulozitako = new JMenuItem("Save As");
JMenuItem exit = new JMenuItem("Exit");
zalozka1.add(otvorit);
zalozka1.add(new JSeparator());
zalozka1.add(ulozit);
zalozka1.add(ulozitako);
zalozka1.add(new JSeparator());
zalozka1.add(exit);
//vytvorenie horneho menu2
JMenu zalozka2 = new JMenu("Edit");
JMenuItem copy = new JMenuItem("Copy");
JMenuItem cut = new JMenuItem("Cut");
JMenuItem paste = new JMenuItem("Paste");
zalozka2.add(copy);
zalozka2.add(cut);
zalozka2.add(paste);
//vytvorenie horneho menu3
JMenu zalozka3 = new JMenu("Help");
JMenuItem help = new JMenuItem("Help");
JMenuItem about = new JMenuItem("About");
zalozka3.add(help);
zalozka3.add(new JSeparator());
zalozka3.add(about);
//pridanie hornych menu do jedneho baru
JMenuBar menu = new JMenuBar();
menu.add(zalozka1);
menu.add(zalozka2);
menu.add(zalozka3);
taby = new JTabbedPane();
//vytvorenie Tabov
panel1 = new JPanel();
panel1.setLayout(null);
panel2 = new JPanel();
panel2.setLayout(null);
//vytvorenie objektov na Tabe1
load = new JButton("Load File");
load.addActionListener(posluchacOpen);
save = new JButton("Save File");
make = new JButton("Make Chart");
make.addActionListener(posluchacMake);
load.setBounds(10,10,100,25);
panel1.add(load);
save.setBounds(10,40,100,25);
panel1.add(save);
make.setBounds(10,70,100,25);
panel1.add(make);
x = new JLabel("axis X:");
x.setBounds(150,70,50,25);
panel1.add(x);
y = new JLabel("axis Y:");
y.setBounds(150,40,50,25);
panel1.add(y);
popis1 = new JLabel("Chose Columns:");
//priprava hlavicky pre ComboBox
volba1 = new JComboBox(vyberHD);;
volba1.setBounds(200,70,100,25);
volba1.setEnabled(false);
panel1.add(volba1);
popis1.setBounds(200,10,100,25);
panel1.add(popis1);
add = new JButton("Add Value Y");
add.setBounds(305,40,110,25);
panel1.add(add);
//napicatie obsahu JTextArey do pola
vyber = new JTextArea("2\n8");
pocetVyberov = 7;/*vyber.getRows();*/
//String test[] = vyber.getText().split("\n");
//System.out.println(pocetVyberov);
scroller2 = new JScrollPane(vyber);
scroller2.setBounds(420,10,100,90);
panel1.add(scroller2);
paintTable();
volba2 = new JComboBox(hldata);
volba2.setBounds(200,40,100,25);
panel1.add(volba2);
paintChart();
popis2 = new JLabel("Interval:");
popis2.setBounds(600,10,100,25);
panel1.add(popis2);
zacInt = new JTextField("1");
zacInt.setBounds(600,40,100,25);
panel1.add(zacInt);
konInt = new JTextField(Integer.toString(poc_riad));
konInt.setBounds(600,70,100,25);
panel1.add(konInt);
//vytvorenie objektov na Tabe2
//paintChart();
ChartPanel Chartpanel = new ChartPanel(xygraf);
Chartpanel.setBounds(10,10,770,470);
panel2.add(Chartpanel);
//pridanie tabov do ramu
taby.addTab("Data",null,panel1,"Data for chart");
taby.addTab("Chart",null,panel2,"Generated chart");
//nastavenie ramu
ram.setJMenuBar(menu);
ram.add(taby);
//ulozenie okna do stredu obrazovky
Toolkit kit = ram.getToolkit();
Dimension d = kit.getScreenSize();
ram.setLocation(d.width/2-400, d.height/2-300);
ram.setSize(800,600);
ram.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
ram.setResizable(false);
ram.setVisible(true);
}
public static void main(String[] args) throws Exception{
Main start = new Main();
start.init();
}
}
Fórum › Java
Tabulka a jfreechart graf


Přidej příspěvek
Toto téma je starší jak čtvrt roku – přidej svůj příspěvek jen tehdy, máš-li k tématu opravdu co říct!
Ano, opravdu chci reagovat → zobrazí formulář pro přidání příspěvku
×Vložení zdrojáku
×Vložení obrázku
×Vložení videa
Aktuálně jsou podporována videa ze serverů YouTube, Vimeo a Dailymotion.
×






























































Uživatelé prohlížející si toto vlákno
Uživatelé on-line: 0 registrovaných, 50 hostů
Podobná vlákna
Implementace grafu jfreechart — založil Pavel
Graf — založil bbeni
Graf 5*x^x — založil Blujacker
PHP Graf - Help — založil Bayerus628
Moderátoři diskuze