Java Desktop Application i JFreeChart

0

Witam

Witam robie aplikacje ktora bedzie zczytywac z serwera MySQL i na podstawie tych danych bedzie robic wykresy. Połączenie mam zrobione, baza danych jest i teraz zostaje dorzucic opcje rysowania wykresu ale nie wiem jak to zrobic ? Biblioteki jfreechart'a i jcommon zaimportowalem lecz nie wiem co dalej.

0

No cóż, w takich sytuacjach należy sięgnąć do dokumentacji biblioteki, której chcesz używać. Praktycznie zawsze można tam znaleźć przykłady jak coś wykonać.

0

Wykres stworzyc wykorzystujac ta biblioteke to pikus - w osobnym projekcie oczywiscie.

Problem jest natomiast w gdy chce ten wykres umiescic a Java Dektop Application poniewaz tam juz mam czesc klas tworzonych automatycznie ktorych nie moge edytowac, totalnie nie orientuje sie w tym kodzie bo nigdy jeszcze nie wykorzystywalem JDA. Niewiem jak dobrac sie do watrosci ktore sa wyswietlane.

Generalnie jakbym doszedl jak "zlapac" te wyswietlane dane z bazy to juz bylo by dobrze.

0

Witam
Mam podobny problem. Otoz kazdy wykres z biblioteki JFreeChart to extends ApplicationFrame Jak zmodyfikowac klase wykresu tak zeby nie wyswietlala sie jako ApplicationFrame tylko np. Panel ? Mam aplet w ktorym chce zeby sie malowal wykres ale zawsze wykes jest osobnym oknem ktory generalnie nie jest zagniezdzony w oknie apletu.

Podaje nizej kod apletu:

package javaapplication9;

import org.jfree.ui.RefineryUtilities;

public class NewJApplet extends javax.swing.JApplet {
public void init() {
try {
java.awt.EventQueue.invokeAndWait(new Runnable() {
public void run() {
initComponents();
}
});
} catch (Exception ex) {
ex.printStackTrace();
}
}

public void start()
{

}

/** This method is called from within the init() method to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {

jDialog1 = new javax.swing.JDialog();
jFrame1 = new javax.swing.JFrame();
jPanel1 = new javax.swing.JPanel();
jButton1 = new javax.swing.JButton();

javax.swing.GroupLayout jDialog1Layout = new
javax.swing.GroupLayout(jDialog1.getContentPane());
jDialog1.getContentPane().setLayout(jDialog1Layout);
jDialog1Layout.setHorizontalGroup(
jDialog1Layout.createParallelGroup
(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 400, Short.MAX_VALUE)
);
jDialog1Layout.setVerticalGroup(
jDialog1Layout.createParallelGroup
(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 300, Short.MAX_VALUE)
);

javax.swing.GroupLayout jFrame1Layout = new
javax.swing.GroupLayout(jFrame1.getContentPane());
jFrame1.getContentPane().setLayout(jFrame1Layout);
jFrame1Layout.setHorizontalGroup(
jFrame1Layout.createParallelGroup
(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 400, Short.MAX_VALUE)
);
jFrame1Layout.setVerticalGroup(
jFrame1Layout.createParallelGroup
(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 300, Short.MAX_VALUE)
);

javax.swing.GroupLayout jPanel1Layout = new
javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup
(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 243, Short.MAX_VALUE)
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup
(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 160, Short.MAX_VALUE)
);

jButton1.setText("Recalculate");
jButton1.addActionListener(new java.awt.event.ActionListener()
{
public void actionPerformed(java.awt.event.ActionEvent
evt) {
jButton1ActionPerformed(evt);
}
});

javax.swing.GroupLayout layout = new javax.swing.GroupLayout
(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup
(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup
(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(28, 28, 28)
.addComponent(jPanel1,
javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(layout.createSequentialGroup()
.addGap(56, 56, 56)
.addComponent(jButton1,
javax.swing.GroupLayout.PREFERRED_SIZE, 164,
javax.swing.GroupLayout.PREFERRED_SIZE)))
.addContainerGap(703, Short.MAX_VALUE))
);
layout.setVerticalGroup(
layout.createParallelGroup
(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(30, 30, 30)
.addComponent(jPanel1,
javax.swing.GroupLayout.PREFERRED_SIZE,
javax.swing.GroupLayout.DEFAULT_SIZE,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addComponent(jButton1,
javax.swing.GroupLayout.PREFERRED_SIZE, 33,
javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap(166, Short.MAX_VALUE))
);
}// </editor-fold>

private void jButton1ActionPerformed(java.awt.event.ActionEvent
evt) {
RandomWalk demo = new RandomWalk("Random Walk");
RefineryUtilities.centerFrameOnScreen(demo);
demo.setVisible(true);
}


// Variables declaration - do not modify
private javax.swing.JButton jButton1;
private javax.swing.JDialog jDialog1;
private javax.swing.JFrame jFrame1;
private javax.swing.JPanel jPanel1;
// End of variables declaration

}

... i klasa wykresu





--------------------------------




package javaapplication9;

import java.awt.Color;
import javax.swing.JPanel;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.NumberAxis;
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;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RectangleInsets;

public class RandomWalk extends ApplicationFrame {

public RandomWalk(String title) {
super(title);
XYDataset dataset = createDataset();
JFreeChart chart = createChart(dataset);
ChartPanel chartPanel = new ChartPanel(chart);
getContentPane().add(chartPanel);


//chartPanel.setPreferredSize(new java.awt.Dimension(500, 270));
//setContentPane(chartPanel);
}
/**
* Creates a sample dataset.
*
* @return a sample dataset.
*/
private static XYDataset createDataset() {
double r;
double[] p = new double[1001];
p[0]=10;
XYSeries series1 = new XYSeries("Share Price");
for (int x = 0; x < 1000; x++) {
r=Math.random()*2-1;
p[x+1]=p[x]+r;
System.out.println (p[x]);
series1.add(x, p[x+1]);
}
XYSeriesCollection dataset = new XYSeriesCollection();
dataset.addSeries(series1);


return dataset;
}
/**
* Creates a chart.
*
* @param dataset the data for the chart.
*
* @return a chart.
*/
private static JFreeChart createChart(XYDataset dataset) {
// create the chart...
JFreeChart chart = ChartFactory.createXYLineChart(
"Ramdom Walk", // chart title
"Days", // x axis label
"Price", // y axis label
dataset, // data
PlotOrientation.VERTICAL,
false, // include legend
true, // tooltips
false // urls
);
// NOW DO SOME OPTIONAL CUSTOMISATION OF THE CHART...
chart.setBackgroundPaint(Color.white);
// get a reference to the plot for further customisation...
XYPlot plot = (XYPlot) chart.getPlot();
plot.setBackgroundPaint(Color.lightGray);
plot.setAxisOffset(new RectangleInsets(5.0, 5.0, 5.0, 5.0));
plot.setDomainGridlinePaint(Color.white);
plot.setRangeGridlinePaint(Color.white);
XYLineAndShapeRenderer renderer
= (XYLineAndShapeRenderer) plot.getRenderer();
renderer.setShapesVisible(true);
renderer.setShapesFilled(true);
// change the auto tick unit selection to integer units only...
NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
// OPTIONAL CUSTOMISATION COMPLETED.
return chart;
}/**
* Creates a panel for the demo (used by SuperDemo.java).
*
* @return A panel.
*/
public static JPanel createDemoPanel() {
JFreeChart chart = createChart(createDataset());
return new ChartPanel(chart);
}
}

