Java: Drawing charts it's easy



Recently I need to draw chart, and after short search I found great library JFreeChart thats provide drawing and saving charts to file


Here is the code sample:
import javax.swing.JFrame;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.xy.XYDataset;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;

public class MyChart {
public static void main(String[] args) {
XYSeries series = new XYSeries("cos(a)");

//calculating points
for(float i = -10; i < 10; i+=0.1){
series.add(i, Math.cos(i));
}

XYDataset xyDataset = new XYSeriesCollection(series);

//creating chart
JFreeChart chart = ChartFactory
.createXYLineChart("y = cos(x)", "x", "y",
xyDataset,
PlotOrientation.VERTICAL,
true, true, true);
JFrame frame =
new JFrame("Chart");

//adding chart to the frame
frame.getContentPane()
.add(new ChartPanel(chart));
frame.setSize(400,300);
frame.show();
}
}


Sursa
2011-01-16 16:47:00



Comenteaza





Ultimele 25 posturi adăugate

20:43:00Год Огненного Коня. —» Александр Ищенко - Размышления
15:28:00AVEȚI O CUTIE CU FOTOGRAFII? —» Leo Butnaru
13:33:53Bilanț, 2025 —» Biblioteca de Arte 'Tudor Arghezi'
06:37:02DE REVELION —» Leo Butnaru
07:06:00DIN REVISTA TINERILOR —» Leo Butnaru
06:14:00DIN POEZIA LUMII —» Leo Butnaru
10:34:00AVANPREMIERA UNUI INTER-VIU —» Leo Butnaru
10:22:00GÂNDURI LA FINAL DE AN 2025 —» Leo Butnaru
06:32:00DIN POEZIA LUMII —» Leo Butnaru
10:45:10Elfrida Koroliova: prin labirintul pasiunilor —» Biblioteca de Arte 'Tudor Arghezi'
06:53:00POEME DIN REVISTA TINERILOR —» Leo Butnaru
14:23:00Stix —» Andrei LANGA. Blogul personal
17:12:43Razie cu scandal filmată de Activistul buzoian —» Curaj.TV | Media alternativă
07:01:00JURNALUL CA MEMORIE —» Leo Butnaru
13:53:00Covor —» Andrei LANGA. Blogul personal
13:53:00Covor —» Andrei LANGA. Blogul personal
07:25:10La Steaua care-a Răsărit — poezie, tradiție și lumină de Crăciun la bibliotecă —» BPR Ungheni's Blog
06:58:00DIN ISTORIA AVANGARDISMULUI EUROPEAN —» Leo Butnaru
08:42:00STRICTUL NECESAR —» Leo Butnaru
17:04:34În sfârșit, știu ce vreau să mă fac când voi fi mare! —» APort | "Pentru un român care știe citi, cel mai greu lucru e să nu scrie." I.L. Carag
11:22:00argivă 2019 —» Leo Butnaru
07:47:00POEZIA FRANCEZĂ —» Leo Butnaru
07:13:00DIN COSMOGRAME —» Leo Butnaru
07:32:00DINTR-O ANTOLOGIE RECENTĂ —» Leo Butnaru
21:49:14Fără Titlu —» Путепроводные Заметки