mysle ze kolega wyzej tez ma z tym problem</quote>

0

Robiłem coś takiego, tylko, że dawno i już za bardzo nie pamiętam co i jak.

Ogólnie pomysł jest taki, że nie rozszerzacie klasy ApplicationFrame ttylko JPanel, tak żeby stworzyć swój komponent Swingowy.

W kodzie wygląda to tak:


public final class HistogramChart extends JPanel {

	private XYSeries dataset;
	private JFreeChart chart;
	private XYBarDataset XYDataset;

	public HistogramChart(){
		super();
	}

	public HistogramChart(double[] values, Color color) {
		super();
		chart = createChart(createDataset(values), color);
		add(new ChartPanel(chart));
	}

	public JFreeChart createChart(IntervalXYDataset dataset, Color color) {
		// TODO Auto-generated method stub
		chart = ChartFactory.createXYBarChart("Histogram", null, false,
				null, dataset, PlotOrientation.VERTICAL, false, false, false);

		XYPlot plot = (XYPlot) chart.getPlot(); 
		XYItemRenderer renderer = (XYItemRenderer) plot.getRenderer();
		renderer.setSeriesPaint(0, color);
		renderer.setBaseOutlinePaint(Color.YELLOW);
		return chart;
	}

	public XYBarDataset createDataset(double[] values) {
		// TODO Auto-generated method stub
		this.dataset = new XYSeries("Obrazek główny");
		for(int i = 0; i < values.length; i++)
			dataset.add(i, values[i]);
		
		XYSeriesCollection collection = new XYSeriesCollection();
		collection.addSeries(dataset);
		XYDataset = new XYBarDataset(collection, 0.9);
		return XYDataset;
	}

	... //potrzebne metody jakieś tam
}

Z tego co zrozumiałem swój niezrozumiały kod to to jest panel na którym wyświetlany będzie wykres. I tu trzeba jeszcze wykres dodać/stworzyć jakoś tak:


	histogramPanel = new HistogramChart();  //panel z wykresem
	chart = new ChartPanel(histogramPanel.createChart(histogramPanel.createDataset(values), Color.BLUE));  //klasa ChartPanel z pakietu JFreeChart, dodanie wykresu do panela histogram
	chart.setPreferredSize(new Dimension(0, 335));  //ustawienie parametrow
	resultPanel.add(chart, BorderLayout.NORTH);  //dodanie do formatki

Ogólnie program jest brzydko zrobiony bo tak na kolanie i w pośpiechu, nie wiem czy Wam to pomoże ale macie może chociaż jakaś wskazówkę do dalszego działania. Ten kod tworzy zwykły BarChart na podstawie Datasetu.

1 użytkowników online, w tym zalogowanych: 0, gości: 1